Page 159 - ARM 64 Bit Assembly Language
P. 159

Structured programming 145





                               Listing 5.37 Initializing a structured data type in AArch64 assembly.
                    1         .data
                    2  sam:   .asciz  "Sam"
                    3  smith:  .asciz  "Smith"
                    4         .text
                    5         .equ   s_first_name, 0
                    6         .equ   s_last_name, 30
                    7         .equ   s_class, 60
                    8         .equ   s_grade, 64
                    9         .equ   s_size, 68
                   10         .equ   s_size_aligned, 80
                   11
                   12         .type  main, %function
                   13         .global main
                   14  main:
                   15          stp    x29, x30, [sp, #32]!
                   16          str    x19, [sp, #16]         // Callee-saved save
                   17
                   18          // struct student newstudent
                   19          sub    sp, sp, #s_size_aligned
                   20          mov    x19, sp                // x19=pointer to struct
                   21          add    x0, x19, #s_first_name  // offset to first name
                   22          adr    x1, sam                // load pointer to "Sam"
                   23          bl     strcpy                 // copy the string
                   24          add    x0, x19, #s_last_name  // offset to last name
                   25          adr    x1, smith              // load pointer to "Smith"
                   26          bl     strcpy                 // copy the string
                   27          mov    w1, #2                 // load constant value of 2
                   28          strb   w1, [x19, #s_class]    // store with offset
                   29          mov    w1, #88                // load value of 88
                   30          str    w1, [x19, #s_grade]    // store with offset
                   31
                   32          // use the struct
                               .
                   33          . .
                   34          // delete the struct
                   35          add    sp, sp, #s_size_aligned
                   36
                   37          // return 0
                   38          mov    w0, #0
                   39          ldr    x19, [sp, #16]         // Callee-saved restore
                   40          ldp    x29, x30, [sp], #32
                   41          ret
                   42          .size  main,(. - main)
   154   155   156   157   158   159   160   161   162   163   164