Shuzheng
Shuzheng

Reputation: 14018

Create array of string addresses (data elements) in NASM?

Suppose I've an 3-element array in NASM

strings: dw 0x00, 0x00, 0x00

, that I want to fill with addresses of strings, also defined within the .data section, e.g.,

hello_word: db "HelloWorld!", 0.

Writing strings: dw hello_world, 0x00, 0x00 is a syntax error.

How do I populate an array with addresses, so that I can loop over it at runtime, each time incrementing the index?

Upvotes: 0

Views: 430

Answers (3)

Nicolas Lykke Iversen
Nicolas Lykke Iversen

Reputation: 4250

You could also load the effective addresses of the strings at runtime using the LEA instruction (x86):

lea eax, [_str1]
mov [_s_table], eax
lea eax, [_str2]
mov [_s_table + 0x04], eax
lea eax, [_str3]
mov [_s_table + 0x08], eax

But, probably, Frank Kotler's approach is better.

Upvotes: 1

Frank Kotler
Frank Kotler

Reputation: 3119

Ah, "-f coff"! Make your addresses dd not dw.

Upvotes: 1

Tommylee2k
Tommylee2k

Reputation: 2731

this worked for me:

segment data
 prompt_msg db "Input a string: ",0
 output_msg db "The reverse is: ",0

 stringPtr dw prompt_msg
           dw output_msg

 -->

0734:0100  49 6E 70 75 74 20 61 20-73 74 72 69 6E 67 3A 20 Input a string:
0734:0110  00 54 68 65 20 72 65 76-65 72 73 65 20 69 73 3A .The reverse is:
0734:0120  20 00 00 00 11

at 0x121: 00 00 and 00 11 are the two pointers to the strings

Upvotes: 1

Related Questions