Hello world in Win32 in assembly:
.386p
.model flat, stdcall
extrn MessageBoxA : PROC
extrn ExitProcess : PROC
.DATA
szCaption db "OSI > *",0
szText db "Hello World",0
.CODE
Main:
push 0
push offset szCaption
push offset szText
push 0
call MessageBoxA
push 0
call ExitProcess
End Main
Personally, i use tasm as my assembler so i dont know about other assemblers.
Assuming the above code is in a file called hello.asm (which is in the same dir as your assembler), the commands to turn it into an executable are:
tasm32 /ml hello
This gives you a object (obj) file, to link it:
tlink32 -x /Tpe /c hello,hello,, import32.lib,,
And now you have your executable. Run it and you’ll see its a message box, but, behind it is a dos window “surely that means it isnt win32 ?” it is, i just haven’t registered a window class, lets break the code down:
.386p
;denotes which instruction set to use (i386)
.model flat, STDCALL
;Tells the assembler which memory model we want to use, windows has a flat model
;and so thats what we use, STDCALL basically sets how the stack is managed
;by the program, in this instance data is pushed from right to left onto the stack.
extrn MessageBox : PROC
extrn ExitProcess : PROC
;Tells the assembler we want to use these external API functions
.DATA
;Tells the assembler the following segment and its content are data
;as opposed to code.
szCaption db ''OSI > *'',0
szText db ''Hello World'',0
;What it says on the tin
.CODE
;Defines the starting point for the code segment.
Now, here’s the trick, to translate the C version
of a function across to ASM, the syntax for the win32 MessageBox function is:
int MessageBox(
HWND hWnd, // handle to parent window
LPCTSTR lpText, // text in message box
LPCTSTR lpCaption, //title of message box
UINT uType, // type of message box
);
In assembly, as you can see in the program, this becomes;
push 0
push offset szCaption
push offset szText
push 0
Then we call the win32 MessageBox function:
call MessageBox
And terminate the thread:
call ExitProcess, 0
This article was originally written by pigsbig78 |