`
deepfuture
  • 浏览: 4335232 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:79439
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:68419
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:101543
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:281288
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:14620
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:65620
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:31339
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:45234
社区版块
存档分类
最新评论

算法下午茶系列-重温汇编(2)[调试汇编]

 
阅读更多

 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

 

 如果转载请注明来源,如有错误之处,请及时指出。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics