Think In Geek

In geek we trust

Exploring AArch64 assembler – Chapter 1

AArch64 is a new 64 bit mode that is part of the ARMv8 architecture presented in 2011 by ARM. It has been progressively been deployed in smartphones and servers. So I think it is a good moment to learn a bit more about the assembler of this architecture.

Hardware availability

Single board computers with ARMv6/ARMv7 architecture are easily available nowadays. One of the most popular choices is the Raspberry Pi.

In contrast, single-board computers that support the 64-bit mode of ARMv8 are less common but they are slowly becoming more popular these days. For instance the Pine64, the ODROID-C2, the Dragonboard 410c, etc. Any of them will do and in general they differ on the specific System on Chip being used.

Note: the Raspberry Pi 3 has a CPU (Cortex-A53) that implements the 64-bit mode of the ARMv8 architecture and technically could run a 64-bit system. But the software system provided by the Raspberry Foundation (Raspbian) is only for 32-bit and there are no official plans for a 64-bit system.

Update: SUSE has a 64-bit version of its OpenSUSE distribution that can run in the Raspberry Pi 3. Arch also has a 64-bit distribution that can be installed in the RPi3.

Software alternative

Does this mean that without hardware it is not possible to play with AArch64? No! We can still do many things using a cross-toolchain and QEMU in user mode.

Example for Ubuntu 16.04

Just install QEMU and a cross-toolchain for AArch64.

$ sudo apt-get install qemu-user gcc-aarch64-linux-gnu

Now test you can run a “Hello world” written in C. Create a hello.c file with the following contents.

#include <stdio.h>
int main(int argc, char *argv[])
  printf("Hello AArch64!\n");
  return 0;

Now compile it with the cross-compiler for AArch64 that we have installed earlier (the -static flag is important).

$ aarch64-linux-gnu-gcc -static -o hello hello.c

Check it is a AArch64 binary.

$ file hello
hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.7.0, BuildID[sha1]=97c2bc66dbe4393aab9e4885df8e223a6baa235a, not stripped

Trying to run it should fail with some confusing error.

$ ./hello 
-bash: ./hello: No such file or directory

But we can run it using the QEMU for AArch64 that we installed earlier.

$ qemu-aarch64 ./hello
Hello AArch64!


Note: If you use this option, remember always to run your programs using qemu-aarch64.

Our first AArch64 assembler program

Let’s write a very simple program that just returns an error code of two.

// first.s
.globl main
     mov w0, #2

Let’s assemble it.

$ aarch64-linux-gnu-as -c first.s

And now link it, for convenience we will use gcc.

$ aarch64-linux-gnu-gcc -static -o first first.o

Run it and check the return.

$ ./first             # or use qemu-aarch64 ./first
$ echo $?


Let’s go through each line of the code above.

// first.s

Line 1 is just a comment with the name of the file used in this example. Any text in a line that follows a // is a comment and it is ignored. Line 2 is an assembler directive that means “now come instructions of the program”. This is because we can also express data in an assembler file (data goes after a .data directive).

.globl main

This is another assembler directive that means main is going to be a global symbol. This means that when constructing the final program, this file will have the global main symbol that is needed by the C library to start a program.

     mov w0, #2 // w0 ← 2
     ret        // return

This is the entry point of our program. Line 6 itself is just a label for the symbol main (that we mentioned above it was a global symbol). Lines 7 and 8 are two instructions. The first one just sets the register w0 to be 2 (we will see what registers are in the next chapter). The second one returns from the main, effectively finishing our program.

When finishing a program, the contents of the register w0 are used to determine the error code of the program. This is the reason why echo $? above prints 2.

Reference documentation

Documentation for the AArch64 instruction set can be found in the ARM® Architecture Reference Manual ARMv8, for ARMv8-A architecture profile

That’s all for today.

