从寄存器打印字符 [英] Printing character from register
问题描述
我使用的是NASM汇编器。
I'm using the NASM assembler.
返回eax寄存器的值应该是一个字符,当我试图打印整数表示时,它的a值看起来像一个内存地址。我期待的字母的十进制表示。例如,如果字符'a'被移动到eax,我应该看到97被打印('a'的十进制表示)。但不是这样。
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
最终我想比较字符,所以重要的是eax获得正确的字符的十进制表示。
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.
推荐答案
mov eax, dword[ebx + edx]
32位)从指向ebx + edx的地址。如果你想要一个单一的字符,你需要加载一个字节。为此,你可以使用movzx指令:
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:
movzx eax, byte[ebx + edx]
这将加载一个字节到 eax
另一种选择是在加载完成后屏蔽掉额外的字节,如下所示:c $ c> al dword,例如:
Another option would be to mask out the extra bytes after loading the dword, e.g.:
and eax, 0ffh
或
movxz eax, al
对于 putchar
,它工作,因为它将传递的值解释为char,寄存器中的高三个字节,只考虑低字节。
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.
这篇关于从寄存器打印字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!