flat assembler
Message board for the users of flat assembler.

Index > OS Construction > About Structures

Author
Thread Post new topic Reply to topic
robstat7



Joined: 03 Apr 2024
Posts: 6
robstat7 07 Oct 2025, 07:12
Hi, I want to understand the following UEFI bootloader code. I already know about the basics of the `struc` directive and I am also good at the C programming language.

Code:
format pe64 efi                                                                 
                                                                                
entry start                                                                     
                                                                                
struc UINT32 {                                                                  
        align 4 ; enusre 4-byte alignment                                       
        . dd ?  ; anon member - reserve 4 bytes of uninitialized space 
}                                                                               
                                                                                
struc void {                                                                    
        align 8                                                                 
        . dq ?  ; reserve 8 bytes of uninitialized space               
}                                                                               
                                                                                
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   
        end virtual       ; ends the virtual block                              
}                                                                               
                                                                                
struc EFI_TABLE_HEADER {                                                        
        .Signature                      void                                    
        .Revision                       UINT32                                  
        .HeaderSize                     UINT32                                  
        .CRC32                          UINT32                                  
        .Reserved                       UINT32                                  
}                                                                               
struct EFI_TABLE_HEADER
    


Here are my questions from the above code:
1. The contents of UINT32 macro consists of the `. dd ?` instruction. What does this anonymous member with only single dot mean? And what does the ? mean at the end?
2. Inside the contents of EFI_TABLE_HEADER macro, there are the members of another structure types. Is it an example of nested structures? If I instantiate this structure with `header_table EFI_TABLE_HEADER`, how it will be assembled in the output file?
3. I didn't understand this struct macro at all. Could you tell me about its usage as in `struct EFI_TABLE_HEADER` and how it works?
Also somewhere in the code, I am doing `mov rcx, [rax + EFI_SYSTEM_TABLE.ConOut] `. How does EFI_SYSTEM_TABLE.ConOut gives the right offset?

Thanks.
Post 07 Oct 2025, 07:12
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8454
Location: Kraków, Poland
Tomasz Grysztar 07 Oct 2025, 08:22
robstat7 wrote:
1. The contents of UINT32 macro consists of the `. dd ?` instruction. What does this anonymous member with only single dot mean? And what does the ? mean at the end?
The dot allows to manually define the label of the structure. In a labeled macro (the kind of macro that you define with STRUC) this lone dot is replaced with the name of the instance.

For example, if you define and instantiate labeled macro like this:
Code:
struc AINT {
        align 4
        dd ?
}
some AINT    
Then the label "some" is going to be generated automatically by fasm's preprocessor:
Code:
some:
align 4
dd ?    
However if you use your macro:
Code:
some UINT32    
then the presence of a "dot" symbol in its body suppresses the generation of automatic label and the generated text is:
Code:
align 4
some dd ?    
As for the "?", it is a special value that you can give to cells defined with data directives like DB or DD, and it means uninitialized data. You can use it anywhere where you do not care about the initial value of the data cell, and this also may help to reduce the size of produced executable, as such areas of data can be stripped from the output when they are at the end of a section.

Also, "DD ?" is exactly equivalent to "RD 1", "DD ?,?" is "RD 2" and so on.

robstat7 wrote:
2. Inside the contents of EFI_TABLE_HEADER macro, there are the members of another structure types. Is it an example of nested structures? If I instantiate this structure with `header_table EFI_TABLE_HEADER`, how it will be assembled in the output file?
You can use the PREPSRC tool from fasm's TOOLS directory to see the state of the source text after it is preprocessed and before it is passed to assembler. This is going to unroll all the nested macros, so you can test various combinations yourself. See TOOLS/README.TXT for basic guide.
Post 07 Oct 2025, 08:22
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8454
Location: Kraków, Poland
Tomasz Grysztar 07 Oct 2025, 08:37
robstat7 wrote:
3. I didn't understand this struct macro at all. Could you tell me about its usage as in `struct EFI_TABLE_HEADER` and how it works?
Also somewhere in the code, I am doing `mov rcx, [rax + EFI_SYSTEM_TABLE.ConOut] `. How does EFI_SYSTEM_TABLE.ConOut gives the right offset?
The are various "struct" macro variants floating around, so you'd need to look at the specific implementation in the headers you use, but usually they define a "default instance" of the structure like this:
Code:
virtual at 0
  EFI_SYSTEM_TABLE EFI_SYSTEM_TABLE
end virtual    
This ends up defining all the labels like EFI_SYSTEM_TABLE.ConOut with addresses starting at 0, so they are effectively offsets into structure that you can use in expressions like [rax + EFI_SYSTEM_TABLE.ConOut].
Post 07 Oct 2025, 08:37
View user's profile Send private message Visit poster's website Reply with quote
robstat7



Joined: 03 Apr 2024
Posts: 6
robstat7 07 Oct 2025, 11:45
Thanks so much. It really helped.
Post 07 Oct 2025, 11:45
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.