NASM Coding: Basics - Examples

  1. ret = write (myfile, arr, 13);

    This is a C syscall for writing the first

    13
    characters of string
    arr
    into a file (or device, like the screen) whose file ID number is in
    myfile
    . The
    write
    syscall returns the number of successfully written characters, or
    -1
    in case of an error.

    Since

    write
    takes only $3$ arguments, we'll use only the registers
    rdi
    ,
    rsi
    , and
    rdx
    to put arguments in, and
    rax
    for the syscall number. After the syscall returns,
    rax
    will include the return value from
    write
    .

    mov       rax, 1                  ; syscall code 1 is for 'write'
    mov       rdi, 1                  ; file ID 1 is for stdout (= screen)
    mov       rsi, arr                ; String variable 'arr' to output
    mov       rdx, 13                 ; number of bytes / characters to output
    syscall                           ; invoke operating system to do the writing