生活随笔
收集整理的這篇文章主要介紹了
C指针原理(3)-ATT汇编
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 helloGNU 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://×××w.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,worldBreakpoint 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) next19 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
總結
以上是生活随笔為你收集整理的C指针原理(3)-ATT汇编的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。