我正在学习汇编语言。我在 MASM 中编写了一个程序,并希望提示用户给我一个数学问题的答案。我的程序可以正常工作,只是一个变量没有正确更新。
示例:第一次尝试:用户输入:123/变量值应为 123 - 程序显示值,123 第二次尝试:用户输入:1/变量值应为 1 - 程序显示值,1231
这是一些与我编写的程序相关的代码。有人可以看看并给我一些关于如何更新变量(input_dec)的提示吗?在此先感谢您的帮助。
.data
new_prb1 BYTE "Another problem? (y/n): ", 0
input_str BYTE 21 DUP(0) ;string to be entered
input_dec DWORD ?
.code
main PROC
; prompt and get user's answer
push OFFSET input_str ;ebp+12
push OFFSET input_dec ;ebp+8
call getData
; play again?
; the program repeats until the user chooses to quit
;push OFFSET again_resp ;ebp+8
;call play_again
play_again:
displayString new_prb1
;get user input
mov edx, OFFSET again_resp ;move OFFSET of again_resp to receive
user response
mov ecx, 12
call ReadString
cmp eax, 1
jg input_notOK
mov esi,edx ;point at char in string
convert_str:
mov al,[esi] ;move value of char into al register
inc esi ;point to next char
cmp al,121 ; 'y' is character 121
je next_game
cmp al,110 ; 'n' is character 110
je game_over
jmp convert_str
input_notOK:
displayString error_msg
jmp play_again
next_game:
push OFFSET input_str
push OFFSET input_dec
call getData
jmp play_again
game_over:
displayString good_bye
call CrLf
exit ; exit to operating system
main ENDP
; ***************************************************************
;Procedure to prompt and get an answer from the user.
;receives: addresses of parameters on the system stack
;returns: user input value for the reference
;preconditions: none
;registers changed: eax, ebx, ecx, edx, esi
; ***************************************************************
getData PROC
push ebp
mov ebp, esp
again:
displayString prompt_1
mov edx, [ebp+12] ;move OFFSET of input_str to receive string of integers
mov ecx, 12
call ReadString
cmp eax, 10
jg input_notOK
mov ecx, eax ;loop for each char in string
mov esi,[ebp+12] ;point at char in string
pushad
convert_str:
mov ebx,[ebp+8]
mov eax,[ebx] ;move address of input_dec into eax
mov ebx,10d ;10d
mul ebx ;multiply answer by 10 - eax=x*10
mov ebx,[ebp+8] ;move address of input_dec into ebx
mov [ebx],eax ;add product to answer
mov al,[esi] ;move value of char into al register
mov eax, [esi]
inc esi ;point to next char
sub al,48d ;subtract 48 from ASCII value of char to get integer
cmp al,0 ;error checking to ensure values are digits 0-9
jl input_notOK
cmp al,9 ;error checking to ensure values are digits 0-9
jg input_notOK
mov ebx,[ebp+8] ;move address of input_dec into ebx
add [ebx],al ;add int to value in input_dec
loop convert_str
popad
jmp continue
input_notOK:
popad
displayString error_msg
jmp again
continue:
pop ebp
ret 8
getData ENDP