News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

STRUCTs and Displacements (my mistake?)

Started by Paul, January 27, 2009, 04:05:23 PM

Previous topic - Next topic

Paul

Hi All,

I have defined and instantiated the follow structure:

KSDS_DATA_RECORD STRUCT
ksdsKey          DB 16 DUP 0   
ksdsPBN          DQ 0     
                               ENDS

ksdsDataRec KSDS_DATA_RECORD  ; instantiate the structure

The code that populates these two fields looks like this:

MOV [ksdsDataRec.ksdsKey+12],EAX
MOV [ksdsDataRec.ksdsPBN],ESI   

Tracing with GoBug through the code execution, I see that ksdsDataRec.ksdsKey resides at 0x4060C0. I expected ksdsPBN to reside exactly 16 bytes (the length of ksdsKey) higher at 0x4060D0. But it doesn't. GoBug shows it at 0x4060FD, 2D bytes higher than it 'should' be.

What am I doing wrong here?

As always, thanks for your help.

Cordially,

Paul


donkey

A quick test shows you're right, the structure offsets are weird...

Line 90: offset ksdsDataRec.ksdsKey = 4219572
Line 91: offset ksdsDataRec.ksdsPBN = 4219633

A difference of 61 BYTEs, however when I look at the values GoAsm assigns to the offsets I get...

Line 93: KSDS_DATA_RECORD.ksdsKey = 0
Line 94: KSDS_DATA_RECORD.ksdsPBN = 16

Which is correct. This probably has an explanation somewhere but I have no idea what it is, one for Jeremy I guess. In the interim you can add an ALIGN directive to get the proper offsets I use 16 here but any alignment should work...

ALIGN 16
ksdsDataRec KSDS_DATA_RECORD <>  ; instantiate the structure

This will take care of the problem until Jeremy can look at it.

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

donkey

This is more and more bizarre, it seems the ALIGN directive can be anywhere before the declaration in the data section, however if I place the declaration after certain things like filled structures the problem is corrected...

ksdsDataRec1 KSDS_DATA_RECORD <>
ksdsDataRec2 KSDS_DATA_RECORD <"",0>

Line 90: offset ksdsDataRec1.ksdsKey = 4219572
Line 91: offset ksdsDataRec1.ksdsPBN = 4219633 <- WRONG
Line 93: offset ksdsDataRec2.ksdsKey = 4219644
Line 94: offset ksdsDataRec2.ksdsPBN = 4219660 <- CORRECT

Switch them up and...

ksdsDataRec2 KSDS_DATA_RECORD <"",0>
ksdsDataRec1 KSDS_DATA_RECORD <>

Line 91: offset ksdsDataRec1.ksdsKey = 4219596
Line 92: offset ksdsDataRec1.ksdsPBN = 4219612 <- CORRECT
Line 94: offset ksdsDataRec2.ksdsKey = 4219572
Line 95: offset ksdsDataRec2.ksdsPBN = 4219588 <- CORRECT

However this is completely out to lunch...

ksdsDataRec1 KSDS_DATA_RECORD <>
ksdsDataRec2 KSDS_DATA_RECORD <>

Line 91: offset ksdsDataRec1.ksdsKey = 4219572
Line 92: offset ksdsDataRec1.ksdsPBN = 4219633 <- WRONG
Line 94: offset ksdsDataRec2.ksdsKey = 4219644
Line 95: offset ksdsDataRec2.ksdsPBN = 4219705 <- WRONG

I'm actually quite glad you pointed this out, it might explain a few problems I have been having when passing structures to the API that have been puzzling me lately.

EDIT >I think it might be a good idea for the time being to put ALIGN xx at the start of the data section of any program you are working on, it has solved at least one nagging problem in a project of mine, evidently due to this bug.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jorgon

Hi Paul and Donkey

The DUP first thing in the struct declaration was causing GoAsm to apply too much padding to structure (GoAsm automatically aligns every structure to a dword boundary in Win32) - but I have now revamped the coding and this version works ok on my tests - GoAsm 56.4n (attached).

Thanks very much for pointing out this bug - one of those things lurking, which only appears on debugger!




[attachment deleted by admin]
Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

Paul

Hi Jeremy and Donkey,

Thank you both for your aggressive prosecution of this bug. I have downloaded Jeremy's updated GoAsm and will test it shortly.

Two questions:

1. Should I be using the <> symbol when I instantiate a STRUCT?
2. Donkey, how did you look at the offsets assigned by GoAsm? I created a listing file in the hope that I would be able to see these offsets. But I can't see them. The instruction that initializes the ksdsPBN field looks like this:

8935[00000000]            MOV [ksdsDataRec.ksdsPBN],ESI 

I assume that the bracketed zeros in the machine instruction represent a relocatable symbol that will be resolved by the linker.

Cordially,

Paul

donkey

#5
Hi Paul,

I use the RadASM IDE, I wrote an inline debugger along the lines of vKim's for GoAsm, in this case I had simply to include the following lines in my code.

PrintDec(offset ksdsDataRec.ksdsKey)
PrintDec(offset ksdsDataRec.ksdsPBN)

For the values that GoAsm assigned to the offsets I use a feature of GoAsm, I also use it in my CoInvoke macro...

PrintDec(KSDS_DATA_RECORD.ksdsPBN)

Since KSDS_DATA_RECORD.ksdsPBN resolves as a constant with the value of the offset it is very useful, for example you can do this...

mov eax,offset ksdsDataRec
add eax,KSDS_DATA_RECORD.ksdsPBN

The debugger is available from my website, RadASM is available from http://www.radasm.com/
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable