2.3 Using a structure in assembly language code

With x.inc defining the offsets of members and the size of the structure, we can now easily access and use structure of type struct X.

2.3.1 Reserving space as a local variable

If obj is a local variable of the type struct X, then the following definition reserves space for it (assuming it is the only local variable):

obj = oldEbp - X_size

2.3.2 Accessing a member

If obj is a parameter (passed by value) or a local variable of the type struct X, then we can initialize member i to zero using the following code:

movl $0,obj+X_i(%ebp)

This assumes that ebp is properly initialized as the frame pointer, and obj is properly defined as the offset from where ebp points to the beginning of obj.

2.3.3 Computing the address of a member

To computer &obj.c.score and put it in register eax, we can use the following code:

movl %ebp,%eax 
addl $obj+X_c_score,%eax

2.3.4 To access a member from a struct pointer

Assuming eax points to a struct X, we can initialize member i to 0 using the following code:

movl $0,X_i(%eax)