C指針原理(3)

轉自:http://blog.csdn.net/myhaspl/article/details/14139759

Linux 平臺的標準彙編器是 GAS,它是 GCC 所依賴的後臺彙編工具,通常包含在 binutils 軟件包中,
--gstabs 告訴彙編器在生成的目標代碼中加上符號表,我們首先完成彙編: 
as -gstabs -o hello.o hello.s
彙編器產生的目標代碼必須經過鏈接器的處理才能生成可執行代碼 ,Linux 使用 ld 作爲標準的鏈接程序,它同樣也包含在 binutils 軟件包中。我們接着進行鏈接:
 ld -o hello hello.o
有了符號表,我們就好進行調試。
先運行一下,看看效果:
 ./hello
hello,world
ABCD
GDB做爲LINUX程序員的一個重要的調試工具,同樣適用於彙編編寫的程序,我們用GDB對上面代碼進行一些簡單的調試操作
首先打開hello程序:
 gdb hello
 
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/deepfuture-lx/private/mytest/hello...done.
然後,我們可以使用list命令列出源代碼
(gdb) list
1 .section .data#初始化的變量
2 output:
3   .ascii "hello,world\n"
4   #要打印的字符串,.data爲初始化值的變量。output是標籤,指示字符串開始的位置,ascii爲數據類型
5 .section .bss#未初始化的變量,由0填充的緩衝區
6   .lcomm num,20
7   #lcomm爲本地內存區域,即本地彙編外的不能進行訪問。.comm是通用內存區域。
8 .section .text#彙編語言指令碼
9   .globl _start#啓動入口
10   _start:
 
使用break命令設置斷點
(gdb) break 17
Breakpoint 1 at 0x4000c6: file hello.s, line 17.
運行至斷點
(gdb) run
Starting program: /home/deepfuture-lx/private/mytest/hello 
hello,world
 
Breakpoint 1, _start () at hello.s:17
繼續運行下條語句
17   movl $0,%eax
(gdb) next
18   movl $num,%edi
 
顯示所有寄存器的值
(gdb) info registers
rax            0x0 0
rbx            0x1 1
rcx            0x60011c 6291740
rdx            0xc 12
rsi            0x0 0
rdi            0x0 0
rbp            0x0 0x0
rsp            0x7fffffffe2d0 0x7fffffffe2d0
r8             0x0 0
r9             0x0 0
r10            0x0 0
r11            0x0 0
r12            0x0 0
r13            0x0 0
r14            0x0 0
r15            0x0 0
rip            0x4000cb 0x4000cb <_start+27>
eflags         0x202 [ IF ]
cs             0x33 51
ss             0x2b 43
ds             0x0 0
es             0x0 0
fs             0x0 0
---Type <return> to continue, or q <return> to quit---
gs             0x0 0
(gdb) next

19   movl $65,1(%edi)#A 的ascii


按十六進制格式輸出edi寄存器的值。/x表示16進制,/d表示10進制,/t表示二進制
(gdb) print/x $rdi
$3 = 0x600128
繼續運行
(gdb) next
20   movl $66,2(%edi)#B 的ascii 
 
顯示某個內存位置的值,x/nyz,其中n爲字段數,y爲格式(c爲字符,d爲10進制,x爲16進制),z爲字段長度(b爲字節,n爲16位字,w爲32位字)
(gdb) next
21   movl $67,3(%edi)#C 的ascii 
(gdb) x/3cb &num
0x600128 <num>: 0 '\000' 65 'A' 66 'B'
(gdb) next
22   movl $68,4(%edi)#D 的ascii
(gdb) next
23   movl $10,5(%edi)#\n的ascii 
(gdb) next
25   movl $4,%eax#調用的系統功能,4爲write    
(gdb) x/4cb &num
0x600128 <num>: 0 '\000' 65 'A' 66 'B' 67 'C'
退出gdb
(gdb)quit



發佈了15 篇原創文章 · 獲贊 22 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章