Page 150 - ARM 64 Bit Assembly Language
P. 150
136 Chapter 5
Listing 5.28 A small C function with a register variable.
1 int doit(void)
2 {
3 int x[20];
4 register int i; /* try to keep i in a register */
5 for(i = 0; i < 20; i++)
6 x[i] = i;
7 return i;
8 }
• registers x19-x29 are non-volatile (they can be used, but their contents must be restored
to their original value before the function returns),
• register x30 can be used by the function, but its contents must be saved so that they can be
loaded into the program counter, which will cause the function to return to its caller.
• Although the platform register, x18, has no special use on Linux systems, it should be
avoided because it may have a special use on some platforms, making user-level assembly
code that uses it less portable. If portability to another operating system is not a concern,
then it can be used as another volatile register.
5.4.7 Automatic variables
In block-structured high-level languages, an automatic variable is a variable that is local to a
block of code and not declared with static duration. It has a lifetime that lasts only as long as
its block is executing. Automatic variables can be stored in one of two ways:
1. the stack is temporarily adjusted to hold the variable, or
2. the variable is held in a register during its entire life.
When writing a subroutine in assembly, it is the responsibility of the programmer to decide
what automatic variables are required, and also where they will be stored. In high-level lan-
guages this decision is usually made by the compiler. In some languages, including C, it is
possible to request that an automatic variable be held in a register. The compiler will attempt
to comply with the request, but it is not guaranteed. Listing 5.28 shows a small function which
requests that one of its variables be kept in a register instead of on the stack.
Listing 5.29 shows how the function could be implemented in assembly. Note that the array
of integers consumes 80 bytes of storage on the stack, and that the loop control variable can
easily be stored in one of the registers for the duration of the function. Also notice that on
line 4 the storage for the array is allocated simply by adjusting the stack pointer, and on line