我有另一个不包含 getUserInput
功能的程序。它只是比较两个字符串,所以我知道 getCheckKey
工作正常。为什么这根本行不通?当用户输入匹配字符串 correct
时,它不会跳转到 cupcake
。您可以尝试:
nasm -f bin -o cmp-input.bin cmp-input.asm
qemu-system-x86_64 cmp-input.bin
ORG 0x7C00
BITS 16
jmp getUserInput ; Call function 'getUserInput'
incorrect:
mov si, invalidKey ; Set text to SI register
call printText ; Call print text function
getUserInput:
mov si, msg ; Set greeting text to SI register
call printText ; Call print text function
mov di, inputString ; set up the string to be read in into di
getKeyLoop:
mov ah, 0x00 ; Set function 'Reading' for interrupt 16h
int 0x16 ; Use 16h interrupt to read next char from keyboard
mov ah, 0x0e ; Set char types and colors
cmp al, 0xD ; when enter button is pressed...
je getCheckKey ; compare the string entered
int 0x10 ; otherwise use 10h interrupt to display character
mov [di], al ; Move AL to memory of DI address
inc di ; DI += 1; increments the address of DI to store the next character
jmp getKeyLoop ; Loop | jump to function 'getKeyLoop'
getCheckKey:
mov di, inputString ; move input string into di
mov si, password ; copy password to si for comparison
CheckKeyLoop:
mov al, [di] ; move 1st character or inputString into al
mov cl, [si] ; move 1st character of key into cl
cmp cl, al ; compare 1st character of both strings
jne incorrect ; if they don't match, jump back to getUserInput but print incorrect message first
cmp al, 0 ; otherwise compare to null terminator
je correct ; password match
inc di ; otherwise check next char
inc si ; check next char
jmp CheckKeyLoop ; resume the loop
correct:
mov si, correctkey
printText: ; Function 'printText'
mov ah, 0x0e ; Set the text print mode
mov bh, 0x00 ; Set screen and text parameters
mov bl, 0x07 ; Set screen and text parameters
printChar:
mov al, [si] ; Move character from si (dereferenced) to al
cmp al, 0 ; If AL = 0...
je endPrinting ; Call function 'endPrinting'
; Else...
int 0x10 ; Use 10h interrupt
add si, 1 ; Move +1 to SI register (inc si)
jmp printChar ; Loop back up to 'printChar' function.
endPrinting: ; Function 'endPrinting'
ret ; Return
password db "cupcake", 0
msg db " Enter the password: ", 0
invalidKey db " <= Incorrect. Try again..", 13, 10, 0
inputString db " ", 0
correctkey db " Congratulations!", 13, 10, 0
times 510 - ($-$$) db 0
dw 0xAA55