These articles are written by Codalogic empowerees as a way of sharing knowledge with the programming community. They do not necessarily reflect the opinions of Codalogic.

The Simplest AArch64 Program - exit

By: Pete, October 2022

(Code for this mini-series can be downloaded from Github)

I'm getting back into assembly programming and have chosen the AArch64 architecture to work on.

It's been harder to find material on this than I had thought. Searching yields lots of near misses!

You can easily find the AArch64 assembly instructions themselves but finding the conventions of how the assembly should be laid out in a file so the assembler can parse it correctly is harder.

This is the simplest correct program you can write on Aarch64. It is derived from the example at: https://peterdn.com/post/2020/08/22/hello-world-in-arm64-assembly/

Even though it is simple it contains a lot of useful information for getting started.

It shows how comments are written (pretty much essential when writing assembler), how to identify the section containing program code as opposed to data, how to mark the start of your program and how to exit.

The program (which I stored in the file exit.s) is as follows:

// Start the segment containing the program code (as opposed to data)
.text

// Specify the application's entry point.
.global _start

// The start of the code
_start:
    // invoke exit using the exit(int status) syscall
    mov     x0, #0      // status = 0
    mov     w8, #93     // exit syscall is #93
    svc     #0          // invoke syscall

To turn the program into an executable you need an assembler and linker. I'm using these on my Raspberry 4, via the following instructions:

pete$ as -o exit.o exit.s
pete$ ld -o exit exit.o

To run the program do:

pete$ ./exit

To make this sequence easier to iterate quickly I've created a bash file called aarch64 that contains:

as -o $1.o $1.s && ld -o $1 $1.o && ./$1

(Don't forget to do chmod +x aarch64 to make the bash file executable.)

For the above case I would then be able to execute:

pete$ aarch64 exit

The program does nothing! That's actually a good thing because one of the things it could do if it was wrong would be to cause a seg fault!

For example, one way to get a seg fault is to naively assume that the way to exit the program is to simply use a ret instruction.

Over the coming weeks I hope to add more assembly programs. However, now I have got one program working I'm hoping I can actually focus on the assembly instructions themselves rather than worrying about how to keep the assembler happy.