The "Go" tools
     The GoAsm manual

Quick start to ....

writing a simple Windows program

by Jeremy Gordon -

This file is intended for those interested in 32 bit assembler programming, in particular for Windows.

Have you ever been frustrated by an application which wonít quite do what you want it to do? Well I want to show you how to write your own applications. Itís free, fulfilling and fun!

This can only be a crash course in program writing, and Iím going to have to miss out a lot of stuff. Iím going to assume you know very little about computer programming, so Iíll start with the basics. By the end this article you will know how to produce your own "Hello World" program. Itís then up to you if you want to go further into this fascinating world. If so you can read the other articles on this site and look at articles on other sites.

Lets start at the beginning. A program contains instructions to the computerís processor. The instructions are called the programís code. The processor executes the code instructions, one by one Ė this is why the program is called an executable. When the program loads, Windows gives the processor the starting address in the code where execution should start. From that point on, it is up to the program to divert execution to the correct place in its code. Iíll come back to this important point later.

Well what does the code actually do? One thing it does is to move numbers to and from the registers inside the processor. There are seven of these for general use. Each register holds 32 bits of data. This is called a dword (literally a double word). Each bit is either on or off (one or zero, set or clear) at any one time. This is a binary number. In 32 bits if all the bits are on, the number in decimal is 4,294,967,295. There are also several arithmetical instructions which can be carried out by the processor. Another thing the code does is to move numbers from the registers to memory and from memory to the registers, or to put numbers on the memory stack. The stack is like a plate warmer. You put a dword on the stack using the PUSH instruction and remove the last dword you have PUSHed on the stack using a POP instruction. The memory Iím referring to here is the RAM (random access memory) inside the computer. The stack is reserved by the system when the program loads. Depending on your data instructions, other memory is reserved by the system when the program loads. This memory is called data. Other memory will have to be requested by the program at run-time, that is during execution.

In the Windows context one of the most important things that the code instructions do is to call a Window function, that is to say to divert execution to that function. The Windows functions are called APIs (literally Application Programmerís Interface). Most of the other code instructions are used to prepare for these calls and to deal with the result of the calls.

It is these calls to APIs which will give your programs extraordinary functionality. The ability to call Windows in this way offers you access to a vast array of processes which will result in interaction with the user, appropriate screen display, printing, file actions etc. All these actions are actually carried out by Windows at the request of the program.

The build process

Well how do you make an executable? You need the developers' tools which are all available from download from my web site. They are all free fully working programs with no expiry date. The tools you need and their uses are as follows:-

  • GoAsm - the assembler. This program takes your source script and converts it into an object file. You write your source script in plain text, using your favourite text editor eg. Notepad or Wordpad, making sure it is saved without any control characters within the text. Traditionally the file extension would be ".asm". The source script contains the lines of source code which contain the code and data instructions you have written for your program. The object file is a file in coded form which can be read by the linker and which is used to provide the final executable.
  • GoRC - the resource compiler. This program takes your resource script and converts it into a res file. The resource script is another plain text file but which contains instructions to establish the Windows controls for our programs - menus, dialogs, icons, bitmaps and stringtables. Traditionally it would have the extension rc. The res file is in coded form and is read by the linker.
  • GoLink - the linker. This program takes the one or more object files and a res file and created the final executable.
  • GoBug - the debugger. With this tool you can watch your program execute each instruction one at a time and see how each instruction affects the registers and memory areas.

So the build process can be shown graphically as follows:-

.asm ® .obj } .exe
.rc ® .res

Making a "do-nothing" Windows program

Here we going to make a program which loads, executes and finishes without doing anything. This is to demonstrate the build process and to explain a few more things.

