assembly programming language: program to exit only when the input is ESC and asks for confirmation (y/n) before exiting, else loop

痞子三分冷 提交于 2020-05-09 17:19:15

问题


i am just a beginner at assembly language programming. our first task is to make the program exit only when the input is ESC. Ask for confirmation before exiting (y/n), else loop. i know ESC has equivalent value in ASCII code, but im confused as to where to insert or if i need more things to add. please help me! this is the program:

.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

回答1:


you could simply add it right after the "read from keyboard"

in .data add the message:

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

and move the DS segment init in front of the code, right at the beginning

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

after you read the char via 21h/01h, you can add the check for 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    ;              *
    ...      
  • = your previous code, to show where I added some



回答2:


@Tommylee2k this is what i did: got 2 severe errors

.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


来源:https://stackoverflow.com/questions/42227604/assembly-programming-language-program-to-exit-only-when-the-input-is-esc-and-as

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!