Anda di halaman 1dari 12

(15A04408) MICRO PROCESSORS & INTERFACING LAB

Course Objective:
To become skilled in 8086 Assembly Language programming.
To understand programmable peripheral devices and their Interfacing.
To understand and learn 8051 microcontroller.
To learn 8051 assembly Language programming

Learning Outcome:
Able to write8086 Assembly Language programs.
Able to understand programmable peripheral devices and their Interfacing.
Able to write 8051 assembly Language programs.

Minimum Ten Experiments to be conducted (Five from each section)

I) 8086 Microprocessor Programs using MASM/8086 kit.


1. Introduction to MASM Programming.
2. Arithmetic operation Multi byte Addition and Subtraction, Multiplication and
Division Signed and unsigned Arithmetic operation, ASCII arithmetic
operation.
3. Logic operations Shift and rotate Converting packed BCD to unpacked BCD, BCD
to ASCII conversion.
4. By using string operation and Instruction prefix: Move Block, Reverse string, Sorting,
Length of the string, String comparison.
Interfacing:
1. 8259 Interrupt Controller and its interfacing programs
2. 8255 PPI and its interfacing programs (A /D, D/A, stepper motor,)
3. 7-Segment Display.

AI) Microcontroller 8051 Trainer kit


1. Arithmetic operation Multi byte Addition and Subtraction, Multiplication and
Division Signed and unsigned Arithmetic operation.
2. Logic operations Shift and rotate.
3. Sorting- Ascending and descending order.
Interfacing using 8051 Trainer kit:
1. Key board Interfacing
2. Seven Segment display
3. Switch Interfacing
4. Relay Interfacing
5. UART
I. Microprocessor 8086
Introduction to MASM/ TASM & DEBUG

There are two ways to execute 8086 programs using a computer. One is using the DOS
tool called DEBUG and the second way is using the Assembler TASM / MASM. Hence both the
ways are to be learned.

1. DEBUG
The DOS utility called DEBUG allows entering assembly language programs, to execute
these programs, to view memory locations and also to trace the program execution.

Starting and Quitting DEBUG


The simple way of starting the DEBUG is to type the command DEBUG at the DOS
prompt. After the DEBUG is loaded the DEBUG prompt - is displayed. Then the DEBUG is
ready to accept any DEBUG commands.
To quit DEBUG the command is Q. This command takes control back to DOS. DEBUG
can be started along with a file also. To load DEBUG with a program say test1.com at the DOS
prompt type DEBUG test1.com.

DEBUG Commands
The commands which are required to run / enter the programs are the following.

A Assemble
D Display / Dump
E Enter data
F Fill memory
G Run the program
L Load
N Name a program
P Proceed
Q Quit
R Register
T Trace
U Un Assemble
W Write
The DEBUG does not detect errors until Enter key is pressed. If there is any syntax
mistake then the command line is redisplayed with the word error added at the point at which the
error was detected. If a command line contains more than one error, only the first error will be
detected and indicated and execution will stop at that point.

Assemble (A) Command


The Assemble command is used for entering assembly language mnemonics and
translating them directly to machine language instructions. The syntax for this command is
A address
Display / Dump (D) Command

This command displays the content of a block of memory locations. The syntax for the
command is

D address1 address2

Address1 is the starting address of the memory block and address2 is the ending address.
When the ending address is not specified then from the starting address the contents of 128
locations will be displayed. The display consists of both HEX and ASCII formats. In each line 16
HEX / ASCII numbers will be displayed.

Enter (E) Command


This command can be used to change the contents of specific memory locations. The
syntax of this command is
E address xxxxxxx
Changes address is the beginning address for entering the changes and changes is an
optional list of changes to be made. If the changes are not specified in the command line then
DEBUG enters a special entry mode in which the values of memory locations, beginning at
address are displayed. Then these values can be changed one byte at a time. After each entry
press space bar to display the contents of next location. To exit the entry mode and to go back to
DEBUG prompt press Enter. If no changes are required for any byte then simply press space bar
to go for the next location without changing the values.

Ex: E 0200 ABCDEFGH


With the above enter command the string ABCEDFGH is stored from the starting
location of DS: 0200.

Go (G) Execute Command


This command can be used to run the machine language programs. The syntax of this
command is
G = Start break1 break2....

= Start is an optional starting address of the program and break1, break2.. ... are the break
point addresses. If the start address is not specified then the execution starts from the current
value of Instruction Pointer. First time when DEBUG is loaded with a COM program IP is set to
100. So to execute the COM program simply type the command G.

Load (L) Command


This command will load a COM file or EXE file into memory. The syntax of the
command is simply L. The file should be given with another command N. The COM file will
load into the memory from CS: 0100 address. After a file has been loaded BX: CX contains the
number of bytes successfully read.
Name (N) Command
The Name command can be used to specify the file name for Load and Write Commands.
The syntax of the command is

N filename

Ex: N test1.com

Register (R) Command


This command allows us to view the contents of any register and also to change the
values. The syntax of this command is

R register

Register is the optional name of the register to modify the any of the AX, BX, CX, DX,
SP, BP, SI, DI, DS, ES, SS, CS, IP or F. If the command is given without any parameter then
DEBUG displays the contents of all the registers. If a particular register is specified then
DEBUG displays the content of that register and allows us to change the content.

Trace (T) Command


Trace command is used for single step execution of the program. After execution of each
instruction the contents of all the registers will be displayed. The syntax of this command is

T = start count
Start is the beginning address and count is the number of instructions to trace, both these
parameters are optional. If the start is not specified then execution will begin with the current
address stored in IP. If the count is excluded then only one instruction will be executed.

Unassemble (U) Command


This command is used to list the program which is already loaded or assembled. The
syntax of this command is

U range

Range is an optional when it is used start and end address should be given separated by a
space. If the range is not specified then DEBUG displays the instructions starting from the
current contents of the IP or with the byte following the last byte displayed by the most recent U
command. When range is not specified 16 bytes will decoded and displayed.

Write (W) Command


This command can be used to store the assembled program on the hard disk or floppy
disk. The syntax is just W.

The program from the location CS: 0100 is stored. Before using this command the
register CX should be initialized with approximate number of bytes of the program using the R
command.

Example 1.1: Let us assemble a small program and execute the same using the commands
mentioned above.

The program is to add the contents of the registers AX, BX and CX. The program is

MOV AX, 1122


MOV BX, 0011
MOV CX, 1133
ADD AX, BX
ADD AX, CX
INT 20

In the above program all the numbers are hex numbers only. The INT 20 instruction is a
DOS break point instruction it will display a message Program Terminated Normally when it is
executed.
Type Debug from the DOS prompt to load DEBUG
C :> Debug
-
Observe the DEBUG prompt -. It indicates DEBUG is loaded and it is ready to accept
any DEBUG commands. Now use the Assemble command A and type the instruction by
instruction of the above program and press enter to come out from assembly mode.

-a 100
108F:0100 MOV AX, 1122
108F:0103 MOV BX, 0011
108F:0106 MOV CX, 1133
108F:0109 ADD AX, BX
108F:010B ADD BX, CX
108F:010D MOV [200], AX
108F:0110 INT 20
108F:0112
-

In the above display 108F is the CS content and 0100 is the starting offset address of the
program. The CS is initialized by the operating system so from computer to computer and from
program to program also it may change. Hence offset address is to be used for any reference.
After typing a line the offset address is automatically updated by the number of bytes required
for that instruction. Without typing any instruction simply by typing enters the assembly mode
terminates.
Now let us store this program on hard disk with the file name TEST1.COM. The
commands are
-r cx
CX 0000
: 0f
-
The previous contents of CX are 0000 and it is changed to 0F. (0F is approximate length
of the program in bytes). The next command is required for assigning the filename.

-N TEST1.ASM
Next to store the program use Write command and observe the display.
-W
After giving this command a message will be displayed indicating the number of bytes
written. Now if required quit the DEBUG using the command Q and check the directory for the
filename TEST1.COM. This step is not compulsory. Now Let us reload DEBUG with

TEST1.COM files. The command is

C :> DEBUG TEST1.EXE


