从 "db 0" 加载寄存器不会将 0 加载到 EAX 中?

Loading a register from a "db 0" doesn't load a 0 into EAX?

我已经用头撞墙一个多小时了,我不明白为什么下面的方法不起作用。 如果我将 b: db 1 更改为 b: db 0 那么它应该打印 10,否则它应该打印 0。相反,程序总是打印 10.

我一直在编写一个编写程序集的项目,这是失败的单元测试之一,我就是不明白。它必须是简单的东西。

extern printf, exit

section .bss

section .data
b: db 1
x: dd 5
y: dd 5
z: dd 0
int_pattern: db "%i", 10, 0

global main

section .text

main:
mov eax, dword [b]
cmp eax, dword 0
je condition_end4

; add x and y
; store into z
mov eax, dword [rel x]
add eax, dword [rel y]
mov [rel z], eax

condition_end4:

; rsi = &z
; rdi = &int_pattern
mov rsi, qword [z]
mov rdi, int_pattern
; not using vector registers
xor rax, rax
; printf(int_pattern, z);
call printf

我正在使用 Debian Linux 和 NASM。 Assembling/linking 和

nasm -f elf64 -o test.o test.asm
gcc test.o -o test.bin

即使 b 为 0,GDB 也会显示 cmp 取消设置 ZF,所以我在这里不知所措。

谢谢!

您已将 b 声明为一个字节:

b: db 1

但您将其加载为双字:

mov eax, dword [b]

这解释了为什么即使 b 为 0 也未设置零标志:因为它也在加载接下来的 3 个字节。

只需更改您的声明:

b: dd 1 

或者,您可以加载一个扩展名为零的字节:movzx eax, byte [b]


同样,您从 z 加载了一个 qword,但您只将其定义为 dd 双字。参见 Which variable size to use (db, dw, dd) with x86 assembly?

此外,使用 default rel 这样所有寻址模式都可以选择 RIP 相对寻址,而不必在任何地方都说 [rel b]