User Tools

Site Tools


spo600:procedure_call

Procedure Call

A procedure is a block of code that may be called by other code. Depending on the language in used, a procedure may also be called a routine, function, subroutine, method, or subprogram.

At the machine level, a procedure is called like this:

  1. A program (known as the caller) uses an instruction to invoke the procedure. This is known as “performing a call”.
    1. The current value of the program counter is saved either to a stack or to a register. This is known as the return address.
    2. The address of the entry point in the called procedure is loaded into the program counter. This causes the first instruction of the procedure to be executed next.
  2. The called procedure (known as the callee) executes zero or more instructions.
  3. The callee executes an instruction to return to the caller.
    1. The return operation is performed by loading the return address into the program counter. This causes the instruction after the call to be executed next.

The actual value of the return address saved during a procedure call may be the address of the first byte of the call instruction, the last byte of the call instruction, or the first byte of the instruction following the call instruction, depending on the architecture of the system.

Note that the return address is saved differently by various architectures:

  • Some systems store the return address into a link register. This has the advantage of avoiding access to the main memory, which is much slower than registers. However, if a called procedure needs to call another procedure, it must save the contents of the link register.
  • Other systems store the return address on the stack. This is slower but does not require additional code when performing a subsequent call.

The location of parameters passed to a procedure, the location of the return value(s), and which registers must be preserved by the called procedure and which ones may be clobbered, is not usually defined as part of the hardware architecture, but as part of an Application Binary Interface (ABI) document, sometimes called a Procedure Call Standard.

spo600/procedure_call.txt · Last modified: 2025/02/19 17:42 by chris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki