从寄存器打印字符
我正在使用 NASM 汇编器。
返回到 eax 寄存器的值应该是一个字符,当我尝试打印整数表示时,它的值看起来像内存地址。我期待这封信的十进制表示形式。例如,如果字符“a”被移动到 eax,我应该会看到打印 97(“a”的十进制表示形式)。但事实并非如此。
section .data
int_format db "%d", 0
;-----------------------------------------------------------
mov eax, dword[ebx + edx]
push eax
push dword int_format
call _printf ;prints a strange number
add esp, 8
xor eax, eax
mov eax, dword[ebx + edx]
push eax
call _putchar ;prints the correct character!
add esp, 4
那么这里给出了什么?最终我想比较该字符,因此 eax 获取该字符的正确十进制表示形式很重要。
I'm using the NASM assembler.
The value returned to the eax register is supposed to be a character, when I attempt to print the integer representation its a value that looks like a memory address. I was expecting the decimal representation of the letter. For example, if character 'a' was moved to eax I should see 97 being printed (the decimal representation of 'a'). But this is not the case.
section .data
int_format db "%d", 0
;-----------------------------------------------------------
mov eax, dword[ebx + edx]
push eax
push dword int_format
call _printf ;prints a strange number
add esp, 8
xor eax, eax
mov eax, dword[ebx + edx]
push eax
call _putchar ;prints the correct character!
add esp, 4
So what gives here? ultimately I want to compare the character so it is important that eax gets the correct decimal representation of the character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在从指向 ebx+edx 的地址加载一个双字(32 位)。如果想要单个字符,则需要加载一个字节。为此,您可以使用 movzx 指令:
这会将单个字节加载到 eax 的低字节(即 al ),并将寄存器的其余部分清零。
另一种选择是在加载双字后屏蔽掉额外的字节,例如:
或
至于
putchar
,它之所以有效,是因为它将传递的值解释为 char,即它忽略了存在于 dword 中的高三个字节。寄存器并仅考虑低字节。You are loading a dword (32 bits) from the address pointed to ebx+edx. If you want a single character, you need to load a byte. For that, you can use movzx instruction:
This will load a single byte to the low byte of
eax
(i.e.al
) and zero out the rest of the register.Another option would be to mask out the extra bytes after loading the dword, e.g.:
or
As for
putchar
, it works because it interprets the passed value as char, i.e. it ignores the high three bytes present in the register and considers only the low byte.