从"db 0"加载寄存器.不会将0加载到EAX中? [英] Loading a register from a "db 0" doesn't load a 0 into EAX?

查看:65
本文介绍了从"db 0"加载寄存器.不会将0加载到EAX中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been bashing my head against the wall for over an hour and I can't understand why the below doesn't work. If I change b: db 1 to b: db 0 then it should print 10, otherwise it should print 0. Instead, the program always prints 10.

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

I've been writing a project that writes assembly and this is one of the unit test that fails and I just don't get it. It has to be something simple.

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一起使用.组装/链接

I'm using Debian Linux with NASM. Assembling/linking with

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

即使 b 为0,GDB也会显示 cmp 取消ZF设置,因此我对此感到茫然.

Even when b is 0, GDB shows that the cmp unsets ZF so I'm at a loss here.

谢谢!

推荐答案

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

You've declared b as a byte:

b: db 1

但您将其作为双字加载:

but you load it as a dword:

mov eax, dword [b]

这说明了为什么即使b为0时也不会设置零标志:因为它也正在加载接下来的3个字节.

This explains why the zero flag is unset even when b is 0: because it's loading the next 3 bytes as well.

只需更改您的声明:

b: dd 1 

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

Alternatively, you could load a byte with zero extension: movzx eax, byte [b]

类似地,您从 z 加载了一个qword,但仅将其定义为 dd dword.请参阅要使用的变量大小(db,dw,dd)使用x86汇编程序?

Similarly, you load a qword from z but you only defined it as a dd dword. See Which variable size to use (db, dw, dd) with x86 assembly?

此外,使用 default rel ,以便所有寻址模式都选择相对于RIP的寻址,而不必到处都说 [rel b] .

Also, use default rel so all addressing modes pick RIP-relative addressing without having to say [rel b] everywhere.

这篇关于从"db 0"加载寄存器.不会将0加载到EAX中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