如何用汇编语言进行字符串输入?

发布于 2024-11-25 19:15:52 字数 62 浏览 2 评论 0原文

请问有人知道如何用汇编语言编写字符串输入吗?我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-12-02 19:15:52

您可以使用 函数 0Ah 读取缓冲的输入。给定 ds:dx 中的字符串缓冲区,它会读取长度最大为 255 的字符串。缓冲区布局如下:

Byte 0 String length (0-255)
Byte 1 Bytes read (0-255, filled by DOS on return)
Bytes 2-..Length+2 (The character string including newline as read by DOS).

读取字符串然后将其回显给用户的小型 COM 文件的示例:

    org 0x100

start:
    push cs
    pop ds ; COM file, ds = cs

    mov ah, 0x0A ; Function 0Ah Buffered input
    mov dx, string_buf ; ds:dx points to string buffer
    int 0x21

    movzx si, byte [string_buf+1] ; get number of chars read

    mov dx, string_buf + 2 ; start of actual string

    add si, dx ; si points to string + number of chars read
    mov byte [si], '

请注意,它会覆盖输入行(因此可能看起来程序没有执行任何操作!)

或者您可以使用 功能01h 并自己循环读取字符。像这样的东西(注意如果输入超过 255 个字符,后面的缓冲区将会溢出):

    org 0x100

start:
    push cs
    pop ax
    mov ds, ax
    mov es, ax; make sure ds = es = cs

    mov di, string ; es:di points to string
    cld ; clear direction flag (so stosb incremements rather than decrements)
read_loop:
    mov ah, 0x01 ; Function 01h Read character from stdin with echo
    int 0x21
    cmp al, 0x0D ; character is carriage return?
    je read_done ; yes? exit the loop
    stosb ; store the character at es:di and increment di
    jmp read_loop ; loop again
read_done:
    mov al, '
 ; 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 string

请注意,它会覆盖输入行(因此可能看起来程序没有执行任何操作!)

或者您可以使用 功能01h 并自己循环读取字符。像这样的东西(注意如果输入超过 255 个字符,后面的缓冲区将会溢出):



    stosb ; 'Make sure the string is '
 ; 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 string

请注意,它会覆盖输入行(因此可能看起来程序没有执行任何操作!)

或者您可以使用 功能01h 并自己循环读取字符。像这样的东西(注意如果输入超过 255 个字符,后面的缓冲区将会溢出):

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 string

请注意,它会覆盖输入行(因此可能看起来程序没有执行任何操作!)

或者您可以使用 功能01h 并自己循环读取字符。像这样的东西(注意如果输入超过 255 个字符,后面的缓冲区将会溢出):

You can use function 0Ah to read buffered input. Given a string buffer in ds:dx it reads a string of up to length 255. The buffer layout is:

Byte 0 String length (0-255)
Byte 1 Bytes read (0-255, filled by DOS on return)
Bytes 2-..Length+2 (The character string including newline as read by DOS).

An example of a small COM file that reads a string and then echos it back to the user:

    org 0x100

start:
    push cs
    pop ds ; COM file, ds = cs

    mov ah, 0x0A ; Function 0Ah Buffered input
    mov dx, string_buf ; ds:dx points to string buffer
    int 0x21

    movzx si, byte [string_buf+1] ; get number of chars read

    mov dx, string_buf + 2 ; start of actual string

    add si, dx ; si points to string + number of chars read
    mov byte [si], '

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):

    org 0x100

start:
    push cs
    pop ax
    mov ds, ax
    mov es, ax; make sure ds = es = cs

    mov di, string ; es:di points to string
    cld ; clear direction flag (so stosb incremements rather than decrements)
read_loop:
    mov ah, 0x01 ; Function 01h Read character from stdin with echo
    int 0x21
    cmp al, 0x0D ; character is carriage return?
    je read_done ; yes? exit the loop
    stosb ; store the character at es:di and increment di
    jmp read_loop ; loop again
read_done:
    mov al, '
 ; 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 string

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):



    stosb ; 'Make sure the string is '
 ; 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 string

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):

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 string

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):

笔落惊风雨 2024-12-02 19:15:52

字符串只是一系列字符,因此您可以在循环内使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文