Get structure size within MASM

5.6k views Asked by At

I am wondering how to get the size of a structure at compilation time in MASM.

Here is the C equivalent code.

struct Point
{
  int x;
  int y;
};

printf("Point Stucture size %d\n", sizeof(struct Point));
2

There are 2 answers

0
Michael On BEST ANSWER

I'm assuming that you'll be declaring the struct in your assembly code.
According to the masm32 documentation you've got the following operators available to you:

LENGTHOF variable

SIZEOF variable

SIZEOF type

LENGTH expression

SIZE expression

Description

The LENGTHOF operator returns the number of data items allocated for <variable>. The SIZEOF operator returns the total number of bytes allocated for <variable> or the size of <type> in bytes. For variables, SIZEOF is equal to the value of LENGTHOF times the number of bytes in each element.

The LENGTH and SIZE operators are allowed for compatibility with previous versions of the assembler. When applied to a data label, the LENGTH operator returns the number of elements created by the DUP operator; otherwise it returns 1. When applied to a data label, the SIZE operator returns the number of bytes allocated by the first initializer at the <variable> label.

The LENGTHOF and SIZEOF operators in MASM 6.1 handle arrays much more consistently. These operators return the number of data items and the number of bytes in an initializer.

See masm32.chm in the masm32/help directory.

0
Quentin Rufin On

MASM provides a SIZEOF keyword.

For a Point Structure like the following one:

Point STRUCT
x DWORD ?
y DWORD ?
Point ENDS

You can use SIZEOF like this.

SIZEOF Point

SIZEOF Point will be replaced by the number 8 in this example.