From 495c13b6993d5e7efdf5180a9e3de07c1f0c7fe8 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Mon, 21 Jun 2021 22:32:16 +0100 Subject: Initial arm64 hello world --- md/writeup/arm64_assembly_hello_world.md | 117 +++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 md/writeup/arm64_assembly_hello_world.md (limited to 'md') diff --git a/md/writeup/arm64_assembly_hello_world.md b/md/writeup/arm64_assembly_hello_world.md new file mode 100644 index 0000000..5601b89 --- /dev/null +++ b/md/writeup/arm64_assembly_hello_world.md @@ -0,0 +1,117 @@ +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 + +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 good 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. */ +msg: + .ascii "Hello, ARM64!\n" +len = . - msg + +.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, =msg /* buf := msg */ + ldr x2, =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 there is 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 + +// Setup the parameters to print hello world +// and then call the Kernel to do it. +_start: + mov X0, #1 // 1 = StdOut + adr X1, helloworld // string to print + mov X2, #13 // length of our string + mov X16, #4 // Unix write system call + svc #0x80 // Call kernel to output the string + +// Setup the parameters to exit the program +// and then call the kernel to do it. + 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 World!\n" +``` + +### 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/) \ No newline at end of file -- cgit v1.2.3