HomeAboutArticles

ARM64 — why did it work?

Start page1 page2 page3 page4 page5 page6 page7 page8 page9 page10

The name svc means service and is where you call into the operating system to get things done.

The first example in the "Hello World" program is this:

    mov x0,1
    adr x1,message
    mov x2,12
    mov x8,0x40
    svc 0

We want to print a string, and we need 4 pieces of information to do that:

The x-registers are 64 bit wide. The instructions in the program are 32 bit wide. The assembler is cheating a little bit with the mov-instructions. There is no room for a 64 bit value, so mov is translated to an instruction, where a 16-bit value is moved to the lower part of the x-register, and the upper 64-16=48 bits are zeroed.

Sort of the same happens with the adr-instruction, where only the difference in addresses between the adr-instruction and the message is stored — this would then be relative addressing. This works since the string is close to the instructions.

The second use of svc is this:

    mov x0,0     /* status code */
    mov x8, 93
    svc 0

The decimal value 93 is the code for stopping the program. We should also put the status code in register x0. A simple 0 when everything is fine, and some positive number to indicate errors.

If we don't set x0 to some value, then the status is whatever is in x0.

This status code can then be read in the line following the execution of the program.

user@rpi4:~/work $ ./hello1
Hello World
user@rpi4:~/work $ echo $?
0

Why use hexadecimal in the first example, and decimal in the second? It is just to show that you can do both.

Change something

Now change the status code to something not 0 — anything will do. And then read it with echo $?.

Start page1 page2 page3 page4 page5 page6 page7 page8 page9 page10

2025-06-16