Tutorial 1. Hello World!
Last updated: 3 January 2000 by
Dolphinz
SUMMARY
Well, this my the first tutorial, which will
threat about windows programming in assembly languaje,so
I assume that the reader knows how to use TASM from Borland,
and that you have a basic knowledge of assembly languaje.
The complete source code and program for this tutorial can be
downloaded from here : Tutorial 1
THEORY
Windows programming is not easy as under DOS, because under
this environment you have some rules.
Windows 95, it's a full 32 bits operative system, with a
"flat" address space, so you can imagine that the memory is a
large and continuous space of 4 Gbytes, and now you can use any
segment register to address any point in the memory space.
Windows 95 supports at least 1000 calls to functions, which the
applications can use.
When a program is executed, this comunicates with windows
throught a process called dinamic linking. The archive
has references to the different Dinamic Link Libraries
(DLL) and use the functions contained on the library. These
libraries are located on the subdirectory \SYSTEM
of Windows.
THE CODE
Well, let's look at the first win32asm code:
.386p
.model flat, stdcall
;(External API functions we will use)
extrn MessageBoxA : PROC
extrn ExitProcess : PROC
;(Initialized data goes here)
.DATA
szCaption db 'Tutorial 1 - Hello World!',0
szText db 'This is our first program under Windows 95'
db 0Dh,0Ah,'made in Turbo Assembler 5.0',0
; (Here goes the program code
.CODE
Main: ; Entrypoint
push 0 ; push style of Message Box
push offset szCaption ; push Title
push offset szText ; push content of Message Box
push 0 ; push handle of parent window
call MessageBoxA ; call function
push 0
call ExitProcess ; Exit program
End Main ; End of code
|
Very simple win32asm program
Now you can save this text using a text editor, later assemble it using Borland's assembler (TASM32).
The correct syntax is:
tasm32 /ml TUTOR1
The compiler will give you a file called tutor1.obj (The switch /ml tells the compiler to assemble and not call the linker). Now you have to call the Borland's linker (TLINK32), and the correct syntax is:
tlink32 -x /Tpe /c TUTOR1,TUTOR1,, import32.lib,,
The switch /c stands for Case Sensitive Link, and
import32.lib is the name of the library which has to be
linked in for the programs to work.
ANALIZING THE CODE
.386p
This is an assembler directive tells the assembler to use 80386 instruction set.
Anything that starts with a '.' (period) is
a directive or instruction to the assembler.
Here you can use .386, .486, .586.
.model flat, STDCALL
This line it's also a directive. Here we're setting the memory model we intend to use, so by writing flat we tell the
assembler to use the memory model used by Windows.
The STDCALL defines teh calling convention. The calling convention sets the way the stack is handled by a program.
Using STDCALL the parameters are pushed from right to left onto the stack, but the caller is responsible for stack balancing.
.data, .data?, .const
Each executable has a .DATA and .CODE segment. It means that the address space is divided into sections.Here we're telling the assembler that the following should be placed in
the .DATA segment.
Now let's examine the next code. If you look in your Win32 API
Reference for MessageBox, you can read:
int MessageBox(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // offset of text in Message Box
LPCTSTR lpCaption, // offset of title of Message Box
UINT uType, // Stype of Message Box
);
|
Then the code to call this function using assembly languaje it
would looks like:
push 0 ; MB_OK=0, style for Message Box
push offset szCaption ; Title of Message Box
push offset szText ; Text inside Message Box
push Hwnd ; push handle of Parent Window
call MessageBoxA ; call the Function
|
At the end of the program, I call the function
ExitProcess( ), which ends the current
running process and all it's threads.
How many styles for MessageBoxA( )
can I use?.
You can find these equates on a complete win32 include file.
For example, the win32 include file, which comes with TASM 5.0, should have a list of MessageBox styles.
|