使用 DOS 中断 MASM 获取字符串输入和显示输入 [英] Getting string input and displaying input with DOS interrupts 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,我添加了偏移增益"(正如沃尔夫冈所说).这是:
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屋!