Skip to content

Instantly share code, notes, and snippets.

@chenhengqi
Last active February 16, 2021 08:48
Show Gist options
  • Save chenhengqi/6b9836a95854abda01337c187d1f8c83 to your computer and use it in GitHub Desktop.
Save chenhengqi/6b9836a95854abda01337c187d1f8c83 to your computer and use it in GitHub Desktop.
Syscall

Syscall

Calling Convention

System call number and return value

Arch/ABI    Instruction           System  Ret  Ret  Error    Notes
                                    call #  val  val2
───────────────────────────────────────────────────────────────────
x86-64      syscall               rax     rax  rdx  -        5
x32         syscall               rax     rax  rdx  -        5

Arguments

Arch/ABI      arg1  arg2  arg3  arg4  arg5  arg6  arg7  Notes
──────────────────────────────────────────────────────────────
x86-64        rdi   rsi   rdx   r10   r8    r9    -
x32           rdi   rsi   rdx   r10   r8    r9    -

Code

    .text
    .global _start
_start:
    movq $1, %rax       # write() system call number
    movq $1, %rdi       # first parameter  -- standard output file descriptor
    movq $message, %rsi # second parameter -- buffer
    movq $12, %rdx      # third parameter  -- buffer length
    syscall             # invoke system call

    movq $60, %rax      # exit() system call number
    xor %rdi, %rdi      # first parameter  -- exit status
    syscall             # invoke system call; this will never return
message:
    .ascii "hello world\n"
$ gcc -c hello.s -o hello.o
$ ld hello.o

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment