Page 144 - ARM 64 Bit Assembly Language
P. 144

130 Chapter 5


                                         Listing 5.21 Simple function call in C.

                1     printf("Hello World");



                                  Listing 5.22 Simple function call in AArch64 assembly.

                1         // load first param (pointer to  format string) in x0
                2         adr     x0, hellostr
                3         // call printf
                4         bl      printf



                  Our first examples of calling a function will involve the printf function from the C standard
                  library. The printf function can be a bit confusing at first, but it is an extremely useful and
                  flexible function for printing formatted output. The first argument is the address of a format
                  string, which is a null-terminated ASCII string. The printf function examines the format
                  string to determine how many other arguments have been passed to it. The format string may
                  include conversion specifiers, which start with the % character.

                  For each conversion specifier, printf assumes that an argument has been passed in the cor-
                  rect register or location on the stack. The argument is retrieved, converted according to the
                  specified format, and printed. The %d format specifier causes the matching argument to be
                  printed as a signed decimal number. Other specifiers include %X to print the matching argu-
                  ment as an integer in hexadecimal, %c to print the matching argument as an ASCII character,
                  and %s to print a null-terminated string. The integer specifiers can include an optional width
                  and zero-padding specification. For example %8X will print an integer in hexadecimal, using
                  eight characters. Any leading zeros will be printed as spaces. The format string %08X will also
                  print an integer in hexadecimal, using eight characters, but in this case, any leading zeros will
                  be printed as zeros. Similarly, %15d can be used to print an integer in base ten using spaces
                  to pad the number up to fifteen characters, while %015d will print an integer in base ten using
                  zeros to pad up to fifteen characters.

                  Listing 5.21 shows a call to printf in C. The printf function requires one argument, but
                  can accept more than one. In this case, there is only one argument, the format string. Since
                  the format string contains no conversion specifiers, printf does not require any additional
                  arguments. Listing 5.22 shows an equivalent call made in AArch64 assembly language. The
                  single argument (the address of the format string) is loaded into r0 in conformance with the
                  AArch64 subroutine calling convention.
   139   140   141   142   143   144   145   146   147   148   149