diff --git a/articles/assembly.md b/articles/assembly.md index bdca559..77e342b 100644 --- a/articles/assembly.md +++ b/articles/assembly.md @@ -4,7 +4,11 @@ Assembly, also known as assembler, is family of low-level [programming languages Assembly is also OS-specific, since it depends on [syscalls](syscall.md) to do anything useful and things like the program entrypoint differ between operating systems. -## Examples (x86-64 assembly, Linux) +An assembler program is then converted into machine code by an *assembler*. Some assemblers are gas, the assembler of the [GNU](gnu.md) +project which supports multiple architecures and [NASM](https://www.nasm.us/), a popular x86-only assembler. + +## Examples +### Hello world (x86-64, Linux) ```nasm default rel ; tell the assembler to use RIP-relative addressing ; instead of absolute memory addresses @@ -33,4 +37,36 @@ nasm -felf64 -o hello.o hello.asm # assemble, generates an object fi cc -static-pie -nostdlib -o hello hello.o # now link to get an executable ``` +### Hello world (ARM64, Linux) +```asm +.global _start +.section .text + +_start: + mov x0, #1 // stdout file descriptor + adr x1, msg // address of buffer, pc relative + mov x2, msg_len // length of buffer + mov w8, #0x40 // service number + svc #0 // do syscall + + mov x0, #39 // return number + mov w8, #0x5d // exit syscall + svc #0 + +.section .rodata +msg: .ascii "Hello, world!\n" +.equ msg_len, . - msg +``` + +For this we use the gas (GNU assembler): +```sh +as -o hello.o hello.asm +cc -nostdlib -o hello hello.o +``` + +### Factorial (x86-64, Linux) +```nasm +``` +TODO + TODO examples for other architectures