To build an executable binary, you need to:
- Write your assembly in a file (often with a
.Sor.ssyntax. - Assemble your binary into an executable object file (using the
ascommand). - Link one or more executable object files into a final executable binary (using the
ldcommand)!
.intel_syntax noprefix tells the assembler that you will be using Intel assembly syntax, and specifically the variant of it where you don't have to add extra prefixes to every instruction.
.intel_syntax noprefix
mov rdi, 42
mov rax, 60
syscallbuild
as -o asm.o asm.sLinking: combining one or more object files into a single executable using the linker (ld).
ld -o exe asm.o # optionalThe _start symbol is, essentially, a note to ld about where in your program execution should begin when the ELF is executed. you can specify the _start symbol, in your code, as so:
hacker@dojo:~$ cat asm.s
.intel_syntax noprefix
.global _start
_start:
mov rdi, 42
mov rax, 60
syscall
hacker@dojo:~$ as -o asm.o asm.s
hacker@dojo:~$ ./exe
hacker@dojo:~$ echo $?
42
hacker@dojo:~$To load the memory contents at memory address say 31337 to a register say rdi, you can do:
mov rdi, [31337]