$\require{cancel}$
1. do some work 2. call function foo 3. do some more work 4. call function foo 5. do some more work
jal jumpAddr
R[31] = pc+8, $pc = jumpAddr
jr $rs
pc = $rs
jal
jr
) to the address pointed to by $ra when finished.
const int ARRAY_SIZE{4}; const int array[ARRAY_SIZE] {3,5,7,9}; void Print(int i); int main() { int i; for (i = 0; i < ARRAY_SIZE; ++i) { Print(i) } return 0; } void Print(int i) { cout <<"A[" << i << "] = " << array[i] << endl; }
.data arraySize: .word 4 array: .word 3 5 7 9 part1: .asciiz "a[" part2: .asciiz "] =" endl: .asciiz "\n" .text .globl main main: # $t1 will hold the index # $t0 will hold the max value lw $t0, arraySize li $t1, 0 TopOfLoop: beq $t0, $t1 done jal print addi $t1, $t1, 1 j TopOfLoop done: li $v0 10 syscall # expect $t1 to contain the index of the element to print # we will clobber $t2 in this function print: la $a0, part1 li $v0, 4 syscall move $a0, $t1 li $v0, 1 syscall la $a0, part2 li $v0, 4 syscall mul $t2, $t1, 4 lw $a0, array($t2) li $v0, 1 syscall la $a0, endl li $v0, 4 syscall jr $ra
print
is a leaf function, so life is easy
a
, v
and ra
are expected to be semi preserved.