[英]How to prompt user for string and display it again in Assembly Language Programming
我的汇编语言代码有问题。
我们被要求提示用户输入字符串,我们应该再次显示它或将它回显到命令行。 我们需要假设它最多只有 20 个字符(在字符串中)
这是示例输出:
输入一个字符串(最多 20 个字符。)
012345678901234567890
您输入的字符串是:
012345678901234567890
当我在 DOSBOX 中运行我的代码时,我输入:0123456789 按回车键后,它向我显示了一堆看起来很奇怪的字符和符号......
这是我的代码:
.186
data segment
message1 db "Enter a string (max 20 char.): " ,13, 10, '$'
message2 db "The string you entered is: " , 13, 10, 'S'
myBStr db 20, 21 dup(?) ,'S'
data ends
stack1 segment stack
db 100 dup(?) ; This is the stack of 100 bytes
stack1 ends
code segment
assume cs:code, ds:data, ss:stack1
start:
mov ax, data
mov ds, ax
mov ax, stack1
mov ss, ax
lea dx, message1 ;load message to dx
mov ah, 9h ;show this message
int 21h
mov ah, 0Ah
lea dx, myBStr ;Load address of string
int 21h
mov ah, 9h ; show message of entered string
int 21h
lea dx, message2 ;load second message to dx
mov ah, 9h ;show this message
int 21h
mov ah, 0Ah
lea dx, myBStr ;Load address of string
int 21h
mov ah, 4ch ;Set up code to specify return to dos
int 21h
code ends
end start
输入一个字符串(最多 20 个字符。)
012345678901234567890
奇怪的是,当要求输入最多 20 个字符时,您可能会收到 21 个字符!
1 mov ah, 0Ah 2 lea dx, myBStr ;Load address of string 3 int 21h 4 mov ah, 9h ; show message of entered string 5 int 21h 6 lea dx, message2 ;load second message to dx 7 mov ah, 9h ;show this message 8 int 21h 9 mov ah, 0Ah 10 lea dx, myBStr ;Load address of string 11 int 21h
第 4 行和第 5 行不合适。 显示第二条消息后,您需要此代码。
第 9、10 和 11 行当前重新输入字符串,而实际上您想要显示它。
lea dx, myBStr ;Load address of INPUT STRUCTURE
mov ah, 0Ah ;Buffered input
int 21h
lea dx, message2 ;Load second message
mov ah, 09h ;Show this message
int 21h
lea dx, myBStr + 2 ;Load address of string
mov ah, 09h ;Show entered string
int 21h
字符串从输入结构的第 3 个字节开始。 这就是为什么你需要写lea dx, myBStr + 2
。
message2 db "The string you entered is: " , 13, 10, 'S'
末尾的“S”没有实际用途。 让我们同意这是一个错字,然后写:
message2 db "The string you entered is: " , 13, 10, '$'
由于您希望能够获得 20 个字符的字符串,因此您需要以不同的方式为 DOS.BufferedInput 函数 0Ah 设置输入结构:
myBStr db 21, 0, 21 dup(0)
第一个字节 ( 21 ) 告诉 DOS 存储空间 ( 21 dup(0)
) 可以容纳20 个字符和 1 个强制回车 CR。
第二个字节 ( 0 ) 将由 DOS 设置为实际输入的字符数(不包括 CR)。 在输出字符串之前,您可以使用此字节正确地以“$”结尾。
mov bl, [myBStr + 1] ;Get length of string
mov bh, 0
mov byte ptr [myBStr + 2 + bx], '$' ;Replace CR with '$'
lea dx, myBStr + 2 ;Load address of string
mov ah, 09h ;Show entered string
int 21h
正如prl 所指出的,如果您要设置SS
段寄存器,那么还要设置SP
寄存器:
mov ax, stack1
mov ss, ax
mov sp, 100
但是通常您根本不需要编写这些行。
来自评论
只是一个小问题,我怎样才能把字符串打印出来
在退出到 DOS之前,只需等待用户按任意键即可。
mov ah, 07h ;DOS.InputCharacterWithoutEcho
int 21h
mov ax, 4C00h ;DOS.TerminateWithExitcode
int 21h
请注意,最好使用 DOS 函数 4Ch 实际指定退出代码。 AL
的零表示程序正常终止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.