The RISC-V ISA in One Picture
A real processor needs a fixed list of instructions that hardware and software both obey. The base list of RISC-V has 40 instructions and 32 registers, and it fits on one map.
You Will Be Able To
- Name the registers of RV32I, and say what is special about register x0.
- Sort an instruction into its group, and say what that group may read and write.
- Write a sum of two memory words as loads, an add and a store, and say why it takes four instructions.
- Lesson
- S2.1
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A new engineer opens a log from the processor of a chip. Every line starts with a short word: lw, add, bne. After it come names she has never met, and sometimes a number in front of a bracket. By the end of this page, she can read every line.
Before You Start
You need the teaching processor of S0.6: its registers, its PC, and its LOAD, STORE, ADD and JUMPNZ. This page gives those parts their real names.
You need addresses and the memory map from S0.2. You do not need to know how an instruction is stored as bits. That is the subject of S2.2.
| Question | |
|---|---|
| 1 | How many registers does RV32I have, and how wide are they? |
| 2 | What happens when a program writes to register x0? |
| 3 | Which groups make up the 40 base instructions? |
| 4 | Which instructions may touch memory? |
| 5 | Why does adding two words of memory take four instructions? |
The Idea: One Short List That Both Sides Obey
The processor of S0.6 had four registers and six instructions. Its list of instructions was ours to invent. A real chip cannot work that way. A compiler is a tool that turns a program into instructions. The people who write the compiler never meet the people who build the processor.
So both sides work from one written list. It names every register and every instruction, and says exactly what each instruction does. This list is called an instruction set architecture, or ISA for short.
RISC-V is an ISA that anyone may build without a fee. Its smallest complete form is called RV32I. The registers are 32 bits wide, and the letter I stands for the base set of integer instructions.
RV32I is small on purpose. It has 32 registers and 40 instructions. This page sorts them into a few groups, so that you never need to learn 40 separate things.
Programs are shown here in assembly. Assembly is text with one line for each instruction: a short name, then the registers and numbers it works on. An assembler is a tool that turns that text into the bits of the instructions.
New Words
| Word | Meaning |
|---|---|
| Instruction set architecture (ISA) | The written list of registers and instructions that a processor and its software both obey. |
| RISC-V | An ISA that anyone may build without a fee. It is said "risk five". |
| RV32I | The base form of RISC-V with registers of 32 bits: 32 registers and 40 instructions. |
| Assembly | A program as text, one line for each instruction. |
| Zero register | The register x0. It always reads as 0, and writes to it are thrown away. |
| Operand | One of the things an instruction works on: a register or a number. |
| Destination register | The register that an instruction writes. In assembly it is written first. |
| Immediate | A constant that is stored inside the instruction itself. |
| Instruction group | A set of instructions that read and write the same kinds of thing. |
lw | Load word: the instruction that copies a word of 32 bits from memory into a register. |
sw | Store word: the instruction that copies a register into a word of memory. |
| Load-store design | An ISA in which only loads and stores touch memory. Everything else works on registers. |
| Branch | An instruction that compares two registers and, if the test holds, writes a new address into the PC. |
| Jump | An instruction that always writes a new address into the PC. |
ebreak | An instruction that stops a program so that it can be inspected. The lab stops when it reaches one. |
| Instruction-level model | A program that acts out one instruction at a time, with no clock. It shows what each instruction does. |
One Picture: The Map of RV32I
- Processor
- Control / FSM
- Memory
- Thirty-two registers
The registers are named x0 to x31. Each holds 32 bits. The PC is a thirty-third value, and no instruction names it.
- Register x0
Register x0 is not a store of anything. It reads as 0, and a write to it is thrown away.
- Arithmetic and logic: 19 instructions
Two registers go in, or one register and a constant from the instruction. One register comes back. Memory is not touched.
- Loads: 5 instructions
A load copies a value from memory into one register. The address is a register plus a constant.
- Stores: 3 instructions
A store copies one register into memory. Loads and stores are the only instructions that use the bus for data.
- Branches and jumps: 8 instructions
A branch compares two registers, and may write the PC. A jump always writes it.
Read the diagram as text
- Program counter (PC): 32 bits: the address of the next instruction
- Zero register: x0: always reads as 0
- Registers x1 to x31: 31 values of 32 bits each
- Arithmetic and logic: add, subtract, compare, shift, and logic on each bit
- Compare for a branch: equal, less than, and their opposites
- Memory and devices: reached over the bus, by address
- Registers x1 to x31 → Arithmetic and logic: two registers, or one and a constant
- Arithmetic and logic → Registers x1 to x31: the result
- Memory and devices → Registers x1 to x31: load
- Registers x1 to x31 → Memory and devices: store
- Registers x1 to x31 → Compare for a branch: two registers
- Compare for a branch → Program counter (PC): branch or jump: a new PC
- Zero register → Arithmetic and logic: a free 0
The whole base set on one map. Five instructions are left: two that build large constants and three for the system.
| Group | How many | Instructions | Reads | Writes |
|---|---|---|---|---|
| Arithmetic and logic on two registers | 10 | add sub sll slt sltu xor srl sra or and | two registers | one register |
| Arithmetic and logic with an immediate | 9 | addi slti sltiu xori ori andi slli srli srai | one register and the immediate | one register |
| Loads | 5 | lb lh lw lbu lhu | one register, then memory | one register |
| Stores | 3 | sb sh sw | two registers | memory |
| Branches | 6 | beq bne blt bge bltu bgeu | two registers | the PC, if the test holds |
| Jumps | 2 | jal jalr | the PC, or one register | the PC, and one register |
| Large constants | 2 | lui auipc | the immediate, or the PC too | one register |
| System | 3 | fence ecall ebreak | nothing | nothing in the registers |
The names follow one pattern. A final i means "with an immediate". The letters b, h and w mean a byte, a half word of 16 bits, and a word.
You have met most of these already. lw is the LOAD of S0.6, sw is its STORE, and bne is close to its JUMPNZ. The large constants and the jump offsets come back in S2.18, and calls in S2.3.
The Smallest Example, Worked by Hand
Two numbers sit in memory, at 8000_0000 and 8000_0004. The task is to add them, and to put the sum at 8000_0008. Register x10 already holds the address 8000_0000.
No instruction of RV32I adds memory to memory. So the program has three parts: bring the numbers in, add them, and send the sum out.
- Processor
- Memory
- lw x5, 0(x10)
The address is x10 plus 0, which is 8000_0000. The word there is 7, and it goes into x5.
- lw x6, 4(x10)
The address is x10 plus 4. The word there is 5, and it goes into x6.
- add x7, x5, x6
Seven plus 5 is 12, and 12 goes into x7. No bus transfer carries data here.
- sw x7, 8(x10)
The address is x10 plus 8. The 12 in x7 is written to that word.
Read the diagram as text
- Value of x10: 8000_0000: the address of the data
- Value of x5: becomes 7
- Value of x6: becomes 5
- Value of x7: becomes 12
- Word at 8000_0000: holds 7
- Word at 8000_0004: holds 5
- Word at 8000_0008: becomes 12
- Word at 8000_0000 → Value of x5: lw x5, 0(x10)
- Word at 8000_0004 → Value of x6: lw x6, 4(x10)
- Value of x5 → Value of x7: add
- Value of x6 → Value of x7: add
- Value of x7 → Word at 8000_0008: sw x7, 8(x10)
Two loads, one add, one store. The sum of two words in memory takes four instructions.
| PC | Assembly | It reads | It writes | Data on the bus |
|---|---|---|---|---|
| 0 | lw x5, 0(x10) | x10, then the word at 8000_0000 | x5 = 7 | one read |
| 4 | lw x6, 4(x10) | x10, then the word at 8000_0004 | x6 = 5 | one read |
| 8 | add x7, x5, x6 | x5 and x6 | x7 = 12 | none |
| c | sw x7, 8(x10) | x10 and x7 | the word at 8000_0008 = 12 | one write |
The PC is in hex, and it goes on by 4: each instruction is four bytes long.
Read lw x5, 0(x10) from the inside out. The part in brackets is a register that holds an address. The number in front is an immediate that is added to it. The register in front of the comma is the destination register.
A store is written the same way. Take care: in sw x7, 8(x10) the first register is the value to write. A store has no destination register.
The Rules
Rule 1: RV32I has 32 registers, x0 to x31, of 32 bits each.
The PC is separate. No instruction names it as an operand.
Rule 2: Register x0 always reads as 0.
An instruction may name it as its destination register. The result is then thrown away.
Rule 3: Arithmetic and logic work on registers and immediates only.
They never read or write memory.
Rule 4: Only loads and stores touch memory.
The address is one register plus an immediate.
Rule 5: A branch compares two registers.
If the test holds, the PC takes the address of the branch target. If not, the PC goes on by 4.
Rule 6: Every instruction is 32 bits long, and names at most three registers.
So the PC goes on by 4, unless a branch or a jump writes it.
Sources for These Rules
Every RISC-V fact on this page is stated as commonly described. That covers the 32 registers, register x0, the 40 instructions and their groups. The RISC-V specifications are not in the reference library. So none of these facts is checked against them. The counts were compared with the instruction tables of the assembler of this track.
Two textbooks treat this material. Patterson and Hennessy do so in sections 2.1 to 2.3, 2.6 and 2.7. Harris and Harris do so in section 6.2. Those section numbers are from the plan of this track and are not checked either.
Now Run It
The lab at the end of this page runs the four instructions on a small model of the processor. It prints one row for each instruction: the PC, the instruction, and what changed.
00000000 | lw x5, 0(x10) | x5 = 00000007 from mem[80000000]
00000004 | lw x6, 4(x10) | x6 = 00000005 from mem[80000004]
00000008 | add x7, x5, x6 | x7 = 0000000c
0000000c | sw x7, 8(x10) | mem[80000008] = 0000000c
00000010 | ebreak | stop
instructions=4Rule 4 at work: rows 1, 2 and 4. Each shows a memory address. These are the only rows with a second bus transfer.
Rule 3 at work: row 3. The add names three registers and no address. Its value c is 12 in hex.
Rule 6 at work: read the first column. It goes 0, 4, 8, c. The last row is ebreak, where the model stops.
A branch needs a loop to show itself. This second program adds 3, then 2, then 1:
00000000 | addi x5, x0, 3 | x5 = 00000003
00000004 | addi x6, x0, 0 | x6 = 00000000
00000008 | add x6, x6, x5 | x6 = 00000003
0000000c | addi x5, x5, -1 | x5 = 00000002
00000010 | bne x5, x0, 0x8 | taken: next is 00000008
00000008 | add x6, x6, x5 | x6 = 00000005
0000000c | addi x5, x5, -1 | x5 = 00000001
00000010 | bne x5, x0, 0x8 | taken: next is 00000008
00000008 | add x6, x6, x5 | x6 = 00000006
0000000c | addi x5, x5, -1 | x5 = 00000000
00000010 | bne x5, x0, 0x8 | not taken
00000014 | ebreak | stop
instructions=11Rule 5 at work: the three bne rows. Twice x5 is not 0, and the PC goes back to 8. The third time x5 is 0, and the PC goes on to 14.
Rule 2 at work, quietly: the first row is how RV32I puts 3 into a register. It adds 3 to the free 0.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Only loads and stores touch memory | An add that reads memory itself | Each instruction makes one bus transfer for data at most. The hardware stays simple. Programs need more instructions. |
| A register that is always 0 | Thirty-two ordinary registers | A copy, a constant and a compare with 0 need no instructions of their own. One register is lost. |
| Three operands: a destination and two sources | Two operands, where the result replaces a source | Both sources survive. Each instruction needs room for three register numbers. |
| Every instruction 32 bits long | Lengths from one byte up | The next instruction is always at PC plus 4. Short instructions waste some bits. |
| Thirty-two registers | Eight or sixteen | Fewer loads and stores, because more values stay inside. More flip-flops, and 5 bits for each register number. |
The Trap: A Value Kept in x0
A programmer needs a place for the number 5. The registers are numbered from 0, so the first one seems a natural choice. The program puts 5 into x0, puts 2 into x6, and adds them into x7.
00000000 | addi x0, x0, 5 | x0 = 00000000
00000004 | addi x6, x0, 2 | x6 = 00000002
00000008 | add x7, x0, x6 | x7 = 00000002
0000000c | ebreak | stop
instructions=3Read the first row. The instruction ran, and x0 still holds 0. Nothing complained.
Read the third row. The sum is 2, not 7. The program is wrong by exactly the value that was lost.
No error is raised for this, because a write to x0 is legal. It is even useful: adding 0 to x0, into x0, is the standard way to do nothing. The rule to keep is rule 2. A value that must last never goes into x0.
At Scale
This section looks ahead. The 40 instructions are the floor that every RISC-V chip stands on.
The processor of ap_soc
The processor of this track is planned with these 40 instructions, and 8 more for system control. Later lessons of this module build it.
Names for the registers
Compilers give each register a job and a second name: x10 is also a0. S2.3 covers these names.
A family, not one ISA
RV32I is the smallest member. Wider registers and extra groups are added by letter: S2.17.
From text to bits
Each line of assembly becomes one word of 32 bits. S2.2 shows how.
Try It
Run the lab. Match each row with the table of the hand example.
Change the 4 in the second row to 8. Predict what x6 and x7 hold before you run it.
Add a row that copies x7 into x8. No instruction is called "copy": use addi and an immediate of 0.
00000000 | lw x5, 0(x10) | x5 = 00000007 from mem[80000000] 00000004 | lw x6, 4(x10) | x6 = 00000005 from mem[80000004] 00000008 | add x7, x5, x6 | x7 = 0000000c 0000000c | sw x7, 8(x10) | mem[80000008] = 0000000c 00000010 | ebreak | stop instructions=4Check Yourself
Common Mistakes
Wrong Keep a value in x0.
Register x0 always reads as 0. The value is gone, and nothing reports it.
Fix Use any of x1 to x31. Use x0 only where a 0 is wanted.
Wrong Write an add with an address in brackets as a source, to add a word of memory.
Arithmetic works on registers and immediates only. No such instruction exists.
Fix Load the word into a register first, then add.
Wrong Read sw x7, 8(x10) as "write into x7".
A store has no destination register. The first register is the value that goes to memory.
Fix Read it as "store x7 at the address x10 plus 8".
Remember
- An ISA is the written list of registers and instructions that hardware and software both obey.
- RV32I has 32 registers of 32 bits and 40 instructions. Register x0 always reads as 0.
- Arithmetic and logic work on registers and immediates. Only loads and stores touch memory.
- A branch compares two registers and may write the PC. Otherwise the PC goes on by 4.