I have a program that converts a string to integers. The program works well as long as the user inputs valid numbers. However, once I added some error checking to ensure that the values entered are actually numbers, I ran into some hitches. Initially, I had the tryAgain tag as the first line of code in the getData procedure. However, the error checking meant that following an invalid input when valid numbers are entered during the next iteration, the program kept returning the message "invalid input". Putting the tryAgain tag in the first line of code causes the program to keep returning the invalid input message even when valid input follows invalid input. First, I attempted to put the tryAgain tag in its current position, but I think it's running on an infinite loop. Second, I attempted to reset variables if the program jumps to invalidInput, but that hasn't fixed it (I tried this with tryAgain in its original position and current position).
This is MASM for x86 processors. Thanks in advance for your suggestions.
Here's the code:
.data
result DWORD ?
temp BYTE 21 DUP(0)
answer DWORD ?
.code
main PROC
(some preliminary procedure calls)
push OFFSET temp ;ebp+16
push OFFSET answer ;ebp+12
push answerSize ;ebp+8
call getData
(more procedure calls)
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
Just so you have a sense of where I'm going with this, here's my pseudocode:
Start at the beginning of the string
Multiply the value of answer by 10.
Split each character off the string and subtract by 48d to get the integer. Ex. student enters 156. 49 is stored as the first char in the variable temp. Subtract 48 from 49. The integer is 1.
Add integer to value of answer.
Inc esi (move one character right).
Loop.
Get rid of the pushad
and popad
. Every time and invalid character is inputed, it jumps back to tryAgain then another pushad
gets executed - each time you jump back to tryAgain.
If you need to save any registers, do it in the prologue and restore them in the epilogue.