Anda di halaman 1dari 12

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 1/12

Introduction
Previous to this class youve undoubtedly done a fair amount of programming in a high-level
programming language where an environment is already set up and you can count on certain tools
being ready for your use: accessing the command line, the system environment, and abstract datatypes such as arrays and structures are all examples of these pre-made tools. The point of EE-314 is
to understand how all of these abstract constructs and facilities are implemented, used, and
manipulated in a computer system. To accomplish this goal, we will study the architecture and
assembly language of the Intel 80x86 family of microprocessors and IBM PC type desktop
computer systems. Assembly language programming starts in the middle of the abstraction
hierarchy with a programmers model of the microprocessor. That is, the elements relevant and
visible to a programmer in accessing and manipulating data. From this point you work in two
directions: you work down from the starting point into the hardware of the system, you also work
up from assembly language to a higher level language (C/C++ in this class).
Primarily, in lab, you will work on mastering assembly language with an emphasis on those
ideas essential to the operation of a microprocessor system. Some labs will involve wiring circuitry
or using circuitry already present in the system, but the bulk of the class will be spent mastering
assembly language programming. You will learn how assembler is used to create high-level
abstractions, work toward interfacing modules written in assembler with higher-level languages,
and finally when you have a good understanding of assembler and microprocessor operation, use
C/C++, with in-line assembler for those areas where it is required. This is the form in which a
programmer can be most productive. Along the way, youll develop both a greater understanding of
C/C++ and of microprocessor systems.
Lab 1 is the very beginning of this process. In this lab, you will get an introduction to the tools
you will use for assembly language programming. Assembly language is a single step away from
the raw (and unreadable and unworkable) binary code the microprocessor actually executes. In
assembly language, you literally tell the microprocessor what to do, step by step, with no high level
abstractions to help. However, even at this low level, several tools are useful and necessary, and you
do operate within a pre-existing environment. This difference is one of the essential trade-offs in
choosing a language: the amount of pre-existing abstractions versus the amount of detail you are
exposed to, and thus the amount of CONTROL you have. In the assembly language environment
you are exposed to all the details and have near complete control. This lab is both an introduction to
those tools and the beginning of the lessons necessary to understand assembly language
programming.
The three tools you will be working with are the Microsoft Macro Assembler (MASM),
Codeview, and Programmers Work Bench. MASM is the assembler, which plays a role similar to a
compiler in translating your text-based source code into the binary code of a computer. Translating
assembly language source code into the equivalent binary machine code is a much simpler task than
translating a program in a high level language. Therefore an assembler is a much simpler program
than a compiler. The assembler translates source code on a statement by statement basis, with each
source statement literally specifying an instruction. One result of this is that the assembler can
produce a listing file (.lst) that shows your assembly code side-by-side with the machine code
generated. This list file can be a useful tool in understanding what is going on and can sometimes be
used to aid in debugging.

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 2/12

Codeview is the debugger provided with the Microsoft Assembly Language development
system. It is a powerful debugger that allows you to run and single step your program. You may set
break-points, view and modify memory, and view and modify registers as your program runs. The
Programmers Workbench is the development environment that ties everything together, much like
any development environment such as Visual Studio, Borland or Watcoms IDE (Integrated
Development Environment), or the GNU tools on Linux. Unlike Visual Studio, PWB is a text based
development environment running under MS-DOS.
Objectives

Be able to configure PWB/MASM to produce executable programs with Codeview debugging


support.
Be able use Codeview to step through programs, while both stepping over subroutines and
through subroutines.
Be able to use Codeview to view register contents, memory contents, and execution point. Be
able to use Codeview to predict the behavior of the next instruction.
Understand some of the different addressing modes available on the 8086, to the point of being
able to answer the lab questions.
Understand segmented addressing and the effect it has on assembly programming (on Intel
hardware).

