Page 145 - ARM 64 Bit Assembly Language
P. 145

Structured programming 131


                                            Listing 5.23 A larger function call in C.

                    1  #include <stdio.h>
                    2
                    3  static int i = 120;
                    4  static int k = 122;
                    5
                    6  int main(void)
                    7  {
                    8   int j = 121;
                    9   printf("The results are: %d %d %d\n",i,j,k);
                   10   return 0;
                   11  }



                     5.4.5.1 Passing arguments in registers

                     Listing 5.23 shows a call to printf in C having four arguments. The format string is the first
                     argument. The format string contains three conversion specifiers, and is followed by three
                     more arguments. Arguments are matched to conversion specifiers according to their posi-
                     tions. The type of each argument matches the type indicated in the conversion specifier. The
                     first conversion specifier is applied to the second argument, the second conversion specifier
                     is applied to the third argument, and the third conversion specifier is applied to the fourth
                     argument. The %d conversion specifiers indicate that the arguments are to be interpreted as
                     integers and printed in base ten. Listing 5.24 shows an equivalent call made in AArch64 as-
                     sembly language. The arguments are loaded into x0, w1, w2, and w3 in conformance with
                     the AArch64 subroutine calling convention.

                     As long as there are eight or fewer arguments that must be passed, they can all fit in registers
                     x0-x7, but when there are more arguments, things become a little more complicated. Any
                     remaining arguments must be passed on the program stack. Care must be taken to ensure that
                     the arguments are pushed to the stack in the proper order. Also, after the function call, the
                     arguments must be removed from the stack, so that the stack pointer is restored to its original
                     value. The tricky part is that, on AArch64 processors, the stack is only allowed to grow or
                     shrink in 16-byte increments.

                     5.4.5.2 Passing arguments on the stack

                     Listing 5.25 shows a call to printf in C having more than eight arguments. The format string
                     is the first argument. The format string contains ten conversion specifiers, which implies that
                     the format string must be followed by ten additional arguments. Arguments are matched to
                     conversion specifiers according to their positions. The type of each argument must match
   140   141   142   143   144   145   146   147   148   149   150