Author Topic: Struc as type  (Read 2011 times)

Klod

  • Guest
Struc as type
« on: January 25, 2011, 04:45:39 AM »
I have been messing around with strucs and unions lately and I came across this:
Types and Sizes in NASM
by Bryant Keller on September 5th, 2009
Code: [Select]
%idefine BYTE_size 1
%idefine WORD_size 2
%idefine DWORD_size 4
%idefine QWORD_size 8
%idefine TWORD_size 10
%idefine BYTE_define DB
%idefine WORD_define DW
%idefine DWORD_define DD
%idefine QWORD_define DQ
%idefine TWORD_define DT
 
%imacro typedef 2
%push _typedef_
%idefine %{2} %{1}
%idefine %{2}_size %{1}_size
%idefine res%{2} resb %{1}_size *
%idefine d%{2} %{1}_define
%idefine %{2}_define %{1}_define
%pop
%endm
 

Having played around with this for a while I start to see the benefits of a "stronger typed" coding style especially in larger projects. This got me thinking of defining strucs as types but so far I have failed to come up with a good solution.

What does a type consist of?
Appears to me as a symbol name and a size as well as some space allocated in the .bss section.

How would a struc have to be designed to allow it to be a type?

I am experimenting with struc something like this and was hoping that struc types could help:
struct one
   set x,4
   set y,2
   set z,16
ends one

struct two
   set a,1
   set one
ends two

struct three
   SET C,4
   SET two
ends three

The set macro will define the necessary symbols. set with 2 parameters will define two.a with size of one byte. set with 1 parameter will append a previously defined struct if defined so that two.one.x and two.one.y and two.one.z will be appended to the struct definition of two with offset of 4 bytes.  This works ok for a nesting depth of 1, fails when attempting to nest multiple levels. struct three shows that.
I'm probably doing this backwards but I hope some one may be able to point me in the right direction

Klod

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 430
  • Country: us
Re: Struc as type
« Reply #1 on: January 25, 2011, 04:34:44 PM »
Klod,

To see nested structures and unions in action get the NASMX package to see how I implemented it:

http://sourceforge.net/projects/nasmx/