A good answer might be:

See below.

Using Variables

The subroutine uses two variables so there is eight bytes of space on the stack frame for them.

#  int mysub( int arg )
#  {
#    int b,c;                     // b: 0($fp)
#                                 // c: 4($fp)
#    b = arg*2;
#    c = b + 7;
#    
#    return c;  
#  }
         .text
         .globl  mysub
mysub:
                                  # 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)

         sub     $sp,$sp,4        #   3. Push register $s1
         sw      $s1,($sp)

         sub     $fp,$sp,8        #   4. $fp = $sp - space_for_variables

         move    $sp,$fp          #   5. $sp = $fp
         
                                  # body of subroutine     
         mul     $s1,$a0,2        # arg*2
         sw      $s1,___(___)     # b = "   "
         
         lw      $t0,___(___)     # get b
         add     $t0,$t0,___      # b+7
         sw      $t0,___(___)     # c = "  "
         
         . . . . .

         jr      $ra              # return to caller 

The program is not very efficient, as written. There is no need to store and then load b. A non-optimizing compiler might do just that, however.


QUESTION 12:

Fill in the blanks. Assume that b is at displacement 0 and that c is at displacement 4.