Introduction

Hello World

Printing variables

User input

Testing printf

Mathematical operations

Functions

Arrays

Loops

Projects:

Estimating sin(x)

Calculating cube roots

Sieve of Eratosthenes

Testing printf

The printf function prints variables to the console. The printf function looks at x0 to find the string to be printed. If that string has placeholders then it looks into the subsequent x registers for values for the placeholders.
Consider the following code:

    
    // testing_printf.s

    .global main
    .extern printf

    .section .data
    output:
        .asciz "%d, %d, %d, %d, %d, %d, %d, %d, %d\n"

    .section .text
    main:
        // Prolog
        stp x29, x30, [sp, -16]!
        mov x29, sp

        // Main code
        ldr x0, =output
        mov x1, #2
        mov x2, #4
        mov x3, #6
        mov x4, #8
        mov x5, #10
        mov x6, #12
        mov x7, #14
        mov x8, #16
        mov x9, #18
        
        bl printf

        // Cleanup
        mov x0, #0
        ldp x29, x30, [sp], 16
        RET

    
    

The list of registers at the beginning of this site shows that only x0 to x7 are used for arguments and return values. It shows that x8 and x9 are temporary / scratch registers. While is is allowable to use them in code, they cannot be used to provide arguments to printf even though the output variable has 9 placeholders with the intention of printing registers x1 - x9.
When the code is run the output looks like:

    
    andrew@master:~/assm2 $ ./testing_printf
    2, 4, 6, 8, 10, 12, 14, -1037451456, -2019529256
    andrew@master:~/assm2 $ 
    
    

The final two variables are essentially random as they are not callee saved / protected and even though they had values in them prior to calling printf, they will have different values in them during and after printf is called.