我正在阅读一些
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页)
特别:
- If the class is MEMORY,pass the argument on the stack.
- If the class is INTEGER,the next available register of the sequence %rdi,%rsi,%rdx,%rcx,%r8 and %r9 is used.
- If the class is SSE,the next available vector register is used,the registers are taken in the order from %xmm0 to %xmm7.
- If the class is SSEUP,the eightbyte is passed in the next available eightbyte chunk of the last used vector register.
- If the class is X87,X87UP or COMPLEX_X87,it is passed in memory.
INTEGER类是适合通用寄存器的任何东西,所以你也可以用它来表示字符串指针.