我正在嘗試在MASM中編寫一個小程序,它將接收用戶輸入的字符串,從每個字符的ASCII值中減去4,然後輸出新字符。彙編程序奇怪的行爲
除了當調用StdOut
時,這是大部分成功的,它不僅打印當前修改的字符,還打印下一個字符。
我一直在試圖弄清楚發生了幾個小時,但仍然沒有線索。這是我的代碼:
.486
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\macros\macros.asm
include \masm32\include\masm32.inc
include \masm32\include\gdi32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.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'得到32位字!不是一個8位的ASCII值。你必須使用'bl'8位寄存器來指向字符。 – Thanushan