探究机器码,深入研究C语言程序的机制

用C高级编程语言写的程序,都要经过编译链接成本机可执行的程序。这个可执行程序究竟是什么呢?在Linux上,我们可以用objdump命令很方便地查看一个可执行程序的机器码。我们从一个简单的示例开始,说一说怎么理解机器码。

一、一个简单的c代码,什么都不做: testc.c

  1 #include 
  2  
  3 int main(int argc, char **argv)
  4 {
  5  
  6     return 1;
  7 }

  这段代码异常简单,只是向系统返回一个值为1的量即可。

二、Makefile文件如下:

  1 testc:testc.o                                                 
  2     gcc -S testc.c -o testc.s
  3     gcc testc.c -o testc
  4     objdump -D testc > dumpTestc.txt
  5 
  6 clean:
  7     rm testc testc.s testc.o dumpTestc.txt

  不要觉得这么简单的程序还需要Makefile文件,但是为了使用方便,还是使用更方便:

  生成了:

    可执行文件testc,

    生成了目标文件testc.o,

    生成了汇编文件testc.s

    生成了机器码与汇编对应的文件dumpTestc.txt

    还可以借助clean完成中间生成文件的清理工作

make

  完成上边工作的一键处理化,最后,查看文件分别如下:

  1     .file   "testc.c"                                                                                                                          
  2     .text
  3     .globl  main
  4     .type   main, @function
  5 main:
  6 .LFB0:
  7     .cfi_startproc
  8     pushq   %rbp
  9     .cfi_def_cfa_offset 16
 10     .cfi_offset 6, -16
 11     movq    %rsp, %rbp
 12     .cfi_def_cfa_register 6
 13     movl    %edi, -4(%rbp)
 14     movq    %rsi, -16(%rbp)
 15     movl    $1, %eax
 16     popq    %rbp
 17     .cfi_def_cfa 7, 8
 18     ret
 19     .cfi_endproc
 20 .LFE0:
 21     .size   main, .-main
 22     .ident  "GCC: (GNU) 9.2.0"
 23     .section    .note.GNU-stack,"",@progbits

  再次查看机器码和汇编对应的代码:

  1 testc:     文件格式 elf64-x86-64
  2 
  3 
  4 Disassembly of section .interp:
  5 
  6 00000000000002a8 <.interp>:
  7  2a8:    2f                       (bad)  
  8  2a9:    6c                       insb   (%dx),%es:(%rdi)
  9  2aa:    69 62 36 34 2f 6c 64     imul   $0x646c2f34,0x36(%rdx),%esp
 10  2b1:    2d 6c 69 6e 75           sub    $0x756e696c,%eax
 11  2b6:    78 2d                    js     2e5 <_init-0xd1b>
 12  2b8:    78 38                    js     2f2 <_init-0xd0e>
 13  2ba:    36 2d 36 34 2e 73        ss sub $0x732e3436,%eax
 14  2c0:    6f                       outsl  %ds:(%rsi),(%dx)
 15  2c1:    2e 32 00                 xor    %cs:(%rax),%al
 16 
 17 Disassembly of section .note.gnu.build-id:
 18 
 19 00000000000002c4 <.note.gnu.build-id>:
 20  2c4:    04 00                    add    $0x0,%al
 21  2c6:    00 00                    add    %al,(%rax)
 22  2c8:    14 00                    adc    $0x0,%al
 23  2ca:    00 00                    add    %al,(%rax)
 24  2cc:    03 00                    add    (%rax),%eax
 25  2ce:    00 00                    add    %al,(%rax)
 26  2d0:    47                       rex.RXB
 27  2d1:    4e 55                    rex.WRX push %rbp
 28  2d3:    00 34 8d 24 f6 a9 18     add    %dh,0x18a9f624(,%rcx,4)
 29  2da:    9b                       fwait
 30  2db:    61                       (bad)  
 31  2dc:    09 fe                    or     %edi,%esi
 32  2de:    36 3f                    ss (bad) 
 33  2e0:    8d                       (bad)  
 34  2e1:    d1 71 dc                 shll   -0x24(%rcx)
 35  2e4:    9f                       lahf   
 36  2e5:    65 b4 80                 gs mov $0x80,%ah
 37 
 38 Disassembly of section .note.ABI-tag:
 39 
 40 00000000000002e8 <.note.ABI-tag>:
 41  2e8:    04 00                    add    $0x0,%al
 42  2ea:    00 00                    add    %al,(%rax)
 43  2ec:    10 00                    adc    %al,(%rax)
 44  2ee:    00 00                    add    %al,(%rax)
 45  2f0:    01 00                    add    %eax,(%rax)
 46  2f2:    00 00                    add    %al,(%rax)
 47  2f4:    47                       rex.RXB
 48  2f5:    4e 55                    rex.WRX push %rbp
 49  2f7:    00 00                    add    %al,(%rax)
 50  2f9:    00 00                    add    %al,(%rax)
 51  2fb:    00 03                    add    %al,(%rbx)
 52  2fd:    00 00                    add    %al,(%rax)
 53  2ff:    00 02                    add    %al,(%rdx)
 54  301:    00 00                    add    %al,(%rax)
 55  303:    00 00                    add    %al,(%rax)
 56  305:    00 00                    add    %al,(%rax)
 57     ...
 58 
 59 Disassembly of section .gnu.hash:
 60 
 61 0000000000000308 <.gnu.hash>:
 62  308:    01 00                    add    %eax,(%rax)
 63  30a:    00 00                    add    %al,(%rax)
 64  30c:    01 00                    add    %eax,(%rax)
 65  30e:    00 00                    add    %al,(%rax)
 66  310:    01 00                    add    %eax,(%rax)
 67     ...
 68 
 69 Disassembly of section .dynsym:
 70 
 71 0000000000000328 <.dynsym>:
 72     ...
 73  340:    38 00                    cmp    %al,(%rax)
 74  342:    00 00                    add    %al,(%rax)
 75  344:    20 00                    and    %al,(%rax)
 76     ...
 77  356:    00 00                    add    %al,(%rax)
 78  358:    1a 00                    sbb    (%rax),%al
 79  35a:    00 00                    add    %al,(%rax)
 80  35c:    12 00                    adc    (%rax),%al
 81     ...
 82  36e:    00 00                    add    %al,(%rax)
 83  370:    54                       push   %rsp
 84  371:    00 00                    add    %al,(%rax)
 85  373:    00 20                    add    %ah,(%rax)
 86     ...
 87  385:    00 00                    add    %al,(%rax)
 88  387:    00 63 00                 add    %ah,0x0(%rbx)
 89  38a:    00 00                    add    %al,(%rax)
 90  38c:    20 00                    and    %al,(%rax)
 91     ...
 92  39e:    00 00                    add    %al,(%rax)
 93  3a0:    0b 00                    or     (%rax),%eax
 94  3a2:    00 00                    add    %al,(%rax)
 95  3a4:    22 00                    and    (%rax),%al
 96     ...
 97 
 98 Disassembly of section .dynstr:
 99 
