Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Your First Program

The following examples demonstrate how to write a simple “Hello, world!” program.

  • Windows:

    Source code:

    .import kernel32.dll GetStdHandle
    .import kernel32.dll WriteFile
    
    .data
    msg:
        .ascii "Hello, world!\r\n"
    written:
        dd 0
    
    .text
    _start:
        sub rsp, 40
    
        mov rcx, -11
        call GetStdHandle
    
        mov rcx, rax
        lea rdx, [rip + msg]
        mov r8, 15
        lea r9, [rip + written]
        mov qword [rsp + 32], 0
        call WriteFile
    
        add rsp, 40
        mov rax, 0
        ret
    

    Running the program:

    # Using the JIT compiler:
    rasm run main.s
    
    # Using AOT compilation:
    rasm build main.s
    
    ./main
    

  • Linux: Source code:

    _start:
        jmp print
    
    msg:
        .ascii "Hello, world!\n"
    
    print:
        mov rax, 1
        mov rdi, 1
        lea rsi, [rip + msg]
        mov rdx, 14
        syscall
    
        mov rax, 60
        xor rdi, rdi
        syscall
    

    The program is built and executed in the same way as on Windows.


  • macOS:

    Examples for macOS will be added after the Mach-O backend has been fully tested.


Why is the code different?

On Windows, output is performed using the WinAPI because direct system calls are not considered a stable user-space programming interface.

On Linux, output is performed using the write system call.