Search code examples
assemblymasmmasm32

Get structure size within MASM


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));

Solution

  • 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.