Name: Date:

HW7 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 can the beq 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 can be loaded into a register and jalr can be used to jump.

  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?

      Since there are 3 bits, \(2^3 = 8\) possible locations, for a range of -4 to 3 instructions (assumming you shift the immediate offset 2 bits to get word-aligned offsets)

    2. (3 points) What is the problem with such a branch range?

      It is very limited and makes writing loops and conditions very hard.

  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