Download laboratory 3: assembling, editing, linking, and executing assembly

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
LABORATORY 3:
ASSEMBLING, EDITING, LINKING, AND EXECUTING
ASSEMBLY LANGUAGE PROGRAMS
NAME:
STUDENT ID#:
Objectives
Learn how to:
z
Use MASM to assemble a source program into object and run modules.
z
Identify and correct syntax errors in a source program.
z
Edit an existing source program.
z
Make a run module with the LINK program.
z
Load and execute a run module with the DEBUG program.
z
Describe a function that is to be performed with a program.
z
Write a program to implement the function.
z
Run the program to verify that it performs the function for which it was written.
Part 1: Assembling a File with MASM
In this part of the laboratory, we begin by assembling an existing source module with
MASM. All programs used will be for the block-move program used as an illustrative
example throughout the chapter. Check off each step as it is completed.
Check
Step
1.
Procedure
Ensure that a path is set to the DOS and MASM directories.
2. Use the “MD C:\LAB03” DOS command to create a working directory
C:\LAB03 on drive C:. Change the current directory to C:\LAB03 by the
DOS command “CD C:\LAB03”.
3. Use an editor to view the source program in the file L03P1.ASM.
4. Assemble and link the program with MASM version 6.11 using the object
file name L03P1.OBJ, source listing name L03P1.LST, and execution file
name L03P1.EXE. The assemble command is
C:\LAB03>ML /F1 L03P1.ASM (↵)
【LAB03-1】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
5. How many warning errors are reported? _________
Severe errors? _________
6.
Use an editor to view the source listing in the file L03P1.LST.
7.
At what lines of the source listing are the machine codes of the instructions
that are used to load the source and destination offset addresses located?
Write down the machine code: _______________ , _______________
8.
What is the source code form of the instruction that is used to load the
source index register? __________________
What is the opcode of the instruction? _______________
What is the immediate operand? _______________
9.
10.
What is the instruction pointer offset associated with the instruction in step 8?
_______________
From the listing file please identify the constants defined in the program.
_________________________________________________________
What does the constant N stand for? _______________
In which line of the source listing does it occur? _______________
Part 2: Correcting a Source Program with Syntax Errors
The source program assembled in the first part of the laboratory did not contain any syntax
error. Here we will assemble one that has errors and use the error information reported by
MASM to correct the program.
Check
Step
Procedure
1.
Ensure that a path is set to the DOS and MASM directories.
2.
Copy the file L03P2.ASM to the working directory C:\LAB03.
3.
Assemble and link the program L03P2.ASM with MASM version 6.11
using the object file name L03P2.OBJ, source listing name L03P2.LST, and
execution file name L03P2.EXE. The assemble command is
C:\LAB03>ML /F1 L03P2.ASM (↵)
【LAB03-2】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
4.
How many warning errors are reported by assembly process? _________
5.
View the source listing file L03P2.LST.
6.
Find the errors in the source listing. List the cause and correction for each
error.________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
7.
Use the editor to make the corrections in the source file L03P2.ASM.
8.
Reassemble the source file L03P2.ASM and verify that no errors exist.
Otherwise, repeat the edit/assemble sequence.
Part 3: Modifying an Existing Source Program
Next, we will take an existing source program, modify it, and then reassemble it.
Check
Step
Procedure
1.
Ensure that a path is set to the DOS and MASM directories.
2.
View the source program L03P1.ASM with an editor.
3.
Modify the program such that the exact same block move operation is
performed, but perform the block transfer with a combination of LOOP and
MOVSB instructions. Be sure the direction flag is set to the appropriate
direction.
4.
Edit the changes to the program and save it into a new file L03P3.ASM.
5.
Reassemble/edit the program until there is no error.
6.
View the corrected program with an editor.
7.
You need to print out the file L03P3.ASM and submit it with this
laboratory report.
Part 4: Creating a Run Module with the LINK Program
In the earlier parts of this laboratory, we assembled a program, identified and corrected
【LAB03-3】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
syntax errors in a program, and modified source programs with an editor. This resulted in
three error-free object modules. Here we will use the linker program to make run modules
that can be executed on the PC.
Check
Step
Procedure
1.
Ensure that a path is set to the DOS and MASM directories.
2.
Use the LINK program to create a run module for the source program in
file L03P1.OBJ. Select the name of the run module file as L03P1.EXE and
the map file as L03P1.MAP. To start the link process use the command
C:\LAB03\>LINK
3.
(↵)
View the link map file with an editor. What are the start and stop addresses
of the code segment? ___________ , ___________
What is the entry of the program? ___________
4.
Repeat steps 2 and 3 for the object module L03P2.OBJ. ___________ ,
___________ , ___________
5.
Repeat steps 2 and 3 for the object module L03P3.OBJ. ___________ ,
___________ , ___________
Part 5: Loading and Executing a Run Module with DEBUG
Now we have several run modules ready to be executed on the PC. In this part of the lab, we
will load the program with the DEBUG program and run it to verify that there is no
execution error.
Check
Step
Procedure
1.
Ensure that a path is set to the DOS and MASM directories.
2.
View the source listing L03P1.LST for use as a reference in the steps that
follow.
Load the run module L03P1.EXE in DEBUG with the command
3.
C:\LAB03>DEBUG L03P1.EXE (↵)
4.
Display the initial values of the MPU’s registers. What is the original value
of DS? _____________
【LAB03-4】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
5.
Verify loading of the program by disassembling the contents of memory.
6.
Execute the program according to the instructions that follow:
a. Reset the value in DS to 2000H and fill the locations from DS:0100
through DS:010F with the value FFH; fill the locations from DS:0120
through DS:012F with the value 00H; then display these memory ranges
to verify correct initialization. Reset DS to its initial value.
b. GO from the beginning of the program down to the instruction
MOV AH,[SI].
What are the values in the following registers?
(AX) = _____________
(DS) = _____________
(SI) = _____________
(DI) = _____________
(CX) = _____________
c. Use another GO command to execute the program down to the
instruction JNZ 0013. Display the contents of the source and destination
blocks of memory. What has changed?
__________________________________________________________
__________________________________________________________
d. Run the program to completion.
e. Redisplay the contents of the source and destination blocks. Describe
the operation performed by the program.
__________________________________________________________
__________________________________________________________
7.
Run the program in file L03P3.EXE in a similar way to that outlined for
L03P1.EXE in steps 2 through 7. Does this program perform the exact
same operation as observed for the block-move program in step 6?
____________________________________________________________
If not , what is the cause of the execution error?
____________________________________________________________
____________________________________________________________
【LAB03-5】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
Part 6: Description of a Program for Average Calculation
Determine the average of a set of data points stored in a buffer. The number of points in the
buffer, the offset address of the beginning of the buffer, and the data segment address are
stored in a table called a parameter table. Figure L3.1(a) shows an example of the
parameters needed for the average program. Notice that the beginning address of this table is
named COUNT. This first address holds the number that indicates how many data points are
in the buffer. Since a byte is used to specify the number of data points, the size of the buffer
is limited to 255 bytes. The offset address of the beginning of the data buffer is stored in the
table location called BUFFER. The data buffer segment is defined by the contents of
location BUFFER+2. Assuming the data points as signed 8-bit binary numbers, write a
program to find their average.
Figure L3.1 (a) Parameter table for average calculation program. (b) Flowchart for
average calculation. (c) The assembly program source code.
【LAB03-6】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
Part 7: Writing the Program
The average can be found by adding all the signed numbers and then dividing their sum by
the number of points that were added. Even though 8-bit data points are being added, the
sum that results can be more than 8 bits. Therefore, we will consider a 16-bit result for the
sum, and it will be held in register DX. The average that is obtained turns out to be just 8 bits
long. It will be available in AL at the completion of the program.
Our plan for the program that will solve this problem is shown in figure L3.1(b). This
flowchart can be divided into six basic operations, which are initialization, preparing the
next point for addition, performing the addition, updating the counter and pointer, testing for
the end of the summation, and computing the average.
Initialization involves establishing the data segment and data buffer addresses and loading
the data point counter. This is achieved by loading the appropriate registers within the MPU
with parameters from the parameter table. The instructions that perform this initialization are
as follows:
MOV
AX, DATA_SEG
MOV
DS, AX
MOV
CL, COUNT
MOV
BL, CL
LDS
SI, BUFFER
The first two instructions define the data segment in which the parameter table resides. This
is achieved by first loading AX with the value of DATA_SEG and then copying it into DS.
The instruction that follows this loads CL from the first address in the parameter table. This
address is COUNT and contains the number of points to be used in forming the average.
Looking at figure L3.1(a), we see that this value is 1016. The next instruction copies the
number in CL into BL for later use. The LDS instruction is used to define the buffer together
with the data segment in which it resides. This instruction first loads SI with the offset
address of the beginning of the buffer from table location BUFFER and the DS with the
address of the data segment in which the data lies from table location BUFFER+2. The sum
must start with zero; therefore, register DX, which is to hold the sum, is loaded with zero by
the instruction.
MOV
DX, 0
The next operation involves obtaining a byte of data from the buffer, making it into a 16-bit
number by sign extension, and adding it to the contents of the DX register. This is
accomplished by the following sequence of instructions.
NEXTPT:MOV AL, [SI]
CBW
ADD
DX, AX
【LAB03-7】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
The first instruction loads AL with the element in the buffer that is pointed to by the address
in SI. The CBW instruction converts the signed byte in AL to a signed word in AX by
extending its sign. Next, the 16-bit signed number in AX is added to the sum in DX. Notice
that the label NXTPT (next point) has been used on the first instruction.
To prepare for the next addition, we must increment the value in SI such that it points to the
next element in the buffer and decrement the count in CL. To do this, we use the following
instructions:
INC SI
DEC CL
If the contents of CL at this point are nonzero, we should go back to obtain and add the next
element from the buffer; otherwise, we just proceed with the next instruction in the program.
To do this, we execute the following instruction:
JNZ NXTPT
Execution of this instruction tests the value in ZF (zero flag) that results from the DEC CL
instruction. If this flag is not set to one, a jump is initiated to the instruction corresponding to
the label NXTPT. Remember that NXTPT is placed at the instruction used to move the byte
into AL for addition to the sum. In this way we see that this part of the program will be
repeated until all data points have been added. After this is completed, the sum resides in DX.
The average is obtained by dividing the accumulated sum in DX by the number of data
points. The count of data points was saved earlier in BL. However, the contents of DX
cannot be divided directly. It must first be moved into AX. Once there, the signed divide
instruction can be used to do the division. This gives the following instructions:
MOV
AX , DX
IDIV
BL
The result of the division, which is the average, is now in AL. The assembly code of the
entire average calculation program is shown in figure L3.1(c).
【LAB03-8】
611 37100 Principles and Applications of Microprocessors
Department of Bio-Industrial Mechatronics Engineering
National Taiwan University
Part 8: Running the Program
The source program in the file LAB03.ASM employs the average calculation algorithm we
just developed as a procedure. This program will be assembled and linked to produce a run
module in file LAB03.EXE. The source listing produced by the assembler is LAB03.LST.
Now we will execute the program on the PC with 16 arbitrarily selected data points. Check
off each step as it is completed.
Check
Step
1.
Procedure
Copy the LAB03.ASM file to the working directory C:\LAB03. Assemble
and link the program to generate a run module LAB03.EXE. Load the run
module LAB03.EXE in DEBUG with the command
C:\LAB03>DEBUG
LAB03.EXE
(↵)
2.
Verify loading of the program by unassembling the contents of memory
start at the current code segment.
3.
Execute the program according to the instructions that follow:
a. GO from address CS:0 through CS:A. What are the contents of the data
segment register? _____________
b. Use DUMP commands to display the contents of the first five data segment
storage locations. What is represented by the byte at DS:0? _____________
What is represented by the next four bytes?
_________________________________________________________
c. GO from address CS:17. What is represented by the contents of registers CL,
BL, SI, and DX?
(CL) _____________________
(BL) _____________________
(SI) _____________________
(DX) _____________________
d. Load DS:0 through DS:F with the values, 4, 5, 6, 4, 5, 6, FF, FE, FF, 1, 2, 0, 1,
5, 5, and 5. Verify loading with a dump command.
e. GO to address CS:25. What is the sum? _____________
What is the integer average? _______ What is the remainder? _______
f. Run the program to completion.
【LAB03-9】