Linux x86-64 Hello World并注册参数的使用

时间:2022-03-16 05:34:00

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

我在这个页面上找到了x86-64在Linux上的Hello World示例:

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

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.

表示system_write调用的整数值被放在第一个寄存器中,后面跟着它的参数。当系统调用及其参数都在正确的寄存器中时,系统将被调用并显示消息。

  • 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?
  • rax是否总是指向函数调用(这总是一个系统调用?)这是它唯一的目的吗?

1 个解决方案

#1


3  

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.

通过“适当的寄存器”,作者指的是x86-64 ABI所指定的寄存器,在Linux内核调用约定部分。系统调用号以rax表示,参数以rdi、rsi、rdx、r10、r8和r9的顺序表示。

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.

这个调用约定(特别是syscall的使用)只用于系统调用,系统调用最多只能有6个参数。应用程序函数使用不同的(但类似的)调用约定,该约定将一些参数泄露给堆栈或其他寄存器。

#1


3  

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.

通过“适当的寄存器”,作者指的是x86-64 ABI所指定的寄存器,在Linux内核调用约定部分。系统调用号以rax表示,参数以rdi、rsi、rdx、r10、r8和r9的顺序表示。

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.

这个调用约定(特别是syscall的使用)只用于系统调用,系统调用最多只能有6个参数。应用程序函数使用不同的(但类似的)调用约定,该约定将一些参数泄露给堆栈或其他寄存器。