# Illustration for the leaf procedure example (similar to example on p. 99 # Sec 2.8. Here we assume ony $s0 needs to be saved). # I have added additional code to ilustrate how the leaf procedure works. # - Yashwant #You can place breakpoints at [BPA] and [BPB] to observe registers before # and after the procedure. # # C code: # int leaf_example (int g, h, i, j) # { int f; # f = (g + h) - (i + j); # return f; # } # Arguments g, …, j in $a0, …, $a3 # f in $s0 (hence, need to save $s0 on stack) # Result in $v0 .text # text section .globl main # call main by SPIM main: li $a0, 16 #load immediate pseudoinstruction li $a1, 4 li $a2, 9 li $a3, 1 li $s0, 7 #the procedure should not change it jal leaf_example # [BPA] add $a0, $v0, $zero #place in $a0 for printing [BPB] li $v0, 1 #syscall for printing syscall # print the content of value in $a0 la $a0,str # put string address into a0 li $v0,4 # system call to print syscall # print string li $v0, 10 # 10 is the exit syscall. syscall # do the syscall. leaf_example: addi $sp, $sp, -4 #Save sw $s0, 0($sp) # $s0 on stack add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) #restore $s0 addi $sp, $sp, 4 # from stack jr $ra .data str: .asciiz ": that is the Result\n" #that's it.