Lab Procedure
During the laboratory period complete the following steps and write down the answers to
questions as you go. Your laboratory report for this week will consist of your answers to the
questions together with printed versions of the listing file of the assembled programs you will
work with in the lab.
Part 1. Lab Manual Experiment 5, Steps 1 6 (Assembling and executing .exe files)
Part 1 is nothing more than steps 1 6 in the Barry B. Brey Laboratory Manual. Part 2 & 3 use this
work as a starting point, and expand on the manuals treatment of Codeview usage. What follows in
part 1 is reproduced from the lab manual, with additional questions. Source files needed are
available in the project directory.
Experiment Five
Introduction to Programmers
Workbench and Codeview
Introduction
Programming the personal computer in assembly language with MASM version 6.X benefits
from the use of the Programmers WorkBench, QuickHelp, and CodeView programs. The
Programmers WorkBench is an editor that uses a series of options to guide the assembly
language linking task and makes program development almost automatic. This program
replaces the earlier, and difficult to use M editor program. The QuickHelp program accesses
online detailed help on assembly language instructions, DOS INT 21H function calls, and BIOS
function calls. The CodeView program is a much enhanced version of DEBUG that allows a

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 3/12

program to be debugged in symbolic form. The tools are part of the MASM package and
although not absolutely required for program development, are a useful and welcomed aid. The
manual assumes that the latest version (version 6.11) is in use or at the least, version 6.X.
Procedure
The Microsoft MASM assembler programming package is actually a programming
environment that contains four major features: the assembler and linker; complete online help
with QuickHelp; Programmers WorkBench, and CodeView.
The assembler and linker program is Microsoft version 6.1 that is called ML.EXE on
the distribution disk. The assembler has all the features of earlier versions and is completely
compatible with them. The ML.EXE program is normally accessed through Programmers
WorkBench and not directly from the DOS command line.
The online help that is provided by Microsoft is accessed from the DOS command line
with QH.EXE (QuickHelp) or usually through Programmers WorkBench. (Note: this feature
may or may-not be available in lab at present) When in Programmers WorkBench, help is
requested by Highlighting a command and then by pressing the F1 key. The online help feature
provides complete help on the instruction set, the assembler and its features, Programmers
WorkBench, and CodeView. It lists all DOS INT 21H function calls as well as the BIOS
functions available to the programmer. (Note: you dont know anything about these mysterious
DOS INT and BIOS function calls at this point, but you will see some of them before this lab is
through. Basically, they are a mechanism for accessing pre-existing functions. Although the
actual mechanics are very different from library function (like printf), youll see that in
assembly they achieve similar results: youll see a printf-like DOS INT 21H. It will be some
time before this class progresses to the point that you can fully understand these instructions,
but for now, just take it on faith that they work).
The Programmers WorkBench or PWB is accessed from the DOS command line by
typing PWB (or you may use the option in DOSSHELL). The PWB program coordinates the
assembly language programming task by allowing access to the editor, online help, assembler,
and CodeView for debugging. The MASM group is an integrated assembly language
development package.
The CodeView feature displays the program in symbolic form and allows the program
to be executed an instruction at a time, by groups of instructions, or completely. (youll
understand what symbolic form means later when we contrast CodeView to the DOS utility
DEBUG which doesnt support symbolic debugging). Debugging is accomplished by the
display of the registers and sections of memory in various windows. Note that a mouse is not
required for using the PWB package, but is highly recommended.
Using PWB
Figure 5-1 shows the startup screen in PWB. Across the top is the menu bar that
contains 9 pulldown menus that access various features of PWB. As with all Microsoft
programs, the mouse is used to activate a pulldown menu or if no mouse is available, the
alternate key and first letter of the menu name.
Figure 5-1

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 4/12

Step 1: Activate the PWB program by typing PWB at the DOS command line. Once PWB is
activated select the File pulldown menu with the mouse or by the alternate plus F key
combination. Next select New to start a new file.
You are now ready to begin entering an assembly language program. Type the program
listed in Example 5-1 (this is also available from the project directory called example.asm).
This program will be used through much of this experiment to learn how to use PWB and its
features. Before entering the program, it is a good idea to name it as this point. To name the
program select Save from the File pulldown menu. Enter the desired file name with the
extension of .ASM for an assembly language source file. Once the name is selected and path,
choose OK to select the new file name. After this the PWB program returns to the main edit
screen so the program can be entered.
The program in Example 5-1 is well documented with comments (sections of lines that
start with semicolons). This program is stored in a single segment called CODE. Because
CodeView will be used later with this example in this experiment, the SEGMENT directive is
followed by the word code to identify this segment to CodeView as the code segment. The
code directive allows CodeView to display the code segment in symbolic form as it appears in
Example 5-1. The word data is used for the same purpose to display a data segment in
symbolic form with CodeView. The example program used three DOS INT 21H function calls
to access DOS features:
1. Function AH = 1 reads and echoes a key from the keyboard and returns the ASCIIcoded character in AL.
2. Function AH = 2 displays the ASCII-coded character from register DL on the video
display.
3. Function AH = 4CH exits to DOS at the end of the main procedure. Note that AL =
00H to indicate no error on the exit to DOS. The value returned in AL can be
examined in a batch file with the IF ERRORLEVEL command.
This example program also illustrates the decomposition and display of the ASCII
coded character entered on the keyboard in hexadecimal format. The CONV procedure

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 5/12

