Skip to content

Instantly share code, notes, and snippets.

@sidsenkumar11
Forked from yellowbyte/compiling_asm.md
Created October 21, 2018 22:10
Show Gist options
  • Save sidsenkumar11/cc569cb6ca8f0b143cdf960f44d5b34d to your computer and use it in GitHub Desktop.
Save sidsenkumar11/cc569cb6ca8f0b143cdf960f44d5b34d to your computer and use it in GitHub Desktop.
how to assemble assembly with NASM assembler to 32-bit or 64-bit ELF binary with or without libc

32-bit ELF binary

how to assemble and link:

nasm -f elf32 -o <filename>.o <filename>.asm
ld -m elf_i386 -o <filename> <filename>.o

template code (hello world):

section .text
global _start

_start:
    mov ebx, 0x1
    mov ecx, hello
    mov edx, helloLen
    mov eax, 0x4
    int 0x80

    xor ebx, ebx
    mov eax, 0x1
    int 0x80

section .data
    hello db "Hello World", 0xa
    helloLen equ $-hello

64-bit ELF binary

how to assemble and link:

nasm -f elf64 -o <filename>.o <filename>.asm
ld -o <filename> <filename>.o

template code (hello world):

section .text
global _start

_start:
    mov rdi, 0x1
    mov rsi, hello
    mov rdx, helloLen
    mov rax, 0x1
    syscall

    xor rdi, rdi
    mov rax, 0x3c
    syscall

section .data
    hello db "Hello World", 0xa
    helloLen equ $-hello

32-bit ELF binary with libc

how to assemble and link:

nasm -f elf32 -o <filename>.o <filename>.asm
ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o <filename> -lc <filename>.o

template code (hello world):

extern puts
extern exit

section .text
global _start

_start:
    push hello
    call puts
    add esp, 0x4

    mov eax, 0xa
    call exit

section .data
    hello db "Hello World"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment