比较 NASM 程序集中的变量 [英] Comparing variables in NASM Assembly
问题描述
尝试让它工作遇到了严重的麻烦......刚刚开始 NASM 组装,如果这是一个菜鸟问题,很抱歉,但感谢任何帮助,谢谢!
试图使两个变量呈现相等,以便 cmp 之后的跳转起作用.这让我非常沮丧,因为直接值(如果 mov eax 和 ebx 为 "5" )它有效,所以这是地址问题吗?我不确定.
section .datastr_equal db "相等!", 0xAlen_equal equ $ - str_equalstr_number_a db "5"str_number_b db "5"节.text全局_start_开始:mov eax, [ str_number_a ]mov ebx, [ str_number_b ]cmp eax, ebxje _Equaljmp_退出回复_平等的:移动 eax, 4 ;系统调用 - 写()移动 ebx, 1 ;标准输出mov ecx, str_equalmov edx, len_equal整数 0x80 ;调用数据内核jmp_退出回复_出口:移动轴,1移动 ebx, 0整数 0x80回复
问题是当您尝试将单个字节从字符串移动到寄存器时,您正在用 32 位数据填充 32 位寄存器:
mov eax, [ str_number_a ]mov ebx, [ str_number_b ]
如果我们看一下内存,除了字符串的第一个字节之外可能还有任何东西:
xx xx xx 35 1F 4A 59 xx xx xx^ ^^^^^^^^'5' 垃圾
由于 eax
和 ebx
是 32 位寄存器,它们读取字符 '5' 和后面的垃圾.这是什么意思?由于两个字符串中超过字符 '5' 的垃圾内存很可能不同,eax
和 ebx
之间的比较将始终不相等.>
这可以通过将 32 位/4 字节比较 (cmp
) 更改为 8 位/1 字节(或单字符)比较来解决:
cmp 字节 eax, ebx
这样,只会比较每个寄存器的第一个字节.
或者,您可以使用 8 位寄存器来存储数据:
mov al, [ str_number_a ]mov啊,[ str_number_b ]cmp 啊,啊
Serious trouble trying to get this working .. just starting NASM assembly so sorry if this is a noob of an issue, but any help is appreciated thankyou!
Trying to get the two variables to render equal so the jump after cmp works. This is frustrating me greatly, as the direct values ( if a mov eax and ebx to be "5" ) it works so is it an address problem? I'm not sure.
section .data
str_equal db "Equal!", 0xA
len_equal equ $ - str_equal
str_number_a db "5"
str_number_b db "5"
section .text
global _start
_start:
mov eax, [ str_number_a ]
mov ebx, [ str_number_b ]
cmp eax, ebx
je _Equal
jmp _Exit
ret
_Equal:
mov eax, 4 ; syscall - write()
mov ebx, 1 ; stdout
mov ecx, str_equal
mov edx, len_equal
int 0x80 ; Call dat Kernel
jmp _Exit
ret
_Exit:
mov eax, 1
mov ebx, 0
int 0x80
ret
The problem is that you are filling 32-bit registers with 32-bits of data when attempting to move the single byte from the strings into registers:
mov eax, [ str_number_a ]
mov ebx, [ str_number_b ]
If we have a look at the memory, there could be anything beyond the first byte of the strings:
xx xx xx 35 1F 4A 59 xx xx xx
^ ^^^^^^^
'5' Garbage
As eax
and ebx
are 32-bit registers, they read in both the character '5' and the garbage following. What does this mean? The since the garbage memory beyond the character '5' in both strings are very likely to be different, the comparison between eax
and ebx
will always be non-equal.
This can be fixed by changing the 32-bit/4 byte comparison (cmp
) to an 8-bit/1 byte (or, single character) comparison:
cmp byte eax, ebx
This way, only the first byte of each register will be compared.
EDIT:
Alternatively, you could use 8-bit registers to store data:
mov al, [ str_number_a ]
mov ah, [ str_number_b ]
cmp al, ah
这篇关于比较 NASM 程序集中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!