Board index » off-topic » How to access array element in struct member

How to access array element in struct member


2004-04-03 02:50:37 AM
off-topic15
In the following code szCSDVersion is a struct member holding a
string. I need to have access to the second byte of that string.
This is the working MASM version:
cmp [esi].OSVERSIONINFOEX.szCSDVersion[1], 'A'
When I port it to a TASM version:
cmp [(OSVERSIONINFOEX esi).szCSDVersion[1]], 'A'
I get a "Need right square bracket" error. I've tried replacing [1] with (1) and <1>, but to no avail. How can this be solved?
Thanks
 
 

Re:How to access array element in struct member

Apprentice wrote:
Quote
cmp [(OSVERSIONINFOEX esi).szCSDVersion[1]], 'A'

I get a "Need right square bracket" error. I've tried replacing [1] with (1) and <1>, but to no avail. How can this be solved?
Yeah, that's C syntax, use ASM syntax: + 1 as in
cmp [(OSVERSIONINFOEX esi).szCSDVersion+1], 'A'
or
cmp byte ptr[ esi.szCSDVersion +1], 'A'
 

Re:How to access array element in struct member

Nope, I don't think this has anything to do with C syntax.
Your first solution gives me the same error I originally had, and your second solution throws a "Pointer expression needs brackets" error.
I hope someone has the answer, because everything I tried so far was a failure.
 

{smallsort}

Re:How to access array element in struct member

Hi Apprentice,
[...]
Quote
I hope someone has the answer, because everything I tried so far was a
failure.
Try to modify Bob's solution as follows:
cmp [1+(OSVERSIONINFOEX esi).szCSDVersion],'A'
or using additional parenthesis:
cmp [((OSVERSIONINFOEX esi).szCSDVersion)+1],'A'
 

Re:How to access array element in struct member

Apprentice wrote:
Quote
Nope, I don't think this has anything to do with C syntax.
Indexing by using [1] is C syntax.
The ASM equivelant is +1 (unless you are indexing words, then it
would be +2, etc.)
Quote
Your first solution gives me the same error I originally had, and your second solution throws a "Pointer expression needs brackets" error.
Then you could move the +1 outside:
cmp byte ptr[ (OSVERSIONINFOEX esi).szCSDVersion ] +1 ,'A'
Or do as Vasya said.
Quote
I hope someone has the answer, because everything I tried so far was a failure.
In ASM there's always a way...
As a last resort:
inc esi
cmp byte ptr[ (OSVERSIONINFOEX esi).szCSDVersion ] ,'A'
dec esi ;if you want to preserve esi
If these things don't fix it, then you need to take a good look at
your definition of OSVERSIONINFOEX and szCSDVersion. The 2 errors you
have shown are about mismatching [ ], either too many or too few.
(Original error) "Need right square bracket"
Means: No Right Bracket.
(Second error) "Pointer expression needs brackets"
Means: No Left Bracket.
Given that we are using the proper number of [ ] in the above code,
that would mean that something in the definition of OSVERSIONINFOEX or
szCSDVersion is messing it up.
In non-IDEAL mode, you should be able to do this:
cmp byte ptr[ esi + szCSDVersion +1 ] ,'A'
if szCSDVersion is properly defined.
 

Re:How to access array element in struct member

Thanks Bob, I've tried Vasya's solution and it works.
Quote
If these things don't fix it, then you need to take a good
look at your definition of OSVERSIONINFOEX and szCSDVersion.
For your information: OSVERSIONINFOEX is an existing structure
in the Windows API, so every programmer using the API has to
live with any design flaws in it. In this case, however,
there's no design flaw whatsoever. It's just TASM that's
nagging me, while in MASM this can be handled smoothly.
Second, I'm using ideal mode, so anything like
cmp byte ptr[ ....
doesn't work there. And I really don't feel like switching
between TASM and MASM mode all the time.
Thanks again
Regards
Apprentice
 

Re:How to access array element in struct member

Apprentice wrote:
Quote
For your information: OSVERSIONINFOEX is an existing structure
in the Windows API, so every programmer using the API has to
live with any design flaws in it.
Ever programmer uses the same API is right, yes, but not every
programmer uses the same ASM headers. Your headers (compiler specific
definition of the API) _could_ be "wrong" for Tasm.
Quote
Second, I'm using ideal mode, so anything like

cmp byte ptr[ ....

doesn't work there. And I really don't feel like switching
between TASM and MASM mode all the time.
Docs say "byte ptr" works the same in Ideal and Masm modes.
I prefer to tell the compiler exactly what I want it to do, not rely
on it to do the "right" thing, as you just never know when the "right"
thing isn't what you intended.
 

Re:How to access array element in struct member

Quote
Your headers (compiler specific definition of the API)
_could_ be "wrong" for Tasm.
I see. What headers are you using then? I only have the include
file that came with TASM (windows.inc), and I add constants,
structs, and so on when I need them from the C/C++ header files
of the C++Builder package.
Anyway, this is my implementation of the OSVERSIONINFOEX struct:
STRUC OSVERSIONINFOEXA
dwOSVersionInfoSize DD ?
dwMajorVersion DD ?
dwMinorVersion DD ?
dwBuildNumber DD ?
dwPlatformId DD ?
szCSDVersion DB 128 dup (?)
wServicePackMajor DW ?
wServicePackMinor DW ?
wSuiteMask DW ?
wProductType DB ?
wReserved DB ?
ENDS
As far as I can tell the struct is the same as the one in the
MSDN Platform SDK Documentation (or the online version on
the MSDN web site). If you think this is wrong, let me know.
Quote
Docs say "byte ptr" works the same in Ideal and Masm modes
That's not what I ment. I'm saying that
cmp byte ptr[ (OSVERSIONINFOEX esi).szCSDVersion ] +1 ,'A'
doesn't work in ideal mode (I don't know about masm mode, I
never use it. I only use masm mode when I'm using MASM, not
TASM.)
In ideal mode, you must use something like this:
cmp [BYTE PTR (OSVERSIONINFOEX esi).szCSDVersion].....
the "byte ptr" must be inside the square brackets, or tasm32
throws an error when it compiles the code.
 

Re:How to access array element in struct member

Apprentice wrote:
Quote
I see. What headers are you using then?
I made my own from scratch. Not dificult in Tasm (masm) mode.
Something like:
extrn GetConsoleMode:near
extrn SetConsoleMode:near
extrn SetConsoleCtrlHandler:near
extrn SetConsoleTitleA:near
And a few structs and equ's. No big deal.
Quote
Anyway, this is my implementation of the OSVERSIONINFOEX struct:
Yeah, that's fine. The error messages were suggesting otherwise to
me. Guess it was just Ideal mode messing with us.
Quote
That's not what I ment. I'm saying that

cmp byte ptr[ (OSVERSIONINFOEX esi).szCSDVersion ] +1 ,'A'

doesn't work in ideal mode
In ideal mode, you must use something like this:

cmp [BYTE PTR (OSVERSIONINFOEX esi).szCSDVersion].....

the "byte ptr" must be inside the square brackets, or tasm32
throws an error when it compiles the code.
How very odd. Beside all the extra typing involved with Ideal, that's
another reason not to use it. I really hate it when you have to jump
through hoops to make a compiler happy.
If you write a lot of Asm, all that extra typing adds up.