-
Now the program can be displayed along with the machine codes by using the
Unassemble command.
-u
108F:0100 B82211 MOV AX, 1122
108F:0103 BB1100 MOV BX, 0011
108F:0106 B93311 MOV CX, 1133
108F:0109 01D8 ADD AX, BX
108F:010B 01CB ADD BX, CX
108F:010D A30002 MOV [0200], AX
108F:0110 CD20 INT 20
108F:0112 33C9 XOR CX, CX
108F:0114 2E CS:
108F:0115 A12FE7 MOV AX, [E72F]
108F:0118 BB2200 MOV BX, 0022
108F:011B BA1201 MOV DX, 0112
108F:011E BF0100 MOV DI, 0001

After the INT 20 instruction it is the previous program or some random instructions
stored.
Now to execute the program use the Go command
-g

Program terminated normally


-
Now to see the answer stored at DS: 200 the Dump/Display command can be used.
-d 200 201
108F:0200 33 11 3.

The result is 1133. Observe the ASCII equivalent for 33 is 3 and 11 is not a text character
so just dot is displayed.

Using the Trace Command


To follow the trace command let us take the previous program load if freshly from the
dos prompt and instead of Go command let us use Trace command to execute the program and to
see the results after each instruction.

C :> DEBUG TEST1.EXE


-t

AX=1122 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000


DS=10A1 ES=10A1 SS=10A1 CS=10A1 IP=0103 NV UP EI PL NZ NA PO NC
10A1:0103 BB1100 MOV BX,0011
-t

AX=1122 BX=0011 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000


DS=10A1 ES=10A1 SS=10A1 CS=10A1 IP=0106 NV UP EI PL NZ NA PO NC
10A1:0106 B93311 MOV CX,1133
-t

AX=1122 BX=0011 CX=1133 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000


DS=10A1 ES=10A1 SS=10A1 CS=10A1 IP=0109 NV UP EI PL NZ NA PO NC
10A1:0109 01D8 ADD AX,BX
-t

AX=1133 BX=0011 CX=1133 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000


DS=10A1 ES=10A1 SS=10A1 CS=10A1 IP=010B NV UP EI PL NZ NA PE NC
10A1:010B 01CB ADD BX,CX
-t

AX=1133 BX=1144 CX=1133 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000


DS=10A1 ES=10A1 SS=10A1 CS=10A1 IP=010D NV UP EI PL NZ NA PE NC
10A1:010D A30002 MOV [0200],AX DS:0200=0244
-t

AX=1133 BX=1144 CX=1133 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000


DS=10A1 ES=10A1 SS=10A1 CS=10A1 IP=0110 NV UP EI PL NZ NA PE NC
10A1:0110 CD20 INT 20

-p

Program terminated normally


-
The above listing indicates after execution of each instruction the contents of all the
registers will be displayed along with the next instruction to be executed. When the next
instruction is observed as INT 20 i.e. the break point then uses the Proceed Command to
complete the execution of INT 20 routine. If again trace command is used then control will be
transferred to INT 20 routine and terminating the program takes lot of steps. To avoid this use the
Proceed Command when the next executable instruction is INT 20. After execution of any
instruction memory contents can be viewed to see the intermediate results and by using the t
command the tracing will continue with the next instruction of the program.
Entering Labels
Whenever any branching commands are used then it is required to use the effective
branching address in the instruction. If this jump is a backward jump then the corresponding
address can be observed from the listing but in case of a forward jump the address is exactly not
known at that time so a dummy address should be typed. After assembling is over using
Unassemble command note down the exact address and replace the forward jump instructions by
assembling the same instructions again with correct addresses.

2. MASM / TASM

