# main()
# {
# int a, b; // a: 0($fp), b: 4($fp)
# write("enter an int:")
# read( a );
# b = fact( a );
# write("factorial is:")
# print( b );
# }
.text
.globl main
main:
# prolog
sub $sp,$sp,4 # 1. Push return address
sw $ra,($sp)
sub $sp,$sp,4 # 2. Push caller's frame pointer
sw $fp,($sp)
# 3. No S registers to push
sub $fp,$sp,8 # 4. $fp = $sp - space_for_variables
move $sp,$fp # 5. $sp = $fp
main()
The next part of main() is straightforward.
The SPIM services four and five for writing a string and
reading an integer are used.
The integer is returned in $v0.
It is saved in the variable a (on the stack).
# write("enter an int:")
li $v0,4 # print string service
la $a0,prompt1 # address of prompt
syscall
# read( a )
li $v0,5 # read integer service
syscall # $v0 gets the integer
sw $v0,0($fp) # save in variable a
Next
the code implements b = fact( a ).
This is done by following the protocol for a subroutine call,
then storing the returned value into the variable b:
Fill in the blanks.