Linux x86-64 Hello World 和注册参数的用法 [英] Linux x86-64 Hello World and register usage for parameters

查看:55
本文介绍了Linux x86-64 Hello World 和注册参数的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个页面有一个适用于 Linux 上 x86-64 的 Hello World 示例:

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

作者说:

代表 system_write 调用的整数值被放置在第一个寄存器,然后是它的参数.当系统调用和它的参数都在适当的寄存器中,系统被称为并显示消息.

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.

  • 他所说的正确"寄存器是什么意思/什么是不正确"寄存器?
  • 如果函数的参数比寄存器多,会发生什么?
  • rax 是否总是指向函数调用(这总是一个系统调用?)?这是唯一的目的吗?
    • 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?
    • 推荐答案

      作者所说的适当的寄存器"是指x86-64 ABI,位于 Linux 内核调用约定部分.系统调用号在 rax 中,参数在 rdirsirdxr10 中r8r9,按此顺序.

      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.

      这个调用约定(特别是使用syscall!)只用于系统调用,它最多只能有六个参数.应用程序函数使用不同(但相似)的调用约定,将一些参数溢出到堆栈或其他寄存器.

      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.

      这篇关于Linux x86-64 Hello World 和注册参数的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