


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



There are many solutions to these problems. My solutions use as few stores and loads as possible, primarily to keep the code simple.
a) (^) lw $t0 y lw $t1 x
sub $t0 $0 $t0 #make y = - sub $t0 $t0 $t1 #y - x sw $t0 y
b) I’m assuming that i is an array offset, and not already adjusted for word alignment.
lw $t0 i #get the values for mem offsets addi $t1 $t0 - addi $t2 $t0 1
sll $t0 $t0 2 #insure word alignment sll $t1 $t1 2 sll $t2 $t2 2
lw $t1 a($t1) #fetch the values lw $t2 a($t2) add $t1 $t1 $t2 #do the add sw $t1 a($t0) #store the value
c) A clever coder could do this without the expensive branch, but it would make the code harder to read.
lw $t0 x lw $t1 y
sgt $t2 $t0 $t1 #is x > y? bne $t2 $0 else #looks like x is < y, so go to else case sub $t0 $t0 $t1 #set x = x - y j done #the new x is done, go to store it else: sub $t0 $t1 $t0 #set x = y -x done: sw $t0 x #store the new x
d) Since there is no guarantee that the loop will execute even one time, i must be checked before executing any loop code.
lw $t0 i #get i
j test #jump to the test while: srl $t0 $t0 1 #shift right 1 test: andi $t0 $t0 1 #and i with 1 beq $t0 $0 while #if test is met, run loop again sw $t0 i #store final value
e) Since the loop will always run the first time, my code enters the loop with i equal to 0. Upon entering the loop structure, i is immediately incremented. For this reason, the loop runs from 1 to 9, since after the eighth time it will return to the top of the loop and begin the ninth and final run.
addi $t0 $0 $0 #set ’i’ to 0 for: addi $t0 $t0 1 #do the i++ part (the first time through, this will make i=1) sll $t1 $t0 2 #multiply for word alignment add $t2 $t0 $t0 #set i = i + i addi $t2 $t2 1 #set i = i + 1 sw $t2 a($t1) #store the new i in memory, using word aligned value slti $t1 $t0 9 #check if i < 9 (because we should stop the 10th time) bne $t1 $0 for #if i is still < 9, do the loop again
f ) Note that this is a fairly complicated address redirection. For the purpose of this class, we will be assuming that the array is filled with indices and not memory pointers.
lw $t0 x #get x sll $t0 $t0 2 lw $t1 a($t0) #get value at a[x] sll $t1 $t1 2 lw $t1 a($t1) #get value at a[a[x]] sw $t1 a($t0) #store the result back in a[x]
a) add $t0 $0 $ add $t1 $0 $ add $t2 $0 $ add $t3 $0 $ add $t4 $0 $ add $t5 $0 $ add $t6 $0 $ add $t7 $0 $ add $t8 $0 $
d) Searches an array to find the first element with a value of zero and returns its index. Assuming the array is terminated by an element with the value of zero (for example, a string), this code would get the length of the array. la $t1,a #gets a pointer to the array ‘a’ loop: lw $t0,($t1) #stores the value of a[$t1] into $t addi $t1,$t1,4 #increments to the next element in the array bne $t0,$0,loop #if the element at a[$t1] is not a zero, keep looking la $t0,a #gets the address of the start of array ‘a’ sub $t0,$t1,$t0 #subtracts the address of ‘a’ from the index where the zero was found sra $t0,$t0,2 #divides the result by 4
In order to reduce the complexity, this code starts at the last element in a and works towards the first. This allows for a simple check for the memory offset to be 0. To find the odd elements, each element is logically ‘and’ed with 1. Since the least significant bit in binary controls whether the value is odd, odd & 1 = 1 while even & 1 = 0.
a: .word -3, 17, 12, 101, -51, 42, 16, 2, 17, -
main: add $t0 $0 $0 #start with the total at zero addi $t1 $0 10 #start at index 10
loop: addi $t1 $t1 -1 #decrement the offset counter sll $t4 $t1 2 #align the value for memory access lw $t2 a($t4) #get the value at a[$t1] andi $t3 $t2 1 #and with 1, the return should be 0 if even bne $t3 $0 check #if the value is not 0 (not even), run loop again add $t0 $t2 $t0 #keep a running sum of the even values check: bne $t1 $0 loop #if we are at the zeroth element, we’re done j $