我已经用头撞墙一个多小时了,我不明白为什么下面的方法不起作用。
如果我将 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
我正在使用带有 NASM 的 Debian Linux。组装/链接
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]
。
关于linux - 从 "db 0"加载寄存器不会将 0 加载到 EAX 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014135/