News:

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

code explanation

Started by fattypotato, November 05, 2008, 06:17:51 PM

Previous topic - Next topic

fattypotato

I need your expert teaching.  I coded the following structure:

question struct 4
   correct sbyte 255 dup("Z")
   answer byte  0
question ends

Then by trial and error I got this declaration to assemble:

workStruct   question   100 dup ({{0}})

It works in my prog. but I don't understand exactly what the last line of code does or how it does it???
If anyone can shed some light on this I would be grateful.

KeepingRealBusy

Fatty,

That type of structure is usually reserved as a skeleton for a real structure that is dynamically allocated, filled in and saved. The BYTE 0 is the key to note - its offset is the first byte of the answer string, but the answer length is not known at assembly time, just its offset in the array. The actual structures are accessed by pointer, and the pointers can be saved (and initialized to 0 until allocated) in an array.

Dave.

fattypotato

Dave, thank you for the reply.  I thought I was creating an array of question structures
that would be initialized to a 255 byte string of Z's followed by a zero byte?

-jim

raymond

You should be able to do what you want as follows:

workStruct   question   100 dup (<>)
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

KeepingRealBusy

Fatty,

You did, but how can you put any answer into a one byte string. The string must have a zero terminator and this leaves 0 bytes for any possible answer to be saved. To define a string with the null terminator, just specify a single unnamed byte of 0 immediately following the named string that is initialized. Better yet, just name the string with the byte count one more than the maximum number of characters you want in the actual string, and use a (0) "correct sbyte 256 dup (0)".

The way you have it defined, it appears that "correct" is an array of characters to hold some sort of question (to be filled in), and "answer" is an array of 1 character to hold the answer string, and you have 100 such structures.

I may be totally wrong in this discussion, since up to now I have not tried to assemble thus with MASM32 10.0, I have only worked with MS MASM 8.0 and its built in macros.

Raymond,

Your reply popped up while was typing. I thought that "<>" what was was needed but need more study on macros, especially since I may be using different assemblers here with different macro support levels.

Dave.

drizz

Jim, just listen to me :)

Your assumption was correct
workStruct00 question 1 dup ({{},})
with second pair of {} brackets you initialize sub structures or dup members


This is how you would normally Initialize your struct
workStruct0 question 1 dup (<'Something',1>)
Now you don't need extra brackets around 'Something' because its already a byte-array
If you need byte after byte initialization then use extra brackets
workStruct1 question 1 dup (<<'S','o','m','e',0>,1>)
If you dont care about some members you can leave them blank
workStruct2 question 1 dup (<<>,1>)

Now a more complex example
suppose you have another structure that uses first
question2 struct 4
   q1 question <>
   correct sbyte 255 dup("A")
   answer byte  0
question2 ends

you would normally Initialize it like this
workStruct3 question2 1 dup (<<"First",1>,"Second",2>)
again same rules as before
workStruct5 question2 1 dup (<<<'F','i'>,1>,<'S','e'>,2>)

one more example

questions struct 4
   q1 question <>
   q2 question <>
   q3 question <>
questions ends

workStruct6 questions 1 dup (<<'First',1>,<<'S','e'>,2>,<'Third',3>>)


ps. <> is the same as {}

Edit: Forgot to mention: "Don't use initialized structs with dup if your ML version is less than 9.00.30729.01, there is a serious bug to it. If you must dup initialized structs use:
workStruct question dup ({'123',2})
question dup ({'123',2})
question dup ({'123',2})
question dup ({'123',2})
; etc., or use:
REPT x
question dup ({'123',2})
ENDM
The truth cannot be learned ... it can only be recognized.

fattypotato

Thank you all! 

Raymond I subbed your line of code for mine and it does work as you said.

Drizz thanks for the detailed desc. it really helps.

-jim