简体   繁体   中英

Linux x86-64 Hello World and register usage for parameters

I found this page which has a Hello World example for x86-64 on Linux:

http://blog.markloiseau.com/2012/05/64-bit-hello-world-in-linux-assembly-nasm/

; 64-bit "Hello World!" in Linux NASM

global _start            ; global entry point export for ld

section .text
_start:

    ; sys_write(stdout, message, length)

    mov    rax, 1        ; sys_write
    mov    rdi, 1        ; stdout
    mov    rsi, message    ; message address
    mov    rdx, length    ; message string length
    syscall

    ; sys_exit(return_code)

    mov    rax, 60        ; sys_exit
    mov    rdi, 0        ; return 0 (success)
    syscall

section .data
    message: db 'Hello, world!',0x0A    ; message and newline
    length:    equ    $-message        ; NASM definition pseudo-instruction

The Author says:

An integer value representing the system_write call is placed in the first register, followed by its arguments. When the system call and its arguments are all in their proper registers, the system is called and the message is displayed.

  • What does he mean by "proper" registers/What would be an im"proper" register?
  • What happens if I have a function with more arguments than I have registers?
  • Does rax always point to the function call (this would always be a system call?)? Is that its only purpose?

By "the proper registers", the author means the registers specified by the x86-64 ABI , in the Linux Kernel Calling Conventions section. The system call number goes in rax , and arguments go in rdi , rsi , rdx , r10 , r8 and r9 , in that order.

This calling convention (especially the use of syscall !) is only used for system calls, which can only have up to six arguments. Application functions use a different (but similar) calling convention which spills some arguments to the stack, or to other registers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM