Name: Box: Date:

HW5 solution

  1. (3 points) In your own words explain what "preserved across call'" means on the MIPS green sheet.

      If a register is preserved across a call that means that it's value is guaranteed to be the same after returning from the call as it was when the jal was performed. Otherwise it may or may not change, but the data is no longer safe to use.
  2. (3 points) In a few words or diagram briefly describe caller's responsibilities in the MIPS calling conventions for procedures.

    
    * move $sp
    * store $ra on stack
    * store any $t's $a's and $v's that we need after the procedure return
    * jal to procedure
    * restore any $t's $a's and $v's we need (don't accidentally overwrite the return value!)
    * restore the $ra 
    * move $sp back
    
  3. (3 points) In a few words or diagram briefly describe callee's responsibilities in the MIPS calling conventions for procedures.

    * move the $sp within the procedure
    * store any $s's that the procedure uses
    * put any return values in the $v registers
    * restore any $s's
    * move the $sp back to where it was at the procedure call
    * jr $ra
    
  4. (7 points) The following MIPS procedure (FOO) calls another procedure (BAR). This code has an error in it that does not follow the procedure calling conventions.

    FOO:   addi $sp, $sp, -8
        sw $ra, 0($sp)
        sw $a1, 4($sp)
        add $s0, $a0, $a1
        add $a0, $a1, $0
        jal BAR
        lw $t0, 4($sp)
        add $v0, $v0, $t0
        lw $ra, 0($sp)
        addi $sp, $sp, 8
        jr $ra

    a. Circle the instructions that cause the error.

     FOO overwrites data in a $s register before backing it up. Therefore when FOO returns $s0 will not be correctly preserved.

    b. Explain why this error could cause problems if the code was not changed.

     If the procedure that called FOO was using $s0 that data is now lost and the caller might make spurious calculations based on the data that FOO put into $s0.
  5. (7 points) The following snippet of MIPS procedure (FOO) that calls another procedure (BAR). This code has an error in it that does not follow the procedure calling conventions.

    FOO:   addi $sp, $sp, -8
        sw $s0, 0($sp)
        sw $a1, 4($sp)
        add $s0, $a0, $a1
        add $a0, $a1, $0
        jal BAR
        lw $t0, 4($sp)
        add $v0, $v0, $t0
        lw $s0, 0($sp)
        addi $sp, $sp, 8
        jr $ra

    a. Circle the instructions that cause the error.

     FOO does not back up $ra before the call to BAR. 

    b. Explain why this error could cause problems if the code was not changed.

     This code will loop infinitely because FOO can never return to it's caller because the return address has been lost.
  6. (7 points) The following snippet of MIPS procedure (FOO) that calls another procedure (BAR). This code has an error in it that does not follow the procedure calling conventions.

    FOO:   addi $sp, $sp, -8
        sw $s0, 0($sp)
        sw $ra, 4($sp)
        add $s0, $a0, $a1
        add $a0, $a1, $0
        jal BAR
        add $v0, $v0, $a1
        lw $ra, 4($sp)
        lw $s0, 0($sp)
        addi $sp, $sp, 8
        jr $ra

    a. Circle the instructions that cause the error.

     FOO used $a1 after the procedure call to BAR, but the value in $a1 is not preserved across a call. 

    b. Explain why this error could cause problems if the code was not changed.

     BAR may have changed $a1 (if it called another procedure, for example) and is not responsible for restoring the original value. Therefore the add after the jal in FOO may be using unintended data.