Wide Values and Offsets: Lui, Auipc, Branches and Jumps
Twelve bits of immediate cannot hold an address of 32 bits, or say how far a jump goes. Three more formats do that work. Their bits look scrambled until you see what they keep fixed.
You Will Be Able To
- Build a constant of 32 bits from lui and addi, and correct the upper part when bit 11 is 1.
- Work out the target of a branch or of jal from the address of the instruction and its offset.
- Collect the bits of a B or J offset from an instruction word, in the right order.
- Lesson
- S2.18
- Time
- 30 min
- Runs on
- Your browser
- Check
- 6 questions
The loop of S2.1 ends with bne x5, x0, 0x8. Its word is fe029ce3. Cut at the six places, that word holds no 8 anywhere. The branch still goes to address 8, every time.
Before You Start
You need the six fields and the I and S formats from S2.2. You need sign extension from the same page.
You need the loop of S2.1, with its branch back to address 8.
| Question | |
|---|---|
| 1 | How does a program put a constant of 32 bits into a register? |
| 2 | When must the upper part of such a constant be one larger than it looks? |
| 3 | From where is the offset of a branch counted? |
| 4 | Why is bit 0 of an offset not stored? |
| 5 | Which bits of a branch word are out of line with a store, and why only those? |
The Idea: Two Halves for a Value, an Offset for a Target
An immediate of 12 bits stops at 2047. A real program needs 8000_0000, the address of the SRAM. No instruction of 32 bits can carry 32 bits of constant and an opcode as well.
So a wide constant is built in two steps. The first instruction sets the upper 20 bits. It is lui, short for load upper immediate. The second is an ordinary addi, which fills in the lower 12 bits. Twenty and twelve make 32.
The format with 20 bits of immediate is the U format. A second instruction uses it, called auipc. It adds the upper 20 bits to the PC and not to 0. That gives an address near the code, wherever the code was placed.
A branch has the same shortage. It must name a target, and it has only 12 bits. So it stores how far away the target is, and not where it is. That distance is the PC-relative offset: the target is the address of the branch itself plus the offset.
Most branches go a few instructions away, so a short offset is enough. The jump jal has more room, 20 bits, for the longer way to another function. Its format is the J format, and that of the branches is the B format.
New Words
| Word | Meaning |
|---|---|
| U format | The format with an immediate of 20 bits in bits 31 to 12, and rd. Used by lui and auipc. |
lui | Load upper immediate: writes the 20 bits into the upper part of rd, and 0 into the lower 12 bits. |
auipc | Add upper immediate to PC: writes the PC of this instruction plus the 20 bits shifted left by 12. |
| PC-relative offset | The distance in bytes from an instruction to its target. Offset for short. It may be negative. |
| B format | The format of the branches: rs1, rs2 and an offset of 13 bits, of which 12 are stored. |
| J format | The format of jal: rd and an offset of 21 bits, of which 20 are stored. |
| MiB | A mebibyte: 1024 times 1024 bytes, which is 1 048 576. |
| Reach of an offset | How far from itself an instruction can name a target. |
One Picture: The Offset of a Branch
- Control / FSM
- Memory
- Start from a store
A branch has rs1 and rs2 and no rd, like a store. So its 12 stored bits sit where the immediate of a store sits.
- Bit 0 is not stored
An offset is always even. Its bit 0 is 0, and no bit of the word is spent on it. The 12 stored bits are offset bits 12 to 1.
- Ten bits stay where they were
Bits 30 to 25 give offset bits 10 to 5. Bits 11 to 8 give offset bits 4 to 1. A store reads the same bits for the same places.
- The sign stays in bit 31
Bit 31 of the word is the sign, as in every format. Here it is offset bit 12.
- One bit is out of line
That leaves offset bit 11. It goes to bit 7 of the word, the place that held bit 0 of a store.
Read the diagram as text
- Bit 31 of the word: one bit
- Bit 7 of the word: one bit, in the place where rd would be
- Bits 30 to 25 of the word: six bits
- Bits 11 to 8 of the word: four bits
- Offset bit 12 and above: the sign, copied up to bit 31
- Offset bit 11: the one bit that is out of line
- Offset bits 10 to 5: in the same place as in a store
- Offset bits 4 to 1: in the same place as in a store; bit 0 below them is always 0
- Bit 31 of the word → Offset bit 12 and above: the sign
- Bit 7 of the word → Offset bit 11: bit 11
- Bits 30 to 25 of the word → Offset bits 10 to 5: bits 10 to 5
- Bits 11 to 8 of the word → Offset bits 4 to 1: bits 4 to 1
The B format is the S format with two bits moved. The J format moves bits in the same spirit.
| Format | Bit 31 | 30 to 25 | 24 to 21 | Bit 20 | 19 to 12 | 11 to 8 | Bit 7 | 6 to 0 |
|---|---|---|---|---|---|---|---|---|
| B | offset 12 | offset 10 to 5 | rs2 | rs2 | rs1, funct3 | offset 4 to 1 | offset 11 | opcode |
| U | value 31 | value 30 to 25 | value 24 to 21 | value 20 | value 19 to 12 | rd | rd | opcode |
| J | offset 20 | offset 10 to 5 | offset 4 to 1 | offset 11 | offset 19 to 12 | rd | rd | opcode |
Read down the columns. Offset bits 10 to 5 are always in bits 30 to 25. The sign is always in bit 31.
The Smallest Example, Worked by Hand
A wide constant: 1234_5678. Split it after the upper five hex digits. The upper part is 12345, and the lower part is 678.
| Instruction | What it writes | x5 afterwards |
|---|---|---|
| lui x5, 0x12345 | the upper 20 bits, and 0 below | 1234_5000 |
| addi x5, x5, 0x678 | adds the lower part | 1234_5678 |
A harder one: 1234_5fff. The lower part is fff. As a 12-bit immediate, fff is -1, because addi sign-extends. Adding -1 to 1234_5000 gives 1234_4fff. That is 4096 too small.
The cure is to start one higher. Write 12346 into the upper part, and let the -1 bring it down: 1234_6000 less 1 is 1234_5fff. The rule: when bit 11 of the constant is 1, add 1 to the upper part.
A branch offset: the word fe029ce3, at address 10. S2.2 cut it into 1111111, 00000, 00101, 001, 11001 and 1100011. Collect the offset from the picture.
| Offset bits | Come from | Value |
|---|---|---|
| 12, the sign | bit 31 of the word | 1 |
| 11 | bit 7 of the word: the last bit of 11001 | 1 |
| 10 to 5 | bits 30 to 25 of the word | 111111 |
| 4 to 1 | bits 11 to 8 of the word: the first four bits of 11001 | 1100 |
| 0 | not stored | 0 |
In order: 1 1 111111 1100 0. As a number of 13 bits in two's complement, that is -8.
The branch is at address 10 in hex, which is 16. Sixteen less 8 is 8. So the target is address 8, the start of the loop.
The Rules
Rule 1: lui writes its 20 bits into bits 31 to 12 of rd, and 0 into bits 11 to 0.
auipc writes the same value plus the PC of the auipc itself.
Rule 2: A constant of 32 bits is a lui and an addi.
When bit 11 of the constant is 1, the upper part must be one larger.
Rule 3: The target of a branch or of jal is the PC of that instruction plus its offset.
The offset is counted from the instruction itself, not from the next one.
Rule 4: Bit 0 of an offset is always 0 and is not stored.
So 12 stored bits reach 4094 bytes forward and 4096 back. Twenty stored bits reach about 1 MiB each way.
Rule 5: In the B format, only offset bits 12 and 11 are out of line with a store.
Bit 12 is in bit 31 of the word, and bit 11 is in bit 7.
Rule 6: The J format stores, from bit 31 down, offset bits 20, 10 to 1, 11, and 19 to 12.
The sign is in bit 31 again.
Sources for These Rules
Every encoding fact on this page is stated as commonly described. That covers the U, B and J formats, the place and the reach of each offset, and what lui and auipc write. The RISC-V specifications are not in the reference library. So none of these facts is checked against them. Each one was compared with the instruction tables of the assembler of this track.
The plan of this track points to two textbooks for this material. They are Patterson and Hennessy, sections 2.5 and 2.10, and Harris and Harris, section 6.4. Those places are not checked either.
Now Run It
The lab at the end of this page builds the two constants of the hand example, and then runs one auipc.
00000000 | lui x5, 0x12345 | x5 = 12345000
00000004 | addi x5, x5, 1656 | x5 = 12345678
00000008 | lui x6, 0x12346 | x6 = 12346000
0000000c | addi x6, x6, -1 | x6 = 12345fff
00000010 | auipc x7, 0x1 | x7 = 00001010
00000014 | ebreak | stop
instructions=5Rule 1 at work: rows 1 and 3. After each lui, the lower three hex digits are 000.
Rule 2 at work: rows 3 and 4. The upper part is 12346, one more than the constant shows. The addi of -1 lands on 1234_5fff.
Rule 1 again: row 5. The auipc is at address 10 in hex. It writes 10 plus 1000, both in hex.
Now two instructions with offsets. This program jumps forward over two rows, and then loops once:
00000000 | addi x5, x0, 2 | x5 = 00000002
00000004 | jal x1, 0x10 | x1 = 00000008, next is 00000010
00000010 | addi x5, x5, -1 | x5 = 00000001
00000014 | bne x5, x0, 0x10 | taken: next is 00000010
00000010 | addi x5, x5, -1 | x5 = 00000000
00000014 | bne x5, x0, 0x10 | not taken
00000018 | ebreak | stop
instructions=6Rule 3 at work: row 2. The jal is at address 4 and lands on 10. Its offset is 12. It also writes 8 into x1: the address of the row after it. S2.3 uses that value.
Here are the words with offsets from this page, cut at the six places. Below them are their immediates, from the instruction tables:
fe029ce3 | B | 1111111 00000 00101 001 11001 1100011
fe029ee3 | B | 1111111 00000 00101 001 11101 1100011
7e628fe3 | B | 0111111 00110 00101 000 11111 1100011
00c000ef | J | 0000000 01100 00000 000 00001 1101111
f01ff06f | J | 1111000 00001 11111 111 00000 1101111
123452b7 | U | 0001001 00011 01000 101 00101 0110111
fe029ce3 | bne x5, x0, 0x8 | immediate -8 = fffffff8
fe029ee3 | bne x5, x0, 0x10 | immediate -4 = fffffffc
7e628fe3 | beq x5, x6, 0xffe | immediate 4094 = 00000ffe
00c000ef | jal x1, 0x10 | immediate 12 = 0000000c
f01ff06f | jal x0, 0x0 | immediate -256 = ffffff00
123452b7 | lui x5, 0x12345 | immediate 305418240 = 12345000Rule 5 at work: rows 1 and 2 differ in one bit of the fifth group. That bit is in bits 11 to 8, so the offsets differ by 4: -8 and -4.
Rule 4 at work: row 3 is the farthest branch forward. Every stored bit except the sign is 1, and the offset is 4094.
Rule 6 at work: rows 4 and 5 are jumps. Row 4 holds 01100 in its second group. That is offset bits 4 to 1 and bit 11: an offset of 12.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| An offset from the PC | The full address of the target | Twelve bits are enough for most branches, and code works at any address. Far targets need more instructions. |
| Bit 0 of the offset not stored | Store every bit | Twice the reach from the same bits. Targets must be at even addresses. |
| Most offset bits in the same place in S, B and J | The offset in plain order in each format | Each bit of the immediate has few possible sources, so the hardware is small. The word is hard to read by eye. |
| Two instructions for a wide constant | Instructions longer than 32 bits | Every instruction keeps one length. A wide constant costs two instructions. |
| auipc beside lui | lui only | Addresses near the code work wherever the code is placed. One more instruction to build and to check. |
The Trap: The Constant That Came Out 4096 Too Small
A programmer needs 1234_5fff in x6. The split looks obvious: the upper five digits for lui, the lower three for addi. The lower three are fff. The assembler takes fff as -1, since that is what those 12 bits mean.
00000000 | lui x6, 0x12345 | x6 = 12345000
00000004 | addi x6, x6, -1 | x6 = 12344fff
00000008 | ebreak | stop
instructions=2Read the last value: 1234_4fff. The fourth digit is wrong, and the result is 4096 too small.
Nothing failed. Both instructions are legal, and half of all constants split this way come out right. The error shows only when bit 11 of the constant is 1.
The check is rule 2. In practice, write the pseudo-instruction li with the whole constant, and let the assembler split it. The assembler of this track then makes the two instructions of the lab.
At Scale
This section looks ahead. Compilers use these few instructions for every address in a program.
Too far for a branch
When a target is beyond the reach of a branch, the tools turn the branch round and put a jal behind it. Common practice.
Too far for a jal
A call to anywhere in 32 bits is an auipc and a jalr together. S2.3 meets jalr.
The immediate generator
One block of the processor builds all five immediates from the word. You write it in the practice of this page.
Addresses of devices
Every device address of ap_soc is wide. Reaching UART0 at 1000_0000 starts with a lui.
Try It
Run the lab. Check rows 1 to 4 against the hand example.
Change the rows for x6 so that it ends with 8000_0800. Decide first whether the upper part needs the correction.
Move the auipc row to the top of the program. Predict what x7 holds before you run it.
00000000 | lui x5, 0x12345 | x5 = 12345000 00000004 | addi x5, x5, 1656 | x5 = 12345678 00000008 | lui x6, 0x12346 | x6 = 12346000 0000000c | addi x6, x6, -1 | x6 = 12345fff 00000010 | auipc x7, 0x1 | x7 = 00001010 00000014 | ebreak | stop instructions=5Check Yourself
Common Mistakes
Wrong Split a constant in the middle and stop.
When bit 11 is 1, the addi subtracts, and the result is 4096 too small.
Fix Add 1 to the upper part in that case, or write li and let the assembler do it.
Wrong Count a branch offset from the next instruction.
In RISC-V the offset is added to the address of the branch itself.
Fix Target = address of the branch + offset.
Wrong Read the 12 stored bits of a branch as an S immediate.
Two bits are in other places, and the whole offset is shifted left by one.
Fix Collect bit 12 from bit 31, bit 11 from bit 7, and put a 0 at the bottom.
Remember
- lui sets the upper 20 bits. A lui and an addi build any constant of 32 bits.
- When bit 11 of the constant is 1, the upper part must be one larger.
- The target of a branch or jal is the address of that instruction plus its offset.
- Bit 0 of an offset is not stored. The sign is in bit 31. Only a few bits are out of line.