3.19.2007

I thought you guys might be interested to see part of what I do for my Organization, Architecture, and Assembly Language class. This is a program in assembly language that accepts two integers, and multiplies them manually, instead of using any built-in function. Assembly language is one step away from machine code, which the actual processor inside the computer can read. To show you what that looks like, I have translated the red bolded command below into machine code.
In hexidecimal notation, it would be 0x0251A024
In binary notation, it would be 00000010010100011010000000100100
Or, going the other direction, to a more sophisticated language, the green italicized part of the assembly code would be the following in pseudocode, which is much closer to languages like c++ or java:

int mult (int a, int b){
if (a == 0 || b == 0){
return 0;

}else if (a[0] == '0'){
return (mult (a<<1,>>1);

}else {
return (a + mult(a<<1,>>1);

}

}


And here is the actual assembly code:
[The weird lines across the page are a result of me using an easy way to indent lines in html...it is really kind of a pain to do it right, and evidently this website makes lines when I use that method]

#Lab 6
#Jonathan Gerrans

#Data Memory Section
.data
enterint: .asciiz "Please enter two integers: "
ans: .asciiz "The answer is: "
error: .asciiz "Error! Overflow!"
.align 2


#Program Memory Section
.text
.globl main




main:
li $v0, 4
la a0, enterint
syscall
li $v0, 5 #read in values
syscall
li $v1, 5
syscall
addi $s0, $zero, 0x00000001 #mask
addi $s1, $zero, 0x80000000 #mask2
move $s2, $v0
move $s3, $v1
move $s6, $zero
addi $s7, $zero, 0x00000020

overflow1:
addi $s6, $s6, 0x00000001 #overflow check
and $s4, $s2, $s1
sll $s2, $s2, 1
beq $s4, $zero, overflow1


overflow2;
addi $s6, $s6, 0x00000001 #overflow part two
and $s5, $s3, #s1
sll $s3, $s3, 1
beq $s5, $zero, overflow2
bgt $s6, $s7, error #print overflow error
add $a0, $zero, $v0 #first value
add $a1, $zero, $v1 #second value

jal mult
j end



mult:
beq $a0, $zero, return #if $a0 is zero, then return .
beq $a1, $zero, return #if $a1 is zero, then return .
and $t0, $a1, $v0 #check if first bit is a zero
bne $t0, $zero, notzero
sll $a0, $a0, 1 #shift a0 left one
srl $a1, $a1, 1 #shift a1 right one
jal mult #recurse
j return


notzero:
add $v0, $v0, $a0
sll $a0, $a0, 1 #shift a0 left one
srl $a1, $a1, 1 #shift a1 right one
jal mult #recurse


return: jr $ra

error:
li $v0, 4
la $a0, error
syscall
li $v0, 11 # ENDLINE
li $a0, 10
syscall
j main


end:
li $v0, 4 #Print the result
la $a0, ans
syscall
li $v0, 1
lw $a0, $v0
syscall
li $v0, 11 # ENDLINE
li $a0, 10
syscall
j main


This is so much fun!

8 comments:

Paul said...

huh? run that one by me again please..

Jonathan Gerrans said...

just so you guys aren't too awed (in case you were in danger of being so), I actually haven't gotten this program to work quite correctly as of yet, but most of what is there is how it will look when I am finished.

Paul said...

just in case you were wondering, was not awed at all:-)

Code is actually pretty cool. I wish I knew more about it.

Christy Joy said...

you and ivan are both freaks. how can you read that stuff!? makes me want to take java just so i know what it all means.

Kristin said...

Oh dear.

Jonathan Gerrans said...

You should take java. It's fun. :)
But you will not know what it All means just from taking java... I don't know what it All means, and probably never will. :P

barry said...

Glad you like it

Andrew said...

Ackkk... the pain... the bad memories... glad that class is over!

Post a Comment