5 Assembly Concepts
Learn how RISC-V assembly uses registers, instructions, labels, branches, and calling conventions to implement algorithms.
Assembly and processor instructions
Assembly language is a human-readable way to express operations supported by a processor. Each directs the CPU to do something, such as add values, load data, or change the next to execute. Assembly is architecture-specific: RISC-V, for example, has its own registers and names. An assembler translates assembly into machine code, while labels and shorthand make programs easier to write and organize.
Keep the architecture in mind when reading examples: an available on one processor may not exist on another.
Registers and forms
A is a small, fast storage location inside the CPU. RISC-V integer registers are named x0 through x31; familiar names describe common uses. For example, zero always reads as zero, ra holds a return address, sp is the stack pointer, t0–t6 are temporary registers, and a0–a7 are commonly used for arguments and return values.
An consists of a mnemonic—the operation name—and operands that identify the values or locations it uses. In this example, add combines values, while addi adds a constant:
Some familiar assembly forms are pseudoinstructions: assembler-provided shorthand translated into real instructions. Examples include li for loading an immediate value, mv for moving a value, and ret for returning from a .
Labels, branches, and control flow
A names a location in a program so a or jump can refer to it without requiring the programmer to calculate an address. A changes the normal sequence when its condition is true. For example, beq t0, t1, equal transfers execution to equal if the values match; otherwise, execution continues with the next .
Branches connect assembly to familiar control structures. A conditional can express an if decision. A combined with a backward can form a loop. Normally, the program counter advances through instructions; when a is taken, execution continues at its destination.
Procedures and calling conventions
A is a named block of instructions that performs a task. A caller transfers control to the , which returns control to the after the call. Procedures coordinate through a : shared rules for passing arguments and results and for handling registers.
In the standard RISC-V convention, a registers carry arguments and return values; the first argument and return value use a0. The ra holds the return address, and sp points to the stack. A must follow rules about which registers it is required to preserve.
RISC-V assemblers commonly accept call name and ret as pseudoinstructions for control flow. If a calls another , it may need to save its own return address first, because the new call updates ra.
Translating an algorithm into assembly
To translate an algorithm into assembly, identify its values and control flow, then assign values to registers or memory and express the operations with instructions. This sums the positive integers up to the input n; if n is zero or negative, it returns zero. The input arrives in a0, and the result is returned in a0.
The first handles inputs that should produce zero. Otherwise, the loop adds the counter to the running total, increases the counter, and branches back while the counter is no greater than n. When the loop ends, the result is placed in a0 and the returns. For input , the result is .
Takeaway: Identify the algorithm’s assignments, decisions, repetitions, and return value; then map them to registers, instructions, labels, and branches.