Page 138 - ARM 64 Bit Assembly Language
P. 138

124 Chapter 5


                                    Listing 5.18 Post-test loop in AArch64 assembly

                1         .section .rodata
                2  str:   .string "Hello World - %d\n"
                3         .text
                           .
                4          . .
                5         ldr     x4, #0      // x4 =i=0
                6  loop:
                7         adr     x0, str     // load pointer to format string
                8         mov     x1, x4      // copy i into x1
                9         bl      printf      // printf("Hello World - %d\n", i);
                10        add     x4, x4, #1  // i++
                11        cmp     x4, #10     // perform comparison
                12        blt     loop        // continue if i < 10
                           .
                13         . .


                  5.4 Subroutines

                  A subroutine is a sequence of instructions to perform a specific task, packaged as a single
                  unit. Depending on the particular programming language, a subroutine may be called a pro-
                  cedure, a function, a routine, a method, a subprogram, or some other name. Some languages,
                  such as Pascal, make a distinction between functions and procedures. A function must return a
                  value and must not alter its input arguments or have any other side effects (such as producing
                  output or changing static or global variables). A procedure returns no value, but may alter the
                  value of its arguments or have other side effects.
                  Other languages, such as C, make no distinction between procedures and functions. In these
                  languages, functions may be described as pure or impure. A function is pure if:

                  1. the function always evaluates the same result value when given the same argument
                     value(s), and
                  2. evaluation of the result does not cause any semantically observable side effect or output.

                  The first condition implies that the function result cannot depend on any hidden information
                  or state that may change as program execution proceeds, or between different executions of
                  the program, nor can it depend on any external input from I/O devices. The result value of
                  a pure function does not depend on anything other than the argument values. If the function
                  returns multiple result values, then these two conditions must apply to all returned values.
                  Otherwise the function is impure. Another way to state this is that impure functions have side
                  effects while pure functions have no side effects.
                  Assembly language does not impose any distinction between procedures and functions, pure
                  or impure. The assembly language will provide a way to call subroutines and return from
   133   134   135   136   137   138   139   140   141   142   143