Name: Date:
HW6 solution
-
You are writing a RISC-V assembly program. Your program has a
beqinstruction at address0x0100 0400. You wish thisbeqto branch to location0x0200 0000.-
(3 points) How close a single
beqinstruction get to the goal address? Express your answer as the address nearest to the target address as possible.The
beqis at0x0100 0400. The biggest positive number that can be put in the immediate value is0x7FF. So
0x0100 0400 + SE(0x7FF << 1) = 0x0100 0400 + SE(0xFFE)= 0x0100 13FE. Note this is not a word aligned address! -
(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
jalinstruction. For even longer ranges, the target address can be loaded into a register andjalrcan be used to jump, or chain multiplebeqandjaltogether.
-
-
You are writing a program for a processor with 8-bit instructions. The branch instructions for this processor only allow 3-bit branch immediate.
-
(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.
-
(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.
-
-
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 0x00400028jal x0, test
0x0040002cloop: lw t1, 0(t2)
0x00400030addi t3, t3, -4
0x00400034test: bne t1, x0, loop
-
(3 points) List both the byte offset and immediate used by the
jalinstruction? Show your work.Given the target
0x0040 0034:
The byte offset is0x0040 0034 - 0x0040 0028 = 0xC, there are 12 bytes between thejaland its target. Meaning there are12/4 = 3instructions (aka words) between thejaland 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 -
(3 points) What is the hex value for the immediate in the
bneinstruction? Show your work.Given a target of
0x0040 002C:
The byte offset is0x0040 002C - 0x0040 0034 = -0x8, so the immediate is half that-0x40x0040 002C = 0x0040 0034 + SE(imm << 1)
Solving results in the decimal number:
imm = -4Or in twos-compliment as a 12 bit immediate:imm = 0xFFC
-