繁体   English   中英

如何在8086中存储字符串

[英]how to store strings in 8086

我正在使用emu8086。

例如,我有一个名为'store'的宏,它接受一个字符串并将其存储在一个数组中,我该怎么做?

示例代码:

arrayStr db 30 dup(' ')

store "qwerty"

store MACRO str
*some code here which stores str into arrayStr*
endm

我在互联网上发现的大多数例子都围绕着已经存储在变量中的字符串(例如字符串db( 这里有一些字符串 ))但我想要的是变量首先被初始化为空的东西。

您想在运行时更改变量吗? 在这种情况下,请查看emu8086.inc中的PRINT宏。 一些变化,你有一个STORE-macro:

store MACRO str
    LOCAL skip_data, endloop, repeat, localdata
    jmp skip_data           ; Jump over data
    localdata db str, '$', 0  ; Store the macro-argument with terminators
    skip_data:
    mov si, OFFSET localdata
    mov di, OFFSET msg
    repeat:                 ; Loop to store the string
    cmp byte ptr [si], 0    ; End of string?
    je endloop              ; Yes: end of loop
    movsb                   ; No:  Copy one byte from DS:SI to ES:DI, inc SI & DI
    jmp repeat              ; Once more
    endloop:
ENDM

crlf MACRO
    LOCAL skip_data, localdata
    jmp skip_data
    localdata db 13, 10, '$'
    skip_data:
    mov dx, offset localdata
    mov ah, 09h 
    int 21h
ENDM    

ORG 100h

mov dx, OFFSET msg
mov ah, 09h 
int 21h

crlf
store "Hello!"

mov dx, OFFSET msg
mov ah, 09h 
int 21h

crlf
store "Good Bye."

mov dx, OFFSET msg
mov ah, 09h 
int 21h

mov ax, 4C00h
int 21h

msg db "Hello, World!", '$'

这取决于你想要用字符串做什么以下是一些例子:

ASCIZ字符串

The string ends with a zero-byte.
The advantage is that everytime the CPU loads a single byte from the RAM the zero-flag is set if the end of the string is reached.
The disadvantage is that the string mustn't contain another zero-byte. Otherwise the program would interprete an earlier zero-byte as the end of the string.

从DOS函数Readln输入字符串(int 21h / ah = 0ah)

The first byte defines, how long the string inputted by the user could be maximally. The effective length is defined in the second byte. The rest contains the string.

准备使用WriteLn输出的字符串(int 21h / ah = 09h)

该字符串以美元符号(ASCII 36)结尾。 优点是你的程序可以使用单个函数输出字符串(int 21h / ah = 09h)。 缺点是字符串不得包含另一个美元符号。 否则,程序会将较早的美元符号解释为字符串的结尾。

字符串,其长度在字符串开头的单词/字节中定义

无格式字符串

You don't have to save the length in a variable nor marking the end, if you save the length to a constant which you can put in a register (e.g. in CX)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM