加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux X86-64程序集和printf

发布时间:2020-12-14 01:25:43 所属栏目:Linux 来源:网络整理
导读:我正在阅读一些 linux汇编手册,并找到了使用printf()函数的想法.我需要它以二进制形式将调试原因的寄存器值输出到终端,但现在我只是尝试用文本测试该函数. 我被困了,因为当我使用pushq而不是pushl时出现段错误.如何更改此程序以输出字符串和二进制形式的寄存
我正在阅读一些 linux汇编手册,并找到了使用printf()函数的想法.我需要它以二进制形式将调试原因的寄存器值输出到终端,但现在我只是尝试用文本测试该函数.

我被困了,因为当我使用pushq而不是pushl时出现段错误.如何更改此程序以输出字符串和二进制形式的寄存器?

.data
input_prompt:
    .string "Hello,world!"

printf_format:
    .string "%5d "

printf_newline:
    .string "n"

size:
    .long 0

.text
.globl main
main:
    pushq $input_prompt
    call  printf

    movl  $0,%eax
    ret

它由GCC编制为:

gcc tmp.S -o tmp

解决方法

Linux(和Windows) x86-64 calling convention的前几个参数不在堆栈上,而是在寄存器中

见http://www.x86-64.org/documentation/abi.pdf(第20页)

特别:

  1. If the class is MEMORY,pass the argument on the stack.
  2. If the class is INTEGER,the next available register of the sequence %rdi,%rsi,%rdx,%rcx,%r8 and %r9 is used.
  3. If the class is SSE,the next available vector register is used,the registers are taken in the order from %xmm0 to %xmm7.
  4. If the class is SSEUP,the eightbyte is passed in the next available eightbyte chunk of the last used vector register.
  5. If the class is X87,X87UP or COMPLEX_X87,it is passed in memory.

INTEGER类是适合通用寄存器的任何东西,所以你也可以用它来表示字符串指针.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读