NASM汇编:如何通过 gdb 进行debug

来源:互联网 发布:excel vba 查询数据库 编辑:程序博客网 时间:2024/05/23 15:07

转载:http://sleepycat.org/tech/os/nasm-debug


目录:

  • 本文简介
  • 1 NASM 版 Hello World 程序
  • 2 编译
  • 3 调试
  • 4 参考文档

本文简介

  • 概要:通过 gdb 调试 nasm 汇编程序
  • 版本:Linux Mint 13(Ubuntu 11.10), nasm v2.09.08, gcc v4.6.1,
  • 日期:2012-07-14
  • 永久链接:http://sleepycat.org/tech/os/nasm-debug

1 NASM 版 Hello World 程序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ vi t.asm
section .data
 
msg:
    db "Hello World!!", 0ah, 0dh
 
 
section .text
    global main
 
main:
 
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, 14
    int 80h
     
    mov eax, 1
    int 80h

2 编译

?
1
2
$ nasm t.asm -f elf -g -F stabs
$ gcc t.o -o t -g

注:nasm => The -F Option: Selecting A Debug Information Format

3 调试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$ gdb t
 
 
(gdb) break main
Breakpoint 1 at 0x80483c0
 
 
(gdb) run
Starting program: /home/study/dev/os/t
 
Breakpoint 1, 0x080483c0 in main ()
 
 
(gdb) set disassembly-flavor intel
 
 
(gdb) disassemble main
Dump of assembler code for function main:
=> 0x080483c0 <+0>:    mov    eax,0x4
   0x080483c5 <+5>:   mov    ebx,0x1
   0x080483ca <+10>:  mov    ecx,0x804a010
   0x080483cf <+15>:  mov    edx,0xe
   0x080483d4 <+20>:  int    0x80
   0x080483d6 <+22>:  mov    eax,0x1
   0x080483db <+27>:  int    0x80
   0x080483dd <+29>:  nop
   0x080483de <+30>:  nop
   0x080483df <+31>:  nop
End of assembler dump.
 
 
(gdb) nexti
 
 
(gdb) info registers
eax            0x1  1
ecx            0xbffff864   -1073743772
edx            0xbffff7f4   -1073743884
ebx            0x2c0ff4 2887668
esp            0xbffff7cc   0xbffff7cc
ebp            0x0  0x0
esi            0x0  0
edi            0x0  0
eip            0x80483c0    0x80483c0 <main>
eflags         0x246    [ PF ZF IF ]
cs             0x73 115
ss             0x7b 123
ds             0x7b 123
es             0x7b 123
fs             0x0  0
gs             0x33 51
 
</main>

注:set disassembly-flavor intel => For use with nasm, it is best to set the flavor to intel

注:nexti => 表示只执行一步指令(To step one instruction)

4 参考文档

推荐:

http://www.csee.umbc.edu/portal/help/nasm/nasm.shtml#gdb


PS:这里使用gcc连接目标文件有需要注意的地方,我的ubuntu是64位的,使用命令gcc t.o -o t -g提示错误如下:

/usr/bin/ld: i386 architecture of input file `t.o' is incompatible with i386:x86-64 output collect2: 错误: ld 返回 1

解决方法如下:

$ gcc t.o -o t -g -m32

原创粉丝点击