Hello world (ASM)

Details

  • Platform: x86_64
  • ASM flavor: Intel
  • Assembler: as (from GNU)

Source code

    .section .text          # Code section
    .global _start          # Entry point
_start:                     # Label (entry point)
    call print              # Call print function
    mov rax, 60             # Syscall number: 60 (= exit)
    xor rdi, rdi            # Syscall arg 0: 0 (= code)
    syscall                 # Perform syscall

print:                      # Label (print function)
    mov rax, 1              # Syscall number: 1 (= write)
    mov rdi, 1              # Syscall arg 0: 1 (= stdout)
    lea rsi, [rip+msg]      # Syscall arg 1: [rip+msg] (= address)
    mov rdx, 12             # Syscall arg 2: 12 (= length)
    syscall                 # Perform syscall
    ret                     # Return to _start

    .section .data          # Data section
msg:                        # Label (print message)
    .string "Hello world\n" # Output string

Generation

$ as -o main.o main.s --64 --msyntax=intel --mnaked-reg
$ ld -o main main.o

Execution

$ ./main
Hello world