如何用汇编语言进行字符串输入?
请问有人知道如何用汇编语言编写字符串输入吗?我使用 int 21
来显示和输入字符。
Please, does anybody know how to code string input in assembly language? I'm using int 21
to display and input characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 函数
0Ah
读取缓冲的输入。给定ds:dx
中的字符串缓冲区,它会读取长度最大为 255 的字符串。缓冲区布局如下:读取字符串然后将其回显给用户的小型 COM 文件的示例:
请注意,它会覆盖输入行(因此可能看起来程序没有执行任何操作!)
或者您可以使用 功能
terminated mov dx, string ; ds:dx points to string mov ah, 0x09 ; Function 09h Print character string int 0x21 ; Exit mov ax, 0x4c00 int 0x21 string: times 255 db 0 ; reserve room for 255 characters ; Terminate string mov ah, 0x09 ; Function 09h Print character string int 0x21 ; ds:dx points to string ; Exit mov ax, 0x4c00 int 0x21 string_buf: db 255 ; size of buffer in characters db 0 ; filled by DOS with actual size times 255 db 0 ; actual string01h
并自己循环读取字符。像这样的东西(注意如果输入超过 255 个字符,后面的缓冲区将会溢出):请注意,它会覆盖输入行(因此可能看起来程序没有执行任何操作!)
或者您可以使用 功能
01h
并自己循环读取字符。像这样的东西(注意如果输入超过 255 个字符,后面的缓冲区将会溢出):You can use function
0Ah
to read buffered input. Given a string buffer inds:dx
it reads a string of up to length 255. The buffer layout is:An example of a small COM file that reads a string and then echos it back to the user:
Note that it will overwrite the input line (and it thus might not look the program is doing anything!)
Alternatively you can use function
terminated mov dx, string ; ds:dx points to string mov ah, 0x09 ; Function 09h Print character string int 0x21 ; Exit mov ax, 0x4c00 int 0x21 string: times 255 db 0 ; reserve room for 255 characters ; Terminate string mov ah, 0x09 ; Function 09h Print character string int 0x21 ; ds:dx points to string ; Exit mov ax, 0x4c00 int 0x21 string_buf: db 255 ; size of buffer in characters db 0 ; filled by DOS with actual size times 255 db 0 ; actual string01h
and read the characters yourself in a loop. Something like this (note it will overflow later buffers if more than 255 characters are entered):Note that it will overwrite the input line (and it thus might not look the program is doing anything!)
Alternatively you can use function
01h
and read the characters yourself in a loop. Something like this (note it will overflow later buffers if more than 255 characters are entered):字符串只是一系列字符,因此您可以在循环内使用 int 21 代码来获取字符串,一次一个字符。在数据段中创建一个标签来保存字符串,每次读取字符时,将其复制到该标签(每次增加一个偏移量,以便按顺序存储字符)。当读取某个字符(可能是输入)时停止循环。
手动完成所有这些工作很乏味(想想退格键如何工作),但你可以做到。或者,您可以链接到 stdio、stdlib 等并调用库函数来为您完成大部分工作。
A string is just a series of characters, so you can use your int 21 code inside of a loop to get a string, one character at a time. Create a label in the data segment to hold your string, and each time you read a character, copy it to that label (incrementing an offset each time so your characters get stored sequentially). Stop looping when a certain character is read (perhaps enter).
Doing all this manually is tedious (think about how backspace will work) but you can do it. Alternatively, you can link against stdio, stdlib, etc. and call library functions to do much of the work for you.