Board index » off-topic » About offset of structure member

About offset of structure member


2005-04-29 06:39:59 PM
off-topic2
Hi
I have declared a structure in IDEAL mode as
STRUC MyStruc
Field1 dw ?
Field2 dw ?
......
Fieldn dw ?
ENDS Mystruc
The number of the structure members can be changed during working with
project.
In program I need to use offset of the Fieldk relative to the start of the
structure. For example offset of Field3 is 4 and so on. How to build
expression such a way that its result will be the offset of given structure
member and that the code will be readable for everyone.
Grigoriev Vladimir
 
 

Re:About offset of structure member

Grigoriev Vladimir wrote:
Quote
STRUC MyStruc
Field1 dw ?
Field2 dw ?
......
Fieldn dw ?
ENDS Mystruc

The number of the structure members can be changed during working with
project.
In program I need to use offset of the Fieldk relative to the start of the
structure. For example offset of Field3 is 4 and so on. How to build
expression such a way that its result will be the offset of given structure
member and that the code will be readable for everyone.
My ASM is getting rusty, but one or more of these should work for you.
lea esi, StructureToUse
mov ax, Field3[esi]
or
mov ax, [esi]Field3
or
mov ax, [esi+Field3]
Or......
mov ecx,Field3
mov ax,[esi+ecx]
Or....
mov ax,word ptr StructureToUse.Field3
Or...
lea esi, StructureToUse.Field3
mov ax,[esi]
(TASM translates "LEA" to "MOV Offset" whenever it can, so I found it
easiest to always code LEA when I'm loading an address.)
 

Re:About offset of structure member

Hi Bob.
Myabe you didn't understand my question or I don't understand your answer.
What I need is how to get a member offset within a structure in IDEAL mode
of TASM.
I've looked through your examples and have resumed that you get a value of
a strucutre member but not an offset. For example you wrote
mov ax,[esi+Field3]
In TASM IDEAL mode this code issues an error as name Field3 is not accessed
opposite of MASM mode.
And one important remark. I don't declare a structure in data segment.
Memory for structure is allocated dinamically. Thus I have not a name of
defined data with type of my structure. I have only declared a corresponding
type.
Anyway thank you.
Vladimir Grigoriev
"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
Grigoriev Vladimir wrote:

>STRUC MyStruc
>Field1 dw ?
>Field2 dw ?
>......
>Fieldn dw ?
>ENDS Mystruc
>
>The number of the structure members can be changed during working with
>project.
>In program I need to use offset of the Fieldk relative to the start of
the
>structure. For example offset of Field3 is 4 and so on. How to build
>expression such a way that its result will be the offset of given
structure
>member and that the code will be readable for everyone.

My ASM is getting rusty, but one or more of these should work for you.

lea esi, StructureToUse
mov ax, Field3[esi]
or
mov ax, [esi]Field3
or
mov ax, [esi+Field3]

Or......
mov ecx,Field3
mov ax,[esi+ecx]
Or....
mov ax,word ptr StructureToUse.Field3
Or...
lea esi, StructureToUse.Field3
mov ax,[esi]

(TASM translates "LEA" to "MOV Offset" whenever it can, so I found it
easiest to always code LEA when I'm loading an address.)


 

{smallsort}

Re:About offset of structure member

Vladimir Grigoriev wrote:
An Ideal syntax example was given some time ago....
See Vasya Pupkin's answer.
tinyurl.com/ar3oc
(same as)
groups-beta.google.com/group/borland.public.tasm/browse_thread/thread/104f5aa0615b45a3/e30fe70414991daf
Quote
Hi Bob.
Myabe you didn't understand my question or I don't understand your answer.
What I need is how to get a member offset within a structure in IDEAL mode
of TASM.
I've looked through your examples and have resumed that you get a value of
a strucutre member but not an offset. For example you wrote
mov ax,[esi+Field3]
So, you want the offset? the offset is offset Field3
Or, you want to calculate the offset at runtime, say from an index?
mov ax,IndexN ; get the 1-based index
{
lea ax,[ax-1]*2 ;offset FieldN
} or {
dec ax ; convert to 0-base
shl ax,1 ; multiply by word size
}
Quote
And one important remark. I don't declare a structure in data segment.
Memory for structure is allocated dinamically.
{
lea si, structure ;or some other way, get the base
} or {
mov si, [structure]
}
add si,ax ; address of the data in the structure
 

Re:About offset of structure member

See also page 59 and 117 of this manual
www.ee.washington.edu/class/472/wtr01/reference/pasmug.pdf
 

Re:About offset of structure member

Grigoriev Vladimir wrote :
Quote
Hi
I have declared a structure in IDEAL mode as

STRUC MyStruc
Field1 dw ?
Field2 dw ?
......
Fieldn dw ?
ENDS Mystruc

The number of the structure members can be changed during working with
project.
In program I need to use offset of the Fieldk relative to the start of the
structure. For example offset of Field3 is 4 and so on. How to build
expression such a way that its result will be the offset of given structure
member and that the code will be readable for everyone.
I think 'offset (MyStruc).Fieldk' will do what you want.
e.g. mov ax, offset (MyStruc).Field3; generates B8 0004
-- Steven
 

Re:About offset of structure member

Hi Steven.
It is great! It works. But I don't even know how to interpretate this
expression (MyStruc).Field3?
I often used such expression as for example
mov ax,[word ((MyStruc es:bx).Field3)+WORD)]
but I had not an idea to construct the expression you pointed.
Vladimir Grigoriev
"Steven Saunderson" < XXXX@XXXXX.COM >wrote in message
Quote
Grigoriev Vladimir wrote :
>Hi
>I have declared a structure in IDEAL mode as
>
>STRUC MyStruc
>Field1 dw ?
>Field2 dw ?
>......
>Fieldn dw ?
>ENDS Mystruc
>
>The number of the structure members can be changed during working with
>project.
>In program I need to use offset of the Fieldk relative to the start of
the
>structure. For example offset of Field3 is 4 and so on. How to build
>expression such a way that its result will be the offset of given
structure
>member and that the code will be readable for everyone.

I think 'offset (MyStruc).Fieldk' will do what you want.

e.g. mov ax, offset (MyStruc).Field3; generates B8 0004

-- Steven