I know it just linear memory, but I want it for design reasons.


Something like what I'm trying to do:


String STRUC
handle dd <?>
len dd <?>
buf dd <?>
String ends

handle will hold a pointer to itself. I'd like len and buf to be in a sub STRUCT under String STRUCT.

Being kicked out of office now.

Thanks.
Posted on 2002-04-12 09:12:23 by ThoughtCriminal
String STRUC [DWORD]

handle dd <?>

sub STRUC
len dd <?>
buf dd <?>
sub ENDS
String ends

mov eax, MyString.sub.len
Is this what your trying?
Posted on 2002-04-12 09:18:41 by bitRAKE
Whats with the " " following the first struct definition? Is it for alignment?

:NaN:
Posted on 2002-04-12 10:11:47 by NaN
yes NaN it is
Syntax: name STRUCT [, NONUNIQUE]
fielddeclarations
name ENDS
Posted on 2002-04-12 10:35:41 by NEMO
Yes Bitrake, that exactly what I was looking for. Thanks for the example.

NaN, yes, it is for alignment. I got it from this file. I think I found this in my VC6 install.


Handy for finding what error codes mean, and other stuff.
Posted on 2002-04-13 01:15:14 by ThoughtCriminal


Sub STRUC
len dd <?>
buf dd <?>
Sub ENDS

String STRUC [DWORD]
handle dd <?>
sub Sub<?>
String ends


The above works.

How bout this?


String STRUC [DWORD]
handle dd <?>
sub Sub<?>
ary dd <?> DUP
String ends


Opps I accidently hit post, so I'll post this before I try anyway.
I'm wondering the line:

ary dd <?> DUP would work? I want the compiler to build any variable declared as 'String' to be able to specify the array size at build(compile) time.
Posted on 2002-04-13 04:02:32 by ThoughtCriminal
Heres the in and out of it all (Ps: Thanx for the alignment confirm, never looked up Struct before :) ) :

You can have a struct:


MyPoint STRUCT
x dd ?
y dd ?
MyPoint ENDS


This is simple enough. I can be used in the following ways:


[b].data[/b]
APoint MyPoint <0,0>
BPoint MyPoint <12, -2>

[b].data?[/b]
CPoint MyPoint <>

[b].code[/b]

lea edx, APoint
; Get X and Y, with two acceptable methods
mov eax, [edx].MyPoint.x
mov ecx, (MyPoint PTR [edx]).y

; if using a point alot, it may be better to do this
assume ebx:PTR MyPoint
lea ebx, CPoint
mov [ebx].X, 13
mov [ebx].Y, 0
...
assume ebx:NOTHING


These are all acceptable ways of dealing with a single struct. To bet more interesting, lets make a 2D Vector:


MyVector STRUCT
head MyPoint <>
tail MyPoint <>
MyVector ENDS

This uses structs in a struct, where we want a point called head, and a point called tail. Each point must have x,y coordinates, so MyPoint is well suited. To access this in code:


[b].data[/b]
BVector MyVector < <1,2> , <1,0> >

[b].data?[/b]
AVector MyVector <>

[b].code[/b]
lea edx, BVector
mov eax, [edx].MyVector.head.x
mov (MyVector PRT [edx]).tail.y , eax


Now You can see the difference in initialztion schemes depending on what segment your allocating memory for structured use.

Now what if you wanted an array of structures to work with. This can be done as well, but NOTE: the label only applies to the first one! So its up to you to keep memory aligned...


[b].data?[/b]
; Allocate memory for 12 * SIZEOF MyVector, uninitialized.
Vectors MyVector 12 dup( <> )

[b].code[/b]
mov ecx, 12 ; 12 vectors to initialize
lea edx, Vectors
@@:
; set all vector heads to 1,0 : tails to -3, 2
mov [edx].MyVector.head.x , 1
mov [edx].MyVector.head.y , 0
mov [edx].MyVector.tail.x , -3
mov [edx].MyVector.tail.y , 2
[i]; move to next Vector in array[/i]
add edx, SIZEOF MyVector
dec ecx
jnz @B


To make a string you can use arrays as well. Also, a neat trick is that you *DONT* have to keep lables in nested structures. Lets make a Labled Vector, just like MyVector except it will have a 16 byte field to hold a lable for it:


LabelledVector STRUCT
[b][i] MyVector <>[/i][/b]
VectorName db 16 dup( <> )
LabelledVector ENDS