converts the rightmost four bits of AL into an ASCII-coded character for the number 0-9 and
letters A-F. It then displays this character on the video screen.
Step 2: Now that the program in Example 5-1 has been entered into the editor of PWB, the
PWB must be configuredto assemble the program in the desired format, ie EXE, COM, etc.
Configuration is accomplished by using the Options pulldown menu. Select the Options menu
and choose the Project Templates entry. Next choose the Set Project Template entry from the
next directory. (See figure 5-2a)
Figure 5-2a

Next choose the Set Project Template entry from the next directory. (Figure 5-2 illustrates
the Set Project Template dialog box.)

Figure 5-2. The Set Project Template menu from PWB.

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 6/12

The Runtime Support section allows the choice of None or Assembler. The normal
choice is None because most programs dont require Runtime support from a separate
Runtime library. Runtime libraries are normally provided for mixed language programming.
(assemply with PASCAL, BASIC, C/C++, etc.) or for Windows programming. Next select
the DOS.EXE entry to generate a DOS.EXE (Execute) file as the target for the assembler
and linker. Once None and DOS.EXE have been selected choose OK at the bottom of the
dialog box.
Step 3: Nowthat the Project isdefined, select the Build Options from the Options menu.
This determines the type of program developed by the assembler and builder program. There
are two build options presented, one selects the release version and the other the debug
version. In most cases it is prudent to use the debug version for testing before release.
Choose the Debug Option. (See figure 5-3a)
Figure 5-3a

Step 4: Next, select the Language options from the Options menu. Now select the Set
Debug Options menu. (Figure 5-3b, 5-3c and Figure 5-3 shows the Macro Assembler
Debug Options menu, and menus selected to get there) Note it may also be a good idea to
indicate that warnings are not to be treated as errors.

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 7/12

Figure 5-3b

Figure 5-3c

In most cases you will need to select the Generate Listing File feature from this list if it
is not already selected. Other options include insruction timings, source lines, and so forth.
The Generate Listing File feature causes the assembler to generate not only the EXE file, but
also a .LST (listing) file. The listing file contains the source program and the object program
in one listing. The listing file is often used as printed program documentation.

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 8/12

Figure 5-3. The Macro Assembler Debug Options Menu


Step 5: Now that the PWB has been configured, the project (Example 5-1) can be built. To
build the .EXE file select the Project menu and choose Build. After a few moments the
system will build the program from example 5-1 and ask if you wish to run or debug it. You
may receive a warning about the stack. This can be ignored unless the program being
assembled uses more than 128 bytes of stack space. This program uses much less.
The final product is now in the form of an .EXE file and also .LST file. Choose the
Cancel option to return to Programmers WorkBench. Now view the assembled file by
selecting the Open feature under the file menu. Open the .LST file. The .LST file generated
by the assembler is listed in Example 5-2 and should match the you see on the screen.
Step 6: Run this program by selecting the Run feature in the Run menu. Try typing a
number 6. Now run the program again and try typing a letter D.
Questions
What is the result of running the program of example 5-1 typing 6?
Typing D?

Answers

Important Note: Under exited normally, PWB will remember your settings. However, these
settings are vital to the successful compilation of the programs in all labs, and if these settings
are somehow changed, youll need to restore them. So before consulting a TA on compilation
errors: Be sure to check your configuration, and be sure you know how to restore the
configuration youve just entered!

Part 2. Using Codeview to examine an .EXE file.


Now we will start exploring how to use the Codeview debugger to learn more about our
program and how it executes. We will return to the .LST file to verify some of the information we

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 9/12

