装配x86 - 检查用户输入错误

时间:2017-12-12 09:03:15

标签: assembly x86

我的程序遇到了问题。我试图让用户输入0-400之间的数字,然后从那里继续。我已经检查了用户输入的情况,它会照顾好所有这些。我失踪并需要帮助的最后一部分是当用户输入像“2aaeios”这样疯狂的东西时,它将其注册为“2”并将循环比较2到0并将jg变为“Case12”

如何强制用户输入0-400之间的数字并推送ErrorExit情况呢?

INCLUDELIB C:\asmIrvine\Examples\Lib32\Irvine32_Library\Irvine32.lib
INCLUDE C:\asmIrvine\Examples\Lib32\Irvine32.inc

.data
ask_GPA BYTE "Please enter your GPA: ", 0
ask_UNITS BYTE "How many units are you taking? ", 0
case_noerror12 BYTE "Congratulations! Your GPA allows you to take up to 12 units.", 0
case_noerror16 BYTE "Congratulations! Your GPA allows you to take up to 16 units.", 0
case_noerror24 BYTE "Congratulations! Your GPA allows you to take up to 24 units. ", 0
invalid BYTE "Your entry is invalid. ", 0
quiter BYTE "You have exceeded the amount of tries. The program will now exit.", 0
GPA DWORD 0
units DWORD 0

.code
main PROC

mov ebx, 0
mov esi, 0
mov ecx, 0

L1:
mov edx, OFFSET ask_GPA
call WriteString
call readdec
mov GPA, eax
mov eax, 350

mov eax, GPA
cmp eax, 400            ;compare to 400
jg Errorexit            ;if higher than 400, errorexit
cmp eax, 350            ;compare to 350
jg Case24               ;if higher than 350, ask how many units to take
cmp eax, 250
jg Case16
cmp eax, 0
jg Case12
jmp Errorexit

Case24:                         
mov edx, OFFSET ask_UNITS       ;ask for unit 
call WriteString
call readdec
mov units, eax
cmp eax, 24                     
jg ErrorCounter                     ;if unit OVER 24, ERROR
cmp eax, 0
jl ErrorCounter                     ;if unit LESS than 0, ERROR
mov edx, OFFSET case_noerror24  ;else, successful message
call WriteString
jmp Quit

Case16:
mov edx, OFFSET ask_UNITS
call WriteString
call readdec
mov units, eax
cmp eax, 16
jg ErrorCounter
cmp eax, 0
jl ErrorCounter 
mov edx, OFFSET case_noerror16
call WriteString
jmp quit

Case12:
mov edx, OFFSET ask_UNITS
call WriteString
call readdec
mov units, eax
cmp eax, 12
jg Errorexit
cmp eax, 0
jl ErrorCounter
mov edx, OFFSET case_noerror12
call WriteString
jmp quit

ErrorCounter:
inc ecx
cmp ecx, 3
je quiterror
jmp L1

Errorexit:
mov edx, OFFSET invalid
call WriteString
jmp ErrorCounter

quiterror:
mov edx, OFFSET quiter
call WriteString
jmp quit

Quit:

exit    
INVOKE ExitProcess, 0
main ENDP
END main

0 个答案:

没有答案