How to make arrays of string in assembler and work with them?
I try:
arrayOfWords BYTE "BICYCLE", "CANOE", "SCATEBOARD", "OFFSIDE", "TENNIS"
and after I want to print second word, but its dont work
mov edx, offset arrayOfWords[2]
call WriteString
but He print me all world.
arrayOfWords
is not an array, not even a variable. It's just a label that tells the assembler where it can find something, in this case a bunch of characters. Irvine's WriteString
expects a null-terminated bunch of characters as string. There are two methods to treat that bunch of characters as string array.
Search the memory for the right address to the desired string. At every null begins a new string.
INCLUDE Irvine32.inc
.DATA
manyWords BYTE "BICYCLE", 0
BYTE "CANOE", 0
BYTE "SCATEBOARD", 0
BYTE "OFFSIDE", 0
BYTE "TENNIS", 0
BYTE 0 ; End of list
len equ $ - manyWords
.CODE
main PROC
mov edx, 2 ; Index
call find_str ; Returns EDI = pointer to string
mov edx, edi
call WriteString ; Irvine32: Write astring pointed to by EDX
exit ; Irvine32: ExitProcess
main ENDP
find_str PROC ; ARG: EDX = index
lea edi, manyWords ; Address of string list
mov ecx, len ; Maximal number of bytes to scan
xor al, al ; Scan for 0
@@:
sub edx, 1
jc done ; No index left to scan = string found
repne scasb ; Scan for AL
jmp @B ; Next string
done:
ret
find_str ENDP ; RESULT: EDI pointer to string[edx]
END main
Build an array of pointers to the strings:
INCLUDE Irvine32.inc
.DATA
wrd0 BYTE "BICYCLE", 0
wrd1 BYTE "CANOE", 0
wrd2 BYTE "SCATEBOARD", 0
wrd3 BYTE "OFFSIDE", 0
wrd4 BYTE "TENNIS", 0
pointers DWORD OFFSET wrd0, OFFSET wrd1, OFFSET wrd2, OFFSET wrd3, OFFSET wrd4
.CODE
main PROC
mov ecx, 2 ; Index
lea edx, [pointers + ecx * 4] ; Address of pointers[index]
mov edx, [edx] ; Address of string
call WriteString
exit ; Irvine32: ExitProcess
main ENDP
END main
BTW: As in other languages, the index starts at 0. The second string would be index = 1, the third index = 2.