如何比较菜单的无效输入
How to compare invalid input for a menu
我在 getcombo
函数中遇到问题。我想检查用户输入的值是否在菜单选项范围内(1-4)。所以我用if else
语句将用户输入的值与1、2、3、4等整数值进行比较。但即使用户输入了正确的值,程序仍会要求用户再次输入该值。
INCLUDE Irvine32.inc
.data
MAX = 80
userInput BYTE MAX+1 DUP (?)
msgName BYTE 'Enter Your Name : ' ,0
msgPass BYTE 'Enter Your Password : ' , 0
comboTitle BYTE'COMBO Meals',0
line1 BYTE'==================',0
line2 BYTE'==================',0
comboMeal1 BYTE '1) Chicken Burger + Coca Cola + Cheesy Wedges | RM 15.00',0
comboMeal2 BYTE '2) Grilled Beef Burger + Pepsi + French Fries | RM 17.00',0
comboMeal3 BYTE '3) Spicy Korean Wings + Coca Cola + salad | RM 10.00',0
comboMeal4 BYTE '4) Chicken Pizza + mountain dew + minched Potato | RM 20.00',0
selectedCombo BYTE ' ',0
prompt1 BYTE 'Please select a Combo Meal : ',0
mealPrice1 dword 15.00
mealPrice2 dword 17.00
mealPrice3 dword 10.00
mealPrice4 dword 20.00
invalidLogin BYTE 'Invalid' , 0
invalidSelection BYTE'Please select within given menu option',0
validPass BYTE 'Valid Password' , 0
loginPassword BYTE "123" , 0
loginName BYTE "bob" , 0
.code
main PROC
Login :
;---------------- Display "Enter Your Name"
mov edx,OFFSET msgName
call WriteString
;---------------- Get name input
call GetInput
;---------------- compare user input and name
mov ebx , OFFSET loginName
.IF eax == [ebx]
;--------------- Correct input
;---------------- Display "Enter Your Password"
mov edx,OFFSET msgPass
call WriteString
;---------------- Get password input
call GetInput
;--------------- compare user input and password
mov ebx , OFFSET loginPassword
.IF eax == [ebx]
;--------------Correct input
jmp displayMenu
.ELSE
;--------------- Incorrect input
jmp InvalidInput
.ENDIF
;---------------
.ELSE
;-------------- Incorrect input
jmp InvalidInput
.ENDIF
InvalidInput :
;---------------Display "Invalid"
mov edx,OFFSET invalidLogin
call WriteString
call Crlf
;--------------- Repeat process
jmp Login
invalidMenuSelect:
xor edx,edx
mov edx, OFFSET invalidSelection
call WriteString
call Crlf
jmp getCombo
display:
mov edx,OFFSET validPass
call WriteString
displayMenu :
mov edx,OFFSET line1
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboTitle
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET line1
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal1
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal2
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal3
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal4
call WriteString
call Crlf
xor edx,edx
call Crlf
mov edx,OFFSET prompt1
call WriteString
call Crlf
xor edx,edx
jmp getCombo
exit
getCombo :
call GetInput
.IF eax == '1'
xor edx,edx
mov edx, OFFSET comboMeal1
;mov selectedCombo , edx
.ELSEIF eax == '2'
xor edx,edx
mov edx, OFFSET comboMeal2
;mov selectedCombo , edx
.ELSEIF eax == '3'
xor edx,edx
mov edx, OFFSET comboMeal3
;mov selectedCombo , edx
.ELSEIF eax == '4'
xor edx,edx
mov edx, OFFSET comboMeal4
;mov selectedCombo , edx
.ELSE
jmp invalidMenuSelect
.ENDIF
main ENDP
GetInput PROC
;-------------
;Get User input
;-------------
xor eax,eax
mov edx,OFFSET userInput
mov ecx,MAX
call ReadString
mov eax,[edx]
ret
GetInput ENDP
END main
您的代码存在问题,您用于输入的 ReadString 函数正在将新的换行符添加到 eax 寄存器。要解决您的问题,您只需检查 .IF al == '1'
等即可。更好的方法是使用 Irvine 库函数 ReadChar
然后检查 al 寄存器。
code example
我在 getcombo
函数中遇到问题。我想检查用户输入的值是否在菜单选项范围内(1-4)。所以我用if else
语句将用户输入的值与1、2、3、4等整数值进行比较。但即使用户输入了正确的值,程序仍会要求用户再次输入该值。
INCLUDE Irvine32.inc
.data
MAX = 80
userInput BYTE MAX+1 DUP (?)
msgName BYTE 'Enter Your Name : ' ,0
msgPass BYTE 'Enter Your Password : ' , 0
comboTitle BYTE'COMBO Meals',0
line1 BYTE'==================',0
line2 BYTE'==================',0
comboMeal1 BYTE '1) Chicken Burger + Coca Cola + Cheesy Wedges | RM 15.00',0
comboMeal2 BYTE '2) Grilled Beef Burger + Pepsi + French Fries | RM 17.00',0
comboMeal3 BYTE '3) Spicy Korean Wings + Coca Cola + salad | RM 10.00',0
comboMeal4 BYTE '4) Chicken Pizza + mountain dew + minched Potato | RM 20.00',0
selectedCombo BYTE ' ',0
prompt1 BYTE 'Please select a Combo Meal : ',0
mealPrice1 dword 15.00
mealPrice2 dword 17.00
mealPrice3 dword 10.00
mealPrice4 dword 20.00
invalidLogin BYTE 'Invalid' , 0
invalidSelection BYTE'Please select within given menu option',0
validPass BYTE 'Valid Password' , 0
loginPassword BYTE "123" , 0
loginName BYTE "bob" , 0
.code
main PROC
Login :
;---------------- Display "Enter Your Name"
mov edx,OFFSET msgName
call WriteString
;---------------- Get name input
call GetInput
;---------------- compare user input and name
mov ebx , OFFSET loginName
.IF eax == [ebx]
;--------------- Correct input
;---------------- Display "Enter Your Password"
mov edx,OFFSET msgPass
call WriteString
;---------------- Get password input
call GetInput
;--------------- compare user input and password
mov ebx , OFFSET loginPassword
.IF eax == [ebx]
;--------------Correct input
jmp displayMenu
.ELSE
;--------------- Incorrect input
jmp InvalidInput
.ENDIF
;---------------
.ELSE
;-------------- Incorrect input
jmp InvalidInput
.ENDIF
InvalidInput :
;---------------Display "Invalid"
mov edx,OFFSET invalidLogin
call WriteString
call Crlf
;--------------- Repeat process
jmp Login
invalidMenuSelect:
xor edx,edx
mov edx, OFFSET invalidSelection
call WriteString
call Crlf
jmp getCombo
display:
mov edx,OFFSET validPass
call WriteString
displayMenu :
mov edx,OFFSET line1
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboTitle
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET line1
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal1
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal2
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal3
call WriteString
call Crlf
xor edx,edx
mov edx,OFFSET comboMeal4
call WriteString
call Crlf
xor edx,edx
call Crlf
mov edx,OFFSET prompt1
call WriteString
call Crlf
xor edx,edx
jmp getCombo
exit
getCombo :
call GetInput
.IF eax == '1'
xor edx,edx
mov edx, OFFSET comboMeal1
;mov selectedCombo , edx
.ELSEIF eax == '2'
xor edx,edx
mov edx, OFFSET comboMeal2
;mov selectedCombo , edx
.ELSEIF eax == '3'
xor edx,edx
mov edx, OFFSET comboMeal3
;mov selectedCombo , edx
.ELSEIF eax == '4'
xor edx,edx
mov edx, OFFSET comboMeal4
;mov selectedCombo , edx
.ELSE
jmp invalidMenuSelect
.ENDIF
main ENDP
GetInput PROC
;-------------
;Get User input
;-------------
xor eax,eax
mov edx,OFFSET userInput
mov ecx,MAX
call ReadString
mov eax,[edx]
ret
GetInput ENDP
END main
您的代码存在问题,您用于输入的 ReadString 函数正在将新的换行符添加到 eax 寄存器。要解决您的问题,您只需检查 .IF al == '1'
等即可。更好的方法是使用 Irvine 库函数 ReadChar
然后检查 al 寄存器。
code example