带汇编器的 gdb:打印进位标志的状态

发布于 2024-10-20 19:14:57 字数 70 浏览 5 评论 0原文

我有一个 x86 汇编程序,正在使用 gdb 进行调试。有没有办法打印 gdb 内进位标志的状态,例如“print $cf”?

I've got an x86 assembler program which I'm debugging with gdb. Is there a way to print the status of the carry flag inside gdb with, like, "print $cf"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷月断魂刀 2024-10-27 19:14:58

我使用“p”检查 EFLAGS 寄存器

(gdb) p $eflags
$3 = [ PF ZF IF ]

,其中“p”只是“print”命令的缩写。

我还发现使用“/t”查看位很有帮助(也可以使用 /x 表示十六进制,/d 表示十进制)。

(gdb) p/t $eflags
$4 = 1001000110

然后,您可以与 EFLAGS 寄存器的图表进行比较。

I check the EFLAGS register using

(gdb) p $eflags
$3 = [ PF ZF IF ]

where "p" is just short for the "print" command.

I also find it helpful to see the bits using "/t" ( also /x for hex, /d for decimal).

(gdb) p/t $eflags
$4 = 1001000110

You can then compare with the chart for the EFLAGS register.

谁的新欢旧爱 2024-10-27 19:14:58

gdb 的另一个不错的选择是在 eflags 上添加一个观察点。

GDB 检查符号表

这是求和的程序12345 和 23456 可以在tutorialspoint的一个很好的教程中找到: TutorialPoint 汇编教程

unroot@unroot-VirtualBox:~/NASM$ gdb -q add5dig
Reading symbols from add5dig...done.
(gdb) set listsize 100
(gdb) list 0
1    section .text
2        global _start
3   
4    _start:
5        mov ecx,5   ;number of digits in each number to be added
6        mov esi,4   ;used to move the esi pointer to point at the rightmost digits to be summed
7        clc         ;clear carry flag
8   
9    add_loop:                   ;iterates through add_loop, decrementing ecx after each iteration until ecx equals 0
10        mov al,[num1+esi]      ;mov
11        adc al,[num2+esi]      ;add with carry, nicely sets carry to 0 if there is no carry and to 1 if there is a carry
12        aaa                    ;ascii adjust after addition
13        pushf                  ;push flags onto stack
14        or al,30h              ;OR value with 0b0001 1110. (essentially adds 0x30 to the ascii adjusted value, ascii 0x30 is '0', which converts the number to its ascii representation for proper display)
15        popf                   ;pop flags from stack
16   
17        mov [sum+esi],al       ;moves sum of the two digits into the correct space in memory
18        dec esi                ;point esi at the next digit to the left
19        loop add_loop          ;checks if exc==0, loops if not, continues if yes
20   ;printing message
21        mov edx,len            
22        mov ecx,msg
23        mov ebx,1
24        mov eax,4
25        int 0x80
26   ;printing sum
27        mov edx,5
28        mov ecx,sum
29        mov ebx,1
30        mov eax,4
31        int 0x80
32   ;exiting
33        mov eax,1
34        int 0x80
35   
36    section .data
37    msg db 'The sum is:',0xa
38    len equ $ - msg
39    num1 db '12345'
40    num2 db '23456'
41    sum db '     '              ;reserves 5 spaces in memory for sum
(gdb) break _start
Breakpoint 1 at 0x8048080
(gdb) break add_loop
Breakpoint 2 at 0x804808b
(gdb) run
Starting program: /home/unroot/NASM/add5dig

