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
enterint: .asciiz "Please enter two integers: "
ans: .asciiz "The answer is: "
error: .asciiz "Error! Overflow!"
.align 2

#Program Memory Section
.globl main

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

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

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

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

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

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

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

This is so much fun!


Paul said...

huh? run that one by me again please..

Johonn 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.

CJK 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.

Johonn 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