Libraries#
Assembly programs link against the same C runtime everyone else uses.
“Libraries” in the assembly sense usually means the ABI for calling
into libc, the syscall interface to the kernel, and a handful of
helper macros distributed with the assembler.
libc#
Linking against libc lets the program call printf, malloc,
and friends directly from assembly.
global main
extern printf
section .data
fmt: db "n = %d", 10, 0
section .text
main:
sub rsp, 8
mov rdi, fmt
mov rsi, 42
xor eax, eax
call printf
add rsp, 8
xor eax, eax
ret
$ nasm -felf64 demo.asm -o demo.o
$ gcc demo.o -o demo
Linux syscalls#
Without libc, talk directly to the kernel via the syscall
instruction. Numbers are in <asm/unistd.h> (e.g. read=0,
write=1, open=2, execve=59, exit=60 on x86-64).
syscall # |
Name |
Args (rdi, rsi, rdx, r10, r8, r9) |
|---|---|---|
0 |
|
fd, buf, count |
1 |
|
fd, buf, count |
2 |
|
pathname, flags, mode |
3 |
|
fd |
59 |
|
pathname, argv, envp |
60 |
|
status |
231 |
|
status |
Macro libraries#
NASM
%include, text-substitution macros,%define,%macro.GAS
.macro/.endm, macros and conditional assembly.nasm-stdlib and similar community macro packs.
References#
Libraries, the C wrappers around the same syscalls.