Page 90 - ARM 64 Bit Assembly Language
P. 90

Load/store and branch instructions  75

                     The C compiler will automatically create a constant array of characters and initialize it to
                     hold the message in the read-only data, .rodata, section. Note that strings in C must be null-
                     terminated, meaning the last character must be 0. The compiler does this automatically. The
                     compiler directive .asciz allocates a null-terminated string in memory. The program will
                     load the address of the first character in the array into register x0 before calling printf().
                     The printf() function will expect to see an address in x0, which it will assume is the ad-
                     dress of the format string to be printed. By convention, when a function is called, it will ex-
                     pect to find its first argument in R0. There are other rules which AArch64 programmers must
                     follow regarding how registers are used when passing arguments to functions and procedures.
                     Those rules will be explained fully in Chapter 5, Section 5.4.

                     Example 8 shows how the bl instruction can be used to call a function from the C standard li-
                     brary to read a single character from standard input. By convention, when a function is called,
                     it will leave its return value in R0.

                     Example 8. Using the bl instruction to read a character. Suppose we want to read a single
                     character from standard input. This can be accomplished in C by calling the getchar() func-
                     tion from the C standard library as follows:
                              .
                    1         . .
                    2  c = getchar();
                              .
                    3         . .
                     The above C code assumes that the variable c has been declared to hold the result of the
                     function. In A64 assembly language, functions always return their results in x0. The assem-
                     bly programmer may then move the result to any register or memory location they choose.
                     In the following example, it is assumed that w9 was chosen to hold the value of the vari-
                     able c:
                              .
                    1         . .
                    2         bl     getchar // Call the getchar function
                    3         mov    w9,w0   // Move the result to register 9
                              .
                    4         . .
                     The full assembly program below accepts one character of input and then prints it, utilizing
                     getchar and bl:

                    1         .section .rodata
                    2  formatString:
                    3         .string "char: %c\n"
                    4         .text
                    5         .type  main, %function
                    6         .global main
                    7  main:
   85   86   87   88   89   90   91   92   93   94   95