How is it possible to access a structure inside of a structure. I was thinking at first maybe a...
struct1.struct2.value ; doesn't work
Is it possible for someone to post an easy to read example on this? I hope I don't have to access the values address and go from there. Thanks for any help you guys are the best!
SECOND STRUCT
onedword DD ?
SECOND ENDS
FIRST struct
...
other SECOND <>
FIRST ends
.data
mystruct FIRST <>
.code
mov eax,mystruct.other.onedword
;--------------------- other style -------------------------------------------
FIRST STRUCT
chose dd ?
STRUCT SECOND
onedword DD ?
ENDS
FIRST ENDS
.data
one FIRST <>
.code
mov eax,one.SECOND.onedword
Quote from: cozofdeath on October 16, 2011, 04:48:29 PM
struct1.struct2.value ; doesn't work
Which simply means you haven't tried hard enough. It does work.
Example:
mov eax, [ebx.TEXTRANGE.CHARRANGE.cpMin]
Quote
mov eax, [ebx.TEXTRANGE.CHARRANGE.cpMin]
Which simply means you haven't tried hard enough
Hum !?
are you sure your sample work ????????
Quote from: ToutEnMasm on October 16, 2011, 05:11:49 PM
Quote
mov eax, [ebx.TEXTRANGE.CHARRANGE.cpMin]
Which simply means you haven't tried hard enough
Hum !?
are you sure your sample work ????????
Yes.
So ebx is an instance of the main structure and TEXTRANGE and CHARRANGE are not instances but are under the "children" of the main structure? and then cpMin is the value? Sorry I'm just trying to understand, I've been trying with the example you gave but it just keeps saying "ERROR A2006: undefined symbol."
include \masm32\include\masm32rt.inc
.code
start: inkey "ok"
exit
mov eax, [ebx.TEXTRANGE.chrg.cpMin]
end start
This one works with ml.exe - the one posted above only with Jwasm. Sorry.
It's cool I finally got it working. It was one of my dumb moments. I was actually looking at a previous post by you (jj2007) on this forum in the beginning of 2010 and bam I realized I was using one of the structure types not the name of it. So I looked up the name in the .inc file and it working perfect! :clap:
I thought to use a register to easily access structure members you needed to use the "assume" directive in order for the example cozofdeath provided to work?
_TEST STRUCT
Member db ?
_TEST ENDS
.data
MyStruct _TEST <0> ; inits Member as null
.code
start:
push ebx ; preserve register
mov ebx,MyStruct
assume ebx:_TEST
mov eax,[ebx].Member ; moves null to eax
assume ebx:NOTHING
pop ebx ; restore register
ret
first, you do not have to use a register to access structure members
mov al,MyStruct.Member
second, if you do use a register, you have 2 options
1) you can use ASSUME, then the assembler knows the type for that register is a PTR to a _TEST structure
2) you can avoid using ASSUME, and explicitly tell the assembler the type
i prefer method 2, unless i am going to reference the structure in many lines of code (using assume can save typing)
however, if you want to use ASSUME...
ASSUME EBX PTR:_TEST
mov ebx,offset MyStruct
mov al,[ebx].Member
ASSUME EBX:Nothing
be sure to tell the assembler to assume nothing about ebx before you try to use it for something else :P
the simpler method...
mov ebx,offset MyStruct
mov al,[ebx]._TEST.Member
no problems "un-assuming" ebx :U
try it all 3 ways and see what code is generated
if i am going to reference members of a structure more than a few times, i try to use a register
it makes for faster, smaller code
if i am only going to reference it once - maybe twice - i will address it directly
What about
mov ebx,offset MyStruct
Mov al, (_test ptr [ebx]).member
I think that's it.
@Gunner: I think that is the "raw" version of it.
@Dave: thanks for the examples (I realized my code wouldn't work as written). I'll have a play tomorrow - I have stayed up too late as usual. ::)
there are a number of ways of stating the same thing :P
the assembler adds for brackets, periods, plus signs
you can make a very complex looking line of code, and the assembler evaluates it into something simple