How Hardware Supports Function Calls and Character Data

Calling a function seems simple in high-level code, but at the hardware level it requires a careful protocol for saving return addresses, passing arguments, and preserving register values. This article explains how procedure calls are implemented using dedicated registers and a stack, then covers how processors represent human-readable text as sequences of encoded characters.

Procedure CallsStack MemoryCharacter Encoding

~3 min read · Updated Sep 6, 2026

What Happens When a Function Is Called

Calling a Procedure (a function or subroutine) requires more than just jumping to its code. The processor must remember where to return afterward, pass input arguments, receive a result, and make sure the calling code's register values are not silently overwritten by the called procedure. RISC-V handles this through a defined convention rather than special hardware magic.

The Jump-and-Link Instruction

RISC-V provides a dedicated instruction for calling procedures that automatically saves the return location:

jal ra, ProcedureAddress

This instruction jumps execution to ProcedureAddress while simultaneously storing the address of the next instruction into a designated register, conventionally called ra (return address). When the procedure finishes, it uses this saved address to jump back:

jalr x0, 0(ra)

This returns control to exactly the instruction that follows the original call.

Register Conventions for Passing Data

To keep procedure calls predictable across different compilers and programs, RISC-V follows a fixed Register Convention:

  • A specific range of registers is reserved for passing Argument Values into a procedure.
  • A specific register is reserved for returning the Result Value back to the caller.
  • Some registers are designated as Saved Registers, which a called procedure must preserve and restore before returning if it intends to use them.
  • Other registers are Temporary Registers, which a called procedure is free to overwrite without any obligation to restore their original value.

The Stack: Handling Nested and Recursive Calls

A single set of registers is not enough when procedures call other procedures, or call themselves recursively. To handle this, RISC-V uses a region of memory called the Stack, managed through a dedicated Stack Pointer register.

Before a procedure overwrites a saved register it needs to reuse, it pushes that register's current value onto the stack; before returning, it restores the value by popping it back:

Reserve stack space:
addi sp, sp, -8

Save a register's value:
sd s0, 0(sp)

Restore it later:
ld s0, 0(sp)

Release stack space:
addi sp, sp, 8

This mechanism allows an arbitrary depth of nested or recursive calls, since each call gets its own private area of stack memory for anything it needs to preserve.

Representing Text: Characters and Encoding

Beyond numbers, computers also need to represent human-readable text. Each individual character is stored using a numeric encoding standard, most commonly ASCII for basic Latin text or Unicode for a much broader range of scripts and symbols.

A sequence of characters forming text is called a String, and it is typically stored in memory as consecutive bytes, one per character, often ending with a special terminating value that marks where the string ends.

Why These Conventions Matter

Because register usage and stack management follow a strict, agreed-upon convention rather than being decided ad hoc by each program, code compiled by different compilers, or written in different languages, can call procedures written by each other correctly and safely — a property essential for building large software systems out of independently developed components.

Written & researched by Dr. Shahin Siami

Related Articles

How Hardware Performs Division: Quotients, Remainders, and Edge Cases

Division is the most hardware-intensive of the basic arithmetic operations, involving repeated subtraction and comparison rather than a single-pass circuit. This article explains the conceptual long-division algorithm hardware follows, how quotient and remainder are produced together, and the special edge cases like division by zero that hardware must explicitly handle.

Continue

How Hardware Multiplies Numbers: From Simple Logic to Real Circuits

Multiplication is far more hardware-intensive than addition, since it fundamentally involves repeated addition and shifting. This article walks through the conceptual algorithm hardware uses to multiply binary numbers, explains why the result needs twice the bit width of the inputs, and covers how signed multiplication differs from the unsigned case.

Continue

How Hardware Performs Addition and Subtraction, and Detects Overflow

Arithmetic looks trivial in software but requires careful circuit design and explicit overflow handling in hardware. This article explains how a processor's adder circuit performs both addition and subtraction using the same hardware, and how overflow conditions are detected and handled for signed and unsigned numbers.

Continue

Arrays Versus Pointers at the Hardware Level

In C, arrays and pointers often look interchangeable, and many programmers treat them as if they were the same thing. At the hardware level, however, they compile down to noticeably different instruction sequences with different performance characteristics. This article compares the two approaches using RISC-V assembly to show exactly why pointer-based code is often faster.

Continue

From Source Code to a Running Process: Translation and a Full Sort Example

Turning a C program into something the operating system can actually run involves several distinct translation stages, each producing a different intermediate file. This article walks through that full pipeline from compiler to loader, then applies the concepts from this chapter to a complete, realistic example: translating a C sorting routine into RISC-V assembly step by step.

Continue

Wide Address Handling and Synchronization in RISC-V

A 32-bit instruction cannot fit a large constant or a far-away memory address directly inside it, and multiple processors sharing memory cannot safely update the same data without coordination. This article explains how RISC-V builds large immediate values and addresses out of smaller pieces, and how atomic instructions allow parallel programs to synchronize safely.

Continue