Linux的X86-64装配和printf [英] Linux X86-64 assembly and printf
问题描述
我读一些Linux汇编手册,发现有关使用printf()函数的想法。我需要它输出寄存器的值进行调试以二进制形式到终端的原因,但现在我试图简单地测试文本该功能。
我坚持,因为段错误的,当我使用pushl的pushq代替。我怎样才能更改此程序输出字符串和寄存器的二进制形式?</ P>
。数据
input_prompt:
.string你好,世界!printf_format:
.string%5Dprintf_newline:
.string\\ n尺寸:
。长0。文本
.globl主
主要:
pushq $ input_prompt
调用printf MOVL $ 0,%EAX
RET
这是由GCC编译为:
的gcc -o tmp.S TMP
Linux操作系统(和Windows) x86-64的调用约定的前几个参数的不的栈上,而是在寄存器而不是
请参阅 http://www.x86-64.org/documentation/abi.pdf (第20页)
具体做法是:
- 如果该类是记忆,在栈上传递的说法。
- 如果该类是整数,序列%RDI的下一个可用的寄存器%RSI,%RDX,%RCX,%R8和R9%使用。
- 如果该类是SSE,下一个可用的矢量寄存器时,寄存器被采取的顺序从%XMM0到%XMM7。
- 如果该类是SSEUP,所述eightbyte传递在最后使用的矢量寄存器的下一个可用eightbyte块。
- 如果该类是X87,X87UP或COMPLEX_X87,它在内存中传递。
块引用>的
INTEGER
类是什么,将适合在一个通用寄存器,所以这就是你会用什么字符串指针为好。I am reading some linux assembly manuals and found idea about using printf() function. I need it to output register values for debugging reasons in binary form to terminal, but now I am tried simply to test that function with text.
I am stuck, because of segfault when I am using pushq instead of pushl. How can I change this program to output strings and binary form of registers?
.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
It was compiled by GCC as:
gcc tmp.S -o tmp
解决方案Linux (and Windows) x86-64 calling convention has the first few arguments not on the stack, but in registers instead
See http://www.x86-64.org/documentation/abi.pdf (page 20)
Specifically:
- 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.
The
INTEGER
class is anything that will fit in a general purpose register, so that's what you would use for string pointers as well.这篇关于Linux的X86-64装配和printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!