NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: nobody on May 29, 2007, 01:59:51 PM

Title: Declare Array
Post by: nobody on May 29, 2007, 01:59:51 PM
How do I declare a array existing of 8 5-byte strings?
Title: Re: Declare Array
Post by: nobody on May 30, 2007, 03:42:40 PM
Assuming that we're talking NUL-terminated ASCII strings here...

db "Better", 0
db "do", 0
db "your", 0
db "own", 0
db "homework!", 0

Precede them with labels, and stick their offsets into another
array, if you need something like C's argv.
Title: Re: Declare Array
Post by: Frank Kotler on May 30, 2007, 06:53:54 PM
D- Those strings aren't 5 bytes each ! :)

If the strings are really fixed-length (5 bytes), you can just stuff 'em in an array, and multiply the "index" by 5 (lea?) to access 'em.

my_string_array db "str01", "str02", "str03", "str04", "str05", str06", "str07", "str08"

Since the lengths are known, no need to zero-terminate them. To access string 7:

mov ecx, 6  ; we want count to start from 0!
lea ecx, [my_string_array + ecx + ecx * 4]
mov edx, 5 ; length
mov ebx, 1 ; stdout
mov eax, 4 ; __NR_write
int 80h

Dos/doze is the same idea, only WriteFile or int 21h/40h...

For the more general case of mixed-length, zero-terminated strings...

str0 db "Better", 0
str1 db "do", 0
str2 db "your", 0
str3 db "own", 0
str4 db "homework!", 0

my_string_array dd str0, str1, str2, str3, str 4

To grab "your":

mov eax, 2
lea eax, [my_string_array + eax * 4]

("dw" array for 16-bit code - and the addressing modes are... a PITA)

Print it with something that expects a zero-terminated string, or calculate strlen ourselves...

Does that help?

Best,
Frank