News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Offset in Structures.

Started by AssemblyChallenge, April 23, 2015, 07:26:01 AM

Previous topic - Next topic

AssemblyChallenge

Good afternoon every one.

Probably you all remember my first post about String Arrays.

That time and acording to the manual, is perfectly fine if you do:


Invoke SomeProc, Addr (StringStruct Ptr MyArray[Edi]).MyString

; or using qWord's advice:

Invoke SomeProc, Addr MyArray[Edi].MyString


Taking the same example, oddly enough, if I try to obtain the offset with mov the compiler throws a "error A2098: invalid operand for OFFSET". The Structure and data are all global, of course. So any of the following fails to compile:


Mov Ebx, Offset (StringStruct Ptr MyArray[Edi]).MyString
Mov Ebx, Offset MyArray[Edi].MyString
Mov Ebx, Offset (MyArray[Edi].MyString)
Mov Ebx, Offset (MyArray[Edi]).MyString


What's wrong please ?

sinsi

OFFSET should resxolve to an actual address but ML doesn't know whay EDI points to.
Try using LEA instead.

Lea Ebx, (StringStruct Ptr MyArray[Edi]).MyString


dedndave

OFFSET is used when the assembler knows an absolute address
ADDR is used when it must be calculated from register contents

the ADDR operator actually generates code that uses the LEA (Load Effective Address) instruction

    lea     ebx,MyArray[edi].MyString

the address of MyArray[edi].MyString is calculated and loaded into EBX
the actual code that is generated will look something like this

    lea     ebx,[edi+nnnnnnnn]

where nnnnnnnn is the address of MyArray + the index of MyString
so, the processor has to make a run-time calculation because the contents of EDI are not known at assembly-time

AssemblyChallenge

I was aware of LEA but not sure enough in this particular case as the structure is global.  :lol:

Thank you all, is working fine now and learned the lesson :t