One practical way to learn an instruction set is to follow a short program all the way through execution. An array-sum loop is enough to expose registers, loads, arithmetic, branches, and a function return. It also helps separate the behavior software can rely on from the implementation choices inside a processor.

Three Different Contracts

The RISC-V instruction set architecture defines the programmer-visible operations. A microarchitecture is an implementation of those operations: a simple multicycle core and a pipelined core can implement the same ISA. The ISA does not by itself tell you their clock frequency or instructions per cycle.

A calling convention adds a software agreement about arguments, results, and saved registers. In the standard integer ABI, a0–a7 are argument registers, with a0 and a1 also serving as return-value registers. Temporary registers can be changed by a called function; callee-saved registers must be restored when used.

The RV32I Pieces Needed Here

RV32I has 32-bit integer registers, including x0, whose value is always zero. Arithmetic operates on registers. Explicit load and store instructions transfer data between registers and memory.

The example below uses lw to read one 32-bit word, add and addi for arithmetic, and conditional branches to control the loop. Its addition wraps modulo 232. These details are part of what the program means, regardless of the pipeline used to execute it.

Follow an Array-Sum Loop

Assume a0 points to readable, aligned 32-bit words, a1 is an unsigned element count, and the caller has put a valid return address in ra. This leaf function makes no calls and uses only argument and temporary registers:

# RV32I leaf function: unsigned sum_words(const unsigned *p, unsigned n)
# a0: pointer to aligned 32-bit words; a1: number of words
# a0 on return: sum modulo 2^32
sum_words:
    addi t0, zero, 0
    beq  a1, zero, done
loop:
    lw   t1, 0(a0)
    add  t0, t0, t1
    addi a0, a0, 4
    addi a1, a1, -1
    bne  a1, zero, loop
done:
    addi a0, t0, 0
    jalr zero, 0(ra)

For the array [3, 5, 7] with a count of three, t0 evolves through 0 → 3 → 8 → 15. The pointer advances by four bytes per element. When the count reaches zero, the result is copied to a0 and control returns to the address in ra.

The first branch matters: a count of zero returns zero without reading memory. Moving the termination test to the end without a separate initial check would accidentally read one word for the empty case. This small boundary condition is a useful first test for either an emulator or an RTL implementation.

Translate the Loop into a Datapath

To execute the load, the processor reads a0, forms an address, receives a word from memory, and writes that value into t1. The add then consumes t1 and t0. The two immediate additions update the pointer and remaining count. Finally, the branch chooses between the next loop iteration and the following instruction.

In a pipeline, the dependency between lw and add forces a timing decision. The next instruction may need to wait, or the implementation may forward a result when it becomes available. A branch introduces a separate control-flow decision. In a multicycle core, these steps can instead be scheduled across explicit states.

Neither design is automatically better for every small project. Compare the area, timing constraints, memory interface, and verification effort for the intended workload. A simpler design that can be explained and tested is a stronger starting point than a pipeline with unresolved corner cases.

Extensions Are Explicit Capabilities

RISC-V organizes additional capabilities into extensions. For example, M adds integer multiplication and division, while C provides compressed instruction encodings. A compiler must target the extensions actually implemented by the processor. The base ISA name alone is not a promise that every extension is present.

Be equally precise about teaching designs. Supporting a selected set of instructions is a useful milestone, but it is not a complete conformance claim. Document unsupported behavior and the execution environment alongside the design.

Turn the Example into Verification Cases

  • Check zero, one, and several elements.
  • Use a sum that wraps around 32 bits.
  • Vary memory response delays without changing the expected result.
  • Check the returned program counter and all registers the function is required to preserve.
  • Test individual instructions separately, including attempted writes to x0.

The loop can be the first program a core executes, but the specification must remain the reference for a full implementation. A small passing program gives a concrete debugging target; broader instruction and exception tests establish whether the processor implements the architecture it claims.

References and Further Reading

  1. RISC-V Unprivileged ISA Manual: Introduction, 20240411 edition
  2. RISC-V Unprivileged ISA Manual: RV32I Base Integer Instruction Set
  3. RISC-V ABIs Specification: Register and Procedure Calling Conventions