汇编代码显示字符串

时间:2011-12-29 07:35:25

标签: string assembly input dos

我有这段汇编代码接受字符串并显示字符串。

我的问题是,由于我们使用buff存储来自用户的输入,因此无法确定名称在name1中的存储方式。

我知道

buff label byte
maxchar db 50
readchar db 0
name1 db 48 dup(0)

与此有关。但是我无法理解它的工作原理。

.model small
.stack
.data
    buff label byte
    maxchar db 50
    readchar db 0
    name1 db 48 dup(0)
    m1 db 10,13,"enter name: $"
    m2 db 10,13,"your name is: $"
.code
    mov ax, @data
    mov ds, ax
    lea dx, m1
    mov ah, 09
    int 21h
    lea dx, buff
    mov ah, 10
    int 21h


    mov ah,0
    mov al, readchar
    add ax, 2
    mov si, al
    mov buff[si],24H ;ascii code for $ to terminate string
    lea dx, m2
    mov ah, 9
    int 21h
    lea dx, name1
    mov ah, 09
    int 21h

    mov ah, 4ch
    int 21h
end

请帮忙!

谢谢。

1 个答案:

答案 0 :(得分:3)

使用DOS函数0x0a(或代码中的十进制10)读取输入,该函数执行缓冲输入。 DS:DX参数指向具有以下格式的缓冲区,该格式位于程序中标记为buff(或等效maxchar)的位置:

 offset    meaning
 ------  -------------
    0      Number of bytes available for the input data (starting at offset 2)
    1      A location for DOS to put the number of characters read into the buffer
    2      A buffer of bytes that can hold the number of characters specified in 
           offset 0

因此,在您的代码中,DS:DX指向buff,表示最多可将50个字符放入name1的缓冲区中。看起来代码有一个潜在的问题,因为缓冲区中只有48个字节,但数据结构表明有50个字节。因此输入可能会覆盖m1的前两个字节。现在,装配程序 - 特别是旧程序 - 以执行各种技巧来节省空间而闻名。在调用DOS函数0x0a之后没有使用m1,所以这可能是有意的(但如果是这样,我不确定为什么更多的m1不可用)。我猜它是无意的,而且这个bug从来没有表现出任何明显的东西。