summaryrefslogtreecommitdiffstats
path: root/md/writeup/arm64_assembly_hello_world.md
blob: 118e7b6b3f97d22f3e23af0c85861237e393459e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
title:ARM64 assembly hello world
keywords:assembly,m1,arm64,macos,linux

# ARM64 assembly hello world

## Intro

Hello world in ARM64 assembly for Linux and Macos. First example
is how to compile hello world for raspberry pi 4, as its supports
ARMv8 instruction set. Second example is how to run assembly on 
Apple M1 chip that also supports ARMv8 instruction set

The two assembly examples are equivalent to C code

```c
int main() {
    char *s="Hello ARM64";
    write(1,s,strlen(s));
    exit(0);
}
```

## Raspberry Pi 4

Running 64bit linux. To detect with architecture and what bitness of 
os run command

```
uname
```

Architecture shown as aarch64 enoughs to indicate that os ir 64bit
```
Linux raspberrypi 5.4.42-v8+ #1319 SMP PREEMPT Wed May 20 14:18:56 BST 2020 aarch64 GNU/Linux
```

```asm
.data

/* Data segment: define our message string and calculate its length. */
helloworld:
    .ascii        "Hello, ARM64!\n"
helloworld_len = . - helloworld

.text

/* Our application's entry point. */
.globl _start
_start:
    /* syscall write(int fd, const void *buf, size_t count) */
    mov     x0, #1              /* fd := STDOUT_FILENO */
    ldr     x1, =helloworld     /* buf := msg */
    ldr     x2, =helloworld_len /* count := len */
    mov     w8, #64             /* write is syscall #64 */
    svc     #0                  /* invoke syscall */

    /* syscall exit(int status) */
    mov     x0, #0               /* status := 0 */
    mov     w8, #93              /* exit is syscall #1 */
    svc     #0                   /* invoke syscall */
```

### Compile

Too compile check if you have installed gnu gcc, other compilers such as clang also 
should work perfectly fine.

```makefile
    as hello.s -o hello.o
    gcc hello.o -o hello
```

## Apple M1

```asm
.global _start            // Provide program starting address to linker
.align 2                  // Make sure everything is aligned properly

/* syscall write(int fd, const void *buf, size_t count) */
_start: 
    mov    X0, #1         // 1 = StdOut
    adr    X1, helloworld     // string to print
    mov    X2, helloworld_len // length of our string
    mov    X16, #4            // Unix write system call
    svc    #0x80              // Call kernel to output the string

/* syscall exit(int status) */
    mov     X0, #0            // Use 0 return code
    mov     X16, #1           // System call number 1 terminates this program
    svc     #0x80             // Call kernel to terminate the program

helloworld:      .ascii  "Hello, ARM64!\n"
helloworld_len = . - helloworld
```

### Compile

Install xcode tools before compilation

```makefile
    as -o hello.o hello.s
	ld -macosx_version_min 11.0.0 -o hello hello.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64
```

## Links

[https://en.wikipedia.org/wiki/ARM_architecture#ARMv8](https://en.wikipedia.org/wiki/ARM_architecture#ARMv8)   
[https://smist08.wordpress.com/2021/01/08/apple-m1-assembly-language-hello-world/](https://smist08.wordpress.com/2021/01/08/apple-m1-assembly-language-hello-world/)  
[https://github.com/below/HelloSilicon](https://github.com/below/HelloSilicon)  
[https://thinkingeek.com/2013/01/09/arm-assembler-raspberry-pi-chapter-1/](https://thinkingeek.com/2013/01/09/arm-assembler-raspberry-pi-chapter-1/  
[https://s-matyukevich.github.io/raspberry-pi-os/docs/lesson01/rpi-os.html](https://s-matyukevich.github.io/raspberry-pi-os/docs/lesson01/rpi-os.html)  
[https://peterdn.com/post/2020/08/22/hello-world-in-arm64-assembly/](https://peterdn.com/post/2020/08/22/hello-world-in-arm64-assembly/)