将符号从 0ah 输入复制到另一个变量

Copying symbols from the 0ah input to another variable

任务听起来像:"Enter the s1 string at least 10 characters in length. Copy the last but one character three times and the first from string s1 to string s2. Display the lines s1 and s2."

这段代码折腾了两天,还是出不了结果,也想不明白怎么解决。除了 int21h / 4ch 之外,我们目前只了解了 0Ah、02h、09h 和 40h 函数。对于下面的代码,编译不允许我输入任何内容并打印 3 个与 6 非常相似的符号。

.model small
.stack
.data

m1 db "Enter the string:", 10, 13,  "$"
maxlength db 11
clength db ?
s1 db 11 dup(?)
s2 db 5 dup(?)
nline db 10,13,"$"

.code

mov ax, @data
mov ds, ax

mov ah, 9
lea dx, m1
int 21h

mov ah, 0Ah
lea dx, s1
int 21h

mov bl, clength
mov bh, 0

mov AL, s1[BX-2]
mov s2+1, AL

mov AL, s1[BX-2]
mov s2+2, AL

mov AL, s1[BX-2]
mov s2+3, AL

mov AL, s1[BX+1]
mov s2+4, AL

mov s2+5, "$"
mov s1[bx], "$"

mov ah, 9
lea dx, s1
int 21h

mov ah, 9
lea dx, nline
int 21h

mov ah, 9
lea dx, s2
int 21h

mov ah, 9
lea dx, nline
int 21h

mov ah, 4ch
int 21h
end

我希望输出是: 输入字符串 (我打印的字符串)

(从 s1 复制的符号)

如果你按照 Michael 的 link to the description of int 21h / 0ah 你可以看到 DS:DX 应该指向一个缓冲区,其中第一个字节是最大字符数,第二个字节是实际字符数读取,然后更多的字节来存储读取的字符串。 您实际上分配了前两个字节(maxlength 和 clength),但是您将 DX 指向 s1,这将是读取字符的第一个字节,而不是整个缓冲区。另外(假设这是 MASM 语法)

mov ah, 0Ah
lea dx, s1
int 21h

在您的程序中,此 DOS 函数的地址需要变为 "maxlength"。
你也可以写成 lea dx, [s1-2].

DOS实际需要的缓冲区是:

MyInput db 11, 0, 11 dup(?)

mov ah, 0Ah
lea dx, MyInput
int 21h

您可以在 中阅读更多关于 0Ah DOS 功能的信息。


and the first from string s1

您已通过以下方式解决了这个问题:

mov AL, s1[BX+1]
mov s2+4, AL

这是不正确的。在 s1[bx+1] 处,内存中只有垃圾。您可以通过 mov al, s1.

获取 s1 的第一个字符
mov AL, s1[BX-2]
mov s2+1, AL
mov AL, s1[BX-2]
mov s2+2, AL
mov AL, s1[BX-2]
mov s2+3, AL
mov AL, s1[BX+1]
mov s2+4, AL
mov s2+5, "$"

s2 字符串只有 5 个位置可用。您将 +1 添加到 +5 将写入此缓冲区!这将破坏您的换行字符串。
您需要使用从 +0 到 +4 的加法以保持在缓冲区的范围内。

mov AL, s1[BX-2]
mov s2, AL            NEW
mov AL, s1[BX-2]
mov s2+1, AL          NEW
mov AL, s1[BX-2]
mov s2+2, AL          NEW
mov AL, s1            NEW
mov s2+3, AL          NEW
mov s2+4, "$"         NEW

当然,您不需要将倒数第二个字符读 3 遍。

mov al, s1[bx-2]
mov s2, al
mov s2+1, al
mov s2+2, al
mov al, s1
mov ah, "$"
mov s2+3, ax