I don't understand how to use Interrupt 21, AH

2020-01-29 02:03发布

问题:

My information is coming from here. The assignment asks for a program that reads in no more than 20 characters, converts those characters to upper case, and then prints the input as capitals.

I have no idea how to access the input from int21/AH=0ah. I really can't ask a more precise question unless I understand what is linked above. Can someone explain? Also, I'm using TASM if that makes any difference. Also, I'm testing this on freedos.

UPDATE1:

Alright, thanks to your help, I believe I understand how the interrupt needs to be set up and behaves.

Setup: I have to designate a ds:dx where I want this buffer to exist

I have to set ds:dx to 20 (which sets the max number of characters the buffer can hold)

I have to set ds:dx+1 to 0 (which I think somehow set a min number of characters to read in)

Actually call int21/AH=0ah, which will go to ds:dx and interpret the preset bytes. It will halt the program while it waits for input

int21/AH=0ah will fill from ds:dx+2+n with my input (where n is the number of characters input including '\r')

My question is now, how do I do this. I've just looked through the x86 Assembly Language Reference again, but haven't been able to find anything helpful yet.

Code I've got so far

           assume          cs:code,ds:code
code       segment
start:

           mov  ax,code ;moves code segment into reg AX
           mov  ds,ax   ;makes ds point to code segment

           mov  ah,0ah
           int  21h
           mov  ax,1234h  ;breakpoint

           mov  ah,9
           mov  dx,offset message
           int  21h

endNow:
           ;;;;;;;;;;ends program;;;;;;;;;;
           mov  ah,0  ;terminate program
           int  21h   ;program ends

message    db   'Hello world!!!',13,10,'$'
code       ends
           end  start

回答1:

That DOS function retrieves a buffer with user input. See this table. It seems that program is using that call to pause execution waiting for the user to resume the program.

Edit: I just reread the question. I thought you were only asking what the function call did in your given source. If you want to read input of no more than 20 characters, you first need memory to store it. Add something like this:

bufferSize  db 21  ; 20 char + RETURN
inputLength db 0   ; number of read characters
buffer      db 21 DUP(0) ; actual buffer

Then fill the buffer:

mov ax, cs
mov ds, ax ; ensure cs == ds
mov dx, offset bufferSize ; load our pointer to the beginning of the structure
mov ah, 0Ah ; GetLine function
int 21h

How to convert to uppercase is left to the reader.



回答2:

That description says you put the address of a buffer in ds:dx before calling the interrupt. The interrupt will then fill that buffer with the characters it reads.

Before calling the interrupt, the first byte of the buffer is how many characters the buffer can hold, or 20 in your case. I do not understand the description of the second byte of the buffer (on input to the interrupt), so I would set it to zero. On return, that byte will tell you how many characters of input were read and placed into the buffer.



回答3:

.model small
.stack 100h
.data 
    N db ?
    msg db 10,13,09,"Enter number of arrays---->$"
.code   
.startup
    mov ax,@data
    mov ds,ax
    call read_N;read N from console




    mov ah,4ch
    int 21h  

Read_N  proc
    ;get number of arrays from user

    push ax
    push dx

 readAgain:   

    mov ax,03h ;Clear screen
    int 10h

    mov dx,offset msg
    mov ah,09h
    int 21h

    call ReadNumber

    ;Inuput number must be in 2<=N<=10 bounery  
    cmp al,2
    js readAgain ;input out of boundary read again
    cmp al,10
    jg readAgain 
    mov N,al
    pop dx
    pop ax
    ret
Read_N endp

ReadNumber proc
    ;read decimal number 0-99 using 
    ;character by character in askii and conver in to decimal   
    ;return result in al
    xor ax,ax
    xor bx,bx
    xor dx,dx

    mov ah,01h
    int 21h

    sub al,'0'  ;conver in to decimal
    mov bl,al  

    mov ah,01h
    int 21h 
    cmp al,0dh  ;Exit if enter pressed
    jnz cont  
    mov al,bl
    jmp exit
  cont:
    sub al,'0'  ;conver in to decimal
    mov dl,al  

    xor al,al
    xor bh,bh
    mov cx,bx
  addnum:    
    add al,10
 loop addnum  

    add al,dl
  exit:   
  ret
 ReadNumber endp  

end