From 0436000f1e792d0b642028a25fb91e8858340b00 Mon Sep 17 00:00:00 2001 From: tocariimaa Date: Tue, 18 Feb 2025 13:38:19 -0300 Subject: [PATCH] articles/assembly.md: new article --- articles/assembly.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 articles/assembly.md diff --git a/articles/assembly.md b/articles/assembly.md new file mode 100644 index 0000000..66fee48 --- /dev/null +++ b/articles/assembly.md @@ -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