determined from using the Codeview debugger. This section is a bare introduction to Codeviews
facilities. We will return to using Codeview in Part 4, where we will use it extensively to help
understand addressing modes.
We will be using the program of example 5-1 for our first introduction to Codeview. With all
windows still open from the exercises of Part 1, configure Codeview to run in 50-line mode. This is
done from the PWB by selecting Options->CodeView Options Then select 50-line mode from
the resultant menu, as shown in Figure 5-4. NOTE: some machines in the lab dont work correctly
with Codeview in 50 line mode. If you have selected 50 line mode, and Codeview seems to be
behaving strangely (for example it doesnt seem to respond to menu commands), try using 43 line
mode instead. This problem seems to be an incompatibility between Codeview and the mouse
driver. On those machines that exhibit the problem, it appears that the mouse driver is reporting
incorrect screen coordinates to Codeveiw on mouse clicks.
Figure 5-4

If you havent already successfully built the program of Example 5-1, request help from your
TA. Once you have successfully built Example5-1, launch it in Codeview by selecting Run>Debug:example.exe. CodeView should launch with your source code visible in a source window.
If the source window shows unintelligible raw assembly: you need to go back to Part 1 and make
sure everything is configured correctly. Using the Windows menu to open a register, source, and
memory window. Do this by selecting Windows->Source 1, Windows->Memory 1, and
Windows->Register.
Since you havent begun executing any instructions, your program should be in an initial state.
Fill out the following table from the register window:
AX =
BX=
CX=
DX=
SP=
BP=
SI=
DI=
DS=
ES=
SS=
CS=
IP=
FL=
Note that the registers in BOLD are variable every time you load the program and these will
probably be different every time (these are the segment registers that are so important in the Intel
Architecture).
Now hit F10 a single time to single step through one instruction. You should notice changes in
the source and register windows.

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 10/12

Which registers changed values?

What are the new values of these registers?

What are the old values (Use the Run->Restart menu to go back and see the old values if
neccessary)?
Does the change in the IP make sense? Explain. (To verify this consider what role the IP
register plays and either look at the hex listing of the MOV instruction in the .LST file, or
look up the instruction in your text).

Now finish execution of the program by pressing F5. This allows the program to finish. The
screen should change to the DOS terminal screen. The program has now executed to the point
where it is waiting for user input, just like when run normally. Enter D, as before. The screen should
then switch back to CodeView with the message Process XXXX terminated normally (0). Did the
program execute correctly? Use the F4 key to switch between input/output screen and Codeview
and check the output.
Now restart the program by selecting Run->Restart. This time step all the way through the
program using the F10 key to step a single instruction at a time. When the program has ended restart
it (you should know how to by now:) and this time single step until the instruction CALL DISP is
highlighted. (You may note that the screen swaps are taking a very long time, to shorten this exit to
PWB, change to 25 line mode, and then restart CodeView in 25-line mode). With CALL DISP
highlighted press F8 to single step instead of F10. Continue hitting F8 until you execute the
instruction CALL CONV. Notice that F8 doesnt operated any differently than F10 on most
instructions. With CALL CONV highlighted, press F8 again.
What is the difference between the F8 and F10 instructions?
Use the F10/F8 key to single step to the first instruction of PROC DISP (the instruction PUSH
AX should be highlighted).
What is the value of IP (remember that this is the offset of the next instruction to be
fetch/executed)?
Use the F10/F8 key to single step to the first instruction of the PROC CONV (the instruction
AND AL, 0FFH should be highlighted).
What is the value of IP (remember that this is the offset of the next instruction to be
fetch/executed)?
Pressing the single step keys repeatedly is a poor way to navigate a program, so use the menu
to restart. In the code window, scroll down to the first instruction in the PROC CONV. To set a
break point either use the Data menu, or simply double-click on the instruction. The instruction

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 11/12

