1

Im using emu8086.

For example i have a macro called 'store' which takes a string and stores it in an array, how do i do that?

sample code:

arrayStr db 30 dup(' ')

store "qwerty"

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

Most examples i found on the internet revolve around already having the string stored in a variable (ex. string db (some string here)) but i want something where the variables get initialized empty first.

rkhb
  • 14,159
  • 7
  • 32
  • 60
ryuuuuuusei
  • 98
  • 5
  • 22

2 Answers2

2

Do you want to change a variable at runtime? In this case take a look at the PRINT-macro in emu8086.inc. A few changes and you've got a 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!", '$'
rkhb
  • 14,159
  • 7
  • 32
  • 60
1

It depends on, what you want to do with the string Here are some examples:

ASCIZ-String

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.

String input from DOS-Function 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.

String which is ready to be outputted using WriteLn (int 21h/ ah=09h)

The string ends with a dollar-sign (ASCII 36). The advantage is that your programm can output the string using a single function (int 21h/ ah=09h). The disadvantage is that the string mustn't contain another dollar-sign. Otherwise the program would interprete an earlier dollar-sign as the end of the string.

String whose length is defined in a word/byte at the beginning of the String

Unformatted String

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)
Van Uitkon
  • 356
  • 1
  • 6