News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Using an array of pointers?

Started by gavin, July 10, 2005, 05:01:56 PM

Previous topic - Next topic

gavin

Hi guys.

Say i have the following.


        server db "255.255.255.255:65535",0
        ip0 db "213.40.198.1:27020",0
        ip1 db "213.40.198.1:27040",0
        ip2 db "213.40.198.1:27045",0
        ip3 db "213.40.198.1:27035",0
        ip4 db "213.40.198.1:27030",0
        ip5 db "213.40.198.1:27025",0
        ip6 db "213.40.198.1:27015",0
       
        Iparray DWORD ip0, ip1, ip2, ip3, ip4, ip5, ip6   


Is this correctly made out.
What i'm trying to do is have an array of pointers Iparray to point to the ip0 ip1 ip2 etc variables.

Im using it in this context.


.elseif uMsg == WM_LBUTTONDBLCLK
     
       
        xor ecx, ecx ; Clear ECX

invoke SendMessage,hWnd,LB_GETSEL, ecx,0
cmp eax, 0            ; compare eax to 0
jne found             ; jump to found if not equal

found:
mov eax, [ip+4*ecx] ;eax now holds the address of the selected item right?



Eax now holds the address of an item in the array ,which is a pointer to the variable ip0 for example.
How can i get the data at  ip0 into a variable temp db "255.255.255.255:65535",0

Thanks

Jeff

unfortunately, AFAIK, that wouldnt work out the way you'd expect it.  rather than traversing the array of pointers, it would traverse the strings.  if you created a pointer to Iparray, it should work out like you'd expect it.

        server db "255.255.255.255:65535",0
        ip0 db "213.40.198.1:27020",0
        ip1 db "213.40.198.1:27040",0
        ip2 db "213.40.198.1:27045",0
        ip3 db "213.40.198.1:27035",0
        ip4 db "213.40.198.1:27030",0
        ip5 db "213.40.198.1:27025",0
        ip6 db "213.40.198.1:27015",0

        Iparray DWORD ip0, ip1, ip2, ip3, ip4, ip5, ip6
        ip DWORD Iparray
.
.
.
        MOV eax,[ip+4*ecx]      ;eax contains the pointer to the string

gavin

Hi thanks for your help.

So the way i'm doing it eax would hold the string? Or the address of the string?
Thanks.

Jeff

i probably should have asked what "ip" was in your example, you might have had it right at first.  ;)  but from the sounds of your problem, it would have needed a little change.  if "ip" was the same as "Iparray", it would look into the string and put in a DWORD of bytes of the string.  ultimately, you would want the pointer to your array of pointers.  so after thinking about it a little more, this would provide an equivalent solution:

mov eax, [Iparray+4*ecx]

im sorry if i might have read your question wrong but the problem is utilizing an array of pointers right?  :)

gavin

Hi Jeff.

No prob man i confuse myself alot.



mov eax, [Iparray+4*ecx]

Ya this seems to make sense to me.

My problem really is not the array, my problem is actually after i have the pointer to my array item in eax,
how do i get the data into a variable.

  xor ecx, ecx ; Clear ECX

invoke SendMessage,hWnd,LB_GETSEL, ecx,0
cmp eax, 0            ; compare eax to 0
jne found             ; jump to found if not equal

found:
mov eax, [Ip+4*ecx]      ; thanks for that ;)


Thanks alot.

raymond

First, there is no need to zero ECX before sending the message.

invoke SendMessage,hWnd,LB_GETSEL, 0,0

Secondly, unless specified otherwise in the description of APIs, returned values are always in EAX and not in any other register.

mov eax, Iparray[eax*4]

would put the address (pointer) of the required null-terminated string into EAX (you could use any other register you prefer to receive that pointer.)

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

donkey

The format to use this type of addressing is :

[BASE+INDEX*SCALE]

Where:
BASE is the address of the start of the array
INDEX is the entry number you are interested in
SCALE is the size of each entry -> 1,2,4 or 8 for BYTEs, WORDs, DWORDs or QWORDs respectively

In your case, when you get the pointer you can use it directly in any API function that requires a string...

mov eax, Array[ecx*4]
invoke SendMessage, hEdit, WM_SETTEXT, 0, eax

What type of "variable" are you trying to get the data into ?
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

gavin

Raymond.
Thanks for the corrections.
In future i will use eax like you said.
I have a question also for you and Donkey.
Why is
mov eax, Array[ecx*4]
Array outside the brackets here?
And Donkey i'm trying to get the string into this

server db "255.255.255.255:65535",0

Thanks alot guys.

raymond

Quotemov eax, Array[ecx*4]
Array outside the brackets here?

I code according to the MASM syntax. When you use a variable name, it does not need to be inserted within brackets to indicate that it is equivalent to its content. So,
mov eax,array
means moving to EAX the current value of the variable.

mov eax array[8]
would mean moving to EAX the dword starting at offset 8 of the array variable.

The same would apply when you use other accepted nomenclature for the offset, such as:
mov eax,array[edi+ebx*4+8]

Note that the above may not be acceptable according to the syntax of other assemblers.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

gavin

Ah thanks for that Raymond .
I have much to learn with masm and asm.