flat assembler
Message board for the users of flat assembler.

Index > Main > How to implement C style union in FASM Assembly?

Author
Thread Post new topic Reply to topic
robstat7



Joined: 03 Apr 2024
Posts: 13
robstat7 19 Nov 2026, 06:30
Given a C structure like this:

Code:
struct dinode {
        union {
                uint32_t di_freelink;
                uint32_t di_dirdepth;
        };
        union {
                 uint32_t di_a;
                 uint16_t di_b;
        };
        union {
                struct {
                        int64_t di_db
                            [12];
                        int64_t di_ib
                            [3];
                };
                char    di_shortlink
                    [(12 + 3) * sizeof(int64_t)];
        };
};
    


I am creating its unions like this:

Code:
; Helper macro for definition of relative structure member offsets

macro struct name {
  virtual at 0    ; Starts a virtual block at memory address 0
    name name     ; Creates an instance of struc name with label name
    sizeof.#name = $ - 0
  end virtual     ; Ends the virtual block
}

struc dinode {
  .union1                         dd ?
  .di_freelink                    equ .union1
  .di_dirdepth                  equ .union1

  .union2                         dd ?
  .di_a                              equ .union2      ; 4 bytes
  .di_b                              equ .union2      ; 2 bytes

  .union3                         db 120 dup (?)
  virtual at .union3          ; ???
    .di_db  db 96 dup (?)
    .di_ib   db 24 dup (?)
   end virtual
   .di_shortlink                 equ .union3
}
struct dinode
    


For the second union, are comments as 4 bytes and 2 bytes enough? And for the third union, should I create a virtual block? Right now the virtual block is giving me an assembler error! Help.
Post 19 Nov 2026, 06:30
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8583
Location: Kraków, Poland
Tomasz Grysztar 19 Nov 2026, 06:46
robstat7 wrote:
For the second union, are comments as 4 bytes and 2 bytes enough?
Not really, because the assembler does not look into comments and so it cannot infer the sizes. Better use LABEL AT:
Code:
  label .di_a : dword at .union2
  label .di_b : byte at .union2    

There is also VIRTUAL, and the manual mentions its usefulness to create unions:
Code:
  .di_a dd ?
  virtual at .di_a
    .di_b db ?
  end virtual    
Post 19 Nov 2026, 06:46
View user's profile Send private message Visit poster's website Reply with quote
robstat7



Joined: 03 Apr 2024
Posts: 13
robstat7 19 Nov 2026, 07:13
Thank you so much. It really helped.
Post 19 Nov 2026, 07:13
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1237
Location: Belarus
DimonSoft 19 Nov 2026, 11:55
But unions used to be implemented in macro\struct.inc and even described in FASM.pdf, AFAIR. Something along the lines of
Code:
struct MyStruct
  field1 dd ?
  field2 dw ?
  union
    field3 dd ?
    struct
      Low dw ?
      High dw ?
    ends
  ends
ends    

If my memory serves me.

Is there really any reason to avoid those? Might not be the best syntax ever, but works. (Although, I guess, I never used them myself, except, maybe, standard .inc’s from EQUATES folder.)
Post 19 Nov 2026, 11:55
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21214
Location: In your JS exploiting you and your system
revolution 19 Nov 2026, 12:10
OP is using raw fasm syntax, not the processed macro versions.
Post 19 Nov 2026, 12:10
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.