我有一个把字符串转换成整数的程序。只要用户输入有效的数字,程序就能正常工作。但是,一旦我添加了一些错误检查,以确保输入的值实际上是数字,我就遇到了一些问题。最初,我将tryAgain标记作为getData过程中的第一行代码。但是,错误检查意味着当在下一次迭代中输入有效数字时,程序会在输入无效的输入后,继续返回消息“无效输入”。将tryAgain标记放在代码的第一行会导致程序继续返回无效的输入消息,即使有效输入跟随无效的输入。首先,我试图将tryAgain标记放在当前位置,但我认为它是在无限循环上运行的。第二,如果程序跳转到invalidInput,我会尝试重置变量,但这还没有修复(我在tryAgain的原始位置和当前位置尝试了这一点)。
这是用于x86处理器的MASM。提前谢谢你的建议。
下面是代码:
.data
result DWORD ?
temp BYTE 21 DUP(0)
answer DWORD ?
.code
main PROC(一些初步程序要求)
push OFFSET temp ;ebp+16
push OFFSET answer ;ebp+12
push answerSize ;ebp+8
call getData(更多的过程调用)
exit ; exit to operating system
main ENDP
;*************************************************
; prompts / gets the user’s answer.
; receives: the OFFSET of answer and temp and value of answerSize
; returns: none
; preconditions: none
; registers changed: eax, ebx, ecx, edx, ebp, esi, esp
;*************************************************
getData PROC
push ebp
mov ebp,esp
tryAgain:
mWriteStr prompt_1
mov edx, [ebp+16] ;move OFFSET of temp to receive string of integers
mov ecx, 12
call ReadString
cmp eax, 10
jg invalidInput
mov ecx, eax ;loop for each char in string
mov esi,[ebp+16] ;point at char in string
pushad
loopString: ;loop looks at each char in string
mov ebx,[ebp+12]
mov eax,[ebx] ;move address of answer into eax
mov ebx,10d
mul ebx ;multiply answer by 10
mov ebx,[ebp+12] ;move address of answer into ebx
mov [ebx],eax ;add product to answer
mov al,[esi] ;move value of char into al register
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 invalidInput
cmp al,9
jg invalidInput
mov ebx,[ebp+12] ;move address of answer into ebx
add [ebx],al ;add int to value in answer
loop loopString
popad
jmp moveOn
invalidInput: ;reset registers and variables to 0
mov al,0
mov eax,0
mov ebx,[ebp+12]
mov [ebx],eax
mov ebx,[ebp+16]
mov [ebx],eax
mWriteStr error
jmp tryAgain
moveOn:
pop ebp
ret 12
getData ENDP为了让你知道我要做什么,这是我的伪代码:
发布于 2012-12-02 04:03:54
去掉pushad和popad。每次输入无效字符时,它跳回tryAgain,然后执行另一个pushad --每次跳回tryAgain。
如果您需要保存任何寄存器,请在序言中这样做,并在结语中还原它们。
https://stackoverflow.com/questions/13666153
复制相似问题