Page 143 - ARM 64 Bit Assembly Language
P. 143
Structured programming 129
registers, x0-x7, are considered to be volatile, because their contents can change whenever
a subroutine is called. If the contents are needed after the subroutine call, then they must be
saved either to a non-volatile register or to the stack before the subroutine is called.
Registers x8-x17 are used for holding local variables in a subroutine. These registers are
also considered to be volatile Some of these registers are used for special purposes by the
operating system and/or compiler. From the perspective of the programmer who is writing a
user-level program, the special purposes are not important, but we will explain them briefly
nonetheless. Register x8 is used when using the svc instruction on the Linux operating sys-
tem, to specify which system call is being invoked. The intra-procedure scratch registers,
x16 and x17, are used by the C library when calling dynamically linked functions. If a sub-
routine does not call any C library functions, then it can use x16 and x17 as extra registers
to store local variables. If a C library function is called, it may change the contents of x16
and x17. Therefore, if x16 or x17 are being used to store a local variable, then their contents
should be saved to another register or to the stack before a C library function is called. This is
called caller-saved because the caller is responsible for saving the contents of the registers and
restoring them after a subroutine.
Registers x19-x28 can also be used for holding local variables. However, before using them,
the subroutine must save their contents (usually on the stack) and their contents must be re-
stored before the subroutine exits. These registers are considered non-volatile because their
contents will not be changed by a subroutine call. More precisely, the subroutine may use
them, but it will restore their contents before it returns.
As mentioned in Section 3.2.2, register x29 can also be referred to colloquially as the frame
pointer FP because it is used by the C compiler to track the stack frame, unless the code is
compiled using the --omit-frame-pointer command line option. This register must be
called x29 in GNU AArch64 code.
5.4.5 Calling subroutines
The stack pointer (sp), link register (x30), and program counter (pc), along with the argument
registers, are all involved in performing subroutine calls. The calling subroutine must place ar-
guments in the argument registers, and possibly on the stack as well. Placing the arguments in
their proper locations is known as marshaling the arguments. After marshaling the arguments,
the calling subroutine executes the bl instruction, which will modify the program counter and
link register. The bl instruction copies the contents of the program counter to the link register,
then loads the program counter with the address of the first instruction in the subroutine that is
being called. The CPU will then fetch and execute its next instruction from the address in the
program counter, which is the first instruction of the subroutine that is being called.