如何比较两个char,第一个是用户输入。
include 'emu8086.inc'
#make_COM#
ORG 100h
answer DW ?
score DW ?
MOV score, 0
PRINT "This is a 10 question Exam"
PUTC 13
PUTC 10
PUTC 13
PUTC 10
PRINT "1.) A is the Answer"
PUTC 13
PUTC 10
PRINT " A.)"
PUTC 13
PUTC 10
PRINT " B.)"
PUTC 13
PUTC 10
PRINT " C.)"
PUTC 13
PUTC 10
PRINT " D.)"
PUTC 13
PUTC 10
PRINT "Your Answer: "
LEA DI, buffer
MOV DX, 10
CALL GET_STRING
MOV answer, AX
PUTC 13
PUTC 10
MOV SI, answer
CALL print_string
CMP SI, answer
JE Correct
JMP result
Correct:
ADD score, 1
JMP result
result:
PRINT "Your Score: "
CALL PRINT_NUM
RET
buffer DB "x"
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
DEFINE_PRINT_STRING
DEFINE_GET_STRING
END
答案 0 :(得分:1)
buffer DB "x"
这不会给你足够的缓冲空间!如果您坚持使用mov dx, 10
的定义,则需要将其更改为:buffer db "1234567890"
。因为我将解释2个字节就足够了,这可以成为buffer db "12"
LEA DI, buffer MOV DX, 10 CALL GET_STRING MOV answer, AX
您似乎不知道 GET_STRING 程序是如何工作的!它将输入零端接存储在DS:DI的DX字节缓冲区中。由于您的答案是单个字符,因此您需要定义DX = 2,然后您可以通过mov ax, [di]
mov answer, ax
检索答案。此后 answer 是一个以零结尾的字符串,其内容为单个字符。
MOV SI, answer CALL print_string
您在此处移动了 answer 的内容。您需要将以零结尾的字符串的地址传递给 PRINT_STRING 过程,因此请写:lea si, answer
call PRINT_STRING
CMP SI, answer JE Correct JMP result
由于您设置SI
的方式,此比较将始终返回正确。您必须将内容与定义的值进行比较:mov al, [si]
cmp al, "A"
(提示:您写的“A是答案”)