装配(或NASM)恼人的问题

Bla*_*ear 4 linux assembly gdb nasm

我曾经用TASM编译我的asm代码(在winXP上),但是我遇到了一些麻烦所以现在我使用NASM(在linux上).这段代码展示了我正在尝试做的事情:

(gdb) list 35
30      xor ecx,ecx             # ecx is a counter
31      mov bl, ' '             # this is what I'm looking for
32  count_spaces:
33      mov al,[esi]            # grab a char
34      jz  spaces_counted      # is this the end?
35      inc esi                 # next char
36      cmp al,bl               # found one?
37      jne count_spaces        # nope, loop
38      inc ecx                 # yep, inc counter
39      jmp count_spaces        # and loop
Run Code Online (Sandbox Code Playgroud)

这对我来说是正确的,但是:

Breakpoint 1, main () at project1.asm:30
30      xor ecx,ecx
(gdb) display (char) $al
1: (char) $al = 0 '\000'
(gdb) display (char) $bl
2: (char) $bl = 0 '\000'
(gdb) next
31      mov bl, ' '
2: (char) $bl = 0 '\000'
1: (char) $al = 0 '\000'
(gdb) 
count_spaces () at project1.asm:33
33      mov al,[esi]
2: (char) $bl = 0 '\000'
1: (char) $al = 0 '\000'
(gdb) 
Run Code Online (Sandbox Code Playgroud)

我不明白为什么al,并bl没有改变.
我确定我的代码正确的,但是......我想我错过了一些NASM的选择?BTW我编译了

nasm -f elf -l project1.lst -o project1.o -i../include/ -g  project1.asm
Run Code Online (Sandbox Code Playgroud)

编译完成后,我反汇编输出并获得:

 80483ec:   31 c9                   xor    %ecx,%ecx
 80483ee:   bb 20 00 00 00          mov    $0x20,%ebx

080483f3 <count_spaces>:
 80483f3:   8b 06                   mov    (%esi),%eax
 80483f5:   3d 00 00 00 00          cmp    $0x0,%eax
 80483fa:   74 0b                   je     8048407 <spaces_counted>
 80483fc:   46                      inc    %esi
 80483fd:   39 d8                   cmp    %ebx,%eax
 80483ff:   75 f2                   jne    80483f3 <count_spaces>
 8048401:   41                      inc    %ecx
 8048402:   e9 ec ff ff ff          jmp    80483f3 <count_spaces>
Run Code Online (Sandbox Code Playgroud)

Jes*_*ter 6

请注意,GDB不知道8位或16位别名寄存器.它将始终打印0 al, bl, ax, bx等,您应该使用eax, ebx,等:

(gdb) info registers bl
Invalid register `bl'
(gdb) info registers bx
Invalid register `bx'
(gdb) info registers ebx
ebx            0xf7730ff4       -143454220
(gdb) p $bl
$1 = void
(gdb) p $bx
$2 = void
(gdb) p $ebx
$3 = -143454220
(gdb) p/x $bl
$4 = Value can't be converted to integer.
(gdb) p/x $bx
$5 = Value can't be converted to integer.
(gdb) p/x $ebx
$6 = 0xf7730ff4
(gdb) p (char) $bl
$7 = 0 '\0'
(gdb) p (char) $bx
$8 = 0 '\0'
(gdb) p (char) $ebx
$9 = -12 'ô'
Run Code Online (Sandbox Code Playgroud)


Mat*_*ery 6

杰斯特有正确的答案,值得投票.

但是,我想添加一些评论太长的内容:gdb如果你愿意,你可以教你显示子寄存器,使用hook-stop钩子,它在任何display事件发生之前运行,通过在.gdbinit文件中添加以下内容:

define hook-stop
set $bl=($ebx & 0xff)
set $bh=(($ebx & 0xff00) >> 8)
set $bx=($ebx & 0xffff)
end
Run Code Online (Sandbox Code Playgroud)

(以明显的方式扩展到其他寄存器). display $bl然后将按预期工作.