Hi, is there some way that one line of code can access different parts of a struct? For example I have a code loop that gets run a lot but sometimes I want to use a different part of a struct. I could do it like this:

cmp _dwOption,1
jne @F
movzx eax,(SESSIONINFO PTR [edi+ebx]).distance
jmp _done
movzx eax,(SESSIONINFO PTR [edi+ebx]).duration

But I was wondering is there some quicker way? I thought perhaps:
movzx eax,(SESSIONINFO PTR [edi+ebx]).distance+_dwOffset
But that is not allowed, is there a better way?

Posted on 2004-08-27 08:15:00 by adamjjackson
Yes it is possible, there are a couple of different ways, for example if _dwOption is always 0 or 1 then try

mov eax,_dwOption
sub eax,1
and eax,SESSIONINFO.distance - SESSIONINFO.duration
; maybe should be SESSIONINFO.duration - SESSIONINFO.distance ???
add eax,ebx
movzx eax,(SESSIONINFO PTR [edi+eax]).distance

What that does is convert eax into a mask, if _dwOPtion if 1 or 0 the eax will be either 0h or 0FFFFFFFFh respectivly which when ANDed with SESSIONINFO.distance - SESSIONINFO.duration becomes either 0 or SESSIONINFO.distance - SESSIONINFO.duration respectivly. This when added to ebx changes the addressing to point to "duration" if _dwOption is 0.

Someone move this :)?
Posted on 2004-08-27 08:27:29 by Eóin
Hi, haven't had time to try it yet but it looks promising, thanks :)

I deleted it then reposted it in the correct forum, I put it in the heap by accident - I was reading a few things then clicked "New topic" in the wrong browser window, oops!
Posted on 2004-08-28 04:47:18 by adamjjackson
If "_dwOption" is a "case" or "index" value rather than a "flag", and your struct has DWORD members, then it shouldn't be too bad - let _dwOption=0 be the first struct member, _dwOption=1 be the second, and so on. I will assuem EDI is your "array base pointer", and EBX is the loop index. I'll also assume the _dwOption doesn't change inside the loop. So, before your loop, do "mov ebx, [_dwOption]" and "lea edi, ".

That's just one way to do it, of course - for more complex structures, you could have an array that maps _dwOption values to member offsets (I can't remember if masm has an "offsetof" or similar thing to refer to member offset, if not you might have to construct the array by hand).
Posted on 2004-08-29 13:14:05 by f0dder