string 使用 DOS 中断 MASM 获取字符串输入和显示输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29504516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:22:36  来源:igfitidea点击:

Getting string input and displaying input with DOS interrupts MASM

stringassemblyinputkeyboardmasm

提问by Jumpman

In MASM, I created a buffer variable to hold the user string input from keyboard. I am stuck on how to hold the string input into that buffer variable. I don't have any libraries linked like the irvine ones and want to do this with DOS interrupts. So far I have something along the lines of

在 MASM 中,我创建了一个缓冲区变量来保存从键盘输入的用户字符串。我被困在如何将字符串输入保存到该缓冲区变量中。我没有像 irvine 那样链接的任何库,并且想用 DOS 中断来做到这一点。到目前为止,我有一些类似的东西

            .model small

            .stack 100h

            .data
buff        db  25 dup(0), 10, 13
lbuff       EQU ($ - buff)              ; bytes in a string

            .code
main:
            mov ax, @data
            mov ds, ax              

            mov ah, 0Ah         ; doesn't work
            mov buff, ah        ; doesn't seem right
            int 21h                 


            mov     ax, 4000h       ; display to screen
            mov     bx, 1           
            mov     cx, lbuff           
            mov     dx, OFFSET buff     
            int     21h 

            mov ah, 4ch
            int 21h

            end main

I assume using 0Ah is correct as it is for reading array of input of buffered characters.

我认为使用 0Ah 是正确的,因为它用于读取缓冲字符的输入数组。

回答by Jose Manuel Abarca Rodríguez

I made some changes to your code. First, the "buff" variable needs the three level format (max number of characters allowed, another byte for the number of characteres entered, and the buffer itself) because that's what service 0AH requires. To use service 0AH I added "offset buff" (as Wolfgang said). Here it is:

我对您的代码进行了一些更改。首先,“buff”变量需要三级格式(允许的最大字符数、输入字符数的另一个字节和缓冲区本身),因为这是服务 0AH 所需要的。为了使用服务 0AH,我添加了“偏移增益”(正如沃尔夫冈所说)。这里是:

            .model small

            .stack 100h

            .data

buff        db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
            db  ?         ;NUMBER OF CHARACTERS ENTERED BY USER.
            db  26 dup(0) ;CHARACTERS ENTERED BY USER.

            .code
main:
            mov ax, @data
            mov ds, ax              

;CAPTURE STRING FROM KEYBOARD.                                    
            mov ah, 0Ah ;SERVICE TO CAPTURE STRING FROM KEYBOARD.
            mov dx, offset buff
            int 21h                 

;CHANGE CHR(13) BY '$'.
            mov si, offset buff + 1 ;NUMBER OF CHARACTERS ENTERED.
            mov cl, [ si ] ;MOVE LENGTH TO CL.
            mov ch, 0      ;CLEAR CH TO USE CX. 
            inc cx ;TO REACH CHR(13).
            add si, cx ;NOW SI POINTS TO CHR(13).
            mov al, '$'
            mov [ si ], al ;REPLACE CHR(13) BY '$'.            

;DISPLAY STRING.                   
            mov ah, 9 ;SERVICE TO DISPLAY STRING.
            mov dx, offset buff + 2 ;MUST END WITH '$'.
            int 21h

            mov ah, 4ch
            int 21h

            end main

When 0AH captures the string from keyboard, it ends with ENTER (character 13), that's why, if you want to capture 25 characters, you must specify 26.

当0AH从键盘捕获字符串时,它以ENTER(字符13)结尾,这就是为什么如果要捕获25个字符,则必须指定26。

To know how many characters the user entered (length), access the second byte (offset buff + 1). The ENTER is not included, so, if user types 8 characters and ENTER, this second byte will contain the number 8, not 9.

要知道用户输入了多少个字符(长度),请访问第二个字节(偏移 buff + 1)。不包括 ENTER,因此,如果用户键入 8 个字符并按 ENTER,则第二个字节将包含数字 8,而不是 9。

The entered characters start at offset buff + 2, and they end when character 13 appears. We use this to add the length to buff+2 + 1 to replace chr(13) by '$'. Now we can display the string.

输入的字符从偏移 buff + 2 开始,并在字符 13 出现时结束。我们使用它来将长度添加到 buff+2 + 1 以将 chr(13) 替换为 '$'。现在我们可以显示字符串了。

回答by Q10Viking

This is my code,maybe can help you.

这是我的代码,也许可以帮助你。

;Input String Copy output

dataarea segment
    BUFFER db 81
           db ?
    STRING DB 81 DUP(?)
    STR1 DB 10,13,'$'   
dataarea ends

extra segment
    MESS1 DB 'After Copy',10,13,'$'
    MESS2 DB 81 DUP(?)
extra ends

code segment
main proc far
    assume cs:code,ds:dataarea,es:extra
start:
    push ds
    sub ax,ax
    push ax

    mov ax,dataarea   
    mov ds,ax

    mov ax,extra      
    mov es,ax

    lea dx,BUFFER     
    mov ah,0ah
    int 21h

    lea si,STRING
    lea di,MESS2
    mov ch,0
    mov cl,BUFFER+1
    cld
    rep movsb
    mov al,'$'
    mov es:[di],al

    lea dx,STR1            ;to next line
    mov ah,09h
    int 21h

    push es
    pop ds

    lea dx,MESS1           ;output:after copy
    mov ah,09h
    int 21h

    lea dx,MESS2
    mov ah,09h
    int 21h

    ret
main endp
code ends
    end start

And the result is:

结果是:

c:\demo.exe
Hello World!
After Copy
Hello World!

回答by rashedcs

You may follow this code :

您可以遵循以下代码:

; Problem : input array from user

.MODEL SMALL
.STACK
.DATA
   ARR DB 10 DUB (?)     

.CODE
 MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    XOR BX, BX
    MOV CX, 5

    FOR: 
      MOV AH, 1
      INT 21H
      MOV ARR[BX], AL
      INC BX
    LOOP FOR

    XOR BX, BX
    MOV CX, 5

    PRINT:  
     MOV AX, ARR[BX] ;point to the  current index 

     MOV AH, 2       ;output
     MOV DL, AX  
     INT 21H     

     INC BX           ;move pointer to the next element 
    LOOP PRINT       ;loop until done

 MAIN ENDP

回答by AbbieRojo

;try this one, it takes a 10 character string input from user and displays it after in this manner, "Hello *10character string input"

;试试这个,它需要用户输入的10个字符串,然后以这种方式显示,“你好*10个字符串输入”

.MODEL TINY
.CODE
.286
ORG 100h

    START:
     MOV DX, OFFSET BUFFER
     MOV AH, 0ah
     INT 21h
     JMP PRINT
     BUFFER DB 10,?, 10 dup(' ')

     PRINT:
     MOV AH, 02
     MOV DL, 0ah
     INT 21h
     MOV AH, 9
     MOV DX, OFFSET M1
     INT 21h
     XOR BX, BX
     MOV BL, BUFFER[1]
     MOV BUFFER [BX+2], '$'
     MOV DX, OFFSET BUFFER +2
     MOV AH, 9
     INT 21h

    M1: db 'Hello $'


 END START
 END