.data newl: .asciiz "\n" strA: .asciiz "foo" # Sample stack frame layout: # 28($sp) unused # 24($sp) $ra # 20($sp) $s1 # 16($sp) $s0 # 12($sp) some architectures pass all on the stack - this is $a3 save space # 8($sp) some architectures pass all on the stack - this is $a2 save space # 4($sp) some architectures pass all on the stack - this is $a1 save space # 0($sp) some architectures pass all on the stack - this is $a0 save space .text main: # Function Prologue: (aka, stack frame building)############################## subu $sp,$sp,32 # create stack frame sw $ra,24($sp) # save return address sw $s1,20($sp) # save $s1 - note for this example: we dont *need* this sw $s0,16($sp) # save $s0 ############################################################################## li $a0, 2 #Setup Step 1: make args available to the caller #jal squareLeaf #Setup Step 2: transfer control (with link) jal cubeNonLeaf mainEpilogue: # Function Epilog: (aka, stack frame deconstruction)########################## lw $ra,24($sp) # restore return address lw $s1,20($sp) # restore $s1 lw $s0,16($sp) # restore $s0 addu $sp,$sp,32 # release stack frame jr $ra # return ############################################################################## #------------------------------------------------------------------------------------- # int squareLeaf( int a ) # # a = $a0 = the input to square, outputted in $v0 squareLeaf: #Execute Step 3 Skipped: we need no stack resources sll $v0, $a0, 1 #Execute Step 4 & 6: Execute algorithm, make output # available to callee #Execute Step 5 Skipped: we don't need to release stack resources jr $ra #Execute Step 7: return to the (address + 4) of the calling jal #question: what if i wanted to print out the input to the cubeNonLeaf function? where #would i have to put the print? #-------------------------------------------------------------------------------------- # int cubeNonLeaf( int a ) # # a = $a0 = the input to cube, outputted in $v0 cubeNonLeaf: subu $sp,$sp,8 #Execute Step 3: Create stack frame sw $ra,0($sp) #Execute Step 3b: Save registers in memory allocated jal squareLeaf #Execute Step 4 & Setup Step 2: Execute algorithm begins #here (continues throughout), while arg is already in a0 move $a0, $v0 #Setup Step 1: Prepare the args jal squareLeaf #Setup Step 2: transfer and link #comment out the next lw for an interesting function return lw $ra, 0($sp) #Execute Step 5: restore previous state addu $sp,$sp,8 #Execute Step 5a: reclaim resources via stack frame pop #Execute Step 6 Skipped: output value is already in $v0 jr $ra #Execute Step 7: jump back