2014-11-23 132 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)