Page 140 - ARM 64 Bit Assembly Language
P. 140

126 Chapter 5


                                      Listing 5.19 Calling scanf and printf in C.

                1  #include <stdio.h>
                2
                3  static char str1[] = "%d";
                4  static char str2[] = "You entered %d\n";
                5  static int n = 0;
                6
                7  int main()
                8  {
                9   scanf(str1, &n);
                10  printf(str2, n);
                11  return 0;
                12  }



                  5.4.3 Standard C library functions

                  Subroutines may be defined within a program, or a set of subroutines may be packaged to-
                  gether in a library. Libraries of subroutines may be used by multiple programs, and most
                  languages provide some built-in library functions. The C language has a very large set of
                  functions in the C standard library. All of the functions in the C standard library are avail-
                  able to any program that has been linked with the C standard library. Even assembly programs
                  can make use of this library. Linking is done automatically when gcc is used to assemble the
                  program source. All that the programmer needs to know is the name of the function and how
                  to pass parameters to it.


                  5.4.4 Passing parameters


                  Listing 5.19 shows a very simple C program which reads an integer from standard input using
                  scanf and prints the integer to standard output using printf. An equivalent program written
                  in AArch64 assembly is shown in Listing 5.20. These examples show how parameters can be
                  passed to subroutines in C and equivalently in assembly language.

                  All processor families have their own standard methods, or function calling conventions,
                  which specify how arguments are passed to subroutines and how function values are returned.
                  The function call standard allows programmers to write subroutines and libraries of subrou-
                  tines that can be called by other programmers. In most cases, the function calling standards
                  are not enforced by hardware, but assembly programmers and compiler writers conform to the
                  standards in order to make their code accessible to other programmers. The basic subroutine
                  calling rules for the AArch64 processor are simple:
   135   136   137   138   139   140   141   142   143   144   145