本文介绍了如何用汇编语言输入字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问,有人知道如何用汇编语言对字符串输入进行编码吗?我正在使用 int 21 来显示和输入字符。

Please, does anybody know how to code string input in assembly language? I'm using int 21 to display and input characters.

推荐答案

您可以使用来读取缓冲的输入。给定 ds:dx 中的字符串缓冲区,它将读取最大长度为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).

一个小的COM文件的示例,该文件读取一个字符串,然后将其回显给用户: / p>

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], '$' ; 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!)

或者,您也可以使用并自己​​循环读取字符。这样的事情(请注意,如果输入的字符数超过255个,它将在以后的缓冲区中溢出):

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

这篇关于如何用汇编语言输入字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 15:57