从汇编的角度分析C语言


C语言代码:

[cpp]
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3.   
  4. void main()  
  5. {  
  6.     char array1[] = { "123456" };  
  7.     char *pointer1 = "123456";  
  8. }  

汇编代码:

[html]
  1. (gdb) disassemble main   
  2. Dump of assembler code for function main:  
  3.    0x08048394 <+0>:     push   %ebp  
  4.    0x08048395 <+1>:     mov    %esp,%ebp  
  5.    0x08048397 <+3>:     sub    $0x10,%esp  
  6.    0x0804839a <+6>:     movl   $0x34333231,-0xb(%ebp)  
  7.    0x080483a1 <+13>:    movw   $0x3635,-0x7(%ebp)  
  8.    0x080483a7 <+19>:    movb   $0x0,-0x5(%ebp)  
  9.    0x080483ab <+23>:    movl   $0x8048484,-0x4(%ebp)  
  10.    0x080483b2 <+30>:    leave    
  11.    0x080483b3 <+31>:    ret      
  12. End of assembler dump.  
  13. (gdb) x/7xb 0x8048484  
  14. 0x8048484 <__dso_handle+4>:     0x31    0x32    0x33    0x34    0x35    0x36    0x00  
  15. (gdb)   

从<+6><+13><+19>三行可以看出,程序为array1分配了7bytes的内存空间,用来存储“123456”+‘\0’。

而<+23>行表示将地址0x8048484赋给了pointer1,我们可以查看内存0x8048484之后内容,7bytes正好为“123456”+‘\0’,这里pointer1只是一个指针,并没有为其分配内存单元。
那么下面的这段代码就不难理解了。

[cpp]
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3.   
  4. void main()  
  5. {  
  6.     char array1[] = { "123456" };  
  7.     char array2[] = { "123456" };  
  8.     char *pointer1 = "123456";  
  9.     char *pointer2 = "123456";  
  10. }  
汇编代码:

[html]
  1. (gdb) disassemble main   
  2. Dump of assembler code for function main:  
  3.    0x08048394 <+0>:     push   %ebp  
  4.    0x08048395 <+1>:     mov    %esp,%ebp  
  5.    0x08048397 <+3>:     sub    $0x20,%esp  
  6.    0x0804839a <+6>:     movl   $0x34333231,-0xf(%ebp)  
  7.    0x080483a1 <+13>:    movw   $0x3635,-0xb(%ebp)  
  8.    0x080483a7 <+19>:    movb   $0x0,-0x9(%ebp)  
  9.    0x080483ab <+23>:    movl   $0x34333231,-0x16(%ebp)  
  10.    0x080483b2 <+30>:    movw   $0x3635,-0x12(%ebp)  
  11.    0x080483b8 <+36>:    movb   $0x0,-0x10(%ebp)  
  12.    0x080483bc <+40>:    movl   $0x8048494,-0x4(%ebp)  
  13.    0x080483c3 <+47>:    movl   $0x8048494,-0x8(%ebp)  
  14.    0x080483ca <+54>:    leave    
  15.    0x080483cb <+55>:    ret      
  16. End of assembler dump.  
  17. (gdb) x/7xb 0x8048494  
  18. 0x8048494 <__dso_handle+4>:     0x31    0x32    0x33    0x34    0x35    0x36    0x00  
  19. (gdb)  

接着这段代码也就顺其自然了! [html]
  1. (gdb) disassemble  main   
  2. Dump of assembler code for function main:  
  3.    0x08048394 <+0>:     push   %ebp  
  4.    0x08048395 <+1>:     mov    %esp,%ebp  
  5.    0x08048397 <+3>:     sub    $0x20,%esp  
  6.    0x0804839a <+6>:     movl   $0x34333231,-0xf(%ebp)  
  7.    0x080483a1 <+13>:    movw   $0x3635,-0xb(%ebp)  
  8.    0x080483a7 <+19>:    movb   $0x0,-0x9(%ebp)  
  9.    0x080483ab <+23>:    movl   $0x34333231,-0x16(%ebp)  
  10.    0x080483b2 <+30>:    movw   $0x3635,-0x12(%ebp)  
  11.    0x080483b8 <+36>:    movb   $0x0,-0x10(%ebp)  
  12.    0x080483bc <+40>:    movl   $0x8048494,-0x4(%ebp)  
  13.    0x080483c3 <+47>:    movl   $0x804849b,-0x8(%ebp)  
  14.    0x080483ca <+54>:    leave    
  15.    0x080483cb <+55>:    ret      
  16. End of assembler dump.  
  17. (gdb) x/7xb 0x8048494  
  18. 0x8048494 <__dso_handle+4>:     0x31    0x32    0x33    0x34    0x35    0x36    0x00  
  19. (gdb) x/9xb 0x804849b  
  20. 0x804849b <__dso_handle+11>:    0x31    0x32    0x33    0x34    0x35    0x36    0x37    0x38  
  21. 0x80484a3 <__dso_handle+19>:    0x00  
  22. (gdb)   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 下一页
【内容导航】
第1页:char *p与char p[]的区别 第2页:gcc代码优化
第3页:指针的赋值 第4页:函数的调用过程
第5页:函数指针与指针的指针 第6页:switch和if else效率分析

相关内容