如何比较菜单的无效输入

时间:2019-01-03 12:14:04

标签: visual-studio assembly x86 irvine32

我在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

1 个答案:

答案 0 :(得分:0)

您的代码的问题在于,用于输入的ReadString函数正在将新的换行符添加到eax寄存器中。要解决您的问题,您只需检查.IF al == '1',依此类推。更好的方法是使用Irvine库函数ReadChar,然后检查al寄存器。

code example