Name: Date:

HW6 solution

  1. You are writing a RISC-V assembly program. Your program has a beq instruction at address 0x0100 0400. You wish this beq to branch to location 0x0200 0000.

    1. (3 points) How close a single beq instruction get to the goal address? Express your answer as the address nearest to the target address as possible.

      The beq is at 0x0100 0400. The biggest positive number that can be put in the immediate value is 0x7FF. So
      0x0100 0400 + SE(0x7FF << 1) = 0x0100 0400 + SE(0xFFE)= 0x0100 13FE. Note this is not a word aligned address!

    2. (3 points) What is a possible method to allow long-range conditional branches?

      Since branches can only go as far as the 12-bit immediate allows (\(-2^{11} -- 2^{11}-1\) ), longer ranges can be implemented by branching to a jal instruction. For even longer ranges, the target address can be loaded into a register and jalr can be used to jump, or chain multiple beq and jal together.

  2. You are writing a program for a processor with 8-bit instructions. The branch instructions for this processor only allow 3-bit branch immediate.

    1. (3 points) Assuming the immediate is sign extended, what is the branch range of the processor? Answer in byte units.

      Since there are 3 bits, \(2^3 = 8\) possible locations, for a range of -4 to 3 instructions.

    2. (3 points) What is the difficulty with such a branch range? Write out a simple while loop example to illustrate the difficulty.

      It is very limited and makes writing loops and conditions very hard. For instance, for a typical while loop, you will need an instruction to increment a counter, another instruction to jump back to the conditional, leaving only one instruction to do meaningful computation.

  1. Compute the correct hex value for the branch targets below, give your answers in the correct number of digits in hexidecimal, including any leading zeros.

    Address Instruction
    0x00400028
          jal x0, test            
    0x0040002c
    loop: lw   t1, 0(t2)  
    0x00400030
          addi t3, t3, -4 
    0x00400034
    test: bne  t1, x0, loop
    1. (3 points) List both the byte offset and immediate used by the jal instruction? Show your work.

      Given the target 0x0040 0034 :
      The byte offset is 0x0040 0034 - 0x0040 0028 = 0xC, there are 12 bytes between the jal and its target. Meaning there are 12/4 = 3 instructions (aka words) between the jal and its target. Since the immediate is in units of half words we divide the byte offset by 2 to get the immediate: 0x6.

      Using the equation from the greensheet all this gets put together: 0x0040 0034 = 0x0040 0028 + SE(imm << 1)
      Solving results in the 20 bit immediate: imm = 0x0 0006

    2. (3 points) What is the hex value for the immediate in the bne instruction? Show your work.

      Given a target of 0x0040 002C:
      The byte offset is 0x0040 002C - 0x0040 0034 = -0x8, so the immediate is half that -0x4

      0x0040 002C = 0x0040 0034 + SE(imm << 1)
      Solving results in the decimal number:
      imm = -4 Or in twos-compliment as a 12 bit immediate: imm = 0xFFC