The processor and the instruction set
The ISA as a contract, RISC versus CISC, modular RISC-V with 32 registers and ABI names, the assembler and RARS memory map — what a program may rely on and what it may not.
The ISA as a contract
Floating point finished Foundations: integers, characters and reals are bit patterns. The first topic showed the fetch–decode–execute loop. Which instructions exist, what each does, and how much of the machine a program may depend on?
Notation. Addresses and instruction words are 32-bit hex (0x00400018). Integer registers are x0–x31 with ABI names (a0, t1, …). pc is the program counter. , and are the factors of the performance equation.
Baseline. Early machines each had their own instructions; a new design threw software away. IBM System/360 (1964) made one ISA for a family and coined architecture as the programmer-visible attributes (Amdahl, Blaauw, Brooks). Software lives for decades; hardware turns over in years — without a stable interface, every improvement throws programs away.
Key idea. The ISA is a contract. Hardware promises every instruction does what the spec says on every implementation; software relies on nothing else — not clock rate, caches, or how many instructions run at once. The ISA fixes the 32 visible registers, the byte-addressable memory model, every encoding and effect (including corner cases), and error behaviour. Microarchitecture chooses physical register files, caches, pipelining, out-of-order width, clock and power.
Corner cases matter: RISC-V div by zero returns −1; rem by zero returns the dividend — no trap. ARMv8 sdiv returns 0; x86 idiv raises an exception. Three contracts. Beside the ISA sit microarchitecture (one implementation) and the ABI (software conventions: which register is ra, how arguments are passed). Different compilers interoperate only because they share one ABI.
RISC and CISC
CISC (VAX, x86): rich addressing, memory operands, variable length — dense code when memory was dear and assembly was written by hand. RISC (Berkeley RISC I, Stanford MIPS): load/store only, fixed length, many registers (~32), few regular formats — compilers use simple ops; hardware pipelines easily. In , CISC cuts ; RISC accepts larger for better CPI and .
Measured. void add_to(int *a,int i,int x){a[i]+=x;} at clang -O2: x86-64 — 3 instructions / 7 bytes with a memory ALU op; RV32IM — 6 / 24 bytes (shift, add, load, add, store, ret); with the C extension, 12 bytes. AArch64 needs 5 / 20 bytes. Which is fastest depends on CPI and , not on byte count alone. Since the Pentium Pro (1995), x86 translates CISC ops into RISC-like micro-ops internally; the software-visible distinction remains: variable vs fixed length, memory operands vs load/store.
RISC-V: base and extensions
RISC-V (UC Berkeley, 2010): open, modular. Bases RV32I/RV64I (32 regs) and RV32E/RV64E (16 regs). RV32I has 40 instructions. Extensions: M mul/div, A atomics, F/D float, C compressed 16-bit encodings, V vectors, Zicsr/Zifencei. G ≈ IMAFD + Zicsr + Zifencei — so RV64GC means the usual Linux-capable target. This course uses RV32IM; RARS also offers F/D.
The 32 registers and ABI names
Registers are the processor’s own storage — one-cycle operands. In a load/store ISA every arithmetic operand must be in a register, so registers are what a program works with most of the time. RV32I: x0–x31. Only x0/zero is special (always 0; writes ignored). ABI names: ra=x1, sp=x2, gp=x3, tp=x4, t0–t2=x5–x7, s0/fp=x8, s1=x9, a0–a7=x10–x17, s2–s11=x18–x27, t3–t6=x28–x31. Call preservation is the procedures topic; until then use t* and a*. pc is not in the 32.
Why 32? A register index is 5 bits; three fields cost 15 bits of every instruction. RV32E halves to 16 for tiny chips where those bits buy immediates or density instead.
The assembler and RARS
Assembly: label: mnemonic ops # comment. Directives (.text, .data, .word, .asciz, .space, .align, .globl, .eqv) are orders to the assembler, not instructions. Pseudos (li, la, mv, j, ret, call) expand to one or two real ops — call becomes auipc+jalr so a procedure anywhere in the address space is reachable.
RARS map: .text @ 0x00400000, .data @ 0x10010000, heap @ 0x10040000, sp starts 0x7fffeffc growing down. Two passes: build symbols (expanding la early), then encode. Little-endian: bytes of "Hello…" sit in order, but a word load reverses significance. System calls go through ecall with a service number in a7 — the bridge to the OS half of the course, once traps are explained.
A first program loads a string address into a0, puts the print-string service in a7, and ecalls; a second sums an array with a rotated loop. Both fit in a page of assembly once the register names and directives are known.
Keep the layers straight when debugging: a wrong return address is usually an ABI bug (forgot to save ra), a wrong opcode encoding is an assembler/ISA issue, and a slow but correct binary is microarchitecture (caches, pipeline stalls). RARS ecalls for print and read are teaching stand-ins for the later Linux syscall ABI — same idea (service number + args in a*), different numbers. When you move from RARS to spike or a board, re-check which extensions the toolchain’s -march actually enables; RV32IM code that uses mul will illegal-instruction trap on a pure RV32I core.
Next: RISC-V instructions and their encoding — arithmetic ops and the six 32-bit formats down to the bits.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.