Assembly Language for Beginners

(nextflipdebug2) #1

3.29. WINDOWS 16-BIT


call MESSAGEBOX
xor ax, ax
mov sp, bp
pop bp
retn 0Ah
WinMain endp


UNIX time is a 32-bit value, so it is returned in theDX:AXregister pair and stored in two local 16-bit
variables. Then a pointer to the pair is passed to thelocaltime()function. Thelocaltime()function
has astruct tmallocated somewhere in the guts of the C library, so only a pointer to it is returned.


By the way, this also implies that the function cannot be called again until its results are used.


For thetime()andlocaltime()functions, a Watcom calling convention is used here: the first four
arguments are passed in theAX,DX,BXandCX, registers, and the rest arguments are via the stack.


The functions using this convention are also marked by underscore at the end of their name.


sprintf()does not use thePASCALcalling convention, nor the Watcom one,
so the arguments are passed in the normalcdeclway (6.1.1 on page 734).


Global variables


This is the same example, but now these variables are global:


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


char strbuf[256];
struct tm *t;
time_t unix_time;


int PASCAL WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{


unix_time=time(NULL);

t=localtime (&unix_time);

sprintf (strbuf, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year+1900, t->tm_mon, t->⤦
Çtm_mday,
t->tm_hour, t->tm_min, t->tm_sec);

MessageBox (NULL, strbuf, "caption", MB_OK);
return 0;
};


unix_time_low dw 0
unix_time_high dw 0
t dw 0


WinMain proc near
push bp
mov bp, sp
xor ax, ax
call time_
mov unix_time_low, ax
mov unix_time_high, dx
mov ax, offset unix_timelow
call localtime

mov bx, ax
mov t, ax ; will not be used in future...
push word ptr [bx] ; seconds
push word ptr [bx+2] ; minutes
push word ptr [bx+4] ; hour

Free download pdf