Sie sind auf Seite 1von 5

Lab 3: Assembly Language Tools and Data Representation

Contents
3.1. Introduction to Assembly Language Tools
3.2. Displaying a Welcome Statement
3.3 Assembling Linking and Debugging

3.1 Introduction to Assembly Language Tools


Software tools are used for editing, assembling, linking, and debugging assembly language
programming. You will need an assembler, a linker, a debugger, and an editor. These tools
are briefly explained below.

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.

3.2 Displaying a Welcome Statement


Example shows a program that displays the traditional “Hello, world!” message on the
screen. It contains the essential ingredients of an assembly language application.
Line 1 contains the Title directive; all remaining characters on the line are treated as
comments, as are all characters on line 3. The source code for this program was written in
assembly language and must be assembled into machine language before it can run. This
program is compatible with both the Microsoft and Borland assemblers.
Segments are the building blocks of programs: The code segment is where program
instructions are stored; the data segment contains all variables, and the stack segment
contains the program’s runtime stack. The stack is a special area of memory that the program
uses when calling and returning from subroutines.

The Hello World Program.


; This program displays “Hello, world!”
.model small
.stack 100h

• 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.

3.3 Assembling Linking and Debugging


By now, we have seen that it’s fairly easy to assemble and run short programs with
Debug. You will soon be using the assembler, a utility program that converts a source
program into an object file, and a linker, a program that converts object files into executable
programs.
One major advantage to using an assembler is that source programs can be more
easily modified with a text editor than with Debug. Another is that you can use symbolic
names for variables, rather than hard-coded numeric addresses. The linker has one
primary advantage—programs can take advantage of existing libraries full of useful
subroutines; the subroutines are “attached” to our programs by the linker.
A Two-Staged Process. Figure 1 shows the stages a program goes through before it can
be executed. A programmer uses a text editor to create the source file of ASCII text. The
assembler program reads the source file and produces an object file, a machine-language
translation of the program. The object file may contain calls to subroutines in an external
link library. The linker then copies the needed subroutines from the link library into the
object file, creates a special header record at the beginning of the program, and produces
an executable program. When we are ready to run the program, we type its name on the
DOS command line, and the DOS loader decodes the header record of the executable
program and loads it into memory. The CPU begins executing the program.

.The Assemble-Link-Execute Cycle

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.

Filename Description Step When Created


hello.asm Source program Edit
hello.obj Object program Assembly
hello.lst Listing file Assembly
hello.exe Executable program Link
hello.map Map file Link

Microsoft Assembler (MASM)


The Microsoft Assembler package contains the ML.EXE program, which assembles and links
one or more assembly language source files, producing an object file (extension .obj) and an
executable file (.exe). The basic syntax is:
ML options filename.ASM
For example, the following command assembles and links hello.asm (with CodeView
debugging information), producing hello.obj and hello.exe:
ml/Zi hello.asm
The following assembles and links hello.asm, producing a listing file called hello.lst, as
well as hello.obj and hello.exe:
ml/Fl hello.asm
The following assembles, links, and produces a map file called hello.map during the link
step. It still produces hello.obj and hello.exe:
ml/Fm hello.asm
The following produces an object file, listing file, map file, executable file, and
includes debugging information. The line following it runs the CodeView debugger:
ml/Zi/Fm/Fl hello.asm
cv hello
Microsoft also supplies the masm.exe program to remain compatible with programs
written under older versions of the assembler. The MASM commands to assemble and link a
program called sample are the following:
masm/l/n/z sample;
link/m/co sample;

Programming Exercises
1. Modify the hello.asm program to display a message of your choice.

Das könnte Ihnen auch gefallen