简体   繁体   English

scanf 函数将输入值存储在哪个寄存器中?

[英]In which register does the scanf function store input values?

I have the following disassembly of a main function in which a user input is stored using scanf function (at address 0x0000089c ).我对 main 函数进行了以下反汇编,其中使用 scanf 函数(地址0x0000089c )存储了用户输入。 Due to the comparison that is made, I suppose that the user input is stored into the rsp register but I cannot figure out why, as rsp doesn't seem to be pushed on the stack (at least, not near the call to the scanf function).由于进行了比较,我认为用户输入存储在 rsp 寄存器中,但我不知道为什么,因为 rsp 似乎没有被推入堆栈(至少,不在对 scanf 的调用附近功能)。 Here is the disassembly:下面是拆解:

0x00000850 sub rsp, 0x18
0x00000854 mov rax, qword fs:[0x28]
0x0000085d mov qword [canary], rax
0x00000862 xor eax, eax
0x00000864 call fcn.00000a3c
0x00000869 lea rsi, str.Insert_input:
0x00000870 mov edi, 1
0x00000875 xor eax, eax
0x00000877 mov dword [rsp], 0
0x0000087e mov dword [var_4h], 0
0x00000886 call sym.imp.__printf_chk
0x0000088b lea rdx, [var_4h]
0x00000890 lea rdi, str.u__u  ; "%u %u" ;const char *format
0x00000897 xor eax, eax
0x00000899 mov rsi, rsp
0x0000089c call sym.imp.__isoc99_scanf ; int scanf(const char *format)
0x000008a1 mov eax, dword [rsp]
0x000008a4 cmp eax, 0x1336
0x000008a9 jg 0x867

On x86_64, parameters are passed in registers, so your call to scanf has 3 parameters stored in 3 registers:在 x86_64 上,参数在寄存器中传递,因此您对 scanf 的调用将 3 个参数存储在 3 个寄存器中:

  • rdi pointer to the string "%u %u" , the format to parse (two unsigned integers) rdi指向字符串"%u %u"的指针,要解析的格式(两个无符号整数)
  • rsi should be a unsigned * , pointer to where to put the first parsed integer rsi应该是一个unsigned * ,指向放置第一个解析整数的位置
  • rdx pointer to where to put the second parsed integer. rdx指向放置第二个解析整数的位置的指针。

If you look just before the call, rsi is set to rsp (the stack pointer) while rdx is set to point at the global variable var_4h (an extern symbol not defined here).如果您在调用之前查看, rsi设置为rsp (堆栈指针),而rdx设置为指向全局变量var_4h (此处未定义的外部符号)。

The stack is used to hold local variables, and in this case rsp points at a block 0x18 "free" bytes (allocated in the first instruction in your block), which is enough space for 6 integers.堆栈用于保存局部变量,在这种情况下, rsp指向块 0x18“空闲”字节(在块中的第一条指令中分配),这对于 6 个整数来说是足够的空间。 The one at offset 0 from rsp is what rsi points to, and it is the value read by the mov instruction immediately after the call.rsp偏移 0 处的那个是rsi所指向的,它是 mov 指令在调用后立即读取的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM