Name:__________________
Quiz #2
1) Registers and Stack Conventions:
A) What rules/precautions must the Caller of a function
follow for the s-registers:
Nothing. The caller assumes that the callee will not change the s-registers.
B) What rules/precautions must a Callee of a function
follow for the s-registers:
The Callee has agreed not to change/contaminate the s-registers,
so, in order to use them, they must be backed up and restored before
the function completes.
2) Write a valid MIPS function to sum the elements of an array of integers:
(Use pointer indexing)
int sum(int *data, int size)
sum:
move $t0,$a0 # Copy pointer to the array
move $t1,$0 # Init a counter
move $v0,$0 # Init a running total
sum_for:
bge $t0,$a1,sum_end # For loop condition check
lw $t3,0($t0) # Load the current word
add $v0,$v0,$t3 # Add it to the running total
add $t0,$t0,4 # Advance the pointer to the next element of the array
add $t1,$t1,1 # Advance the for loop counter
j sum_for # Return to top of for loop
sum_end:
jr $ra # Return to the caller