See below
Here
is the code for main.
# main()
# {
# int a; // a: 0($fp)
# a = mysub( 6 );
# print( a );
# }
.text
.globl main
main:
# prolog
# 1. when main gets control from QTSPIM
# the stack pointer points to the top
# of the stack.
sub $fp,$sp-4 # 2. $fp = $sp - space_for_variables
move $sp,$fp # 3. $sp = $fp
# subroutine call
# 7. No T registers to push
li $a0,6 # 8. Put argument into $a0
jal mysub # 9. Jump and link to subroutine
# regaining control from the subroutine
# Return value is in $v0
# 10. No T registers to restore
sw $v0,() # a = mysub( 6 )
# print a
lw $a0,() # load a into $a0
li $v0,1 # print integer service
syscall
# epilog
li $v0,10 # 11. return to OS
syscall # 12.
In
this example, main()
did not save any T registers.
It does not need to restore them.
The value the subroutine returned is in
$v0.
This value is assigned to variable a .
Look down to the section where main
regains control.
Fill in the blanks that follow so that the
value returned by the subroutine
(contained in $v0) is copied
to the variable a on the stack.
The next few statements
load $a0 with the value
of a from the stack,
and then print that value by using
a SPIM service.
This could have been done
without the stack.
The return value could have been copied directly into $a0.
But this example shows the type of
code that a non-optimizing compiler
might produce.
Fill in the blanks.