![](https://csdnimg.cn/release/download_crawler_static/780913/bg1.jpg)
December 10, 2003
JEFF HUANG
(huang6@uiuc.edu)
Windows Assembly Programming Tutorial
Version 1.02
Copyright © 2003, Jeff Huang. All rights reserved.
![](https://csdnimg.cn/release/download_crawler_static/780913/bg2.jpg)
Windows Assembly Programming Tutorial
by Jeff Huang
i
Table of Contents
Introduction................................................................................ 2
Why Assembly? .............................................................................. 2
Why Windows? ............................................................................... 2
I. Getting Started ....................................................................... 3
Assemblers ..................................................................................... 3
Editors............................................................................................. 3
II. Your First Program................................................................ 4
Console Version.............................................................................. 4
Windows Version ............................................................................ 6
ADDR vs OFFSET.................................................................................... 6
III. Basic Assembly.................................................................... 7
CPU Registers ................................................................................ 7
Basic Instruction Set ....................................................................... 8
Push and Pop ........................................................................................... 8
Invoke .......................................................................................................9
Example Program ........................................................................... 9
IV. Basic Windows................................................................... 10
Preliminaries ................................................................................. 10
Macros ....................................................................................................10
Functions ................................................................................................ 10
Variables................................................................................................. 10
A Simple Window.......................................................................... 11
V. More Assembly and Windows............................................. 13
String Manipulation ....................................................................... 13
File Management .......................................................................... 13
Memory................................................................................................... 14
Example Program ...................................................................................14
Controls......................................................................................... 15
Additional Resources .............................................................. 16
WWW...................................................................................................... 16
Books...................................................................................................... 16
MASM32 ................................................................................................. 16
MSDN Library ......................................................................................... 16
Newsgroups............................................................................................ 16
IRC.......................................................................................................... 16
![](https://csdnimg.cn/release/download_crawler_static/780913/bg3.jpg)
Windows Assembly Programming Tutorial
By Jeff Huang
2
"This is for all you folks out there, who want to learn the magic art of Assembly programming."
- MAD
Introduction
I have just started learning Windows assembly programming yesterday, and this tutorial
is being written while I'm learning the language. I am learning assembly from reading
various tutorials online, reading books, and ask questions in newsgroups and IRC.
There are a lot of assembly programming tutorials online, but this tutorial will focus on
Windows programming in x86 assembly. Knowledge of higher level programming
languages and basic knowledge of computer architecture is assumed.
Why Assembly?
Assembly has several features that make it a good choice many some situations.
1. It's fast – Assembly programs are generally faster than programs created in
higher level languages. Often, programmers write speed-essential functions in
assembly.
2. It's powerful – You are given unlimited power over your assembly programs.
Sometimes, higher level languages have restrictions that make implementing
certain things difficult.
3. It's small – Assembly programs are often much smaller than programs
written in other languages. This can be very useful if space is an issue.
Why Windows?
Assembly language programs can be written for any operating system and CPU model.
Most people at this point are using Windows on x86 CPUs, so we will start off with
programs that run in this environment. Once a basic grasp of the assembly language is
obtained, it should be easy to write programs for different environments.
Introduction
![](https://csdnimg.cn/release/download_crawler_static/780913/bg4.jpg)
Windows Assembly Programming Tutorial
By Jeff Huang
3
I. Getting Started
To program in assembly, you will need some software, namely an assembler and an
editor. There is quite a good selection of Windows programs out there that can do
these jobs.
Assemblers
An assembler takes the written assembly code and converts it into machine code.
Often, it will come with a linker that links the assembled files and produces an
executable from it. Windows executables have the .exe extension. Here are some of the
popular ones:
1. MASM – This is the assembler this tutorial is geared towards, and you should
use this while going through this tutorial. Originally by Microsoft, it's now
included in the MASM32v8 package, which includes other tools as well. You
can get it from http://www.masm32.com/.
2. TASM – Another popular assembler. Made by Borland but is still a
commercial product, so you can not get it for free.
3. NASM – A free, open source assembler, which is also available for other
platforms. It is available at http://sourceforge.net/projects/nasm/. Note that
NASM can't assemble most MASM programs and vice versa.
Editors
An editor is where you write your code before it is assembled. Editors are personal
preferences; there are a LOT of editors around, so try them and pick the one you like.
1. Notepad – Comes with Windows; although it lacks many features, it's quick
and simple to use.
2. Visual Studio – Although it's not a free editor, it has excellent syntax
highlighting features to make your code much more readable.
3. Other – There are so many Windows editors around that it would be pointless
to name all of them. Some of the more popular ones are:
a. Ultraedit (my personal favorite) http://www.ultraedit.com/
b. Textpad http://www.textpad.com/
c. VIM http://www.vim.org/
d. Emacs http://www.gnu.org/software/emacs/emacs.html
e. jEdit http://www.jedit.org/
Chapter 1
Note:
There will be several
directives and macros
used in this tutorial that
are only available in
MASM, so it's highly
encouraged that you
start with this first
![](https://csdnimg.cn/release/download_crawler_static/780913/bg5.jpg)
Windows Assembly Programming Tutorial
By Jeff Huang
4
II. Your First Program
Now that we have our tools, let's begin programming! Open up your text editor and
following the instructions below. This is the most commonly written program in the
world, the "Hello World!" program.
Console Version
The console version is run from the Windows console (also known as the command
line). To create this program, first paste the following code into your text editor and
save the file as "hello.asm".
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
HelloWorld db "Hello World!", 0
.code
start:
invoke StdOut, addr HelloWorld
invoke ExitProcess, 0
end start
Now, open up the command line by going into the Start Menu, clicking on the Run…
menu item, and typing in "
cmd" without the quotes. Navigate to the directory
"hello.asm" is saved in, and type "\masm32\bin\ml /c /Zd /coff hello.asm".
Hopefully, there are no errors and your program has been assembled correctly! Then
we need to link it, so type "\masm32\bin\Link /SUBSYSTEM:CONSOLE
hello.obj
". Congratulations! You have successfully created your first assembly
program. There should be a file in the folder called Hello.exe. Type "hello" from the
command line to run your program. It should output "Hello World!".
So that was quite a bit of code needed to just display Hello World! What does all that
stuff do? Let's go through it line by line.
.386
This is the assembler directive which tells the assembler to use the 386 instruction set.
There are hardly any processors out there that are older than the 386 nowadays.
Alternatively, you can use .486 or .586, but .386 will be the most compatible
instruction set.
Chapter 2