我正在尝试用MASM编写一个小程序,该程序将接受一串用户输入,从每个字符的ASCII值中减去四,然后输出新字符。
这大多是成功的,除了调用StdOut
时,它不仅打印当前修改的字符,还打印下一个字符。
以来,我一直试图弄清楚发生了什么,但仍然一无所知。 这是我的代码:
.486
.model flat, stdcall
option casemap :none
include masm32includewindows.inc
include masm32macrosmacros.asm
include masm32includemasm32.inc
include masm32includegdi32.inc
include masm32includeuser32.inc
include masm32includekernel32.inc
includelib masm32libmasm32.lib
includelib masm32libgdi32.lib
includelib masm32libuser32.lib
includelib masm32libkernel32.lib
.data?
inputtxt dw 10000 dup(?)
current dd ?
.code
start:
call main
exit
main proc
Invoke StdIn, addr inputtxt, 10000
xor esi, esi
processLoop:
movzx eax, inputtxt[esi] ; Get the character at index ESI
sub eax, 4 ; Subtract 4 from the character's ASCII code
mov current, eax ; StdOut can't print a register
Invoke StdOut, addr current ; Print the character: the problem lies here.
inc esi ; Increment the loop counter
cmp byte ptr[inputtxt[esi]], 0 ; If the next character is NUL, we're done
jne processLoop ; If it's not, loop again
ret
main endp
end start
下面是一个示例输入和输出:
输入:HE
输出:DEA
D
和A
是正确的,但E
不正确,并且与D
打印在同一通道中。
试着弄清楚这里发生了什么吗?
汇编
程序假定您的movzx
应该将 16 位数据转换为 32 位数据,因为您尚未在指令中指定源数据的大小。
添加byte ptr
说明符:
; Move a byte from [inputtxt+esi] to eax, zero-extended
movzx eax, byte ptr inputtxt[esi]