Is there a way use lea
on an array of pointers?
For example, we have an array of strings, is it possible to do a single line with lea
instead of the two lines marked with +
?
.data
ARR DWORD STR1, STR2 ...
STR1 byte "asdad", 0
...
.code
;;;lea edx, [ARR+4*eax]
mov edx, offset ARR ;+
mov edx, [edx+4*eax] ;+
call writestring
The commented out lea
doesn't work and everything else I try doesn't even pass assembly.
You don't actually want to use LEA
since it calculates an address where you want the data contained in that address.
If you used LEA
that would give you the address to location in the array. You can't use that in writing the string. Compared to C, for example, this would be the char**
whereas you want char*
.
If you want to write the string you have to do exactly as you're doing. You're actually reading the pointer contained in the array.
So in short, LEA
is not what you want, you want the MOV
s you are already using. You may be able to shorten it to one MOV
:
mov edx, ARR[4*eax]
or with nasm
mov edx, [4*eax + ARR]