articles/assembly.md: new article

This commit is contained in:
tocariimaa 2025-02-18 13:38:19 -03:00
parent c9a068f430
commit 0436000f1e

36
articles/assembly.md Normal file
View file

@ -0,0 +1,36 @@
# Assembly
Assembly, also known as assembler, is family of low-level [programming languages](programming_language.md) that are closely tied to a certain specific
[CPU](cpu.md) architecture machine code, thus by definition assembly is not portable.
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)
```nasm
default rel ; tell the assembler to use RIP-relative addressing
; instead of absolute memory addresses
section .text ; start of the section containing the program's code (.text)
global _start ; make entrypoint symbol global
_start: ; On Linux, the executable entrypoint symbol name is `_start`
mov rax, 0x1 ; sys_write
mov rdi, 0x1 ; stdout
mov rdx, hello_str_len ; length of buffer
lea rsi, [hello_str] ; address of buffer
syscall
mov rax, 0x3c ; sys_exit (60)
xor rdi, rdi ; return code, zero
syscall
section .rodata ; start of the section containing readonly data (.rodata)
hello_str: db "Hello world!", 0xa ; `db` is an assembler directive for embdedding data (a string here)
hello_str_len: equ $-hello_str ; assemble-time length calculation
```
Assembling and linking:
```sh
nasm -felf64 -o hello.o hello.asm # assemble, generates an object file
cc -static-pie -nostdlib -o hello hello.asm # now link to get an executable
```
TODO examples for other architectures