[b].data[/b]
LblVect LabelledVector <>

[b].code[/b]
lea edx, LblVect.VectorName
inovke SetTextInNamePointer, edx

lea edx, LblVect
mov [edx].LblVect.head.x , 0
mov [edx].LblVect.head.y , 0
mov [edx].LblVect.tail.x , 0
mov [edx].LblVect.tail.y , 0

Notice I didnt add a name for the label. This is a basic form of inheritance, where the new structure inherits properties of another, and forms a uniquely new structure. It allows us to assume things with the structures that inherit others like this proc:
[b].code[/b]

VectorLength PROC lpVect:DWORD

mov edx, lpVect
assume edx:PTR MyVector

mov eax, [edx].head.x
sub eax, [edx].tail.x
mov ecx, [edx].head.y
sub ecx, [edx].tail.y

assume edx:NOTHING
invoke PathagreonsTheorem, edx, eax
return eax
VectorLenght ENDP

Now, since both structures begin the same, IE have the exact same layout for the fist four DWORDS (head.xy, tail.yx), this proc will work flawlessly on either the LabelledVector pointer, or a MyVector Pointer. However, if the text came first, this would not hold, as the # of bytes to get to "head.x" is ZERO (originally) and 16 (for the latter with a text field of 16 bytes preceeding the offset).

In this case, if you tried the above proc. It would then take the X,Y data from the 16 bytes of text; this makes sense, after all masm is told to assume it was simply a MyVector anyways ;)

I think i will hold the tutorial at here.. i think i've covered all the various corners of structures.. so play and have fun :grin:

:alright:
NaN
Posted on 2002-04-13 06:07:19 by NaN
EGAAD!! Thanks forthe ins and outs of structs. That dont need the label thing is kinda neat.


nm
Posted on 2002-04-13 11:10:01 by ThoughtCriminal
I hope i addressed your array concerns better?

As well, i think i should also give the free advice to avoid putting alot of stuctured memory objects in .data or even .data? . Its fine for 10 or so, but if you have *alot* to be working with its probabaly better to allocate some heap memory and play with it (IMHO).

To allocate structures from the heap, simply use the SIZEOF command when entering how many bytes you want.

Ie) SIZEOF MyVector , will allocate memory sized for one Vector
Ie) 12 * SIZEOF MyVector, will allocate enough for an array of 12 vector objects... etc.

Anywho.. just thought i would toss out some more free advice.
:alright:
NaN
Posted on 2002-04-13 17:01:37 by NaN
Can I get one more little peice of free advice?

You post about does not make the syntax clear to me how to use SIZEOF to create a structure on the heap.

Currently the only way I know to create a named variable is using .data/.data?, so allocating from the heap interests me greatly.

Could I get a quick example of using SIZEOF with a structure to allocate from the heap? Just to see the proper syntax?
Thanks.
Posted on 2002-04-14 01:13:27 by ThoughtCriminal
sure...



invoke GetProcessHeap
invoke HeapAlloc, eax, NULL, SIZEOF MyVector
mov lpVector, eax

...

; Before you exit your program...
invoke GetProcessHeap
invoke HeapFree, eax, NULL, lpVector


Enjoy... (all the questions you've been asking lately is basically a HOTO make out OOP model. They all hit on key issues in making an objects orientated structure :) )

:alright:
NaN
Posted on 2002-04-14 03:56:05 by NaN
Your example is very interesting. I'm not working on an OOP model, but I am using stuff from OOP methodologies, which I know very little about, except I'm trying to make and object that contains data and functions that can be instanced.

Thanks.:grin:

Hmmm, GetProcessHeap, I may have to change my constructor from HeapCreate....

Another way to put what I'm trying to do is create a data type that contains functions to act on the data type. Every instance will contain a pointer to the functions that act on it. One could change the pointer to a different set of function if they wished.
Posted on 2002-04-14 07:34:20 by ThoughtCriminal
Well weather you realize it or not, your making an Object, for OOP programming :)

If you want to post your solution, i would be interested in seeing your approach when you are done. As well, you might want to look at our model (Thomas and I), but i aslo dont want to taint your own ideas with our solutions.

Good Luck..
:alright:
NaN
Posted on 2002-04-14 15:48:03 by NaN
... and dont forget PhatObjects ;)

btw NaN, i have finished the .lib version
will be posted soon
Posted on 2002-04-16 08:38:03 by exzito