Beruflich Dokumente
Kultur Dokumente
Contents
3.1. Introduction to Assembly Language Tools
3.2. Displaying a Welcome Statement
3.3 Assembling Linking and Debugging
3.1.1 Assembler
An assembler is a program that converts source-code programs written in assembly
language into object files in machine language. Popular assemblers have emerged over the
years for the Intel family of processors. These include MASM (Macro Assembler from
Microsoft), TASM (Turbo Assembler from Borland), NASM (Netwide Assembler for both
Windows and Linux), and GNU assembler distributed by the free software foundation.
3.1.2 Linker
A linker is a program that combines your program's object file created by the assembler with
other object files and link libraries, and produces a single executable program. You need a
linker utility to produce executable files. Two linkers: LINK.EXE and LINK32.EXE are
provided with the MASM 6.15 distribution to link 16-bit real-address mode and 32-bit
protected-address mode programs respectively.
We will also use a link library for basic input-output. Two versions of the link library exist
that were originally developed by Kip Irvine. The 32-bit version is called Irvine32.lib and
works in Win32 console mode under MS-Windows, while the 16-bit version is called
Irvine16.lib and works under MS-DOS.
3.1.3 Debugger
A debugger is a program that allows you to trace the execution of a program and examine
the content of registers and memory.
For 16-bit programs, MASM supplies a 16-bit debugger named CodeView. CodeView can be
used to debug only 16-bit programs and is already provided with the MASM 6.15
distribution.
For 32-bit protected-mode programs, you need a 32-bit debugger. The latest version of the
32-bit Windows debugger is available for download for free from Microsoft.
3.1.4 Editor
You need a text editor to create assembly language source files. You can use NotePad , or
any other editor that produces plain ASCII text files. You can also use the ConTEXT editor,
which is distributed as a freeware at http://www.context.cx. ConTEXT is a powerful editor
that can be easily customized and can be used as a programming environment to program in
assembly language. It has built-in syntax highlighting feature.
• The .modelsmall directive indicates that the program uses a type of structure in which
the program uses no more than 64K of memory for code, and 64K for data. The .stack
directive sets aside 100h (256) bytes of stack space for the program.
.data
MyTitle db "Hello World!",0
MyText db "I am here!",0
.data includes all initialized data. Since our data in this tutorial is not ever going to change
values we could use .const instead of .data for it. There is also .data? which hold uninitialized
data.
MyTitle and MyText are our string names. db stands for define byte. Our variables hold the
text after db. These strings are then terminated by a null terminator character 0 or by using
NULL. You can use either as they mean the same thing.
Now that we have that taken care of, we need to get to the actual program.
.code
start:
push 0
mov eax, offset MyTitle
push eax
push offset MyText
push 0
call MessageBoxA
call ExitProcess
end start
Well that's a pretty big chunk to wade through. Lets take it a step at a time now.
We start out pushing 0 but before we get into that we need to understand the data stack.
The data stack operates on a last on first off principle. This means that the last thing pushed
onto the stack is the first thing popped off the stack. Think of it as a stack of plates. The last
one you put onto the stack of plates is the first one you take off the stack. The memory stack
works the same way.
Our messagebox function goes like this:
MessageBoxA(Handle Window, Text, Title, Style)
Since we are pushing on the stack we have to work backwords so we will push in the
opposite order:
Style, Title, Text, Handle Window
So first we need to push 0 which is the standard messagebox style consisting of an OK button
onto the memory stack.
After that we move the offset (location) of our title into the eax register. The eax register is a
general purpose register that we can use for virtually everything. We then push the register's
value onto the stack.
After we have the style and the text we push the offset of the title and finally the handle
window which is none or 0. Instead of moving the offset of the message into eax and pushing
the register we just push the offset directly onto the stack. This is just for demonstration
purposes. You can do it either way you like.
Now that we have everything for our messagebox on the stack, we need to call the
messagebox method. That's what we do with our call MessageBoxA command. It reads the
last four pieces of information on the stack and combines it into our messagebox.
Finally we call our ExitProcess method to end the program execution.
Our final code is
include \masm32\include\masm32rt.inc
.data
MyTitle db "Hello world!",0
MyText db "I am here!",0
.code
start:
push 0
push offset MyTitle
push offset MyText
push 0
call MessageBoxA
call ExitProcess
end start
The masm32rt include file is set up as a sort of standard library for assembly language
programmers. Since assemblers do not have a library, masm32rt was made to try to make up
for that. By including this we have access to all of the libraries, includes that we will need as
well as the start of a program.
The assembler produces an optional listing file, which is a copy of the program’s
source file (suitable for printing) with line numbers and translated machine code. The
linker produces an optional map file, which contains information about the program’s
code, data, and stack segments.
A link library is a file containing subroutines that are already compiled into
machine language. Any procedures called by your program are copied into the executable
program during the link step. (If you’ve ever programmed in Pascal or C, you have
made extensive use of link libraries, perhaps without realizing it). Table 2 displays a list
of filenames that would be created if we assembled and linked the Hello World program.
Programming Exercises
1. Modify the hello.asm program to display a message of your choice.