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.

Five questions this page answers
Question
1How many registers does RV32I have, and how wide are they?
2What happens when a program writes to register x0?
3Which groups make up the 40 base instructions?
4Which instructions may touch memory?
5Why 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

New words on this page
WordMeaning
Instruction set architecture (ISA)The written list of registers and instructions that a processor and its software both obey.
RISC-VAn ISA that anyone may build without a fee. It is said "risk five".
RV32IThe base form of RISC-V with registers of 32 bits: 32 registers and 40 instructions.
AssemblyA program as text, one line for each instruction.
Zero registerThe register x0. It always reads as 0, and writes to it are thrown away.
OperandOne of the things an instruction works on: a register or a number.
Destination registerThe register that an instruction writes. In assembly it is written first.
ImmediateA constant that is stored inside the instruction itself.
Instruction groupA set of instructions that read and write the same kinds of thing.
lwLoad word: the instruction that copies a word of 32 bits from memory into a register.
swStore word: the instruction that copies a register into a word of memory.
Load-store designAn ISA in which only loads and stores touch memory. Everything else works on registers.
BranchAn instruction that compares two registers and, if the test holds, writes a new address into the PC.
JumpAn instruction that always writes a new address into the PC.
ebreakAn instruction that stops a program so that it can be inspected. The lab stops when it reaches one.
Instruction-level modelA program that acts out one instruction at a time, with no clock. It shows what each instruction does.

One Picture: The Map of RV32I

The Registers, and What Each Group of Instructions May TouchStep through the picture. Each step is one group of instructions. Watch which boxes it reads and which it writes.
  • Processor
  • Control / FSM
  • Memory
The Registers, and What Each Group of Instructions May TouchStep through the picture. Each step is one group of instructions. Watch which boxes it reads and which it writes. component Program counter(PC) 32 bits: the addressof the nextinstruction component Zero register x0: always reads as 0 component Registers x1 tox31 31 values of 32 bitseach component Arithmetic andlogic add, subtract,compare, shift, andlogic on each bit component Compare for abranch equal, less than, andtheir opposites component Memory anddevices reached over the bus,by address two registers, or oneand a constantthe resultloadstoretwo registersbranch or jump: a newPCa free 0
  1. component Zero registerx0: always reads as 0
    • a free 0 → Arithmetic and logic
  2. component Registers x1 to x3131 values of 32 bits each
    • two registers, or one and a constant → Arithmetic and logic
    • store → Memory and devices
    • two registers → Compare for a branch
  3. component Program counter (PC)32 bits: the address of the next instruction
  4. component Arithmetic and logicadd, subtract, compare, shift, and logic on each bit
    • the result → Registers x1 to x31
  5. component Memory and devicesreached over the bus, by address
    • load → Registers x1 to x31
  6. component Compare for a branchequal, less than, and their opposites
    • branch or jump: a new PC → Program counter (PC)
  1. 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.

  2. Register x0

    Register x0 is not a store of anything. It reads as 0, and a write to it is thrown away.

  3. 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.

  4. Loads: 5 instructions

    A load copies a value from memory into one register. The address is a register plus a constant.

  5. Stores: 3 instructions

    A store copies one register into memory. Loads and stores are the only instructions that use the bus for data.

  6. 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.

The 40 instructions in groups
GroupHow manyInstructionsReadsWrites
Arithmetic and logic on two registers10add sub sll slt sltu xor srl sra or andtwo registersone register
Arithmetic and logic with an immediate9addi slti sltiu xori ori andi slli srli sraione register and the immediateone register
Loads5lb lh lw lbu lhuone register, then memoryone register
Stores3sb sh swtwo registersmemory
Branches6beq bne blt bge bltu bgeutwo registersthe PC, if the test holds
Jumps2jal jalrthe PC, or one registerthe PC, and one register
Large constants2lui auipcthe immediate, or the PC tooone register
System3fence ecall ebreaknothingnothing 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.

Four Instructions, One Step EachStep through the program. Each step is one instruction of the lab. The values are the recorded ones.
  • Processor
  • Memory
