Well since I'm learning I might as well teach others what I've learned. Maybe this will get some activity in the section.
We're going to make a simple little (less than 400 bytes) hello world program. We're going to have it pop up a messagebox saying hello.
First we need some tools. We either need an assembler or we need Visual Studio 2005 or later.
I would recommend using MASM32.
You can download MASM32 from its site
here
If you use VS2005 or later you can create a new blank C++ (cpp) file. Right click on the project and select 'custom build' and then check the Microsoft Macro Assembler box.
Now that we've got our software we can start. Open up qeditor and we'll add this code.
Code:
.386
.model flat, stdcall
option casemap :none
.386 sets up the 386 instruction set. There is also .486 and .586 but .386 is the most used so its the best to go with.
.model flat sets the memory model to flat. Flat is the only model in 32 bit assembly. Previously there were different models but we don't need to concern ourselves with that.
option casemap :none will make our program case sensitive. This way, hello and Hello will be treated as different things. This is a good practice to get into.
Now we need to import some things.
Code:
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
Include files required for Windows programs. windows.inc is always included, because it contains the declarations for the Win32 API constants and definitions. kernel32.inc contains the ExitProcess function we are going to use. The lib files are libraries. Functions need libraries if they are going to function (haha) correctly. These are the standard imports you are going to use for almost all your windows 32 bit assembly programs.
Now that we have those, we need our actual program.
Code:
.data
HelloWorld db "Hello World!", 0
.code
start:
invoke MessageBoxA, 0, addr HelloWorld, addrHelloWorld, 0
invoke ExitProcess, 0
end start
That finishes up our program. Let me explain what everything does now.
.data is the start of the data section. This contains initialized data. We can also use .data? which contains uninitialized data. .const contains initialized constants. These will never change.
We declare a variable HelloWorld. db stands for define byte. This string is given the text "Hello World!" and is followed by a 0 or NULL character. ANSI strings must be followed by a null. We have the option to either use 0 or NULL. They mean the same thing.
.code is the start of the code section. This is where we will actually make things happen.
start: says that everything following is to be executed.
invoke lets us call a method. MessageBoxA is that method. The first addr HelloWorld says that the message is given the string found in the address HelloWorld. The second one says that the title is given the string found in the address HelloWorld. We could use seperate variables if we wanted to.
Finally, we have to invoke (call) ExitProcess. It puts 0 into ExitProcess which will exit the program.
end start says that it is the end of the executed code section. This is the end of our program.
Our final code is
Code:
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
HelloWorld db "Hello World!", 0
.code
start:
invoke MessageBoxA, 0, addr HelloWorld, addr HelloWorld, 0
invoke ExitProcess, 0
end start
Thats it for this tutorial. I hope you learned something. At first assembly may seem impossible but just wait until you get into the pushing, popping, stacking, eax, edx and others! Just kidding, don't be intimidated by assembly language. You'll get the hang of it! Just practice and try to understand what you're doing.