should now be highlighted in bold. This means the break-point is set. Now simply execute the
program (F5), enter the data, and you will note that the program stops execution before the
highlighted instruction is executed.
Part 3. Using the .LST file to examine an .exe program.
Now exit CodeView and open the example.LST file. The .LST file begins with a header listing
Assembler version and source file. It then has the source code with Hex machine language listings.
This IS the machine language your code is translated to that is executed by the computer. Here you
can see the one-to-one correlation between your assembler source and machine language. This
section is followed by (under our configuration) a listing of Segments and Groups. Finally there is a
list of Procedures, parameters and local variables, and a symbol table.
You will notice that the locals listing includes information on type, value, and attributes. For
example the procedure main is type Far Procedure (P Far), Value 0000, with attribute CODE
Length= 0014 Public. Dont worry that most of this is incomprehensible at this point. Were only
going to look at a few useful areas to introduce some important ideas.
As already mentioned, assembly programming is much lower level than an abstract language.
At this level you are telling the computer exactly what to do using numbers and the arithmetic and
logic functions that the computer is built to perform. In an abstract language you used variables to
hold data types such as Integers, and executed function calls. In assembly you use an array of
memory (the Programmers Model youve discussed in class) and numbers to keep track of
information. What the .LST file lists is information the assembler has calculated for you. The
assembler has calculated that the Code segment is 14H bytes long (20 decimal), the DISP procedure
is 0dh bytes long, and the CONV procedure is 11h bytes long. Even more importantly, the
assembler has calculated where these procedures begin and associated the locations with a label;
(DISP or PROC). Without the assembler, you would have to calculate all these values yourself.
When you write the code CALL CONV, the assembler produces the right instruction and data to
change the point of execution to the beginning of CONV. (That is it makes IP equal the value of
CONV, the location at which CONV begins).
As an example, recall the value of IP at the beginning of the procedure CONV. Now add the
lengths of the procedure MAIN and DISP.
What is the relation between the value of CONV (where in memory the procedure starts)
and the length of DISP and MAIN
All three procedures are located in memory one after the other. First MAIN, then DISP, then
CONV. Realizing this, the relation between the offset (value) of CONV, and the sum of the sizes of
DISP and MAIN should make sense. (Draw a diagram if necessary).
Part 4. Using Codeview to understand addressing modes.
Now we will start fresh with a new program. You can find this program listed in Appendix C or in
the project directory with the name Modes.asm. Modes.asm is ready to be built, and should require
no modification by you.

EE 314 Lab #1

Introduction to Assembly, MASM, and Codeview

Page 12/12

In this program you will see example of various addressing modes. Fill out the following table.
In the table you will see a listing of instructions from modes.ASM that you will have to answer
using CodeView. Arrays of data have been set up in memory that are unique. This was done so you
would know where the data came from. Even more important than being able to complete the table
is taking this opportunity to explore and understand how the different addressing modes work. With
CodeView you have the capability to start/stop execution, look into memory locations, and look at
the values of all registers. If an instruction doesnt behave the way you think it should, use
CodeView in combination with the text book ( the Addressing Modes section of Chapter 3) to
increase your understanding.
Instruction
MOV AX, 1a1Ah
MOV AX, BX
MOV BX, CS:[0003H]
MOV AX, [DI]
MOV AX, [DI+3]
MOV DX, [BX + SI]
MOV CS:[0002H], BL
MOV AH, [DI]
MOV AX, [DI-1]
MOV AX, CS:[DI]
(1st) MOV DX, [BX][SI]
(2nd) MOV DX, [BX][SI]
(3rd) MOV DX, [BX][SI]

Addressing Mode
Immediate
Register
Direct
Index
Index+Disp

Register/Memory values after Execution.


AX = ______
AX = ______ , BX = _______
BX = ______
AX = ______, DI = ______
AX = ______, DI = ______
DX = ______
CS:[0002H] = _____ (WORD), BX = _____
AX = _____
,DI =
AX = _____
AX = _____
DX = _____
DX = _____
DX = _____

Add the following instructions to MODES.ASM. They can be placed anywhere within the code
segment. These are not a meaningful sequence of instructions, they are merely to illustrate the
instruction format and to introduce some kinds of syntax errors that can occur. For each of these
lines, explain why there is an error:
(i)
MOV [BX], [DI]
(ii)
PUSH AL
(iii)
XCHG AX, FFH
(iv)
MOV AL, OFFSET TABLE
(v)
MOV AX, [CX]
Lab report
Your laboratory report for this week will consist of your answers to the questions along with a print
out of the modes.LST file with the added instructions from part 4, in working form, with the source
of the error noted on the .LST file. (The reason, if any, they were invalid instructions).

Anda mungkin juga menyukai