Linux x86-64 Hello World and register usage for parameters

落花浮王杯 提交于 2020-12-06 04:14:29

问题


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?

回答1:


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.



来源:https://stackoverflow.com/questions/19743373/linux-x86-64-hello-world-and-register-usage-for-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!