/* 
 * File:        loop.s
 * Description: Prints the numbers from 1 to n.
 */


        
        .arch   armv8-a

        .global main

        .text
        .align  2
main:
        stp     fp, lr, [sp,-16]!
        mov     fp, sp
        
        adr     x0, prompt         // Pass the prompt in x0
        bl      printf             // Call printf
        
        adr     x0, infmt          // Pass the input format in x0
        adr     x1, n              // Pass the address of n in x1
        bl      scanf              // Call scanf
        
        adr     x0, i              // Put the address of i in x0
        str     xzr, [x0]          // Initialize i to 0
test:                              // Top of loop
        adr     x1, i              // Copy i to x1
        ldr     x1, [x1]
        adr     x2, n              // Copy n to x2
        ldr     x2, [x2]
        cmp     x1, x2             // Compare i and n
        beq     done               // Jump out of loop if equal
        
        adr     x0, outfmt         // Pass the output format in x0
        bl      printf             // Call printf
        
        adr     x2, i              // Copy i to x1
        ldr     x1, [x2]

        add     x1, x1, #1         // Increment
        str     x1, [x2]
        b       test               // Jump to the top of the loop
done:   
        mov     x0, #0
        ldp     fp, lr, [sp], 16
        ret
        
        .data                      // Start of data segment
        .align  3                  // Align on an address divisible by 8

i:      .quad   0                 // Loop control
n:      .quad   0                 // Upper bound
        
prompt: .string "n = "             // Prompt string
infmt:  .string "%ld"              // Input format
outfmt: .string "%ld\n"            // Output format
