Structures

As a developer, I believe you would agree that most of the time we are not working with arrays of uniform data (I am definitely not underestimating the power of a regular array). Since data may be anything, starting with 8-bit numbers and ending with complex structures, we need a way to describe such data for the assembler, and the term structure is the key. Flat Assembler, just as any other assembler, lets us declare structures and treat them as additional types of data (similar to the typedef struct in C).

Let's declare a simple structure, an entry of a string table, and then see what is what:

struc strtabentry [s]
{
.length dw .pad - .string ; Length of the string
.string db s, 0 ; Bytes of the string
.pad rb 30 - (.pad - .string) ; Padding to fill 30 bytes
.size = $ - .length ; Size of the structure (valid
; in compile time only)
}
The dot (.) preceding the names of members of the struct denotes that they are part of a larger namespace. In this specific case, the name .length belongs to strtabentry.

Such a declaration would be equivalent to the following one in C:

typedef struct
{
short length;
char string[30];
}strtabentry;

However, while in C, we would have to initialize the variable of the type strtabentry, like this:

/* GCC (C99) */
strtabentry my_strtab_entry = {.length = sizeof("Hello!"), .string = {"Hello!"} };

/* MSVC */
strtabentry my_strtab_entry = {sizeof("Hello!"), {"Hello!"} };

In Assembly, or to be more precise, when using Flat Assembler, we would initialize such a variable in a simpler way:

my_strtab_entry strtabentry "Hello!"

Either way, the structure is 32-bytes in size (as the string buffer is statically allocated and is 30 bytes) and has only two members:

  • length: This is the word size integer containing the length of the string, plus 1 for a null terminator
  • string: This is the actual text
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset