The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: gavin on July 10, 2005, 05:01:56 PM

Title: Using an array of pointers?
Post by: gavin on July 10, 2005, 05:01:56 PM
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
Title: Re: Using an array of pointers?
Post by: Jeff on July 10, 2005, 05:23:57 PM
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
Title: Re: Using an array of pointers?
Post by: gavin on July 10, 2005, 05:47:39 PM
Hi thanks for your help.

So the way i'm doing it eax would hold the string? Or the address of the string?
Thanks.
Title: Re: Using an array of pointers?
Post by: Jeff on July 10, 2005, 09:39:19 PM
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?  :)
Title: Re: Using an array of pointers?
Post by: gavin on July 10, 2005, 11:05:52 PM
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.
Title: Re: Using an array of pointers?
Post by: raymond on July 11, 2005, 01:58:06 AM
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
Title: Re: Using an array of pointers?
Post by: donkey on July 11, 2005, 04:36:54 AM
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 ?
Title: Re: Using an array of pointers?
Post by: gavin on July 11, 2005, 09:01:54 AM
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.
Title: Re: Using an array of pointers?
Post by: raymond on July 11, 2005, 03:08:43 PM
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
Title: Re: Using an array of pointers?
Post by: gavin on July 11, 2005, 09:35:22 PM
Ah thanks for that Raymond .
I have much to learn with masm and asm.