Breakpoint 1, 0x08048080 in _start ()
(gdb) watch $eflags
Watchpoint 3: $eflags
(gdb) info registers
eax            0x0    0
ecx            0x0    0
edx            0x0    0
ebx            0x0    0
esp            0xbffff0b0    0xbffff0b0
ebp            0x0    0x0
esi            0x0    0
edi            0x0    0
eip            0x8048080    0x8048080 <_start>
eflags         0x202    [ IF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x0    0
(gdb) cont
Continuing.

Breakpoint 2, 0x0804808b in add_loop ()
(gdb) info registers
eax            0x0    0
ecx            0x5    5
edx            0x0    0
ebx            0x0    0
esp            0xbffff0b0    0xbffff0b0
ebp            0x0    0x0
esi            0x4    4
edi            0x0    0
eip            0x804808b    0x804808b <add_loop>
eflags         0x202    [ IF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x0    0
(gdb) cont
Continuing.

Watchpoint 3: $eflags

Old value = [ IF ]
New value = [ CF AF IF ]
0x08048098 in add_loop ()
(gdb) info registers
eax            0x101    257
ecx            0x5    5
edx            0x0    0
ebx            0x0    0
esp            0xbffff0b0    0xbffff0b0
ebp            0x0    0x0
esi            0x4    4
edi            0x0    0
eip            0x8048098    0x8048098 <add_loop+13>
eflags         0x213    [ CF AF IF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x0    0
(gdb) 

观察点捕获当程序添加“6”和“5”时,eflags 寄存器中的进位标志和调整标志已被更改

Another nice option with gdb is to add a watchpoint on eflags.

GDB Examining the Symbol Table

This is program to sum 12345 and 23456 as found in a good tutorial from tutorialspoint: TutorialPoint assembly tutorial

unroot@unroot-VirtualBox:~/NASM$ gdb -q add5dig
Reading symbols from add5dig...done.
(gdb) set listsize 100
(gdb) list 0
1    section .text
2        global _start
3   
4    _start:
5        mov ecx,5   ;number of digits in each number to be added
6        mov esi,4   ;used to move the esi pointer to point at the rightmost digits to be summed
7        clc         ;clear carry flag
8   
9    add_loop:                   ;iterates through add_loop, decrementing ecx after each iteration until ecx equals 0
10        mov al,[num1+esi]      ;mov
11        adc al,[num2+esi]      ;add with carry, nicely sets carry to 0 if there is no carry and to 1 if there is a carry
12        aaa                    ;ascii adjust after addition
13        pushf                  ;push flags onto stack
14        or al,30h              ;OR value with 0b0001 1110. (essentially adds 0x30 to the ascii adjusted value, ascii 0x30 is '0', which converts the number to its ascii representation for proper display)
15        popf                   ;pop flags from stack
16   
17        mov [sum+esi],al       ;moves sum of the two digits into the correct space in memory
18        dec esi                ;point esi at the next digit to the left
19        loop add_loop          ;checks if exc==0, loops if not, continues if yes
20   ;printing message
21        mov edx,len            
22        mov ecx,msg
23        mov ebx,1
24        mov eax,4
25        int 0x80
26   ;printing sum
27        mov edx,5
28        mov ecx,sum
29        mov ebx,1
30        mov eax,4
31        int 0x80
32   ;exiting
33        mov eax,1
34        int 0x80
35   
36    section .data
37    msg db 'The sum is:',0xa
38    len equ $ - msg
39    num1 db '12345'
40    num2 db '23456'
41    sum db '     '              ;reserves 5 spaces in memory for sum
(gdb) break _start
Breakpoint 1 at 0x8048080
(gdb) break add_loop
Breakpoint 2 at 0x804808b
(gdb) run
Starting program: /home/unroot/NASM/add5dig

Breakpoint 1, 0x08048080 in _start ()
(gdb) watch $eflags
Watchpoint 3: $eflags
(gdb) info registers
eax            0x0    0
ecx            0x0    0
edx            0x0    0
ebx            0x0    0
esp            0xbffff0b0    0xbffff0b0
ebp            0x0    0x0
esi            0x0    0
edi            0x0    0
eip            0x8048080    0x8048080 <_start>
eflags         0x202    [ IF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x0    0
(gdb) cont
Continuing.

Breakpoint 2, 0x0804808b in add_loop ()
(gdb) info registers
eax            0x0    0
ecx            0x5    5
edx            0x0    0
ebx            0x0    0
esp            0xbffff0b0    0xbffff0b0
ebp            0x0    0x0
esi            0x4    4
edi            0x0    0
eip            0x804808b    0x804808b <add_loop>
eflags         0x202    [ IF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x0    0
(gdb) cont
Continuing.

Watchpoint 3: $eflags

Old value = [ IF ]
New value = [ CF AF IF ]
0x08048098 in add_loop ()
(gdb) info registers
eax            0x101    257
ecx            0x5    5
edx            0x0    0
ebx            0x0    0
esp            0xbffff0b0    0xbffff0b0
ebp            0x0    0x0
esi            0x4    4
edi            0x0    0
eip            0x8048098    0x8048098 <add_loop+13>
eflags         0x213    [ CF AF IF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x0    0
(gdb) 

The watchpoint catches that the Carry Flag and Adjust Flag in the eflags register have been altered when the program adds '6' and '5'

惯饮孤独 2024-10-27 19:14:57

您可以使用:

info registers eflags

来获取整组标志。您将看到如下一行:

eflags  0x41  [ CF ZF ]

这意味着 eflags 寄存器设置为 0x41,并设置了进位和零标志。

You can use:

info registers eflags

to get the entire set of flags. You'll see a line like:

eflags  0x41  [ CF ZF ]

which means that the eflags register is set to 0x41, with the carry and zero flags set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文