21 thoughts on “Exploring AArch64 assembler – Chapter 1

  • For those who are interested in running your software on AArch64 hardware directly, without any additional firmware or operating system in between, I’ve developed a boot loader and tool chain to test and execute unikernels on a Pine64.

    Nice to see there are other people who are into the “deeper” layers and ARM as well.

  • B2B says:

    Very nice. Looking forward for more to come.

  • Luis Guilherme says:

    Hope to get all the way to interface with a framebuffer (maybe Raspberry PI) and i/o other than GPIOs :o)

  • Javi says:

    Hi, i’m interested in code an operating system from scratch for my Raspberry Pi 3, the idea is starting doing something like Baking Pi. Due to your experience in this topic, do you see this project feasible? Should I use AArch64 assembly for program my PI? What are resources should I look for (because baking pi is for Raspberry Pi 1 and not very extensive). I hope for your answer, very nice tutorial BTW.

    • Roger Ferrer Ibáñez says:

      Hi Javi,

      this is not my field of expertise so I’m afraid I cannot give you any useful pointers or resources for an AArch64-based OS.

      I’ll let you know if I learn of any project like this.

      Kind regards,

  • David Todd says:

    Roger, I worked through your ARMv7 tutorials on my Pi-1 several years ago and found them to be excellent, so I was very excited to see this series now that I have a Pi-3! I’ve done three tutorials so far this morning! Thank you SOOO much for doing these. And I’ve enjoyed your other tutorials along the way. Excellent work! And much appreciated.

    • Roger Ferrer Ibáñez says:

      Thanks David. I will be a bit slower with this one because I have a strong feeling of “explaining again the same just with 64-bit”. So I try to spend more time being a bit more creative introducing the material. Apologies if I’m slower with new chapters.

  • David Todd says:

    Roger, For those wanting to follow your tutorial under an OS running native on the RPi-3 hardware, it is possible now to do so. A month after you published chapter 1, SUSE announced that they had ported SUSE Linux Enterprise Server (SLES 12.2) to the Raspberry Pi-3 as an aarch64 implementation [ ]. A variety of openSUSE aarch64 implementations for RPi-3 are also available [ ]. To test this out in a low-overhead manner, it is also possible to copy the image to a USB thumbdrive (or harddrive or SSD) and boot the RPi-3 from that USB device [ , see comments].

    Some components haven’t been ported yet, and some don’t have full functionality. But through your chapter 3, at least, the code compiles with gcc and runs, and gdb works (though without full debug info). Though “perf” works, it doesn’t have all the counters implemented — just gives timings, at least on the openSUSE I’m running.

    It appears that Fedora 26 will also have an aarch64 Raspberry Pi-3 implementation; last I saw, it was tentatively expected in June, 2017.

  • Chris Sears says:

    You can install a 64b Arch Linux on a Raspberry PI 3.

    • Roger Ferrer Ibáñez says:

      Hi Chris,

      this is now true but it wasn’t when I wrote that post. I will amend it, though.

      Kind regards,

  • Jason Awuku says:

    Thanks for the tutorial!

    I ran this tutorial on my Android phone.

    I am using an Android app, called Termux, on my 64-bit phone. This installs a minimal Linux-like environment, where you can operate a command-line console interface.

  • Krishna says:

    Trying to install gcc-aarch64-linux-gnu on pi 3 (sudo apt-get install qemu-user gcc-aarch64-linux-gnu)

    However I get this error during install gcc-aarch64-linux-gnu: Depends: gcc-5-aarch64-linux-gnu (>= 5.2.1-13cross1) but it is not installable E: Unable to correct problems, you have held broken packages.

    The way I understand this, the repositories have missing dependencies. Any suggestions on how to fix this?

  • Alexander says:

    Hey Roger! Thanks a lot for the tutorial!

    Unfortunately, I don’t understand the difference between “aarch64-linux-gnu-as” and “as”. I guess, If I have aarch64 hardware like Pinebook I don’t need cross compiler, I can just run “as”, right? But it seems, that “as” and “aarch64-linux-gnu-as” used different syntax…

    Just as a feedback – a bit information about environment differences and ways of assembling… would be great to have 🙂


    p.s. There is no company called “SuSE” today. It’s SUSE. And there is no distribution called “openSuSE”. It’s openSUSE (Leap or Tumbleweed).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.