使用DOS获取字符串输入并显示输入会中断MASM [英] Getting string input and displaying input with DOS interrupts MASM

查看:299
本文介绍了使用DOS获取字符串输入并显示输入会中断MASM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

            .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

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

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

推荐答案

我对您的代码进行了一些更改.首先,"buff"变量需要三级格式(允许的最大字符数,用于输入字符数的另一个字节以及缓冲区本身),因为这是0AH服务所需要的.为了使用服务0AH,我添加了偏移增益"(如Wolfgang所说).在这里:

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:

            .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

0AH从键盘捕获字符串时,它以ENTER(字符13)结尾,因此,如果要捕获25个字符,则必须指定26.

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.

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

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 + 2开始,并在出现字符13时结束.我们使用它来将长度加到buff + 2 + 1上,以'$'代替chr(13).现在我们可以显示字符串了.

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.

这篇关于使用DOS获取字符串输入并显示输入会中断MASM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