What's the best way to use arrays in masm32? I am trying to create an array of POINT structures so that I can pass them to the Polygon API function. I've tried creating 6 dwords (two for each POINT) eg something like this:

p1x dw ?
p1y dw ?
p2x dw ?
p2y dw ?
p3x dw ?
p3y dw ?

and then giving them values like this:

mov p1x,100
mov p1y,50
mov p2x,50
mov p2y,100
mov p3x,60
mov p3y,90

and then invoking Polygon like this:

invoke Polygon,hdc,addr p1x,3

but this only seems to work for the first point and the rest appear in random places on the window.

Ideally I'd like to do something like this:

;declare point array

;set values
mov Points[1].x,100
mov Points[1].y,50

;invoke Polygon
invoke Polygon,hdc,addr Points,3

Any suggestions?
Posted on 2003-11-21 06:17:30 by DeX
dw = declare word
dd = declare dword

Since when do you declare dword with dw?
Posted on 2003-11-21 06:25:37 by roticv
you could also do it within a proc with locals

Posted on 2003-11-21 07:35:14 by ENF

dw = declare word
dd = declare dword

Since when do you declare dword with dw?

ah, that would explain the random values for the other two points. Also shows how new I am at this...

I tried using a local variable as you demonstrate ENF, but I couldn't get that to work either. When you create an array like that is the index 1 or 0 based? Do I store values in the array using something like this?

mov p[1].x,100

That's just a guess since I can't test it at the moment. Is that correct?
Posted on 2003-11-21 08:02:06 by DeX
this was not correct


it should be


thse both do the same thin

mov p[3].x, 100
mov p.x[3], 100
Posted on 2003-11-21 08:31:21 by ENF
Hmm, that's exactly what I tried. For some reason the values won't enter the array correctly. Eg if I do something like this:


and then this:

mov p[3].x, 100
mov ecx, p[3].x

The contents of ecx will not be 100. It will be something like 1572100. IE the low word (I think) of p[3].x becomes 100 but the rest of the contents are unchanged. Do I need to clear the array somehow before filling it with values?
Posted on 2003-11-21 10:10:54 by DeX
x and y are defined as words so either clear ecx first or use cx
Posted on 2003-11-21 10:39:13 by ENF
Be aware that the [3] is a byte offset and not a real "index". You will need to scale it by the size of the data structure...

mov p[3*(SIZEOF POINT)].x, 100

If you are using variable indexing (via a register), you may actually need to multiply it out in a separate IMUL instruction. You won't need to with POINT (it has a size of 8), but with other data structures, you might.

mov p.x, 100

The following should produce the correct result:

mov p[3].x, 100
mov ecx, p[3].x

But if you interpose some code, it definitely won't:

mov p[3].x, 100
mov p[4].x, 200 ; overwrites part of the DWORD starting at p[3].x
mov ecx, p[3].x ; retrieve altered value

Check out AddressingModes and AllAboutArrays at http://www.asmcommunity.net/board/phpwiki/.
Posted on 2003-11-21 13:33:43 by tenkey