Four Instructions, One Step EachStep through the program. Each step is one instruction of the lab. The values are the recorded ones. component Value of x10 8000_0000: theaddress of the data component Value of x5 becomes 7 component Value of x6 becomes 5 component Value of x7 becomes 12 component Word at8000_0000 holds 7 component Word at8000_0004 holds 5 component Word at8000_0008 becomes 12 lw x5, 0(x10)lw x6, 4(x10)addaddsw x7, 8(x10)
  1. component Value of x108000_0000: the address of the data
  2. component Word at 8000_0000holds 7
    • lw x5, 0(x10) → Value of x5
  3. component Value of x5becomes 7
    • add → Value of x7
  4. component Word at 8000_0004holds 5
    • lw x6, 4(x10) → Value of x6
  5. component Value of x6becomes 5
    • add → Value of x7
  6. component Value of x7becomes 12
    • sw x7, 8(x10) → Word at 8000_0008
  7. component Word at 8000_0008becomes 12
  1. lw x5, 0(x10)

    The address is x10 plus 0, which is 8000_0000. The word there is 7, and it goes into x5.

  2. lw x6, 4(x10)

    The address is x10 plus 4. The word there is 5, and it goes into x6.

  3. add x7, x5, x6

    Seven plus 5 is 12, and 12 goes into x7. No bus transfer carries data here.

  4. 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.

The four instructions, by hand
PCAssemblyIt readsIt writesData on the bus
0lw x5, 0(x10)x10, then the word at 8000_0000x5 = 7one read
4lw x6, 4(x10)x10, then the word at 8000_0004x6 = 5one read
8add x7, x5, x6x5 and x6x7 = 12none
csw x7, 8(x10)x10 and x7the word at 8000_0008 = 12one 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

  1. Rule 1: RV32I has 32 registers, x0 to x31, of 32 bits each.

    The PC is separate. No instruction names it as an operand.

  2. Rule 2: Register x0 always reads as 0.

    An instruction may name it as its destination register. The result is then thrown away.

  3. Rule 3: Arithmetic and logic work on registers and immediates only.

    They never read or write memory.

  4. Rule 4: Only loads and stores touch memory.

    The address is one register plus an immediate.

  5. 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.

  6. 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.

Printed by the lab on Icarus. The instruction-level model of this track printed the same rows for the same program.
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=4

Rule 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:

The same model, with a loop of three instructions. The branch target is written as an address: 0x8.
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=11

Rule 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 choiceThe alternativeWhat the choice buys, and costs
Only loads and stores touch memoryAn add that reads memory itselfEach instruction makes one bus transfer for data at most. The hardware stays simple. Programs need more instructions.
A register that is always 0Thirty-two ordinary registersA copy, a constant and a compare with 0 need no instructions of their own. One register is lost.
Three operands: a destination and two sourcesTwo operands, where the result replaces a sourceBoth sources survive. Each instruction needs room for three register numbers.
Every instruction 32 bits longLengths from one byte upThe next instruction is always at PC plus 4. Short instructions waste some bits.
Thirty-two registersEight or sixteenFewer 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.

The same model. The programmer expects 7 in 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=3

Read 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

The teaching processor of S0.64 registers, 6 instructions
RV32I32 registers, 40 instructions

This section looks ahead. The 40 instructions are the floor that every RISC-V chip stands on.

  1. 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.

  2. Names for the registers

    Compilers give each register a job and a second name: x10 is also a0. S2.3 covers these names.

  3. A family, not one ISA

    RV32I is the smallest member. Wider registers and extra groups are added by letter: S2.17.

  4. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=4

Check Yourself