100 00000000000003b8 <.dynstr>:
101  3b8:    00 6c 69 62              add    %ch,0x62(%rcx,%rbp,2)
102  3bc:    63 2e                    movslq (%rsi),%ebp
103  3be:    73 6f                    jae    42f <_init-0xbd1>
104  3c0:    2e 36 00 5f 5f           cs add %bl,%ss:0x5f(%rdi)
105  3c5:    63 78 61                 movslq 0x61(%rax),%edi
106  3c8:    5f                       pop    %rdi
107  3c9:    66 69 6e 61 6c 69        imul   $0x696c,0x61(%rsi),%bp
108  3cf:    7a 65                    jp     436 <_init-0xbca>
109  3d1:    00 5f 5f                 add    %bl,0x5f(%rdi)
110  3d4:    6c                       insb   (%dx),%es:(%rdi)
111  3d5:    69 62 63 5f 73 74 61     imul   $0x6174735f,0x63(%rdx),%esp
112  3dc:    72 74                    jb     452 <_init-0xbae>
113  3de:    5f                       pop    %rdi
114  3df:    6d                       insl   (%dx),%es:(%rdi)
115  3e0:    61                       (bad)  
116  3e1:    69 6e 00 47 4c 49 42     imul   $0x42494c47,0x0(%rsi),%ebp
117  3e8:    43 5f                    rex.XB pop %r15
118  3ea:    32 2e                    xor    (%rsi),%ch
119  3ec:    32 2e                    xor    (%rsi),%ch
120  3ee:    35 00 5f 49 54           xor    $0x54495f00,%eax
121  3f3:    4d 5f                    rex.WRB pop %r15
122  3f5:    64 65 72 65              fs gs jb 45e <_init-0xba2>
123  3f9:    67 69 73 74 65 72 54     imul   $0x4d547265,0x74(%ebx),%esi
124  400:    4d 
125  401:    43 6c                    rex.XB insb (%dx),%es:(%rdi)
126  403:    6f                       outsl  %ds:(%rsi),(%dx)
127  404:    6e                       outsb  %ds:(%rsi),(%dx)
128  405:    65 54                    gs push %rsp
129  407:    61                       (bad)  
130  408:    62                       (bad)  
131  409:    6c                       insb   (%dx),%es:(%rdi)
132  40a:    65 00 5f 5f              add    %bl,%gs:0x5f(%rdi)
133  40e:    67 6d                    insl   (%dx),%es:(%edi)
134  410:    6f                       outsl  %ds:(%rsi),(%dx)
135  411:    6e                       outsb  %ds:(%rsi),(%dx)
136  412:    5f                       pop    %rdi
137  413:    73 74                    jae    489 <_init-0xb77>
138  415:    61                       (bad)  
139  416:    72 74                    jb     48c <_init-0xb74>
140  418:    5f                       pop    %rdi
141  419:    5f                       pop    %rdi
142  41a:    00 5f 49                 add    %bl,0x49(%rdi)
143  41d:    54                       push   %rsp
144  41e:    4d 5f                    rex.WRB pop %r15
145  420:    72 65                    jb     487 <_init-0xb79>
146  422:    67 69 73 74 65 72 54     imul   $0x4d547265,0x74(%ebx),%esi
147  429:    4d 
148  42a:    43 6c                    rex.XB insb (%dx),%es:(%rdi)
149  42c:    6f                       outsl  %ds:(%rsi),(%dx)
150  42d:    6e                       outsb  %ds:(%rsi),(%dx)
151  42e:    65 54                    gs push %rsp
152  430:    61                       (bad)  
153  431:    62                       .byte 0x62
154  432:    6c                       insb   (%dx),%es:(%rdi)
155  433:    65                       gs
156     ...
157 
158 Disassembly of section .gnu.version:
159 
160 0000000000000436 <.gnu.version>:
161  436:    00 00                    add    %al,(%rax)
162  438:    00 00                    add    %al,(%rax)
163  43a:    02 00                    add    (%rax),%al
164  43c:    00 00                    add    %al,(%rax)
165  43e:    00 00                    add    %al,(%rax)
166  440:    02 00                    add    (%rax),%al
167 
168 Disassembly of section .gnu.version_r:
169 
170 0000000000000448 <.gnu.version_r>:
171  448:    01 00                    add    %eax,(%rax)
172  44a:    01 00                    add    %eax,(%rax)
173  44c:    01 00                    add    %eax,(%rax)
174  44e:    00 00                    add    %al,(%rax)
175  450:    10 00                    adc    %al,(%rax)
176  452:    00 00                    add    %al,(%rax)
177  454:    00 00                    add    %al,(%rax)
178  456:    00 00                    add    %al,(%rax)
179  458:    75 1a                    jne    474 <_init-0xb8c>
180  45a:    69 09 00 00 02 00        imul   $0x20000,(%rcx),%ecx
181  460:    2c 00                    sub    $0x0,%al
182  462:    00 00                    add    %al,(%rax)
183  464:    00 00                    add    %al,(%rax)
184     ...
185 
186 Disassembly of section .rela.dyn:
187 
188 0000000000000468 <.rela.dyn>:
189  468:    28 3e                    sub    %bh,(%rsi)
190  46a:    00 00                    add    %al,(%rax)
191  46c:    00 00                    add    %al,(%rax)
192  46e:    00 00                    add    %al,(%rax)
193  470:    08 00                    or     %al,(%rax)
194  472:    00 00                    add    %al,(%rax)
195  474:    00 00                    add    %al,(%rax)
196  476:    00 00                    add    %al,(%rax)
197  478:    10 11                    adc    %dl,(%rcx)
198  47a:    00 00                    add    %al,(%rax)
199  47c:    00 00                    add    %al,(%rax)
200  47e:    00 00                    add    %al,(%rax)
201  480:    30 3e                    xor    %bh,(%rsi)
202  482:    00 00                    add    %al,(%rax)
203  484:    00 00                    add    %al,(%rax)
204  486:    00 00                    add    %al,(%rax)
205  488:    08 00                    or     %al,(%rax)
206  48a:    00 00                    add    %al,(%rax)
207  48c:    00 00                    add    %al,(%rax)
208  48e:    00 00                    add    %al,(%rax)
209  490:    c0 10 00                 rclb   $0x0,(%rax)
210  493:    00 00                    add    %al,(%rax)
211  495:    00 00                    add    %al,(%rax)
212  497:    00 20                    add    %ah,(%rax)
213  499:    40 00 00                 add    %al,(%rax)
214  49c:    00 00                    add    %al,(%rax)
215  49e:    00 00                    add    %al,(%rax)
216  4a0:    08 00                    or     %al,(%rax)
217  4a2:    00 00                    add    %al,(%rax)
218  4a4:    00 00                    add    %al,(%rax)
219  4a6:    00 00                    add    %al,(%rax)
220  4a8:    20 40 00                 and    %al,0x0(%rax)
221  4ab:    00 00                    add    %al,(%rax)
222  4ad:    00 00                    add    %al,(%rax)
223  4af:    00 d8                    add    %bl,%al
224  4b1:    3f                       (bad)  
225  4b2:    00 00                    add    %al,(%rax)
226  4b4:    00 00                    add    %al,(%rax)
227  4b6:    00 00                    add    %al,(%rax)
228  4b8:    06                       (bad)  
229  4b9:    00 00                    add    %al,(%rax)
230  4bb:    00 01                    add    %al,(%rcx)
231     ...
232  4c5:    00 00                    add    %al,(%rax)
233  4c7:    00 e0                    add    %ah,%al
234  4c9:    3f                       (bad)  
235  4ca:    00 00                    add    %al,(%rax)
236  4cc:    00 00                    add    %al,(%rax)
237  4ce:    00 00                    add    %al,(%rax)
238  4d0:    06                       (bad)  
239  4d1:    00 00                    add    %al,(%rax)
240  4d3:    00 02                    add    %al,(%rdx)
241     ...
242  4dd:    00 00                    add    %al,(%rax)
243  4df:    00 e8                    add    %ch,%al
244  4e1:    3f                       (bad)  
245  4e2:    00 00                    add    %al,(%rax)
246  4e4:    00 00                    add    %al,(%rax)
247  4e6:    00 00                    add    %al,(%rax)
248  4e8:    06                       (bad)  
249  4e9:    00 00                    add    %al,(%rax)
250  4eb:    00 03                    add    %al,(%rbx)
251     ...
252  4f5:    00 00                    add    %al,(%rax)
253  4f7:    00 f0                    add    %dh,%al
254  4f9:    3f                       (bad)  
255  4fa:    00 00                    add    %al,(%rax)
256  4fc:    00 00                    add    %al,(%rax)
257  4fe:    00 00                    add    %al,(%rax)
258  500:    06                       (bad)  
259  501:    00 00                    add    %al,(%rax)
260  503:    00 04 00                 add    %al,(%rax,%rax,1)
261     ...
262  50e:    00 00                    add    %al,(%rax)
263  510:    f8                       clc    
264  511:    3f                       (bad)  
265  512:    00 00                    add    %al,(%rax)
266  514:    00 00                    add    %al,(%rax)
267  516:    00 00                    add    %al,(%rax)
268  518:    06                       (bad)  
269  519:    00 00                    add    %al,(%rax)
270  51b:    00 05 00 00 00 00        add    %al,0x0(%rip)        # 521 <_init-0xadf>
271  521:    00 00                    add    %al,(%rax)
272  523:    00 00                    add    %al,(%rax)
273  525:    00 00                    add    %al,(%rax)
274     ...
275 
276 Disassembly of section .init:
277 
278 0000000000001000 <_init>:
279     1000:    f3 0f 1e fa              endbr64 
280     1004:    48 83 ec 08              sub    $0x8,%rsp
281     1008:    48 8b 05 d9 2f 00 00     mov    0x2fd9(%rip),%rax        # 3fe8 <__gmon_start__>
282     100f:    48 85 c0                 test   %rax,%rax
283     1012:    74 02                    je     1016 <_init+0x16>
284     1014:    ff d0                    callq  *%rax
285     1016:    48 83 c4 08              add    $0x8,%rsp
286     101a:    c3                       retq   
287 
288 Disassembly of section .text:
289 
290 0000000000001020 <_start>:
291     1020:    f3 0f 1e fa              endbr64 
292     1024:    31 ed                    xor    %ebp,%ebp
293     1026:    49 89 d1                 mov    %rdx,%r9
294     1029:    5e                       pop    %rsi
295     102a:    48 89 e2                 mov    %rsp,%rdx
296     102d:    48 83 e4 f0              and    $0xfffffffffffffff0,%rsp
297     1031:    50                       push   %rax
298     1032:    54                       push   %rsp
299     1033:    4c 8d 05 66 01 00 00     lea    0x166(%rip),%r8        # 11a0 <__libc_csu_fini>
300     103a:    48 8d 0d ef 00 00 00     lea    0xef(%rip),%rcx        # 1130 <__libc_csu_init>
301     1041:    48 8d 3d d1 00 00 00     lea    0xd1(%rip),%rdi        # 1119 
302 1048: ff 15 92 2f 00 00 callq *0x2f92(%rip) # 3fe0 <__libc_start_main@GLIBC_2.2.5> 303 104e: f4 hlt 304 104f: 90 nop 305 306 0000000000001050 : 307 1050: 48 8d 3d d1 2f 00 00 lea 0x2fd1(%rip),%rdi # 4028 <__TMC_END__> 308 1057: 48 8d 05 ca 2f 00 00 lea 0x2fca(%rip),%rax # 4028 <__TMC_END__> 309 105e: 48 39 f8 cmp %rdi,%rax 310 1061: 74 15 je 1078 0x28> 311 1063: 48 8b 05 6e 2f 00 00 mov 0x2f6e(%rip),%rax # 3fd8 <_ITM_deregisterTMCloneTable> 312 106a: 48 85 c0 test %rax,%rax 313 106d: 74 09 je 1078 0x28> 314 106f: ff e0 jmpq *%rax 315 1071: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 316 1078: c3 retq 317 1079: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 318 319 0000000000001080 : 320 1080: 48 8d 3d a1 2f 00 00 lea 0x2fa1(%rip),%rdi # 4028 <__TMC_END__> 321 1087: 48 8d 35 9a 2f 00 00 lea 0x2f9a(%rip),%rsi # 4028 <__TMC_END__> 322 108e: 48 29 fe sub %rdi,%rsi 323 1091: 48 89 f0 mov %rsi,%rax 324 1094: 48 c1 ee 3f shr $0x3f,%rsi 325 1098: 48 c1 f8 03 sar $0x3,%rax 326 109c: 48 01 c6 add %rax,%rsi 327 109f: 48 d1 fe sar %rsi 328 10a2: 74 14 je 10b8 0x38> 329 10a4: 48 8b 05 45 2f 00 00 mov 0x2f45(%rip),%rax # 3ff0 <_ITM_registerTMCloneTable> 330 10ab: 48 85 c0 test %rax,%rax 331 10ae: 74 08 je 10b8 0x38> 332 10b0: ff e0 jmpq *%rax 333 10b2: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 334 10b8: c3 retq 335 10b9: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 336 337 00000000000010c0 <__do_global_dtors_aux>: 338 10c0: f3 0f 1e fa endbr64 339 10c4: 80 3d 5d 2f 00 00 00 cmpb $0x0,0x2f5d(%rip) # 4028 <__TMC_END__> 340 10cb: 75 33 jne 1100 <__do_global_dtors_aux+0x40> 341 10cd: 55 push %rbp 342 10ce: 48 83 3d 22 2f 00 00 cmpq $0x0,0x2f22(%rip) # 3ff8 <__cxa_finalize@GLIBC_2.2.5> 343 10d5: 00 344 10d6: 48 89 e5 mov %rsp,%rbp 345 10d9: 74 0d je 10e8 <__do_global_dtors_aux+0x28> 346 10db: 48 8b 3d 3e 2f 00 00 mov 0x2f3e(%rip),%rdi # 4020 <__dso_handle> 347 10e2: ff 15 10 2f 00 00 callq *0x2f10(%rip) # 3ff8 <__cxa_finalize@GLIBC_2.2.5> 348 10e8: e8 63 ff ff ff callq 1050 349 10ed: c6 05 34 2f 00 00 01 movb $0x1,0x2f34(%rip) # 4028 <__TMC_END__> 350 10f4: 5d pop %rbp 351 10f5: c3 retq 352 10f6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 353 10fd: 00 00 00 354 1100: c3 retq 355 1101: 66 66 2e 0f 1f 84 00 data16 nopw %cs:0x0(%rax,%rax,1) 356 1108: 00 00 00 00 357 110c: 0f 1f 40 00 nopl 0x0(%rax) 358 359 0000000000001110 : 360 1110: f3 0f 1e fa endbr64 361 1114: e9 67 ff ff ff jmpq 1080 362 363 0000000000001119
: 364 1119: 55 push %rbp 365 111a: 48 89 e5 mov %rsp,%rbp 366 111d: 89 7d fc mov %edi,-0x4(%rbp) 367 1120: 48 89 75 f0 mov %rsi,-0x10(%rbp) 368 1124: b8 01 00 00 00 mov $0x1,%eax 369 1129: 5d pop %rbp 370 112a: c3 retq 371 112b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 372 373 0000000000001130 <__libc_csu_init>: 374 1130: f3 0f 1e fa endbr64 375 1134: 41 57 push %r15 376 1136: 4c 8d 3d eb 2c 00 00 lea 0x2ceb(%rip),%r15 # 3e28 <__frame_dummy_init_array_entry> 377 113d: 41 56 push %r14 378 113f: 49 89 d6 mov %rdx,%r14 379 1142: 41 55 push %r13 380 1144: 49 89 f5 mov %rsi,%r13 381 1147: 41 54 push %r12 382 1149: 41 89 fc mov %edi,%r12d 383 114c: 55 push %rbp 384 114d: 48 8d 2d dc 2c 00 00 lea 0x2cdc(%rip),%rbp # 3e30 <__init_array_end> 385 1154: 53 push %rbx 386 1155: 4c 29 fd sub %r15,%rbp 387 1158: 48 83 ec 08 sub $0x8,%rsp 388 115c: e8 9f fe ff ff callq 1000 <_init> 389 1161: 48 c1 fd 03 sar $0x3,%rbp 390 1165: 74 1f je 1186 <__libc_csu_init+0x56> 391 1167: 31 db xor %ebx,%ebx 392 1169: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 393 1170: 4c 89 f2 mov %r14,%rdx 394 1173: 4c 89 ee mov %r13,%rsi 395 1176: 44 89 e7 mov %r12d,%edi 396 1179: 41 ff 14 df callq *(%r15,%rbx,8) 397 117d: 48 83 c3 01 add $0x1,%rbx 398 1181: 48 39 dd cmp %rbx,%rbp 399 1184: 75 ea jne 1170 <__libc_csu_init+0x40> 400 1186: 48 83 c4 08 add $0x8,%rsp 401 118a: 5b pop %rbx 402 118b: 5d pop %rbp 403 118c: 41 5c pop %r12 404 118e: 41 5d pop %r13 405 1190: 41 5e pop %r14 406 1192: 41 5f pop %r15 407 1194: c3 retq 408 1195: 66 66 2e 0f 1f 84 00 data16 nopw %cs:0x0(%rax,%rax,1) 409 119c: 00 00 00 00 410 411 00000000000011a0 <__libc_csu_fini>: 412 11a0: f3 0f 1e fa endbr64 413 11a4: c3 retq 414 415 Disassembly of section .fini: 416 417 00000000000011a8 <_fini>: 418 11a8: f3 0f 1e fa endbr64 419 11ac: 48 83 ec 08 sub $0x8,%rsp 420 11b0: 48 83 c4 08 add $0x8,%rsp 421 11b4: c3 retq 422 423 Disassembly of section .rodata: 424 425 0000000000002000 <_IO_stdin_used>: 426 2000: 01 00 add %eax,(%rax) 427 2002: 02 00 add (%rax),%al 428 429 Disassembly of section .eh_frame_hdr: 430 431 0000000000002004 <__GNU_EH_FRAME_HDR>: 432 2004: 01 1b add %ebx,(%rbx) 433 2006: 03 3b add (%rbx),%edi 434 2008: 28 00 sub %al,(%rax) 435 200a: 00 00 add %al,(%rax) 436 200c: 04 00 add $0x0,%al 437 200e: 00 00 add %al,(%rax) 438 2010: 1c f0 sbb $0xf0,%al 439 2012: ff (bad) 440 2013: ff 44 00 00 incl 0x0(%rax,%rax,1) 441 2017: 00 15 f1 ff ff 5c add %dl,0x5cfffff1(%rip) # 5d00200e <_end+0x5cffdfde> 442 201d: 00 00 add %al,(%rax) 443 201f: 00 2c f1 add %ch,(%rcx,%rsi,8) 444 2022: ff (bad) 445 2023: ff (bad) 446 2024: 7c 00 jl 2026 <__GNU_EH_FRAME_HDR+0x22> 447 2026: 00 00 add %al,(%rax) 448 2028: 9c pushfq 449 2029: f1 icebp 450 202a: ff (bad) 451 202b: ff c4 inc %esp 452 202d: 00 00 add %al,(%rax) 453 ... 454 455 Disassembly of section .eh_frame: 456 457 0000000000002030 <__FRAME_END__-0xac>: 458 2030: 14 00 adc $0x0,%al 459 2032: 00 00 add %al,(%rax) 460 2034: 00 00 add %al,(%rax) 461 2036: 00 00 add %al,(%rax) 462 2038: 01 7a 52 add %edi,0x52(%rdx) 463 203b: 00 01 add %al,(%rcx) 464 203d: 78 10 js 204f <__GNU_EH_FRAME_HDR+0x4b> 465 203f: 01 1b add %ebx,(%rbx) 466 2041: 0c 07 or $0x7,%al 467 2043: 08 90 01 00 00 14 or %dl,0x14000001(%rax) 468 2049: 00 00 add %al,(%rax) 469 204b: 00 1c 00 add %bl,(%rax,%rax,1) 470 204e: 00 00 add %al,(%rax) 471 2050: d0 ef shr %bh 472 2052: ff (bad) 473 2053: ff 2f ljmp *(%rdi) 474 2055: 00 00 add %al,(%rax) 475 2057: 00 00 add %al,(%rax) 476 2059: 44 07 rex.R (bad) 477 205b: 10 00 adc %al,(%rax) 478 205d: 00 00 add %al,(%rax) 479 205f: 00 1c 00 add %bl,(%rax,%rax,1) 480 2062: 00 00 add %al,(%rax) 481 2064: 34 00 xor $0x0,%al 482 2066: 00 00 add %al,(%rax) 483 2068: b1 f0 mov $0xf0,%cl 484 206a: ff (bad) 485 206b: ff 12 callq *(%rdx) 486 206d: 00 00 add %al,(%rax) 487 206f: 00 00 add %al,(%rax) 488 2071: 41 0e rex.B (bad) 489 2073: 10 86 02 43 0d 06 adc %al,0x60d4302(%rsi) 490 2079: 4d 0c 07 rex.WRB or $0x7,%al 491 207c: 08 00 or %al,(%rax) 492 207e: 00 00 add %al,(%rax) 493 2080: 44 00 00 add %r8b,(%rax) 494 2083: 00 54 00 00 add %dl,0x0(%rax,%rax,1) 495 2087: 00 a8 f0 ff ff 65 add %ch,0x65fffff0(%rax) 496 208d: 00 00 add %al,(%rax) 497 208f: 00 00 add %al,(%rax) 498 2091: 46 0e rex.RX (bad) 499 2093: 10 8f 02 49 0e 18 adc %cl,0x180e4902(%rdi) 500 2099: 8e 03 mov (%rbx),%es 501 209b: 45 0e rex.RB (bad) 502 209d: 20 8d 04 45 0e 28 and %cl,0x280e4504(%rbp) 503 20a3: 8c 05 44 0e 30 86 mov %es,-0x79cff1bc(%rip) # ffffffff86302eed <_end+0xffffffff862feebd> 504 20a9: 06 (bad) 505 20aa: 48 0e rex.W (bad) 506 20ac: 38 83 07 47 0e 40 cmp %al,0x400e4707(%rbx) 507 20b2: 6e outsb %ds:(%rsi),(%dx) 508 20b3: 0e (bad) 509 20b4: 38 41 0e cmp %al,0xe(%rcx) 510 20b7: 30 41 0e xor %al,0xe(%rcx) 511 20ba: 28 42 0e sub %al,0xe(%rdx) 512 20bd: 20 42 0e and %al,0xe(%rdx) 513 20c0: 18 42 0e sbb %al,0xe(%rdx) 514 20c3: 10 42 0e adc %al,0xe(%rdx) 515 20c6: 08 00 or %al,(%rax) 516 20c8: 10 00 adc %al,(%rax) 517 20ca: 00 00 add %al,(%rax) 518 20cc: 9c pushfq 519 20cd: 00 00 add %al,(%rax) 520 20cf: 00 d0 add %dl,%al 521 20d1: f0 ff lock (bad) 522 20d3: ff 05 00 00 00 00 incl 0x0(%rip) # 20d9 <__GNU_EH_FRAME_HDR+0xd5> 523 20d9: 00 00 add %al,(%rax) 524 ... 525 526 00000000000020dc <__FRAME_END__>: 527 20dc: 00 00 add %al,(%rax) 528 ... 529 530 Disassembly of section .init_array: 531 532 0000000000003e28 <__frame_dummy_init_array_entry>: 533 3e28: 10 11 adc %dl,(%rcx) 534 3e2a: 00 00 add %al,(%rax) 535 3e2c: 00 00 add %al,(%rax) 536 ... 537 538 Disassembly of section .fini_array: 539 540 0000000000003e30 <__do_global_dtors_aux_fini_array_entry>: 541 3e30: c0 10 00 rclb $0x0,(%rax) 542 3e33: 00 00 add %al,(%rax) 543 3e35: 00 00 add %al,(%rax) 544 ... 545 546 Disassembly of section .dynamic: 547 548 0000000000003e38 <_DYNAMIC>: 549 3e38: 01 00 add %eax,(%rax) 550 3e3a: 00 00 add %al,(%rax) 551 3e3c: 00 00 add %al,(%rax) 552 3e3e: 00 00 add %al,(%rax) 553 3e40: 01 00 add %eax,(%rax) 554 3e42: 00 00 add %al,(%rax) 555 3e44: 00 00 add %al,(%rax) 556 3e46: 00 00 add %al,(%rax) 557 3e48: 0c 00 or $0x0,%al 558 3e4a: 00 00 add %al,(%rax) 559 3e4c: 00 00 add %al,(%rax) 560 3e4e: 00 00 add %al,(%rax) 561 3e50: 00 10 add %dl,(%rax) 562 3e52: 00 00 add %al,(%rax) 563 3e54: 00 00 add %al,(%rax) 564 3e56: 00 00 add %al,(%rax) 565 3e58: 0d 00 00 00 00 or $0x0,%eax 566 3e5d: 00 00 add %al,(%rax) 567 3e5f: 00 a8 11 00 00 00 add %ch,0x11(%rax) 568 3e65: 00 00 add %al,(%rax) 569 3e67: 00 19 add %bl,(%rcx) 570 3e69: 00 00 add %al,(%rax) 571 3e6b: 00 00 add %al,(%rax) 572 3e6d: 00 00 add %al,(%rax) 573 3e6f: 00 28 add %ch,(%rax) 574 3e71: 3e 00 00 add %al,%ds:(%rax) 575 3e74: 00 00 add %al,(%rax) 576 3e76: 00 00 add %al,(%rax) 577 3e78: 1b 00 sbb (%rax),%eax 578 3e7a: 00 00 add %al,(%rax) 579 3e7c: 00 00 add %al,(%rax) 580 3e7e: 00 00 add %al,(%rax) 581 3e80: 08 00 or %al,(%rax) 582 3e82: 00 00 add %al,(%rax) 583 3e84: 00 00 add %al,(%rax) 584 3e86: 00 00 add %al,(%rax) 585 3e88: 1a 00 sbb (%rax),%al 586 3e8a: 00 00 add %al,(%rax) 587 3e8c: 00 00 add %al,(%rax) 588 3e8e: 00 00 add %al,(%rax) 589 3e90: 30 3e xor %bh,(%rsi) 590 3e92: 00 00 add %al,(%rax) 591 3e94: 00 00 add %al,(%rax) 592 3e96: 00 00 add %al,(%rax) 593 3e98: 1c 00 sbb $0x0,%al 594 3e9a: 00 00 add %al,(%rax) 595 3e9c: 00 00 add %al,(%rax) 596 3e9e: 00 00 add %al,(%rax) 597 3ea0: 08 00 or %al,(%rax) 598 3ea2: 00 00 add %al,(%rax) 599 3ea4: 00 00 add %al,(%rax) 600 3ea6: 00 00 add %al,(%rax) 601 3ea8: f5 cmc 602 3ea9: fe (bad) 603 3eaa: ff 6f 00 ljmp *0x0(%rdi) 604 3ead: 00 00 add %al,(%rax) 605 3eaf: 00 08 add %cl,(%rax) 606 3eb1: 03 00 add (%rax),%eax 607 3eb3: 00 00 add %al,(%rax) 608 3eb5: 00 00 add %al,(%rax) 609 3eb7: 00 05 00 00 00 00 add %al,0x0(%rip) # 3ebd <_DYNAMIC+0x85> 610 3ebd: 00 00 add %al,(%rax) 611 3ebf: 00 b8 03 00 00 00 add %bh,0x3(%rax) 612 3ec5: 00 00 add %al,(%rax) 613 3ec7: 00 06 add %al,(%rsi) 614 3ec9: 00 00 add %al,(%rax) 615 3ecb: 00 00 add %al,(%rax) 616 3ecd: 00 00 add %al,(%rax) 617 3ecf: 00 28 add %ch,(%rax) 618 3ed1: 03 00 add (%rax),%eax 619 3ed3: 00 00 add %al,(%rax) 620 3ed5: 00 00 add %al,(%rax) 621 3ed7: 00 0a add %cl,(%rdx) 622 3ed9: 00 00 add %al,(%rax) 623 3edb: 00 00 add %al,(%rax) 624 3edd: 00 00 add %al,(%rax) 625 3edf: 00 7d 00 add %bh,0x0(%rbp) 626 3ee2: 00 00 add %al,(%rax) 627 3ee4: 00 00 add %al,(%rax) 628 3ee6: 00 00 add %al,(%rax) 629 3ee8: 0b 00 or (%rax),%eax 630 3eea: 00 00 add %al,(%rax) 631 3eec: 00 00 add %al,(%rax) 632 3eee: 00 00 add %al,(%rax) 633 3ef0: 18 00 sbb %al,(%rax) 634 3ef2: 00 00 add %al,(%rax) 635 3ef4: 00 00 add %al,(%rax) 636 3ef6: 00 00 add %al,(%rax) 637 3ef8: 15 00 00 00 00 adc $0x0,%eax 638 ... 639 3f05: 00 00 add %al,(%rax) 640 3f07: 00 07 add %al,(%rdi) 641 3f09: 00 00 add %al,(%rax) 642 3f0b: 00 00 add %al,(%rax) 643 3f0d: 00 00 add %al,(%rax) 644 3f0f: 00 68 04 add %ch,0x4(%rax) 645 3f12: 00 00 add %al,(%rax) 646 3f14: 00 00 add %al,(%rax) 647 3f16: 00 00 add %al,(%rax) 648 3f18: 08 00 or %al,(%rax) 649 3f1a: 00 00 add %al,(%rax) 650 3f1c: 00 00 add %al,(%rax) 651 3f1e: 00 00 add %al,(%rax) 652 3f20: c0 00 00 rolb $0x0,(%rax) 653 3f23: 00 00 add %al,(%rax) 654 3f25: 00 00 add %al,(%rax) 655 3f27: 00 09 add %cl,(%rcx) 656 3f29: 00 00 add %al,(%rax) 657 3f2b: 00 00 add %al,(%rax) 658 3f2d: 00 00 add %al,(%rax) 659 3f2f: 00 18 add %bl,(%rax) 660 3f31: 00 00 add %al,(%rax) 661 3f33: 00 00 add %al,(%rax) 662 3f35: 00 00 add %al,(%rax) 663 3f37: 00 fb add %bh,%bl 664 3f39: ff (bad) 665 3f3a: ff 6f 00 ljmp *0x0(%rdi) 666 3f3d: 00 00 add %al,(%rax) 667 3f3f: 00 00 add %al,(%rax) 668 3f41: 00 00 add %al,(%rax) 669 3f43: 08 00 or %al,(%rax) 670 3f45: 00 00 add %al,(%rax) 671 3f47: 00 fe add %bh,%dh 672 3f49: ff (bad) 673 3f4a: ff 6f 00 ljmp *0x0(%rdi) 674 3f4d: 00 00 add %al,(%rax) 675 3f4f: 00 48 04 add %cl,0x4(%rax) 676 3f52: 00 00 add %al,(%rax) 677 3f54: 00 00 add %al,(%rax) 678 3f56: 00 00 add %al,(%rax) 679 3f58: ff (bad) 680 3f59: ff (bad) 681 3f5a: ff 6f 00 ljmp *0x0(%rdi) 682 3f5d: 00 00 add %al,(%rax) 683 3f5f: 00 01 add %al,(%rcx) 684 3f61: 00 00 add %al,(%rax) 685 3f63: 00 00 add %al,(%rax) 686 3f65: 00 00 add %al,(%rax) 687 3f67: 00 f0 add %dh,%al 688 3f69: ff (bad) 689 3f6a: ff 6f 00 ljmp *0x0(%rdi) 690 3f6d: 00 00 add %al,(%rax) 691 3f6f: 00 36 add %dh,(%rsi) 692 3f71: 04 00 add $0x0,%al 693 3f73: 00 00 add %al,(%rax) 694 3f75: 00 00 add %al,(%rax) 695 3f77: 00 f9 add %bh,%cl 696 3f79: ff (bad) 697 3f7a: ff 6f 00 ljmp *0x0(%rdi) 698 3f7d: 00 00 add %al,(%rax) 699 3f7f: 00 03 add %al,(%rbx) 700 ... 701 702 Disassembly of section .got: 703 704 0000000000003fd8 <.got>: 705 ... 706 707 Disassembly of section .got.plt: 708 709 0000000000004000 <_GLOBAL_OFFSET_TABLE_>: 710 4000: 38 3e cmp %bh,(%rsi) 711 ... 712 713 Disassembly of section .data: 714 715 0000000000004018 <__data_start>: 716 ... 717 718 0000000000004020 <__dso_handle>: 719 4020: 20 40 00 and %al,0x0(%rax) 720 4023: 00 00 add %al,(%rax) 721 4025: 00 00 add %al,(%rax) 722 ... 723 724 Disassembly of section .bss: 725 726 0000000000004028 <__bss_start>: 727 ... 728 729 Disassembly of section .comment: 730 731 0000000000000000 <.comment>: 732 0: 47 rex.RXB 733 1: 43 rex.XB 734 2: 43 3a 20 rex.XB cmp (%r8),%spl 735 5: 28 47 4e sub %al,0x4e(%rdi) 736 8: 55 push %rbp 737 9: 29 20 sub %esp,(%rax) 738 b: 39 2e cmp %ebp,(%rsi) 739 d: 32 2e xor (%rsi),%ch 740 f: 30 00 xor %al,(%rax)

  代码看着近750行,但是实际有用的行数并不多。

1 0000000000001119 
: 2 1119: 55 push %rbp 3 111a: 48 89 e5 mov %rsp,%rbp 4 111d: 89 7d fc mov %edi,-0x4(%rbp) 5 1120: 48 89 75 f0 mov %rsi,-0x10(%rbp) 6 1124: b8 01 00 00 00 mov $0x1,%eax 7 1129: 5d pop %rbp 8 112a: c3 retq 9 112b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)

只有大约这10行才是实现了返回值为1的情况,其他都是类似的。

其实还要说下字节序的问题,大端模式还是小端模式。

参考文档:https://www.cnblogs.com/guocai/archive/2012/10/18/2730048.html

 

你可能感兴趣的:(探究机器码,深入研究C语言程序的机制)