Action Explanation
Create a new folder in your c: drive called "prog". This helps you organise your work. You can put all your programming work and tools in this folder or in sub-folders if you prefer.
Copy GoAsm, GoRC, GoLink and GoBug into this folder.  
Then using your text editor, create a new file and type these lines:-
Save this file in the prog folder calling it, say, Nothing.asm.
This is the "do-nothing" program. "CODE SECTION" tells the assembler that what follows are code instructions. "START" is a code label. It also tells the linker that this is where program execution should start. "RET" is a mnemonic (processor instruction in words) which tells the processor to return to the caller of the code (which in this case is going to be Windows itself).
Now using your text editor, create a new file and type these lines:-
GoAsm Nothing
GoLink Nothing.obj /console
Save this file in the prog folder calling it gonothing.bat
Here you are creating a batch file which is a very useful little file which executes line by line from the MS-DOS (command prompt) window. The first line runs the assembler GoAsm, giving it the Nothing.asm file (asm is assumed as the extension). This creates the file Nothing.obj which is then given to GoLink, to create the executable Nothing.exe. GoLink is also told to create a console program - this is a program which does not make use of the Windows Graphical User Interface (it does not have windows).
Go to the "start" menu, programs, and click on "MS-DOS prompt" (note the position of this command may differ between computers, for example in XP this is "start", all programs, accessories, command prompt). Now you should see what is called the "C prompt" in the MS-DOS (command prompt) window. Type "c:" and press enter, and then type "cd prog" and press enter. If you are not used to DOS commands, this will be a mystery to you. But here you are changing the current directory in the MS-DOS (command prompt) window to c:/prog, which is the folder where all your programming work resides.
Type "gonothing" and press enter. Here you are running the batch file gonothing.bat. It will execute line by line and you can see this happening in the MS-DOS (command prompt) window. You will see the files Nothing.obj and Nothing.exe being created by the tools.
If you prefer you can simply double click on the batch file in Windows Explorer.
Finally type "Nothing" and press enter. Here you run the executable you have made (Nothing.exe). See that it doesn't actually do anything. The DOS prompt will just move down a line. But Windows was quite happy with this program Ė congratulations you've made your first Windows program!

Making a "Hello World" console program

Here we are going to make a program which loads from the MS-DOS (command) prompt window and which writes "Hello World" to the screen. This is simply to demonstrate how to call a Windows API. This type of program is called a console program because it makes no windows, and therefore makes no use of the Graphical User Interface (known as the "GUI").

The process is the same as before except this time you might call the source script say, HelloWorld1.asm, and the batch file say, GoHello1.bat. Donít forget to change the instructions to the assembler and linker in the batch file so that they operate on the correct files.

Action Explanation
Create a new file and type these lines:-
"DATA SECTION" tells the assembler that a data instruction or instructions follow. "WRKEEP" is a data label which is a name indicating a particular place in data.

"DD" creates a dword (4 bytes) in data (literally "declare dword").

"0" initialises the dword to a value, in this case zero.

Now type these lines:-
PUSH -11
CALL GetStdHandle
"CODE SECTION" and "START" we have seen before.

"PUSH" puts a value onto the stack. The value in this case is minus 11 (expressed as an ordinary decimal number). This is pushed on the stack ready to send to the API call as a parameter.

"CALL" transfers execution to a procedure, in this case to the Window's API GetStdHandle. This API takes one parameter and returns a handle in the EAX register to suit. Here we ask for handle -11 which is the Standard Output Handle for the console. The console is the interface with the user, which in this case will be the MS-DOS window (also known as the "command prompt". Using this handle we can write to the window.

PUSH 11, 'Hello World'
CALL WriteFile
Here we push parameters ready for the call to the Windows API WriteFile and then call the API. This API writes to the handle passed to it in the last parameter (still held in the EAX register). It writes the string which is 11 characters long. We also give it the address of WRKEEP which was the data dword made earlier. This is required because WriteFile will report into that dword how many characters it actually writes.
Here we move the value 0 into the EAX register (to indicate a successful finish) and then use RET to return to the system (closing the program).
Now you can assemble and link this file using the same techniques as before, but with a slight change. Your batch file will now be:-
GoAsm HelloWorld1
GoLink HelloWorld1.obj /console kernel32.dll
Here, apart from the different filenames, you have included "kernel32.dll" in the GoLink command line. This tells GoLink to look at the Windows system file kernel32.dll when linking the file for any "unknowns", in this case the Windows APIs GetStdHandle and WriteFile which are needed for the program to run.
When the program is run you will see the string appear in the MS-DOS (command prompt) window.

So you now know how to create a simple program using assembler and the Windows operating system.

I suggest you also read the "For those new to .." articles and look at the HelloWorld samples. Then you may be ready to move on to the intermediate articles and to the GoAsm manual.

Copyright © Jeremy Gordon 2002-2003
Back to top