汇编编程语言:程序仅在输入为 ESC 时退出,并在退出前要求确认 (Y/n),否则循环



我只是汇编语言编程的初学者。 我们的第一个任务是仅在输入为 ESC 时才使程序退出。在退出(y/n(之前要求确认,否则循环。我知道 ESC 在 ASCII 代码中具有等效的价值,但我对插入哪里或是否需要添加更多内容感到困惑。 请帮助我!这是程序:

.model small
.stack 100h
.data
    msg1 db " <-- Non alpha!$"
msg2 db "Bye!$"
.code
start:
            ; display ?
mov dl, "?"         ; copy ? to dl
mov ah, 2h      ; display subprogram
int 21h         ; display ?
mov dl, " "         ; Provide space
mov ah, 2h      ; display subprogram
int 21h         
            ; read character from keyboard
mov ah, 1h      ; keyboard input subprogram
int 21h         ; read character into al 
            ; save character while we display a Return and Line-feed
    cmp al,65d      ; 'A'
    jb Non_alpha
    cmp al,90d      ; 'Z'
    ja above_alpha_upper
    mov bl, al              ; save character in bl
    add bl,32d      ; ADD 32 decimal
mov dl, bl      ; copy character to dl
mov ah, 2h      ; display subprogram
int 21h         ; display character in dl
    jmp x
Non_alpha:
    mov ax, @data
    mov ds,ax
    mov dx,offset msg1
    mov ah,9h
    int 21h
    jmp x           ; je,ja, jb
 above_alpha_upper:
cmp al,97d      ; 'a'
jb Non_alpha
cmp al,122d     ; 'z'
ja Non_alpha
mov bl, al              ; save character in bl
    sub bl,32d      ; SUBTRACT 32 decimal
mov dl, bl      ; copy character to dl
mov ah, 2h      ; display subprogram
int 21h         ; display character in dl
x:              ; exit
mov dl, 13d             ; dl = CR, Dh
    mov ah, 2h              ; display subprogram
    int 21h                 ; display CR
            ; display Line-feed
    mov dl, 10d             ; dl = LF , Ah
    mov ah, 2h              ; display subprogram
    int 21h                 ; display LF
            ; display character read from keyboard
mov ax, @data
    mov ds,ax
    mov dx, offset msg2 ; Bye
    mov ah,9h
    int 21h
mov ax, 4c00h       ; return to ms-dos
int 21h
end start

您可以简单地将其添加到"从键盘读取"之后

在 .data 中添加消息:

query_quit  db "Sure to quit? (y/n)$"

并将 DS 段 init 移到代码前面,就在开头

mov ax, @data       ; this should be set initially
mov ds,ax           ; not somewhere in middle of the code

通过 21h/01h 读取字符后,您可以添加 ESC 检查

read_next:     
    mov ah, 1h      ; keyboard input subprogram     *
    int 21h         ; read character into al        *
    cmp al, 1bh     ; check for [ESC]
    jnz not_escape  ; skip asking if it wasnt
    mov dx, offset query_quit
    mov ah, 9
    int 21h         ; display query string
    mov ah,1
    int 21h        ; read keyboard again (for y/n)
    cmp ah,'y'     ; quit to dos if 'y' or 'Y' was pressed
    jz x
    cmp ah,'Y'
    jz x
    jmp read_next  ; or jmp start if you want the prompt again
not_escape:
    cmp al,65d      ; 'A'        ; *
    jb Non_alpha    ;              *
    ...      
  • = 你以前的代码,显示我在哪里添加了一些
@Tommylee2k这就是

我所做的: 有 2 个严重错误

.model small
.stack 100h
.data
msg1 db " <-- Non alpha!$"
msg2 db "Bye!$"
query_quit  db "Sure to quit? (y/n)$"
.code
start:
mov ax, @data           ; this should be set initially
mov ds,ax               ; not somewhere in middle of the code
        ; display ?
mov dl, "?"         ; copy ? to dl
mov ah, 2h      ; display subprogram
int 21h         ; display ?
mov dl, " "         ; Provide space
mov ah, 2h      ; display subprogram
int 21h         
        ; read character from keyboard
mov ah, 1h      ; keyboard input subprogram
int 21h         ; read character into al 
        ; save character while we display a Return 
read_next:     
mov ah, 1h      ; keyboard input subprogram     *
int 21h         ; read character into al        *
cmp al, 1bh     ; check for [ESC]
jnz not_escape  ; skip asking if it wasnt
mov dx, offset query_quit
mov ah, 9
int 21h         ; display query string
mov ah,1
int 21h        ; read keyboard again (for y/n)
cmp ah,'y'     ; quit to dos if 'y' or 'Y' was pressed
jz x
cmp ah,'Y'
jz x
jmp read_next  ; or jmp start if you want the prompt again
not_escape:
cmp al,65d      ; 'A'        ; *
jb Non_alpha    ;              *

and Line-feed
cmp al,65d      ; 'A'
jb Non_alpha
cmp al,90d      ; 'Z'
ja above_alpha_upper
mov bl, al              ; save character in bl
add bl,32d      ; ADD 32 decimal
mov dl, bl      ; copy character to dl
mov ah, 2h      ; display subprogram
int 21h         ; display character in dl
jmp x
Non_alpha:
mov ax, @data
mov ds,ax
mov dx,offset msg1
mov ah,9h
int 21h
jmp x           ; je,ja, jb
above_alpha_upper:
cmp al,97d      ; 'a'
jb Non_alpha
cmp al,122d     ; 'z'
ja Non_alpha
mov bl, al              ; save character in bl
sub bl,32d      ; SUBTRACT 32 decimal
mov dl, bl      ; copy character to dl
mov ah, 2h      ; display subprogram
int 21h         ; display character in dl
jmp start       ; looping?

x:              ; exit
mov dl, 13d             ; dl = CR, Dh
mov ah, 2h              ; display subprogram
int 21h                 ; display CR
        ; display Line-feed
mov dl, 10d             ; dl = LF , Ah
mov ah, 2h              ; display subprogram
int 21h                 ; display LF
        ; display character read from keyboard
mov ax, @data
mov ds,ax
mov dx, offset msg2 ; Bye
mov ah,9h
int 21h
mov ax, 4c00h       ; return to ms-dos
int 21h
end start

最新更新