开发者

Assembly analysis problem

(gdb) l main
1   #include <stdio.h>
2   
3   int main(void)
4   {
5       int i = 6;
6       printf("%d",sizeof(unsigned short));
7       return 0;
8   }
(gdb) disas main
Dump of assembler code for function main:
0x0000000000400498 <main+0>:    push   %rbp
0x0000000000400499 <main+1>:    mov    %rsp,%rbp
0x000000000040049c <main+4>:    sub    $0x10,%rsp
0x00000000004004a0 <main+8>:    movl  开发者_运维问答 $0x6,-0x4(%rbp)
0x00000000004004a7 <main+15>:   mov    $0x2,%esi
0x00000000004004ac <main+20>:   mov    $0x4005c8,%edi
0x00000000004004b1 <main+25>:   mov    $0x0,%eax
0x00000000004004b6 <main+30>:   callq  0x400398 <printf@plt>
0x00000000004004bb <main+35>:   mov    $0x0,%eax
0x00000000004004c0 <main+40>:   leaveq 
0x00000000004004c1 <main+41>:   retq   

I have two doubts:

  1. For int i = 6,only 4 bytes is needed,why 16 bytes allocated?
  2. some functions use stack to pass parameters(by push xxx),but why printf uses esi and edi to do this ?

UPDATE

seems printf is not fetching from esi and edi:

(gdb) disas printf
Dump of assembler code for function printf@plt:
0x0000000000400398 <printf@plt+0>:  jmpq   *0x2004c2(%rip)        # 0x600860 <_GLOBAL_OFFSET_TABLE_+24>
0x000000000040039e <printf@plt+6>:  pushq  $0x0
0x00000000004003a3 <printf@plt+11>: jmpq   0x400388


why 16 bytes

Because x86_64 ABI requires that the stack be 16-byte aligned before the call instruction

why printf uses esi and edi

Because x86_64 ABI specifies that (first 6) integer parameters be passed in rdi, rsi, rdx, rcx, r8 and r9 registers.


  1. It should be 4 bytes for int, 8 bytes for stack frame pointer(rbp 64 bit register) and 4 bytes for return value (in stack).

  2. I've not really witnessed it(i'm oldschool :P), but i've heard it's a new way to push values. It's supposed to be faster by directly storing values to stack.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