U P R M C: INEL 4206 8086 Microprocessors Introduction To Assembly Programming
U P R M C: INEL 4206 8086 Microprocessors Introduction To Assembly Programming
U P R M C: INEL 4206 8086 Microprocessors Introduction To Assembly Programming
MAYAGÜEZ CAMPUS
INEL 4206
8086 Microprocessors
Introduction to Assembly Programming
INTRODUCTION................................................................................................................ 1
(XIII) EXERCISES...........................................................................................................15
(XIV) REFERENCES........................................................................................................16
i
Introduction
After the program is properly written and saved the next step is
to run it. For this purpose you need to access Command Prompt
available in Programs. At this point you are accessing a DOS
environment.
C:\> This is the prompt that appears
1
C:\>E: Change from C to E directory to access the assembler,type E:
E:\> This will now appear
E:\>dir/w To view content of directory E type dir\w
E:\>cd assem To access the assembler
E:\>ASSEM This will now appear
Once you have access the assembler you need to create an object
and an excecutable file using MASM and LINK. These directives
examine and run the program. If an error occurs the errors will be
displayed along with the line number they are at. For program
running follow these steps:
E:\ASSEM>cd masm Access MASM to create the object file
E:\ASSEM\MASM>masm Type masm
You have just created the object file, at this point the errors and
warnings are shown along with the line number. If there is no error
the next step is to create the executable file, if there are errors you
need to go to your program and fix it then create the object file
again. To create the executable:
E:\ASSEM\MASM>link Type link
Object Modules[.obj]:X:*.obj Type your program’s name.obj
Run File[E:*.exe]:X:*.exe Type your program’s name.exe
List File[NUL>MAP]: ↵ Enter
Libraries[.lib]: ↵ Enter
Now the executable file of your program has been created. To run it:
E:\ASSEM\MASM>X:*.exe Type X:your program’s name.obj and your
program will be executed
2
(III) Segments of a Program
data segment
var1 db ? ;define variable named var1
mes1 dw “hello$” ;define message
data ends
stack segment stack
db 64 dup(?) ;reserve 64 bytes of memory for the stack
stack ends
code segment
assume ds:data,cs:code
start: ;start of program
mov ax,data
mov ds,ax
… ;program continues
code ends
end start ;at end of program
The programs can also be run using a debugger. The debugger lets
you run the program step by step (line by line) and view what is
happening with certain important files. In this assembler there is
a similar approach called the code view. To access the code view:
3
E:\ASSEM\MASM>:cv *.exe Type cv (space) your program’s
name.exe
When you access the code view window there are three major
columns. The leftmost column, which consists on numbers, is the
logical address and machine language opcode and operand of that
specific line. The center column is the program you typed itself.
And the third column appears when entering key F2. Those are the
most important registers (AX,BX,CX,…,SS,IP) and each has a 16-
bit value which changes along with the running of the program. To
run the program line by line use key F8 and you will see the
registers value changing. Also to see the output press key F4.
Using this tool is very important because you can see what your
program is doing step by step or instruction by instruction and you
can visualize the errors or bad manipulation of data.
Type and save the following program with the name “ex1.asm”
according the instructions above.
Comments:
DATA SEGMENT ;initializing the data segment
MES1 DB “HELLO THERE!!$” ;defining a message
DATA ENDS
STACK SEGMENT ;reserving the stack segment
DW 100 DUP(?)
STACK ENDS
CODE SEGMENT ;defining code segment
ASSUME DS:DATA,CS:CODE
START: ;the start of the program
MOV AX,DATA
MOV DS,AX
6
C) Jumps and Calls
It is often necessary to transfer program control to a
different location. Two instructions that perform this are
jumps and calls. If control is transferred to a memory location
within the current code segment it is NEAR, otherwise is FAR.
Jumps
There are conditional and unconditional jumps. For conditional
jumps control is transferred to a new location if a certain
condition is met indicated by the flag register. For example JNZ
red, the processor looks at the zero flag to see if it is raised. If
not the CPU starts fetching and executing instruction from the
address of the label red. If ZF=1 it will not jump and execute the
next instruction normally. “JMP red” is an unconditional jump in
which control is transferred unconditionally to location red.
Call
It is used to call a procedure that performs a task that needs to
be performed frequently. To call procedure MULT: CALL MULT.
The DO-WHILE loop is also provided for version 6.X which is used
with the .WHILE and .ENDW statements. There is also available
the REPEAT-UNTIL loop which is repeated until some condition
occurs. The .REPEAT statements defines the start and the end is
defined by .UNTIL statement which is followed by a condition.
Every program has to have a part at the end that lets it return
control to the operating system (DOS). For this purpose the two
last instructions of a program are:
MOV AH,4CH
INT 21H
8
(VIII) Interrupts
The most widely used interrupts are the INT21H and the INT10H.
Each one can perform many functions. Before the service of either of
these interrupts is requested, certain registers must have specific
values in them depending on the function being requested.
INT 10H
Some of its functions are changing the color of characters or
background, clearing the screen or changing the location of the
cursor. These options are chosen changing the value of register AH.
(Please refer text Appendix A for more details and functions)
1) Clearing the screen: AH=06, AL=00, BH=07, CX=0000, DH=24,
DL=79. The code will be like this:
MOV AH,06
MOV AL,00
MOV BH,07
MOV CH,00
MOV CL,00
MOV DH,24
MOV DL,79
INT 10H
Exercise problem: Write a program that clears the screen and sets
the cursor at the center of the screen.
9
3) Get current cursor position: AH=03. When executing the following
code registers DH and DL will have the current row and column
values and CX provides information about the cursor:
MOV AH,03
MOV BH,00
INT 10H
INT 21H
This interrupt is provided by DOS and performs extremely useful
functions.(Refer to text, Appendix A for more details and
functions)
1) Outputting a string of data to the monitor:AH=09, DX=the
offset address of the ASCII data to be displayed. Example:
DATA DB “HELLO THERE!!$”
MOV AH,09
MOV DX,OFFSET DATA
INT 21H
10
(IX) Models
(X) Macros
Macros allow the programmer to write a task only once and to invoke
it whenever it is needed. Every macro definition must have three
parts as follows:
name MACRO dummy1,dummy2…dummyN
…
ENDM
The MACRO directive indicates the start and ENDM its end. The
dummies are names, parameters or registers that are mentioned in
the body of the macro. After it is written it can be called by its
name. The following is a macro for displaying a string:
STRING MACRO DATA1 ;DATA1 is a dummy
MOV AH,09
MOV DX,OFFSET DATA1
INT 21H
ENDM
If I want to display the message ‘Hello there’:
MES1 DB “HELLO THERE$”
…
STRING MES1 ;invoke the macro
11
(XI) Using Disk Files
It is useful to know how to create, read, write, append and close disk
files. For this purpose DOS INT 21H is used. For this purpose you
must save the following program that contains four macros that are
useful in developing programs. Two of these macros display data on
the video display, one reads a key and another exits to DOS.
DISP MACRO P1
MOV AH,2
MOV DL,P1
INT 21H
KEY MACRO
MOV AH,1
INT 21H
ENDM
STRING MACRO WHERE
MOV AH,9
MOV DX,OFFSET WHERE
INT 21H
ENDM
EXIT MACRO
MOV AX,4C00H
INT 21H
ENDM
12
Add this macro to the macro file MACS.INC. Now, the next example
shows how to write data to a file. It also shows a macro that closes
that file. A single segment should be used for this purpose.
Now follows an example that creates a file on floppy disk A and then
closes it using the macros in MACS.INC:
Arrays
The DUP directive creates an array as shown here:
LIST DB 10 DUP (?)
This reserves 10 locations of memory but stores no value on them. If
a 10 DUP (2) had been written instead, 10 bytes of memory would have
been reserve with each location of LIST array initialized with a 02H.
14
(XIII) Exercises
1) Implement a program that reads one line of data (your name) from the
keyboard and stores it into an area of memory. When enter key is
pressed the line of data has finished. Use INT21H with AH=0AH and the
“enter” key is stored in memory as 0DH.
4) Modify exercise #3 for it to add two 2-digit numbers. Then two 4-digit
numbers.
8) Create two macros, one that reads bytes of data from a file and another
one that opens an existing file for a read or write operation.
15
(XIV) References
Mazidi, Muhammad A., Janice Gillispie Mazidi, The 8086 IBM PC and
Compatible Computers, Vol I & II. New Jersey, Prentice Hall, Inc.,
1995
16
(XV) Appendix
17