Virtual Machines and structs

C

Chris Cranford

I'm trying to write a virtual machine and relies on a stack and I have a
question about how I would work with UDTs such as:

typedef struct tagTData {
unsigned long ulData;
char szData[28];
} TData;

The above structure is seen as a 32-byte block of memory space. If a variable
is defined in a function like so:

void myFunc(void) { TData data; /* do stuff here */ return; }

Essentially 32-bytes (8 4-byte/32-bit blocks) are reserved on the stack. If
the struct was used as a pointer, only 1 32-bit block that contains the memory
pointer to the structure is allocated on the stack.

Is it then up to the compiler to calculate the byte offsets for the data
structure when assembly is written when code accessing the UDT fields?

For example:

TData *pData = new TData();
pData->ulData = 1; // accessed via memptr+0 byte offset
pData->szData = NULL; // accessed via memptr+4 byte offset

TData data;
data.ulData = 1; // SP to variable data + 0 byte offset
data.szData = NULL; // SP to variable data + 4 byte offset

Thanks
Chris
 
T

those who know me have no need of my name

in comp.lang.c i read:
I'm trying to write a virtual machine and relies on a stack and I have a
question about how I would work with UDTs such as:
Essentially 32-bytes (8 4-byte/32-bit blocks) are reserved on the stack.
If the struct was used as a pointer, only 1 32-bit block that contains the
memory pointer to the structure is allocated on the stack.

your udt assumed the size of various fundamental types, something which you
should avoid if possible.
TData *pData = new TData();

this is c++ code, yet you've posted to comp.lang.c -- it would appear you
want comp.lang.c++ instead.
 
B

Barry Schwarz

I'm trying to write a virtual machine and relies on a stack and I have a
question about how I would work with UDTs such as:

typedef struct tagTData {
unsigned long ulData;
char szData[28];
} TData;

The above structure is seen as a 32-byte block of memory space. If a variable
is defined in a function like so:

void myFunc(void) { TData data; /* do stuff here */ return; }

Essentially 32-bytes (8 4-byte/32-bit blocks) are reserved on the stack. If
the struct was used as a pointer, only 1 32-bit block that contains the memory
pointer to the structure is allocated on the stack.

Is it then up to the compiler to calculate the byte offsets for the data
structure when assembly is written when code accessing the UDT fields?

For example:

TData *pData = new TData();

Ignoring the c++ism since it has no bearing on your question. The
only requirement is that pData point to an instance of the struct.
pData->ulData = 1; // accessed via memptr+0 byte offset
pData->szData = NULL; // accessed via memptr+4 byte offset

TData data;
data.ulData = 1; // SP to variable data + 0 byte offset
data.szData = NULL; // SP to variable data + 4 byte offset
When the compiler generates code to store a value in an object, the
code always uses the address of the object (ignoring register objects
for this discussion). The only question is where it gets the address
from.

In the case of pData, the code generated extracts the address from
the value stored in pData.

In the case of data, the compiler knows where data was allocated
and refers to that address "directly." For those systems where actual
address are determined only when the program is loaded for execution
(the vast majority of non-embedded systems), the loading is done by a
loader which updates all memory references to the actual addresses for
this particular execution.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top