Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Created June 3, 2020 20:48
Show Gist options
  • Save NanoDano/b5024d8bb147b0eaef045987071e3ceb to your computer and use it in GitHub Desktop.
Save NanoDano/b5024d8bb147b0eaef045987071e3ceb to your computer and use it in GitHub Desktop.
Nasm NetAssembler Assembly examples

nasm Net Assembler Assembly

Compile and link

Compile with:

nasm -f elf64 file.asm
# or -f elf for 32 bit

Link with:

ln -d -o outfile file.o

Hello world Assembly program

This program prints hello world.

SECTION .DATA
    hello:     db 'Hello world!',10
    helloLen:  equ $-hello

SECTION .TEXT
    GLOBAL _start

_start:
    mov eax,4            ; 'write' system call = 4
    mov ebx,2            ; file descriptor 1 = STDOUT
    mov ecx,hello        ; string to write
    mov edx,helloLen     ; length of string to write
    int 80h              ; call the kernel

    ; Terminate program
    mov eax,1            ; 'exit' system call
    mov ebx,0            ; exit with error code 0
    int 80h              ; call the kernel

Static library in Assembly

This creates a static library with a print_hello() function. This could be called from C, like in the next example.

; Compile this program using
; nasm -f elf64 static_lib.asm
; gcc myprogram.c static_lib.o
; ./a.out

SECTION .DATA
    hello:     db 'Hello world!',10
    helloLen:  equ $-hello

SECTION .TEXT
    GLOBAL print_hello

print_hello:
    mov eax,4            ; 'write' system call = 4
    mov ebx,2            ; file descriptor 1 = STDOUT
    mov ecx,hello        ; string to write
    mov edx,helloLen     ; length of string to write
    int 80h              ; call the kernel

    ; Terminate program
    mov eax,1            ; 'exit' system call
    mov ebx,0            ; exit with error code 0
    int 80h              ; call the kernel

Call assembly function from C

// Compile with
// gcc c_calling.c static_lib.o
// Automatically links the elf binary .o generated by
// the assembly compiler to resolve references

#include <stdio.h>

int main(int argc, char *argv[]) {

    // Define external from a separate library
    // Defined in something.o from a compiled
    // assembly program
    extern print_hello();

    // Call external assembly function
    print_hello();
}

Write a main() function in Assembly

This example creates a main() function in Assembly that is compatible with gcc.

; Because we have a reference to 'main'
; we can compile with nasm to create the
; .o object file, and then compile that with
; gcc. Example
; nasm -f elf64 c_main.asm
; gcc c_main.o
; ./a.out

SECTION .DATA
    hello:     db 'Hello world!',10
    helloLen:  equ $-hello

SECTION .TEXT
    GLOBAL main

main:
    mov eax,4            ; 'write' system call = 4
    mov ebx,2            ; file descriptor 1 = STDOUT
    mov ecx,hello        ; string to write
    mov edx,helloLen     ; length of string to write
    int 80h              ; call the kernel

    ; Terminate program
    mov eax,1            ; 'exit' system call
    mov ebx,0            ; exit with error code 0
    int 80h              ; call the kernel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment