Assembly Language for Beginners

(Jeff_L) #1
1.24. STRUCTURES

76 $LC5: .ascii "Seconds: %d\n"<0>


This is an example where the branch delay slots can confuse us.

For example, there is the instructionaddiu $a1, 1900at line 35 which adds 1900 to the year number.

It’s executed before the correspondingJALRat line 34, do not forget about it.

Structure as a set of values

In order to illustrate that the structure is just variables laying side-by-side in one place, let’s rework our
example while looking at thetmstructure definition again: listing.1.333.

#include <stdio.h>
#include <time.h>

void main()
{
int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst;
time_t unix_time;

unix_time=time(NULL);

localtime_r (&unix_time, &tm_sec);

printf ("Year: %d\n", tm_year+1900);
printf ("Month: %d\n", tm_mon);
printf ("Day: %d\n", tm_mday);
printf ("Hour: %d\n", tm_hour);
printf ("Minutes: %d\n", tm_min);
printf ("Seconds: %d\n", tm_sec);
};

N.B. The pointer to thetm_secfield is passed intolocaltime_r, i.e., to the first element of the “structure”.

The compiler warns us:

Listing 1.337: GCC 4.7.3
GCC_tm2.c: In function 'main':
GCC_tm2.c:11:5: warning: passing argument 2 of 'localtime_r' from incompatible pointer type [⤦
Çenabled by default]
In file included from GCC_tm2.c:2:0:
/usr/include/time.h:59:12: note: expected 'struct tm *' but argument is of type 'int *'

But nevertheless, it generates this:

Listing 1.338: GCC 4.7.3
main proc near

var_30 = dword ptr -30h
var_2C = dword ptr -2Ch
unix_time = dword ptr -1Ch
tm_sec = dword ptr -18h
tm_min = dword ptr -14h
tm_hour = dword ptr -10h
tm_mday = dword ptr -0Ch
tm_mon = dword ptr -8
tm_year = dword ptr -4

push ebp
mov ebp, esp
and esp, 0FFFFFFF0h
sub esp, 30h
call __main
mov [esp+30h+var_30], 0 ; arg 0
call time
mov [esp+30h+unix_time], eax
lea eax, [esp+30h+tm_sec]
Free download pdf