如何在8086中存储字符串

时间:2014-11-23 09:37:49

标签: string variables assembly x86 x86-16

我正在使用emu8086。

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

示例代码:

arrayStr db 30 dup(' ')

store "qwerty"

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

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

2 个答案:

答案 0 :(得分:2)

您想在运行时更改变量吗?在这种情况下,请查看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!", '$'

答案 1 :(得分:1)

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

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)