how can I get the size of an assembly language structure

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how can I get the size of an assembly language structure

2,736 Views
michaelhuslig
Contributor IV

I decided to use a structure form for data for my xgate program.  But I haven't found an easy way to get the size of the structure ("sizeof()").  I guess I could add a byte just before the end of the structure, eg. SIZE DS 1.  But that seems wasteful.

0 Kudos
Reply
3 Replies

2,581 Views
kef2
Senior Contributor V

C sizeof() can determine memory size of fully defined object. Do you have some typedef for your asm struct? If not and you want just size, then use extra end label and take address difference between two labels, like this

            XDEF   astruct, astructend

astruct     DS.B   1
            DS.B   5
astructend

#include <string.h>

extern char astruct, astructend;

  memset(&astruct, 0, &astructend - &astruct); // "struct" size to clear is 6

0 Kudos
Reply

2,581 Views
michaelhuslig
Contributor IV

First of all, everything is in assembly language.  The following works although it can be prone to mistakes if altered.  The structure is defined in an .inc file included by each .asm file.  It would be nice if there was a sizeof operator in the assembly language implementation

;MAXIMUM SIZE OF STRUCTURE IS 32 BYTES FOR 5 BIT OFFSET ADDRESSING
MY_STRUCT:    STRUCT
VECTOR    DS    2
BIT_CNT        DS    1
DATA_BYTES        DS    4

; ADD VARIABLES AS NEEDED
        ENDSTRUCT

;be careful here if structure is modified
SIZEOF_MY_STRUCT    EQU    MY_STRUCT->DATA_BYTES + 4 - MY_STRUCT->VECTOR

   IF   SIZEOF_MY_STRUCT > 32

   ERR

   ENDIF

Mike

0 Kudos
Reply

2,581 Views
kef2
Senior Contributor V

Thanks for clarifying it. Yes, no sizeof operator, as well dummy end-of-struct label in struct declaration without DS or something seems being ignored.

But since -> notation works for struct "typedef", you can declare another struct, which would include whole your struct and additional "theend" field

MY_STRUCT_size:    STRUCT
msstart   TYPE MY_STRUCT

msstop   DS.B 1

EDED
        ENDSTRUCT

 


SIZEOF_MY_STRUCT    EQU    MY_STRUCT_size->msstop - MY_STRUCT_size->msstart

0 Kudos
Reply