# Chapter 6 Examples 21 & 18 # # save and restore the frame pointer # this part adapted from example 6.18 .data X : .word 2 # arbitrary values choosen to test program for x Y : .word 3 # and y Z : .space 4 .text .globl main # declaration of main as a global variable main: la $8, X # store address of x into $8 pushw $8 # push this parameter onto stack - call by reference lw $8, Y # store value of y into $8 pushw $8 # push this parameter onto stack - call by value addi $sp,$sp,-4 #save space for return jal strange # call subroutine and put return address into $31 popw $9 # pop return value of function from stack into $9 sw $9, Z # store into location Z addi $sp,$sp,8 # remove parameters from stack move $a0, $9 # print value of z onto console li $v0,1 # syscall for print_decimal syscall # print_decimal z # the last 2 lines of main li $v0, 10 # syscall for exit syscall # exit your code # end of main # this part adapted from example 6.21 res = 20 # offset to return value (skip over 5 registers) p2 = 24 # offset to second parameter p1 = 28 # offset to first parameter strange: pushw $fp # save old frame pointer pushw $9 # save values in registers to be modified pushw $10 pushw $11 pushw $12 move $fp, $sp # initialize frame pointer to stack pointer lw $9, p1($fp) # load first parameter into $9 lw $10, p2($fp) # load second parameter into $10 lw $11, 0($9) # get value stored at address of first parameter mul $12,$11,$10 # z = x * y sw $12, res($fp) # save return value into proper offset add $11, $10, $11 # x = x + y sw $11, 0($9) # store value into address pointed to # by p1 popw $12 # restore modified registers popw $11 popw $10 popw $9 popw $fp # restore frame pointer jr $31 # return to caller program at next instruction # END OF PROGRAM (leave this line here)