gpt4 book ai didi

user-input - 如何在汇编语言中进行字符串输入?

转载 作者:行者123 更新时间:2023-12-03 11:24:45 24 4
gpt4 key购买 nike

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

最佳答案

您可以使用 function 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], '$' ; 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

请注意,它将覆盖输入行(因此它可能看起来程序没有做任何事情!)

或者,您可以使用 function 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, '$'
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

关于user-input - 如何在汇编语言中进行字符串输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6780671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com