NASM Coding: Basics - Examples

  1. ret = read (myfile, arr, 64);

    This is a C syscall for reading up to

    64
    characters from a file (or device, like the keyboard) whose file ID number is in
    myfile
    into string
    arr
    . The
    read
    syscall returns the number of successfully read characters, or
    -1
    in case of an error.

    As it was with

    write
    , the
    read
    syscall also takes only $3$ arguments and uses 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
    read
    .

    mov       rax, 0                  ; syscall code 0 is for 'read'
    mov       rdi, 0                  ; file ID 0 is for stdin (= keyboard)
    mov       rsi, arr                ; String variable 'arr' to read into
    mov       rdx, 64                 ; max number of bytes / characters to read
    syscall                           ; invoke operating system to do the reading