In assembly programming, linking and loading are critical steps that transform the raw assembly code into an executable program. This process involves:
Use an assembler (e.g., NASM) to convert assembly code into an object file.
nasm -f elf64 my_program.asm -o my_program.o
Link the object file to create an executable using a linker (e.g., ld
).
ld my_program.o -o my_program
Run the final executable:
./my_program
Here’s a simple assembly program that prints a message to the console:
section .data message db 'Hello, world!', 0xA msg_len equ $ - message section .text global _start _start: mov rax, 1 ; System call: write mov rdi, 1 ; File descriptor: stdout mov rsi, message ; Address of the message mov rdx, msg_len ; Length of the message syscall mov rax, 60 ; System call: exit xor rdi, rdi ; Exit code: 0 syscall
To assemble, link, and run the above program:
nasm -f elf64 hello.asm -o hello.o
ld hello.o -o hello
./hello
objdump
: Inspect the object file or executable.strace
: Trace system calls made by the program.gdb
: Debug the program step by step.-f elf64
for 64-bit systems).