8.9. Structs in Assembly

A struct is another way to create a collection of data types in C. Unlike arrays, structs enable different data types to be grouped together. C stores structs like single dimensional arrays, where data elements (fields) are stored contiguously.

Let’s revisit the studentT struct from Chapter 1:

struct studentT {
    char name[64];
    int  age;
    int  grad_yr;
    float gpa;
};

struct studentT student;

Figure 1 shows how the student struct is laid out in memory. For the sake of example, assume that student starts at address x0. Each xi denotes the address of a field.

structArray
Figure 1. The memory layout of the student struct.

Each field is stored contiguously next to each other in memory in the order in which they are declared. In [structArray6] the age field is allocated at the memory location directly after the name field (at byte offset x64), and is followed by the grad_yr (byte offset x68) and gpa (byte offset x72) fields. This organization enables memory-efficient access to the fields of the struct.

To understand how the compiler generates assembly code to work with structs, consider the function initStudent():

void initStudent(struct studentT *s, char *nm, int ag, int gr, float g) {
    strcpy(s->name, nm);
    s->grad_yr = gr;
    s->age = ag;
    s->gpa = g;
}

The initStudent() function uses the base address of a student T struct as its first parameter, and desired values for each field as its remaining parameters. The listing below depicts this function in assembly. In general, parameter i to function initStudent() is located at stack address (ebp+8)+4 x i.

<initStudent>:
 <+0>:     push   %ebp                    # save ebp
 <+1>:     mov    %esp,%ebp               # update ebp (new stack frame)
 <+3>:     sub    $0x18,%esp              # add 24 bytes to stack frame
 <+6>:     mov    0x8(%ebp),%eax          # copy first parameter (s) to eax
 <+9>:     mov    0xc(%ebp),%edx          # copy second parameter (nm) to edx
 <+12>:    mov    %edx,0x4(%esp)          # copy nm to esp+4
 <+16>:    mov    %eax,(%esp)             # copy s to top of stack (esp)
 <+19>:    call   0x8048320 <strcpy@plt>  # call strcpy(s->name,nm)
 <+24>:    mov    0x8(%ebp),%eax          # copy s to eax
 <+27>:    mov    0x14(%ebp),%edx         # copy fourth parameter (gr) to edx
 <+30>:    mov    %edx,0x44(%eax)         # copy gr to offset eax+68 (s->grad_yr)
 <+33>:    mov    0x8(%ebp),%eax          # copy s to eax
 <+36>:    mov    0x10(%ebp),%edx         # copy third parameter (ag) to edx
 <+39>:    mov    %edx,0x40(%eax)         # copy ag to offset eax+64 (s->age)
 <+42>:    mov    0x8(%ebp),%edx          # copy s to edx
 <+45>:    mov    0x18(%ebp),%eax         # copy g to eax
 <+48>:    mov    %eax,0x48(%edx)         # copy g to offset edx+72 (s->gpa)
 <+51>:    leave                          # prepare to leave the function
 <+52>:    ret                            # return

Being mindful of the byte offsets of each field is key to understanding this code. A few things to keep in mind:

  • the strcpy() call takes the base address of the name field of struct s and the address of array nm as its two arguments. Recall that since name is the first field in the studentT struct, the address of s is synonymous with the address of s→name.

 <+6>:     mov    0x8(%ebp),%eax          # copy first parameter (s) to eax
 <+9>:     mov    0xc(%ebp),%edx          # copy second parameter (nm) to edx
 <+12>:    mov    %edx,0x4(%esp)          # copy nm to esp+4
 <+16>:    mov    %eax,(%esp)             # copy s to top of stack (esp)
 <+19>:    call   0x8048320 <strcpy@plt>  # call strcpy(s->name,nm)
  • The next part of the code (instructions <initStudent+24> thru <initStudent+30>) places the value of the gr parameter at an offset of 68 from the start of s. Revisiting the memory layout of the struct in [structArray6] shows that this address corresponds to s→grad_yr.

 <+24>:    mov    0x8(%ebp),%eax          # copy s to %eax
 <+27>:    mov    0x14(%ebp),%edx         # copy fourth parameter (gr) to edx
 <+30>:    mov    %edx,0x44(%eax)         # copy gr to offset eax+68 (s->grad_yr)
  • The next section of code (instructions <initStudent+33> thru <initStudent+39>) copies the ag parameter to the s→age fields of the struct. Afterwards, the g parameter value is copied to the s→gpa field (byte offset 72) of the struct.

 <+33>:    mov    0x8(%ebp),%eax          # copy s to eax
 <+36>:    mov    0x10(%ebp),%edx         # copy third parameter (ag) to edx
 <+39>:    mov    %edx,0x40(%eax)         # copy ag to offset eax+64 (s->age)
 <+42>:    mov    0x8(%ebp),%edx          # copy s to edx
 <+45>:    mov    0x18(%ebp),%eax         # copy g to eax
 <+48>:    mov    %eax,0x48(%edx)         # copy g to offset edx+72 (s->gpa)

8.9.1. Data Alignment and Structs

Consider the following modified declaration of our studentT struct:

struct studentTM {
    char name[63]; //updated to 63 instead of 64
    int  age;
    int  grad_yr;
    float gpa;
};

struct studentTM student2;

The size of the name field of the struct is modified to be 63 bytes, instead of the original 64 bytes. Consider how this affects the way the struct is laid out in memory. It may be tempting to visualize it like so:

struct2wrong
Figure 2. An incorrect memory layout for the updated struct studentTM. Note that the struct’s "name" field is reduced from 64 to 63 bytes.

In this depiction, the age field occupies the byte immediately following the name field. But this is incorrect. Figure 3 depicts the actual layout of the struct in memory:

struct2right
Figure 3. The correct memory layout for the updated struct studentTM. Byte x63 is added by the compiler to satisfy memory alignment constraints, but it doesn’t correspond to any of the struct’s fields.

IA32’s alignment policy requires that 2-byte data types (i.e, short) reside at a 2-byte aligned address while 4-byte data types (int, float, long, and pointer types) reside at 4-byte aligned addresses, and 8-byte data types (double, long long) reside at 8-byte aligned addresses. For structs, the compiler adds empty bytes as "padding" between fields to ensure that each field satisfies its alignment requirements. For example, in the struct declared above, the compiler adds a byte of empty space (or padding) at byte x63 to ensure that the age field starts at an address that is at a multiple of 4. Values aligned values properly in memory can be read or written in a single operation, enabling greater efficiency.

Consider what happens when the struct is defined as the following:

struct studentTM {
    int  age;
    int  grad_yr;
    float gpa;
    char name[63];
};

struct studentTM student3;

Moving the name array to the end of the struct ensures that age, grad_yr and gpa are 4-byte aligned and will cause most compilers to remove the filler byte at the end of the struct. However, if the struct is ever used in the context of an array (e.g., struct studentTM courseSection[20];) the compiler will once again add the filler byte between each struct in the array to ensure that alignment requirements are properly met.