1.24. STRUCTURES
mov [esp+30h+var_2C], eax
lea eax, [esp+30h+unix_time]
mov [esp+30h+var_30], eax
call localtime_r
mov eax, [esp+30h+tm_year]
add eax, 1900
mov [esp+30h+var_2C], eax
mov [esp+30h+var_30], offset aYearD ; "Year: %d\n"
call printf
mov eax, [esp+30h+tm_mon]
mov [esp+30h+var_2C], eax
mov [esp+30h+var_30], offset aMonthD ; "Month: %d\n"
call printf
mov eax, [esp+30h+tm_mday]
mov [esp+30h+var_2C], eax
mov [esp+30h+var_30], offset aDayD ; "Day: %d\n"
call printf
mov eax, [esp+30h+tm_hour]
mov [esp+30h+var_2C], eax
mov [esp+30h+var_30], offset aHourD ; "Hour: %d\n"
call printf
mov eax, [esp+30h+tm_min]
mov [esp+30h+var_2C], eax
mov [esp+30h+var_30], offset aMinutesD ; "Minutes: %d\n"
call printf
mov eax, [esp+30h+tm_sec]
mov [esp+30h+var_2C], eax
mov [esp+30h+var_30], offset aSecondsD ; "Seconds: %d\n"
call printf
leave
retn
main endp
This code is identical to what we saw previously and it is not possible to say, was it a structure in original
source code or just a pack of variables.
And this works. However, it is not recommended to do this in practice.
Usually, non-optimizing compilers allocates variables in the local stack in the same order as they were
declared in the function.
Nevertheless, there is no guarantee.
By the way, some other compiler may warn about thetm_year,tm_mon,tm_mday,tm_hour,tm_minvari-
ables, but nottm_secare used without being initialized.
Indeed, the compiler is not aware that these are to be filled by
localtime_r()function.
We chose this example, since all structure fields are of typeint.
This would not work if structure fields are 16-bit (WORD), like in the case of theSYSTEMTIMEstructure—
GetSystemTime()will fill them incorrectly (because the local variables are aligned on a 32-bit boundary).
Read more about it in next section: “Fields packing in structure” (1.24.4 on page 359).
So, a structure is just a pack of variables laying in one place, side-by-side. We could say that the structure
is the instruction to the compiler, directing it to hold variables in one place. By the way, in some very
early C versions (before 1972), there were no structures at all [Dennis M. Ritchie,The development of the
C language, (1993)]^161.
There is no debugger example here: it is just the same as you already saw.
Structure as an array of 32-bit words
#include <stdio.h>
#include <time.h>
void main()
{
(^161) Also available ashttp://go.yurichev.com/17264