Name: Date:

HW7 solution

  1. (3 points) In your own words explain what the "saver" column means on the RISC-V green sheet register table, make sure you explain the difference between what ''caller'' and ''callee'' mean in this table.

      ''Saver'' specifies who is responsible for the data during a procedure call.
      If a 'caller' is responsible then any data in those registers may be changes
      during a procedure call, and if the caller needs that data in the future they 
      must back up the data before the call. 
      If a 'callee' is responsible then the callee must ensure that any such registers 
      it uses must have the same value they started with before the procedure
      returns. Meaning they must back them up before modifying them (or use them
      carefully as in sp).
      All 'caller' registers are 'unsafe' to use after a procedure call without first
      modifying them, either restoring their data from the stack or putting some other
      value into them. With the exception of function return values.
  2. (3 points) In a few words or diagram briefly describe caller's responsibilities in the RISC-V calling conventions for procedures. For full credit your answer must address how all the different kinds of registers are used before and after a calling convention. Including t's, s's, a's, ra, and sp.

    
    * move sp
    * store ra on stack
    * store any t's and a's  that we need after the procedure return
    * jal to procedure
    * restore any t's and a'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 RISC-V calling conventions for procedures. For full credit address all the register types listed in the previous problem.

    * move the sp within the procedure
    * store any s's that the procedure uses
    * put any return values in a0/a1 registers
    * restore any s's
    * move the sp back to where it was at the procedure call
    * jalr x0, 0(ra)
    
  4. (7 points) The following RISC-V 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, x0
        jal ra, BAR
        lw t0, 4(sp)
        add a0, a0, t0
        lw ra, 0(sp)
        addi sp, sp, 8
        jalr x0, 0(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 RISC-V 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, x0
        jal ra, BAR
        lw t0, 4(sp)
        add a0, a0, t0
        lw s0, 0(sp)
        addi sp, sp, 8
        jalr x0, 0(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 RISC-V procedure (FOO) that calls another procedure (BAR). This code has an error in it that does not follow the procedure calling conventions. The error in this one is subtle. If you get stuck consult the green sheet and your answers to problems 1 and 2.

    FOO:   addi sp, sp, -8
        sw s0, 0(sp)
        sw ra, 4(sp)
        add s0, a0, a2
        add a0, a2, x0
        jal ra, BAR
        add a0, a0, a2
        lw ra, 4(sp)
        lw s0, 0(sp)
        addi sp, sp, 8
        jalr x0, 0(ra)

    a. Circle the instructions that cause the error.

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

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

    BAR may have changed a2 (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.