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

本文介绍如何通过gdb调试NASM汇编语言编写的HelloWorld程序,包括程序编译、调试步骤和相关参考文档。文章详细展示了如何在Linux Mint 13环境下进行调试,提供了一个完整的NASM HelloWorld程序示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


目录:

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

本文简介

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值