.data
hello:
.ascii "Hello\n"
helloLen = . - hello
.text
.global _start
_start:
// Set up registers for write syscall
mov x0, #1 // stdout file descriptor
ldr x1, =hello // address of string to print
mov x2, #helloLen // length of string
mov x8, #64 // write syscall number
svc #0 // invoke syscall
// Set up registers for exit syscall
mov x0, #0 // exit status
mov x8, #93 // exit syscall number
svc #0 // invoke syscall
Explanation:
-
In the .data section, we define the "Hello" string with a newline character at the end. We also calculate the length of the string using the . symbol which represents the current address.
-
In the .text section, we define the _start label as the entry point.
-
We set up the registers for the write syscall:
x0 is set to 1 for stdout file descriptor
x1 is loaded with the address of the hello string using ldr
x2 is set to the length of the string
x8 is set to 64, the syscall number for write
svc #0 invokes the syscall
-
After printing, we set up the registers for the exit syscall:
x0 is set to 0 for exit status
x8 is set to 93, the syscall number for exit
svc #0 invokes the syscall
To assemble and run:
$ as hello.s -o hello.o
$ ld hello.o -o hello
$ ./hello
Hello
This program follows the standard 64-bit ARM assembly conventions for Linux. The syscall numbers and register usage are specific to ARM64 architecture.