aarch64-pg
4.3 · Printing Hello World in ARMv8 AArch64 Assembly

Printing Hello World in ARMv8 AArch64 Assembly

note

The Assembly language discussed in this lesson refers specifically to ARMv8 AArch64 Assembly.

In this lesson, we will cover the concepts behind printing a string to standard output using the low-level programming language Assembly. Printing to standard output in a low-level programming language is not as straightforward as using a print statement in a high-level programming language.

A high-level programming language provides built-in functions that allow programmers to easily display information on the standard output stream. The code below demonstrates this process in the C programming language using the printf function.

#include <stdio.h>int main() {    printf("Hello, World!\n");    return 0;}
Open in playground

The above code outputs the value passed to the printf function to the standard output stream. In this example, the string "Hello, World!" is passed as an argument to printf, causing it to be displayed on the screen.

However, printing in Assembly is not as simple. Several additional steps must be performed before data can be displayed on the standard output. This page will explore these steps and provide an understanding of how printing is achieved in ARMv8 Assembly.

The Big Picture

Printing in Assembly can be achieved by calling the C printf function. To display output on the standard output stream, the following steps must be performed:

  1. Store the string to a variable.
  2. Load the variable's address into the appropriate registers.
  3. Pass the address as an argument and calling the printf function.

Storing the String

The first step in printing a string in Assembly is to store the string in a variable. This is done by using the .data directive to define a data section in the Assembly code. The string is then stored in a variable using the .string directive. The following code demonstrates this process:

.data     message: .string "Hello, World!\n"
Open in playground

In the above code, the string "Hello, World!\n" is stored in a variable named message. The .string directive specifies that the data being stored is a string.

Loading the Variable's Address

The next step in printing a string in Assembly is to load the variable's address into the appropriate registers. This is done using the ldr instruction, which loads the address of the variable into a register. The following code demonstrates this process:

// pseudo-ops and directives main:     // program prologue    ..    ldr x0, =message    ..    // program epilogue
Open in playground

In the above code, the address of the message variable is loaded into register x0 using the ldr instruction.

Calling the printf Function

The final step in printing a string in Assembly is to call the printf function. This is done using the bl instruction. The following code demonstrates this process:

// pseudo-ops and directivesmain:     //program prologue    ..        ldr x0, =message     bl printf     ..    //program epilogue
Open in playground

In the above code, the printf function from C is called using the bl instruction. The address of the message variable is passed as an argument to printf, causing it to be displayed on the standard output stream.

Putting it All Together

The following code demonstrates the complete process of printing a string in Assembly:

loading editor...
figure 4.3.1runnable — step it and watch the registersOpen in playground