Predict the outputThis program starts with 1 in x7 and 4 in x5. Each turn of the loop adds x7 to itself and takes 1 from x5. What does the last row say?
// What does x7 hold at the end?
// The program is the task at the top. Each row is one instruction: i(kind, a, b, c).
// The comment beside a row is the same instruction in assembly.
module tb;
  task automatic the_program();
    i(ADDI,   5,  0,  4);     // 00: addi x5, x0, 4
    i(ADDI,   7,  0,  1);     // 04: addi x7, x0, 1
    i(ADD,    7,  7,  7);     // 08: add x7, x7, x7      the loop starts here
    i(ADDI,   5,  5,  -1);    // 0c: addi x5, x5, -1
    i(BNE,    5,  0,  'h8);   // 10: bne x5, x0, 0x8
    i(EBREAK, 0,  0,  0);     // 14: ebreak
  endtask

  // ---- From here down: a small model of an RV32I processor, as test code. You need not change it. ----
  localparam int LUI = 0, AUIPC = 1, JAL = 2, JALR = 3, BEQ = 4, BNE = 5, BLT = 6, BGE = 7, BLTU = 8, BGEU = 9, LW = 10, SW = 11,
                 ADDI = 12, SLTI = 13, SLTIU = 14, XORI = 15, ORI = 16, ANDI = 17, SLLI = 18, SRLI = 19, SRAI = 20,
                 ADD = 21, SUB = 22, SLL = 23, SLT = 24, SLTU = 25, XOR = 26, SRL = 27, SRA = 28, OR = 29, AND = 30, EBREAK = 31;
  int kind [0:63], fa [0:63], fb [0:63], fc [0:63];   // the program: one row for each instruction
  int rows = 0;
  task automatic i(input int k, input int a, input int b, input int c);   // add one row to the program
    if (rows < 64) begin kind[rows] = k; fa[rows] = a; fb[rows] = b; fc[rows] = c; rows++; end
  endtask
  logic [31:0] x [0:31];             // the 32 registers
  logic [31:0] pc;                   // the address of the next instruction
  logic [31:0] mem [0:16383];        // 64 KiB of data memory, from address 8000_0000 up
  int   steps;
  bit   stopped, faulted;
  function automatic string rn(input int n);
    return $sformatf("x%0d", n);
  endfunction
  function automatic string nm(input int k);                // the name of a kind of instruction
    case (k)
      0: return "lui"; 1: return "auipc"; 2: return "jal"; 3: return "jalr"; 4: return "beq"; 5: return "bne"; 
      6: return "blt"; 7: return "bge"; 8: return "bltu"; 9: return "bgeu"; 10: return "lw"; 11: return "sw"; 
      12: return "addi"; 13: return "slti"; 14: return "sltiu"; 15: return "xori"; 16: return "ori"; 17: return "andi"; 
      18: return "slli"; 19: return "srli"; 20: return "srai"; 21: return "add"; 22: return "sub"; 23: return "sll"; 
      24: return "slt"; 25: return "sltu"; 26: return "xor"; 27: return "srl"; 28: return "sra"; 29: return "or"; 
      30: return "and"; 31: return "ebreak"; 
      default: return "?";
    endcase
  endfunction
  // One instruction: read the row at the PC, do what it says, and print one line.
  task automatic step();
    int k, a, b;
    logic [31:0] c, r1, r2, res, next, ad;
    string text, effect;
    bit writes, go, take;
    go = 1;  k = EBREAK;  a = 0;  b = 0;  c = 0;
    if (pc[1:0] != 0 || pc >= 256) begin
      $display("%08h | no instruction at this address", pc);  faulted = 1;  stopped = 1;  go = 0;
    end
    else begin k = kind[pc >> 2];  a = fa[pc >> 2];  b = fb[pc >> 2];  c = fc[pc >> 2]; end
    r1 = x[b[4:0]];  r2 = x[c[4:0]];
    next = pc + 4;  writes = 1;  res = 0;  effect = "";
    if (go) case (k)
      LUI:   begin res = c << 12;       text = $sformatf("lui %s, 0x%0h", rn(a), c); end
      AUIPC: begin res = pc + (c << 12); text = $sformatf("auipc %s, 0x%0h", rn(a), c); end
      JAL:   begin res = pc + 4;  next = c;  text = $sformatf("jal %s, 0x%0h", rn(a), c); end
      JALR:  begin res = pc + 4;  next = (r1 + c) & ~32'd1;  text = $sformatf("jalr %s, %0d(%s)", rn(a), $signed(c), rn(b)); end
      BEQ, BNE, BLT, BGE, BLTU, BGEU: begin
        r1 = x[a[4:0]];  r2 = x[b[4:0]];  writes = 0;
        case (k)
          BEQ:  take = (r1 == r2);
          BNE:  take = (r1 != r2);
          BLT:  take = ($signed(r1) < $signed(r2));
          BGE:  take = ($signed(r1) >= $signed(r2));
          BLTU: take = (r1 < r2);
          default: take = (r1 >= r2);
        endcase
        if (take) next = c;
        text = $sformatf("%s %s, %s, 0x%0h", nm(k), rn(a), rn(b), c);
        if (take) effect = $sformatf("taken: next is %08h", next);
        else effect = "not taken";
      end
      LW, SW: begin
        ad = r1 + c;
        text = $sformatf("%s %s, %0d(%s)", nm(k), rn(a), $signed(c), rn(b));
        if (ad[1:0] != 0 || ad < 32'h8000_0000 || ad > 32'h8000_fffc) begin
          $display("%08h | %s | no data memory at %08h", pc, text, ad);  faulted = 1;  stopped = 1;  go = 0;
        end
        else if (k == LW) begin res = mem[ad[15:2]];  effect = $sformatf(" from mem[%08h]", ad); end
        else begin mem[ad[15:2]] = x[a[4:0]];  writes = 0;  effect = $sformatf("mem[%08h] = %08h", ad, x[a[4:0]]); end
      end
      ADDI:  res = r1 + c;
      SLTI:  res = ($signed(r1) < $signed(c)) ? 1 : 0;
      SLTIU: res = (r1 < c) ? 1 : 0;
      XORI:  res = r1 ^ c;
      ORI:   res = r1 | c;
      ANDI:  res = r1 & c;
      SLLI:  res = r1 << c[4:0];
      SRLI:  res = r1 >> c[4:0];
      SRAI:  res = $signed(r1) >>> c[4:0];
      ADD:   res = r1 + r2;
      SUB:   res = r1 - r2;
      SLL:   res = r1 << r2[4:0];
      SLT:   res = ($signed(r1) < $signed(r2)) ? 1 : 0;
      SLTU:  res = (r1 < r2) ? 1 : 0;
      XOR:   res = r1 ^ r2;
      SRL:   res = r1 >> r2[4:0];
      SRA:   res = $signed(r1) >>> r2[4:0];
      OR:    res = r1 | r2;
      AND:   res = r1 & r2;
      default: begin
        $display("%08h | ebreak | stop", pc);  stopped = 1;  go = 0;
      end
    endcase
    if (k >= ADDI && k <= SRAI) text = $sformatf("%s %s, %s, %0d", nm(k), rn(a), rn(b), $signed(c));
    if (k >= ADD && k <= AND)   text = $sformatf("%s %s, %s, %s", nm(k), rn(a), rn(b), rn(c[4:0]));
    if (go && writes) begin
      if (a[4:0] != 0) x[a[4:0]] = res;                      // register 0 is never written
      effect = {$sformatf("%s = %08h", rn(a), x[a[4:0]]), effect};
      if (k == JAL || k == JALR) begin                       // a jump with x0 as its destination keeps no return address
        if (a[4:0] == 0) effect = $sformatf("next is %08h", next);
        else effect = {effect, $sformatf(", next is %08h", next)};
      end
    end
    if (go) begin
      $display("%08h | %s | %s", pc, text, effect);
      pc = next;  steps++;
    end
  endtask
  task automatic run(input logic [31:0] start);
    pc = start;  steps = 0;  stopped = 0;  faulted = 0;
    while (!stopped && steps < 2000) step();
  endtask
  initial begin
    for (int n = 0; n < 32; n++) x[n] = 0;
    for (int n = 0; n < 16384; n++) mem[n] = 0;
    the_program();
    run(0);
    if (!stopped) $display("still running after %0d instructions", steps);
    $display("x7=%0d", x[7]);
  end
endmodule
ConceptHow many registers does RV32I have, and how wide is each?
ConceptA program adds 9 to x0, with x0 as the destination register. What does x0 hold afterwards?
ConceptWhich of these is not one of the groups of RV32I?
ConceptWhich instructions may read or write memory for data?
ConceptWhy does adding two words of memory take four instructions in RV32I?

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.

Practice

Report a problem with this page