Menu Close

Registers and Core Concepts

Posted in Risc-V

==================================================

1. Registers

Registers are tiny, very fast storage locations inside the CPU.

RV32I has 32 integer registers:

  • x0 to x31

Each register is 32 bits wide.

You can think of registers as named boxes that hold values the CPU is actively using.

==================================================

2. Register Diagram

+---------------------------------------------+
| Register File                               |
+---------------------------------------------+
| x0  | x1  | x2  | x3  | ... | x30 | x31    |
+---------------------------------------------+

Each box stores one 32-bit value.

==================================================

3. Important Rule: x0 is Always Zero

x0 is special.

  • Reading x0 always gives 0
  • Writing to x0 has no effect

Examples:

addi x5, x0, 9     # x5 = 9
addi x0, x0, 7     # ignored, x0 stays 0

This makes x0 extremely useful.

==================================================

4. ABI Names

Registers also have ABI names used by programmers and compilers.

Common ones:

x0 = zero constant zero
x1 = ra return address
x2 = sp stack pointer
x5 = t0 temporary
x6 = t1 temporary
x7 = t2 temporary
x8 = s0/fp saved register / frame pointer
x9 = s1 saved register
x10 = a0 function argument / return value
x11 = a1 function argument / return value
x12 = a2
x13 = a3
x14 = a4
x15 = a5
x16 = a6
x17 = a7

You can write either x10 or a0 depending on style.

==================================================

5. Register Roles

Very simplified categories:

  • zero register:
    x0
  • temporaries:
    t0-t6
    used for temporary calculations
  • saved registers:
    s0-s11
    preserved across function calls
  • argument / return registers:
    a0-a7
    used to pass inputs and outputs
  • special:
    ra, sp

==================================================

6. CPU Datapath Idea

A simplified view:

The CPU reads source registers, performs an operation, then may write the result back to a destination register.

==================================================

7. Example: Using Registers

Code:

addi x5, x0, 3
addi x6, x0, 7
add  x7, x5, x6

Meaning:

  • x5 = 3
  • x6 = 7
  • x7 = 10

==================================================

8. Step-by-Step Execution Trace

Initial:
x0 = 0
x5 = 0
x6 = 0
x7 = 0

Step 1
Instruction = addi x5, x0, 3
Read:

  • x0 = 0
    Compute:
  • 0 + 3 = 3
    Write:
  • x5 = 3

Step 2
Instruction = addi x6, x0, 7
Read:

  • x0 = 0
    Compute:
  • 0 + 7 = 7
    Write:
  • x6 = 7

Step 3
Instruction = add x7, x5, x6
Read:

  • x5 = 3
  • x6 = 7
    Compute:
  • 3 + 7 = 10
    Write:
  • x7 = 10

==================================================

9. Register Data Dependency

Notice the third instruction depends on earlier results.

addi x5, x0, 3
addi x6, x0, 7
add  x7, x5, x6

The add instruction needs the values produced by the first two instructions.

This idea becomes important later in pipeline hazards.

==================================================

10. Signed Integers

RV32I registers hold 32-bit patterns.
Those patterns may be interpreted as:

  • signed integers
  • unsigned integers
  • memory addresses
  • bit patterns

For signed values, two’s complement is used.

Examples:

  • 0x00000005 = 5
  • 0xFFFFFFFF = -1
  • 0xFFFFFFFE = -2

==================================================

11. Common Beginner Mistakes

Mistake 1:
Thinking x0 can store a value.
It cannot.

Mistake 2:
Confusing register names and memory addresses.
Registers are inside the CPU; memory is outside the register file.

Mistake 3:
Assuming all registers are automatically preserved across function calls.
Only some are saved by convention.

==================================================

12. Exercises

  1. How many integer registers are in RV32I?
  2. What value is always read from x0?
  3. What happens if you execute:
    addi x0, x0, 12
  4. Which register is the stack pointer?
  5. Which register usually stores a return address?
  6. What is the value of x7 after:
    addi x5, x0, 8
    addi x6, x0, 2
    sub x7, x5, x6
  7. Write a 2-instruction sequence that puts 20 into x9 using x0.
  8. Why are registers faster than memory?

==================================================

13. Exercise Answers

  1. 32
  2. 0
  3. Nothing changes; x0 stays 0.
  4. x2, also called sp
  5. x1, also called ra
  6. 6
  7. Example:
    addi x9, x0, 10
    addi x9, x9, 10
  8. Because registers are built directly into the CPU and are designed for very fast access.

Leave a Reply

Your email address will not be published. Required fields are marked *