MASM / TASM is an assembler converts the assembly program to machine code. While
writing the program labels can be used so that there is no need to calculate the addresses as in
case of DEBUG. The program can be just entered in the same way as we write. The machine
code generated by the assembler will not be in the format ready for execution. The memory
specifications will not be there. For this Linker software is required. The linker software
substitutes all the required memory values and makes the machine program ready for execution.
The input file given to the assembler should be .ASM. It can be created by any text
editors. The assembler converts this .ASM file to .OBJ file when the given program is syntax
error free. This OBJ file is converted into .EXE by using the Linker software. The machine
program thus generated can be executed by using DEBUG. If the results are stored in memory
then DEBUG is compulsory to execute the program. If the results are to be displayed on CRT
then the program can be directly executed just by typing the name of the EXE program at the
DOS prompt.
In addition to the mnemonics used in a program we can write some instructions to the
compiler also. These instructions are called Assembler Directives. Assembler directives are not
converted into machine language.

The .EXE Format

.MODEL small ; program model declaration


.STACK 100h ; stack declaration
.DATA ; data segment
m1 DB Hello Welcome to Assembly Language Programming,$
.CODE ; beginning of the program
MOV AX, @DATA
MOV DS, AX ; initializing the data segment
MOV DX, OFFSET M1 ; initialize DX with the string m1 start address
MOV AH, 09 ; required service no 9
INT 21H ; call the DOS service to display the string on CRT
MOV AH, 4CH ; service 4cH
INT 21H ; call service to exit to DOS
END

In the above program comments are written after the semicolon ;. The first dot
command instructs the assembler about the total memory i.e. code + data segments. The
following table specifies the different memory models.
Memory Model Meaning

Small Program code must fit within a single 64kb segment and program data must fit
within a separate 64Kb segment. Both code and data are near

.STACK 100h defines the stack with 256 locations. However in this program stack instructions
are not used.

.DATA defines all the program data variables or constants along with their data types and their
initial values. In the example program m1 is defined as an array of bytes. The db is called data
definition directive. The other data definition directives are
DB 1byte
DW one word (2 bytes)
DD one double word (4bytes)

While tying the initial value type the last digit h for hex number, o for octal and b for
binary. If no letter is specified then it is assumed that the number is decimal. Whenever hex
number starts with an alphabet i.e. A, B...F then add 0 at the beginning of the number to
differentiate the number from a variable.

To enter an array of numbers just type the numbers separated by comma ,. In case the
data is a single ASCII character then type the character within single quotes. In case if the data is
a string type the string with in double quotes.

In the above program $ symbol is used as last character of the string because the DOS
service routine (no 9) will display the characters up to $.

A constant can be stored with EQU directive. Ex: COUNT EQU 50

The number 50 is stored in the variable COUNT. In the main program the COUNT word
is substituted with 50 at the time of compiling.

When the initial data is not known then ? can be used just to create a memory location
without initial value.

Ex: ANS DB ? ANS ; location is created without any initial byte.

To initialize an array with same character DUP directive can be used.

Ex: ARRAY1 DB DUP 10 (55).

The ARRAY1 is created with ten locations and each location is initialized with the value
55. Just to create an array ? can be used along with DUP directive.

After initializing the data segment then to enter the program .CODE segment directive
can be used. Once the .CODE starts it indicates the end of the previous segment and the
beginning of the CODE segment.
In the CODE segment, initialization of data segment register DS is compulsory. The first
two instructions will do this. After this the actual program can be typed. Whenever comments are
required they can be given after typing semicolon.

To end the program i.e. to transfer the control to DOS the service 4Ch should be used for
EXE programs. INT 21H is a DOS service and the actual service number should be stored in the
register AH. HLT should not be used it will force the system to halt then it is required to
RESTART the computer.

The OFFSET directive the example program loads the starting address of the array not
the starting data.

The END directive indicates the physical end of the program. The program written after
END will not be compiled.

After typing the program in an assembler store this program as say prog1.asm. At the
DOS prompt type TASM or MASM prog1.asm. This command activates the assembler and an
object code file is created with same name and extension as .OBJ. The OBJ file will not be
created in case of any errors. Errors should be corrected by looking at appropriate messages.

To convert the .OBJ file to .EXE file type the command TINK prog1.obj at DOS prompt.

To execute this EXE file it should be loaded into DEBUG. However like in example
program the result is displayed on the screen so it can be directly executed just by typing the
name of the file at the DOS prompt itself. The program will display the message Hello
Welcome to Assembly Language on the screen.