Friday, October 4, 2013

Assign 2 - 2.1 MIPS

.data
f: .word 1 # Initial declaration of f
g: .word 2 # Initial declaration of g
h: .word 3 # Initial declaration of h
i: .word 4 # Initial declaration of i
bf: .word 1 # Initial declaration of f
bg: .word 2 # Initial declaration of g
bh: .word 3 # Initial declaration of h
bi: .word 4 # Initial declaration of i
space: .asciiz "\n" # Print New Line
.text
__start:
jal exercise211a
jal resetvariables
jal exercise211b
jal resetvariables
jal exercise214a
# no storing result so no need for reset
jal exercise214b
j done
resetvariables: #should really be done through a stack in the real world
#however, for the purpose of this assignment, this is much faster
#and easier on the eyes
lw $t0, bf
sw $t0, f
lw $t0, bg
sw $t0, g
lw $t0, bh
sw $t0, h
lw $t0, bi
sw $t0, i
jr $ra
exercise211a: #2.1.1 - 2.1.3 a
lw $t2, g #4($s1) replaced with g
lw $t3, h #8($s1) replaced with h
sub $t1, $t2, $t3 #$t1 = g - h
sw $t1, f #store $t1 into f
#end exercise problem - begin post processing
move $a0, $t1 #set print int into $a0
addi $sp, $sp, -4 #move stack to save jump pointer
sw $ra, 0($sp) #pointer saved into stack
jal printint #launch print procedure
lw $ra, 0($sp) #load previous pointer
addi $sp, $sp, 4 #pop stack
jr $ra
#end exercise211a
exercise211b: #2.1.1 - 2.1.3 b
lw $t3, h #8($s1) replaced with h
addi $t3, $t3, -5 #add -5 to h and store result in $t3
lw $t2, g #4($s1) replaced with g
add $t1, $t3, $t2 #add g and h, store in f, $t1
sw $t1, f #store $t1 into f
#end exercise problem - begin post processing
move $a0, $t1 #set print int into $a0
addi $sp, $sp, -4 #move stack to save jump pointer
sw $ra, 0($sp) #pointer saved into stack
jal printint #launch print procedure
lw $ra, 0($sp) #load previous pointer
addi $sp, $sp, 4 #pop stack
jr $ra #end exercise211b
exercise214a:
lw $t1, f #store into temp reg
addi $t1, $t1, 4 #add 4 to f
#end exercise problem - begin post processing
move $a0, $t1 #set print int into $a0
addi $sp, $sp, -4 #move stack to save jump pointer
sw $ra, 0($sp) #pointer saved into stack
jal printint #launch print procedure
lw $ra, 0($sp) #load previous pointer
addi $sp, $sp, 4 #pop stack
jr $ra #end exercise214a
exercise214b:
lw $t1, f #store into temp reg
lw $t2, g #store into temp reg
lw $t3, h #store into temp reg
lw $t4, i #store into temp reg
add  $t1, $t2, $t3 #f = g + h
add  $t1, $t4, $t1 #f = i + f
#end exercise problem - begin post processing
move $a0, $t1 #set print int into $a0
addi $sp, $sp, -4 #move stack to save jump pointer
sw $ra, 0($sp) #pointer saved into stack
jal printint #launch print procedure
lw $ra, 0($sp) #load previous pointer
addi $sp, $sp, 4 #pop stack
jr $ra #end exercise214b
printint: addi $v0, $zero, 1 # load appropriate system call code into register $v0;
# code for printing integer is 1
syscall # call operating system to perform operation
addi $v0, $zero, 4   # load appropriate system call code into register $v0;
# code for printing string is 4
la $a0, space       # load address of the string
syscall
jr $ra
done:
#program output:
#-1
#0
#5
#9

No comments:

Post a Comment