了解gdb程序集的指针差异

时间:2017-07-14 18:29:20

标签: c assembly x86 gdb

对于此特定c代码。

#include <stdio.h>
int main()
{
     int *p = (int *)20;
     int *q = (int *)30;
     printf("%d", q-p);
}

这是gdb disass /m main

的转储
Dump of assembler code for function main:
   0x0000000100000f45 <+0>:    push   %rbp // save the current base pointer
   0x0000000100000f46 <+1>:    mov    %rsp,%rbp // base pointer reference the highest stack entry
   0x0000000100000f49 <+4>:    sub    $0x10,%rsp // Move stack pointer 0x10/4 = (4) memory entries downwards 
   0x0000000100000f4d <+8>:    movq   $0x14,-0x8(%rbp) // save (0x14 = 20), 0x8/4 mem address lower than base pointer ref ( 'q' for quad word )
   0x0000000100000f55 <+16>:    movq   $0x1e,-0x10(%rbp) // save (0x14 = 30), 0x10/4 mem address lower than base pointer ref
   0x0000000100000f5d <+24>:    mov    -0x10(%rbp),%rdx  // Save  0x10/4 memory addresses lower than the base pointer is referencing to rdx 
   0x0000000100000f61 <+28>:    mov    -0x8(%rbp),%rax // Save  0x8/4 memory addresses lower than the base pointer is referencing to rax
   0x0000000100000f65 <+32>:    sub    %rax,%rdx // subtract rdx from rax and result in rax
   0x0000000100000f68 <+35>:    mov    %rdx,%rax // move value of rax to rdx
   0x0000000100000f6b <+38>:    sar    $0x2,%rax // shift right 2 ( divide the value of rax by 4 )
   0x0000000100000f6f <+42>:    mov    %rax,%rsi // ( move value of rsi to rax )
   0x0000000100000f72 <+45>:    lea    0x31(%rip),%rdi        # 0x100000faa
   0x0000000100000f79 <+52>:    mov    $0x0,%eax
   0x0000000100000f7e <+57>:    callq  0x100000f8a
   0x0000000100000f83 <+62>:    mov    $0x0,%eax
   0x0000000100000f88 <+67>:    leaveq
   0x0000000100000f89 <+68>:    retq
End of assembler dump.

我尝试过,从搜索和理解,我已经评论了每一步。但在此之后我不知道这个程序实际上是如何运行的。

请帮忙。

EDIT。在-O2之后我得到了这个。

Dump of assembler code for function main:
   0x0000000100000f70 <+0>: sub    $0x8,%rsp
   0x0000000100000f74 <+4>: mov    $0x2,%esi
   0x0000000100000f79 <+9>: xor    %eax,%eax
   0x0000000100000f7b <+11>:    lea    0x2c(%rip),%rdi        # 0x100000fae
   0x0000000100000f82 <+18>:    callq  0x100000f8e
   0x0000000100000f87 <+23>:    xor    %eax,%eax
   0x0000000100000f89 <+25>:    add    $0x8,%rsp
   0x0000000100000f8d <+29>:    retq
End of assembler dump.

0 个答案:

没有答案