2014-11-23 90 views
1

我正在使用emu8086。如何在8086中存储字符串

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

示例代码:

arrayStr db 30 dup(' ') 

store "qwerty" 

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

我在互联网上找到的大多数例子围绕已经有((前列DB一些字符串这里)。)存储在一个变量的字符串,但我想要的东西,其中变量首先被初始化为空。

回答

2

你想在运行时更改变量吗?在这种情况下,请查看emu8086.inc中的PRINT宏。有几个变化,你已经有了一个存储宏:

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

这取决于你想用串 这里做什么是一些例子:

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 /啊= 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)输出ady

字符串以美元符号(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)