x86-64 TUTORIAL: INPUT OUTPUT FUNCTIONS


x86-64 TUTORIAL: HELLO WORLD!  |  x86-64 TUTORIAL: MULTIPLICATION & DIVISION

Here are some print functions for strings, integers and newline characters. There is also a function for reading an integer. All the code is in NASM’s syntax. The macros prologue and epilogue, are used to save space and avoid repetitiveness.

NOTE: Remember that the registers RBP, RBX and R12-R15 need to be saved across function calls.

All of the print functions here use the C library printf() function. Ideally we should use the write() system call, but I haven’t written an implementation of printf() using write() yet. Similary we use scanf() instead of the read() system call.

The below code will be referenced as the file asm_io.asm for the rest of the tutorial. Make a note that instead of using the data section, the read-only data section .rodata has been used.

%macro prologue 0
        push    rbp
        mov     rbp,rsp
        push    rbx
        push    r12
        push    r13
        push    r14
        push    r15
%endmacro
%macro epilogue 0
        pop     r15
        pop     r14
        pop     r13
        pop     r12
        pop     rbx
        leave
        ret
%endmacro

section .rodata
    int_format  db  "%i",0
    string_format db "%s",0

section .text
    global  print_string, print_nl, print_int, read_int
    extern printf, scanf, putchar

    print_string:
        prologue
        ; string address has to be passed in rdi
        mov     rsi,rdi
        mov     rdi,dword string_format
        xor     rax,rax
        call    printf
        epilogue

    print_nl:
        prologue
        mov     rdi,0xA
        xor     rax,rax
        call    putchar
        epilogue

    print_int:
        prologue
        ;integer arg is in rdi
        mov     rsi, rdi
        mov     rdi, dword int_format
        xor     rax,rax
        call    printf
        epilogue

    read_int:
        prologue
        ;rdi is assumed to have the address of the int to be read in
        mov     rsi, rdi
        mov     rdi, dword int_format
        xor     rax,rax
        call    scanf
        epilogue

The arguments to these functions have been passed in the RDI register so as to follow the x86-64 ABI specifications. C library functions need to be passed their arguments according to the x86-64 ABI specifications. Remember that functions like printf() and putchar() are buffered output functions, i.e. they buffer the output and then print it on screen when the buffer is full or when explicitly flushed. This is unlike the write() system call where the data is written onto the file (or stdout) pointed to by the file descriptor immediately.

The below code will be placed in the include file asm_io.inc and will be included in other programs if they call the above defined functions.

extern  print_string, print_nl, print_int, read_int

Download asm_io.inc and asm_io.asm.


x86-64 TUTORIAL: HELLO WORLD!  |  x86-64 TUTORIAL: MULTIPLICATION & DIVISION
SUPPORT THIS SITE
Donate DOGECOIN to DBevjMg3fd8C5oxZbV8sFpAffo6Tas1s8Q. DBevjMg3fd8C5oxZbV8sFpAffo6Tas1s8Q Donate BITCOIN to 19hrWWw1dPvBE1wVPfCnH8LqnUwsT3NsHW. 19hrWWw1dPvBE1wVPfCnH8LqnUwsT3NsHW
As an Amazon Associate I earn from qualifying purchases.