Vous êtes sur la page 1sur 110

DEPARTMENT OF COMPUTER SCIENCE & ENGG.

CERTIFICATE

This is to certify that Ms./Mr. …………………...……………………………………

Reg. No. …..…………………… Section: ……………… Roll No: ………………... has

satisfactorily completed the lab exercises prescribed for Microprocessors Lab [CSE 2211]

of Second Year B. Tech. Computer Science and Engineering Degree at MIT, Manipal, in

the academic year 2018-2019.

Date: ……...................................

Signature
Faculty in Charge
CONTENTS

LAB PAGE
TITLE REMARKS
NO. NO.

COURSE OBJECTIVES AND OUTCOMES i

EVALUATION PLAN ii

INSTRUCTIONS TO THE STUDENTS ii – iii

SAMPLE LAB OBSERVATION NOTE PREPARATION iv

INTRODUCTION TO MASM AND PROGRAMS ON


1 1 –6
ADDITION AND SUBTRACTION
8086 PROGRAMS ON MULTIPLICATION AND
2 7–9
DIVISION

3 ARITHMETIC AND BRANCHING INSTRUCTIONS 10 – 15

4 LIST OPERATIONS AND LOOP INSTRUCTIONS 16 – 18

5 STRING OPERATIONS 19 – 21

6 8086 DOS INTERRUPTS 22– 26

INTRODUCTION TO PROCEDURES AND MACROS


7 27 – 30
IN 8086

8 DISPLAY INTERFACING 31 – 38

9 KEYBOARD INTERFACING 39 – 42

10 ELEVATOR INTERFACING 43 – 45

11 DAC AND ADC INTERFACING 46 – 50


STEPPER MOTOR AND LOGIC CONTROLLER
12 51 –54
INTERFACING
REFERENCES 55
Course Objectives
 To state the 8086 programming model
 To explain simple assembly programs to solve mathematical problems involving
number crunching
 To write and analyze 8086 assembly programs to solve problems involving
complex operations like sorting, searching, interrupts.
 To design programs to interface external devices to 8086 processor.

Course Outcomes
At the end of this course, students will be able to
 Write basic programming model
 Write and execute simple 8086 assembly programs.
 Apply the instructions of 8086 to solve complex problems and to analyze the
program.
 Design and execute interfacing programs.

Evaluation plan
 Internal Assessment Marks : 60%

 Continuous evaluation component (for each experiment):10 marks


 The assessment will depend on punctuality, program execution, maintaining the
observation note and answering the questions in viva voce
 Total marks of the 12 experiments reduced to marks out of 60

 End semester assessment of 2 hour duration: 40 %

Page i of iv
INSTRUCTIONS TO THE STUDENTS
Pre- Lab Session Instructions
1. Students should carry the Class notes, Lab Manual and the required stationery to
every lab session
2. Be in time and follow the Instructions from Lab Instructors
3. Must Sign in the log register provided
4. Make sure to occupy the allotted seat and answer the attendance
5. Adhere to the rules and maintain the decorum

In- Lab Session Instructions


 Follow the instructions on the allotted exercises given in Lab Manual
 Show the program and results to the instructors on completion of experiments
 On receiving approval from the instructor, copy the program and results in the
Lab record
 Prescribed textbooks and class notes can be kept ready for reference if required

General Instructions for the exercises in Lab


 The programs should meet the following criteria:
o Programs should be interactive with appropriate prompt messages, error
messages if any, and descriptive messages for outputs.
o Use meaningful names for variables and procedures.
 Plagiarism (copying from others) is strictly prohibited and would invite severe
penalty during evaluation.
 The exercises for each week are divided under three sets:
o Solved exercise
o Lab exercises - to be completed during lab hours
o Additional Exercises - to be completed outside the lab or in the lab to
enhance the skill
 In case a student misses a lab class, he/ she must ensure that the experiment is
completed at students end or in a repetition class (if available) with the permission
of the faculty concerned but credit will be given only to one day’s experiment(s).
 Questions for lab tests and examination are not necessarily limited to the questions
in the manual, but may involve some variations and / or combinations of the
questions.

Page ii of iv
 A sample note preparation is given later in the manual as a model for observation.

THE STUDENTS SHOULD NOT


 Bring mobile phones or any other electronic gadgets to the lab.
 Go out of the lab without permission.

Page iii of iv
Sample lab observation note preparation

LAB NO: 1 Date:

Title: INTRODUCTION TO MASM AND SIMPLE PROGRAMS

1. ALP to perform subtraction of 2 8-bit numbers

Program:
data segment
a db 07h
b db 05h
c db ?
data ends
code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov al,a
mov bl,b
sub al,bl
mov c,al
mov ah,4ch
int 21h
code ends
end start

Sample Output

Page iv of iv
LAB NO: 1

LAB NO: 1 Date:

INTRODUCTION TO MASM AND PROGRAMS ON ADDITION AND SUBTRACTION

Objectives:

In this lab, student will be able to:

1. Understand the 8086 microprocessor architecture.


2. Identify different components of assembly language program.
3. Illustrate and analyze the working of MASM software to run assembly language programs.
4. Write, compile and execute assembly language programs in MASM.

I. INTRODUCTION TO 8086 MICROPROCESSOR

 Intel 8086 microprocessor is a 16-bit microprocessor wherein ALU, registers, instructions are designed
to work with 16-bit binary words.
 Consists of 16-bit data bus and 20-bit address bus
 Data bus allows read/write data from/to memory and ports either 16 bits or 8 bits at a time.
 Address bus can address any one of 220 or 1MB memory locations
8086 Internal Architecture

Fig 1.1 8086 Internal Block Diagram[1]


 8086 CPU is divided into two independent functional parts which speeds up processing
1. Bus Interface Unit
2. Execution Unit
1. Bus Interface Unit (BIU)
 Sends out addresses, fetches instructions from memory, reads data from memory, ports, and
writes data to memory and ports.
 Handles all data transfers for Execution Unit.
Queue
Page 1 of 57
LAB NO: 1

 FIFO (First-in-First-out) register to store the instruction bytes prefetched by BIU.


 While Execution Unit does not require the use of the buses, BIU fetches up to six instruction bytes
of following instructions and puts in this queue.
 This queue is to promote pipelining for faster processing.
Segment Registers
 8086 works with only four 64KB segments within the 1MB memory range at any given time.
The four segments are
 Code Segment(CS)
 Data Segment(DS)
 Stack Segment(SS)
 Extra segment(ES)

Fig 1.2 One-way four 64KB segments might be positioned


within the 1MBaddress space of 8086[1]
 Four segment registers hold the upper 16 bits of the starting addresses of these four memory
segments that 8086 is working with at a particular time and they are named as CS register, DS
register, SS register and ES register.
 BIU always inserts zeroes for the lowest 4 bits of the 20-bit starting address of the segment.
Instruction Pointer (IP)
 Holds the 16-bit address or offset of the next code byte within the code segment.
 16 bit offset in IP is added to the 16-bit base address in code segment to get the 20-bit physical
address, which can be represented, in segment base: offset form.
2. Execution Unit
 Tells BIU where to fetch data or instructions from, decodes instructions and executes instructions.
 Includes control circuitry to direct internal operations, decoder to translate instructions to series
of actions and ALU to carry out arithmetic and logic operations.

Page 2 of 57
LAB NO: 1

Flag Register
 Flip-flop indicates some condition produced because of execution of an instruction or controls
certain operations of EU.
 Consists of nine active flags, which can be classified into six conditional flags and three control
flags.
 The six conditional flags are CF, PF, ZF, SF, AF and OF and the three control flags are TF, DF
and IF.

Fig 1.3
General Purpose Registers
 EU has eight general-purpose registers labelled as AH, AL, BH, BL, CH, CL, DH and DL which
can be used individually for temporary storage of 8-bit data.
 These registers can be used together to store 16-bit words and the acceptable register pairs are
AH and AL, BH and BL, CH and CL and DH and DL.
 The AH-AL pair is referred to as AX register, BH-BL pair as BX register and DH-DL pair as
DX register.
Pointer and Index Registers
Stack Pointer Register (SP)
 Holds the 16-bit offset from the start of stack segment to the memory location where a word
was most recently stored on stack.
Base Pointer Register (BP)
 Helps in referencing the parameter variables passed to a subroutine. The address in SS register
is combined with the offset in BP to get the location of the parameter. BP can be combined with
DI and SI as base register for special addressing.
Source Index Register (SI) and Destination Index Register (DI)
 16-bit registers used for temporary storage of data. Have special functionality in string
operations.

II. ASSEMBLY LANGUAGE PROGRAMMING


Assembly language statements are usually written in a standard form that has four fields namely Label,
Opcode, Operands and Comment.
Label
 Symbol or group of symbols used to represent an address, which is not specifically known at the
time it is written. It is followed by a colon.
 It is not mandatory to have labels for each statement and can be inserted as required.
Opcode
 Represents mnemonic for the instruction to be performed

Page 3 of 57
LAB NO: 1

 Ex: ADD
Operands
 Contains the data, memory address, port address, or the name of register on which the instruction
is to be performed.

Required Set of 8086 Instructions


1. MOV
Category : Data Transfer Instruction
Description : copies a word or byte of data from a specified source to a specified destination.
Syntax : MOV Destination, Source

2. ADD
Category : Arithmetic Instruction
Description : Add a number from some source to a number from some destination and put the result in
the specified destination.
Syntax : ADD Destination, Source

3. SUB
Category : Arithmetic Instruction
Description : Subtract a number from some source to a number from some destination and put the
result in the specified destination.
Syntax : SUB Destination, Source

Assembler Directives
 Assembler directives are the commands to the assembler that direct the assembly process.
 They do not generate any machine code i.e. they do not contribute to the final size of machine code
and they are assembler specific.
Some assembler directives are defined below:
1. SEGMENT AND ENDS DIRECTIVES
 Used to identify a group of data items or instructions that we want to put together in a particular
segment.
 SEGMENT marks the beginning of logical segment and ENDS denotes the end of logical segment
2. ASSUME
 Tell the assembler the name of the logical segment it should use for a specified segment.
3. DB
 Used to declare a byte type variable or to store a byte in memory location.
4. DW
 Used to define a variable of type word or to reserve storage location of type word in memory.
5. END
 Placed after the last statement of a program to tell the assembler that this is the end of the program
module. The assembler will ignore any statement after an END directive.

Running a sample ALP in MASM


Various steps in executing an assembly language program includes
1. Create a directory with section followed by roll number (to be unique); e.g. A21

Page 4 of 57
LAB NO: 1

2. In the MASM command window, type edit filename.asm or edit to open up an Editor window.
Type the assembly language program and select FileSave. If edit is used, select FileSave As
and save the program with .asm extension in the required directory. Then select FileExit.
3. To assemble the source code into an object file, type the command masm filename.asm; and press
Enter.
4. To link the object file with other modules or libraries into an executable program, type the
command link filename.obj; the file will be compiled and errors if any will be displayed.
5. To load the program into memory, type the command td filename and press Enter.
6. To perform line-by-line execution of the program, F7 is used and when each line is being executed,
corresponding changes in the registers and flags can be observed.
7. To view the results, select ViewDump.

Solved Exercise
Write ALP to perform addition of two 8-bit numbers

data segment
a db 02h
.
b db 04h
c db ?
data ends
code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov al,a
mov bl,b
add al,bl
mov c,al
mov ah,4ch
int 21h
code ends
end start

Page 5 of 57
LAB NO: 1

Sample Output

Lab Exercises

Write assembly language programs to perform

1. Addition of two 8-bit unsigned numbers.


2. Addition of two 16-bit unsigned numbers.
3. Subtraction of two 8-bit signed numbers.
4. Subtraction of two 16-bit signed numbers.

Additional Exercises

1. Write ALP to perform addition of two 8-bit numbers represented in the following number systems:
a. Binary b. Decimal c. Octal
2. Write ALP to perform the subtraction of an unsigned byte from signed word.

--------------------------------------------------------------------------------------------------------------------------

Page 6 of 57
LAB NO: 2

LAB NO: 2 Date:

8086 PROGRAMS ON MUTLIPLICATION AND DIVISION

Objectives:

In this lab, student will be able to:

1. Identify and use the instructions required to perform multiplication and division operations for
signed and unsigned numbers.
2. Perform sign extension of values in Accumulator register to obtain valid results.

REQUIRED SET OF 8086 ARITHMETIC INSTRUCTIONS


1. MUL
Description: Multiplies an unsigned byte or word from some source times an unsigned byte in AL or
an unsigned word from some source times an unsigned word in AX.
Syntax: MUL source
 Source can be a register or memory location
 When a byte from some source is multiplied by AL, the 16-bit result will be in AX.
 When a word from some source is multiplied by AX, the most significant word of 32-bit result will
be in DX and the least significant word will be in AX.

2. DIV
Description: divide an unsigned word by a byte or an unsigned double word by a word.
Syntax: DIV source
Source can be a register or memory location
Division of a word by byte.
 The word must be in AX register.
 After division, AL will contain an 8-bit result (quotient) and AH will have an 8-bit
remainder.
Division of a double word by a word.
 The most significant word of double word must be in DX and least significant word in AX.
 After division, AX will contain 16-bit result (quotient) and DX will have the 16-bit remainder.
3. IMUL
 Performs same operation as MUL instruction but operates only on signed numbers
4. IDIV
 Performs same operation as DIV instruction but operates only on signed numbers.

5. CBW
Description: copies the sign bit of a byte in AL to all bits in AH.
Syntax: CBW

6. CWD
Description: copies the sign bit of a word in AX to all bits in DX register.

Page 7 of 57
LAB NO: 2

Syntax: CWD
7. LEA

Category : Data Transfer Instruction


Description : Determines offset of the variable or memory location named as source and loads into
16-bit register.
Syntax : LEA Register, Source

Solved Exercise

Write ALP to perform multiplication of two 16-bit unsigned numbers

data segment
a dw 0304h
b dw 0101h
c dw ?,?
data ends
code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov ax,a
mov bx,b
mul bx
mov c,ax
mov c+2,dx
mov ah,4ch
int 21h
code ends
end start

Sample Output

Lab Exercises

Page 8 of 57
LAB NO: 2

Write assembly language programs to do the following:

1. Multiply a 32-bit number by a 16-bit number


2. Multiply two 32-bit numbers.
3. Divide a 16-bit number by an 8-bit number.
4. Divide a 32-bit number by a 16 bit number.
5. Multiply a signed byte by a signed word.

Additional exercises

Write assembly language programs to do the following:

1. Multiply a 64 bit number by a 32 bit number.


2. Divide a signed word by another signed word.
--------------------------------------------------------------------------------------------------------------------------

Page 9 of 57
LAB NO: 3

LAB NO: 3 Date:

ARITHMETIC AND BRANCHING INSTRUCTIONS

Objectives:

In this lab, student will be able to

1. Learn different kinds of branching instructions in 8086.


2. Perform bit manipulation instructions in 8086.

REQUIRED SET OF 8086 INSTRUCTIONS

1. CMP
Category: Arithmetic Subtraction Instruction
Description: compare two specified bytes or two specified words by subtracting the source from
destination and the flags are set to indicate the comparison results. Both source and destination will remain
unchanged.
Syntax: CMP Destination, Source
 Destination can be register/memory location
 Source can be register/memory location/immediate number
 Both destination and source cannot be memory locations in the same instruction.
 All the six conditional flags can be affected.

2. Conditional and Unconditional Jump Instructions


Category: Program Execution Transfer Instruction
Description: Instruct 8086 to fetch instruction from some new address specified in the instruction based
on occurrence or non-occurrence of a specific condition. It will not affect any flags.

Unconditional JMP
 Instruct 8086 to fetch instruction from some new address specified in the instruction without the
occurrence of any condition.
Syntax: JMP label

Conditional JMP
 Instruct 8086 to fetch instruction from some new address specified in the instruction with the
occurrence of any condition.
 Syntax remains same as that of unconditional jump with different mnemonics according to the
condition.

Page 10 of 57
LAB NO: 3

Table 3.1 Conditional Jump Instructions [1]

3. Shift Instructions
Category: Bit Manipulation Instructions
SAL/SHL
Description : Shift operand bits left and put 0 in LSB
Syntax : SAL/SHL Destination, count
 Shift operand bits in destination towards left by specified count

Page 11 of 57
LAB NO: 3

 Destination can be register/memory location and if the count is greater than 1, it should be
loaded into CL register.
CF--------MSB----------LSB----0

SAR
Description : Shift operand bits right and new MSB= old MSB
Syntax : SAR Destination, Source
 Shift operand bits in destination towards right by specified number
 Destination can be register/memory location
 Source can be immediate number (if it is 1) or Count register CL (if it is >1)
MSB--MSB--------LSB----CF
SHR
Description : Shift operand bits right and put 0 in the MSB
Syntax : SHR Destination, Source
 Destination can be register/memory location
 Source can be immediate number (if it is 1) or Count register CL (if it is >1)
0MSB--------LSB----CF

4. Rotate Instructions
Category: Bit Manipulation Instructions
ROL
Description : Rotate operand bits in destination towards left by specified number.
Syntax : ROL Destination, Source
 Destination can be register/memory location
 Source can be immediate number (if it is 1) or Count register CL (if it is >1)

ROR
Description : Rotate operand bits in destination towards right by specified number.
Syntax : ROR Destination, Source
 Destination can be register/memory location
 Source can be immediate number (if it is 1) or Count register CL (if it is >1)

5. INC
Category : Arithmetic instruction
Description : Adds 1 to a specific register or a memory location
Syntax : INC Destination

Page 12 of 57
LAB NO: 3

 Destination can be a register or a memory location

6. DEC
Category : Arithmetic instruction
Description : Subtracts1 from a specific register or a memory location
Syntax : DEC Destination
 Destination can be a register or a memory location
7. AAA
Category : Arithmetic instruction
Description : Ensures that the result is in correct unpacked BCD format after addition of two
numbers in ASCII.
Syntax : AAA
 Works on AL register only
8. AAS
Category : Arithmetic instruction
Description : Ensures that the result is in correct unpacked BCD format after subtraction of two
numbers.
Syntax : AAS
 Works on AL register only
9. AAM
Category : Arithmetic instruction
Description : Adjusts the product to two unpacked BCD digits after the multiplication of two
unpacked BCD digits.
Syntax : AAM
 Works on AL register only
10. AAD
Category : Arithmetic instruction
Description : Converts two unpacked BCD digits in AH and AL to equivalent binary number in
AL. AAD should be used before division of two unpacked digits in AX by an
unpacked BCD byte.
Syntax : AAD
11. DAA
Category : Arithmetic instruction
Description : Adjusts the result of addition of two packed BCD numbers to be a legal BCD number.
Syntax : DAA
 Works on AL register only

12. DAS
Category : Arithmetic instruction
Description : Adjusts the result of subtraction of two packed BCD numbers to be a legal BCD
number.
Syntax : DAS

Page 13 of 57
LAB NO: 3

 Works on AL register only

Solved Exercise
Write ALP to unpack a 2 byte packed BCD number and represent in ASCII format.

data segment
bcd db 18h,15h
asc dw ?,?
count db 02
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov cl,4
lea si,bcd
lea di,asc
do:
mov al,[si]
mov ah,00h
rol ax,cl
shr al,cl
add ax,3030h
mov [di],ax
inc si
inc di
inc di
dec count
jnz do
mov ah,4ch
int 21h
code ends
end start

code ends
Page 14 of 57
end start
LAB NO: 3

Sample Output

Lab exercises
Write assembly language programs to do the following:

1. Find GCD and LCM of two numbers.


2. Generate Fibonacci series (up to N terms).
3. Convert a 16-bit number to ASCII.
4. Convert the given 4 byte unpacked BCD in ASCII format to packed BCD format.
5. Consider an array of bytes in the memory. Store the perfect numbers (A positive integer that
is equal to the sum of its factors excluding the number itself is called a perfect number) in
another array.

Additional exercises

Write assembly language programs to do the following:

1. Check whether a given number is perfect square or not.


2. Check whether given number is positive, negative or zero.
-----------------------------------------------------------------------------------------------------------------------------

Page 15 of 57
LAB NO: 4

LAB NO: 4 Date:

LIST OPERATIONS AND LOOP INSTRUCTIONS

Objectives:

In this lab, student will be able to:

1. Perform advanced list operations in a given list or array.


2. Write programs on different kinds of loop instructions.

REQUIRED SET OF 8086 INSTRUCTIONS

1. LOOP
Category : Iteration Control Instruction
Description : Repeats a series of instructions ‘n’ times and the count ‘n’ has to be loaded into CX register.
Syntax : LOOP label
 Each time the loop instruction executes, CX is automatically decremented by 1 and if CX≠0, execution
will go back to label given in the instruction.
 When CX=0, control goes to next instruction after LOOP instruction.

2. LOOPE/LOOPZ
Category : Iteration Control Instruction
Description : Loop as long as ZF=1 and CX! = 0
Syntax : LOOPE label

3. LOOPNE/LOOPNZ
Category : Iteration Control Instruction
Description : Loop as long as ZF=0 and CX! = 0
Syntax : LOOPNE label

4. XCHG
Category : Data Transfer Instruction
Description : exchange contents of a register or memory location with the contents of another register
or memory location
Syntax : XCHG Destination, Source
 Both source and destination cannot be a memory location in one instruction.

Page 16 of 55
LAB NO: 4

Solved exercise:

Write ALP to insert one element into an unsorted list in a given position

data segment
list db 9,4,3,1,7,?
ele db 6
pos db 3
data ends
code segment
assume cs:code,ds:data
start: mov ax, data
mov ds, ax
lea si,list
add si, pos
mov cl,3
mov al, ele
rep1: xchg al,[si]
inc si
dec cl
jnz rep1
code ends
end start

Sample Output

Lab Exercises

Write assembly language programs to do the following:

1. Sort an array in ascending order.


2. Insert 1 or more elements into a sorted list. The number to be inserted, and the elements can be
assumed to be present in successive memory locations.
3. Delete 1 or more elements from a sorted list. The number to be deleted and the elements can be
assumed present in successive memory locations.
4. Generate prime numbers between two given limits N1 and N2.
5. Convert 4 byte ASCII to binary.

Page 17 of 55
LAB NO: 4

Additional Exercises

1. Write an 8086 ALP to perform the following for the list of numbers stored in the memory
a. Count the total number of even and odd numbers in the list
b. Count the total number of elements whose value is greater than 10.
2. Write an ALP to search for an element in an array. The program has to insert the element in the array
there is no match and sort the resultant array.
3. Write an ALP to find the sum of all the elements in the given array
---------------------------------------------------------------------------------------------------------------

Page 18 of 55
LAB NO: 5

LAB NO: 5 Date:

STRING OPERATIONS

Objectives:

In this lab, student will be able to:

1. Define and perform operations on strings.


2. Execute ALP using various string instructions.

STRING INSTRUCTIONS

1. MOVS/MOVSB/MOVSW
Description : Copies a byte or a word from a location in the data segment to a location in the extra
segment.
Syntax : MOVS Destination, Source
 Offset of the source byte/word in the data segment must be contained in SI register and offset of
the destination byte/word in the extra segment must be in DI register.
 After a byte /word move, both SI and DI will be automatically adjusted to point to next source &
destination.
 If direction flag DF=0, both SI and DI will be automatically incremented by one or two after a
byte/word move.
 If DF=1, both SI and DI will be automatically decremented by one or two after a byte/word move.

2. LODS/LODSB/LODSW
Description : Loads string byte into AL or string word into AX pointed to by SI.
Syntax : LODS source_string

3. STOS/STOSB/STOSW
Description : Copies a byte from AL or a word from AX to a memory location in the extra segment
pointed to by DI
Syntax : STOSB/STOSW

4. CMPS/CMPSB/CMPSW
Description: To compare byte/word in one string with byte/word in another string
Syntax : CMPSB (Compares two strings byte by byte)
 SI holds the offset of source string defined in Data segment and DI holds the offset of destination
string defined in Extra segment.

5. SCAS/SCASB/SCASW
Description : Compares a byte in AL or a word in AX with a byte or word pointed to by DI in ES.
Syntax : SCASB (Scans byte by byte)
 The String to be scanned must be in extra segment and DI must contain the offset of byte or word
to be compared

Page 19 of 55
LAB NO: 5

6. LOCATION COUNTER
Description: To calculate length of the string. ‘$’ location counter symbol represents the current value of
the location counter (for example please refer the below program)

Solved Exercise
Write ALP to reverse the given string
data segment
str db "manipal#"
len equ $-str-1
str1 db 10 dup(0)
data ends
code segment
assume cs:code,ds:data,es:data
start:
mov ax,data
mov ds,ax
mov es,ax
mov cx,len
lea si,str+len-1
lea di,str1
again:mov al,[si]
mov [di],al
inc di
dec si
jnz again
mov al,[si]
mov [di],al
exit: mov ah,4ch
int 21h
code ends
end start

Sample Output

Page 20 of 55
LAB NO: 5

Lab Exercises
Write 8086 programs to:
1. Search for the occurrence of a character in a string and find its count.
2. Check if the given string is a palindrome or not.
3. Concatenate two strings, which are stored in non-consecutive memory locations.
4. Find the substring in the main string
5. Reverse the case of each character in a given string.

Additional Exercises

Write an assembly language program to do the following


1. Count the number of vowels in a given string.
2. Reverse the case of alphabets in a string consisting of alphabets and other characters.
----------------------------------------------------------------------------------------------------------

Page 21 of 55
LAB NO: 6

LAB NO: 6 Date:

DOS INTERRUPTS

Objectives:

In this lab, student will be able to:


1. Learn how an interrupt is handled in 8086.
2. Understand the functioning of different kinds of 8086 interrupts.
3. Perform operations on files

INTRODUCTION TO INTERRUPTS
 An 8086 interrupt can come from any three sources:
 Hardware interrupt: An interrupt caused by a signal applied to either NMI or INTR pin
 Software interrupt: Execution of INT instruction
 Some error condition produced as a result of execution of an instruction (divide-by-zero)
 At the end of each instruction cycle, 8086 checks for interrupt request and if so, it does series of major
actions as shown in the figure 6.1

Fig 6.1 8086 Interrupt Response[1]


Required Set of Interrupts
1. INT 21H / AH=01H
 Read character from standard input, with echo to the standard output device and result is stored
in AL.
 Expects
 AH=01H
 Returns
 AL=8 bit input data(ASCII code of input key)

2. INT 21H/AH=09H
 Writes a string pointed by DX, terminated with $ to standard output device.
 Expects
 AH=09H
 DS: DX = segment: offset of string terminated by ‘$’.
 Returns
 String to the standard output device.

Page 22 of 55
LAB NO: 6

3. INT 21H/AH=0AH
 Get a String Input from the keyboard and store in a buffer pointed by DX in DS.
 User has to define the buffer as Byte db max_length, actual_ length, space for storing the string
Ex: buffer db 15, 14 dup (0)
 Expects
 AH=0AH
 DS: DX = segment: offset of buffer
Returns: Stores the read string in buffer.
Note: The last byte in the buffer will be for enter key. Hence, the size is always one less than the
maximum size.

4. INT 21H/AH=3CH
 Create a new file given an ASCIIZ file specification.
 If the specified file already exists, it is truncated to zero length.
 Expects
 AH=3CH
 CX=0 (file attribute)
 DS: DX = segment: offset of ASCIIZ file specification
 Returns
 If no error: CF=clear, AX=file handle
 If error : CF=set, AX=error code

5. INT 21H/AH=3DH
 Open a specified file in the designated or default directory and a file handle (16-bit token) will be
returned for further access of the file.
 Expects
 AH=3DH
 DS: DX = segment: offset of ASCIIZ file specification
 AL= access and file sharing modes
00H=read only
01H=write only
02H=read/write
 Returns
 If no error : CF=clear, AX=file handle
 If error : CF=set, AX=error code

6. INT 21H/AH=3EH
 Close a file specified by file handle
 Expects
 AH=3EH
 BX = file handle
 Returns
 If no error : CF=clear
 If error : CF=set, AX=error code

7. INT 21H/AH=3FH
 Read a file specified by file handle and stores in designated file buffer and updates file pointer to
point to next record.

Page 23 of 55
LAB NO: 6

 Expects
 AH=3FH
 BX = file handle
 CX=no. of bytes to read
 DS:DX = segment: offset of buffer space.
 Returns
 If no error : CF=clear, AX=Actual no. of bytes read
 If error : CF=set, AX=error code
 If AX=0 : reading from EOF

8. INT 21H/AH=40H
 Write the contents of designated buffer to a file specified by file handle and updates file pointer to
point to next record.
 Expects
 AH=40H
 BX = file handle
 CX=no. of bytes to write
 DS:DX=segment: offset of buffer space.
 Returns
 If no error : CF=clear, AX=no. of bytes actually written
 If error : CF=set, AX=error code

9. INT 21H/AH=42H
 Move the file pointer to desired address i.e., sets the file pointer location relative to start of a file,
end of a file or the current file position.
 Expects
 AH=42H
 BX = file handle
 CX:DX=contains a 32-bit unsigned long integer that specifies how many bytes to move the
file pointer from the position specified in AL.
 AL= 0 for byte offset from start of the file
1 for byte offset from current location
2 for byte offset from end of file
 Returns
 If no error : CF=clear, DX:AX=new position of file read/write pointer
 If error : CF=set, AX=error code

10. INT 21H/AH=02H


 Writes a character to the standard output device.
 Expects
 AH=02H
 DL=8 bit output data in ASCII
 Returns
 Character returned to the output device
Note: Refer INTRLIST for bitwise details on file attribute of interrupt INT 21H/AH=3CH, access
and sharing modes of interrupt INT 21H/AH=3DH.

Page 24 of 55
LAB NO: 6

Solved Exercise
Write ALP to read a string from the user and count the number of words in the string. Display the
result on the screen. [If the count is greater than 9 , display the count in decimal. Hint: Divide the
count value by 10 to extract the digits]
data segment
str1 db 'enter string:$'
msg1 db 0dh,0ah,'No. of words are:', 0dh, 0ah, '$'
str11 db 15,?,14 dup(0)
count db 1
data ends
code segment
assume cs:code, ds:data, es:data
start: mov ax,data
mov ds,ax
mov es,ax
mov ah,09h
mov dx,offset str1
int 21h
mov dx,offset str11
mov ah,0ah
int 21h
mov al,' '
mov cl, str11+1
lea si,str11+2
back: cmp [si],al
je space
inc si
dec cl
jnz back
jmp last
space: inc si
dec cl
cmp [si],al
jz space
inc count
jmp back
last: lea dx, msg1
mov ah, 09
int 21h
mov dl, count
add dl, 30h
mov ah, 02
int 21h
mov ah, 4ch Page 25 of 55
int 21h
code ends
end start
LAB NO: 6

Sample Output

Lab Exercises

1. Read a string from the keyboard, toggle all lowercase alphabets, and display it on the screen.
2. Read a multi digit number from the keyboard. Calculate the sum of the digits of that number
and display the sum in decimal form.
3. Create a file and write into it.
4. Read a string from the keyboard, append it to the contents of an existing file and display the
contents of the file on the screen.

Additional Exercises
Write assembly language programs to do the following
1. Read a file and write the contents into another file.
2. Read a string from the keyboard, reverse the string and display it on the screen.
---------------------------------------------------------------------------------------------------------------

Page 26 of 55
LAB NO: 7

LAB NO: 7 Date:

INTRODUCTION TO PROCEDURES AND MACROS IN 8086

Objectives:
In this lab, student will be able to:

1. Define and differentiate macros and procedures in 8086.


2. Illustrate different ways of passing parameters to procedures.
3. Understand how to pass parameter to a macro.
4. Learn how to call a procedure in a recursive manner.

WRITING AND USING ASSEMBLER MACROS


 A macro is a group of instructions we bracket and give a name to at the start of the program.
 For every macro call, assembler inserts the set of instructions in the place of call and this is known
as ‘expanding the macro’.

Defining and Calling Macros


a) Without Parameters
 Defining Macro
macro_name MACRO
……
……
ENDM
 Calling Macros
macro_name
b) With Parameters
 Defining Macro
macro_name MACRO parameter 1, parameter 2,.. parameter n
……
……
ENDM
 Calling Macros
macro_name values for each parameter separated by commas.

WRITING AND USING PROCEDURES


 When we have to repeat a particular sequence of instructions at several points in a program, we
can write the sequence as a separate subprogram called “procedure”.
 In order to execute the sequence of instructions contained in the procedure, we use CALL
instruction to send the control to the starting address of the procedure in the memory.
 A RET instruction at the end of the procedure returns the execution to the next instruction in the
mainline.
 Procedures can be nested.

Page 27 of 55
LAB NO: 7

Fig 7.1 Program flow to and from procedures[1]


(a) Single procedures. (b) Nested Procedures.
 Procedures can be classified into two categories:
 Near Procedure: Procedure is in the same code segment as the calling program.
 Far Procedure: Procedure is in different code segment and the calling program is in another code
segment.

Defining and Calling Procedures


Procedure Definition
Procedure_Name PROC Attribute
.
.
RET
Procedure_Name ENDP
 PROC is a statement used to indicate the beginning of a procedure or subroutine and ENDP indicates
the end of the procedure.
 Attribute can be NEAR or FAR depending on the procedure.
Procedure Call
CALL Procedure_Name
Passing parameters to and from procedures
There are four ways of passing parameters to and from procedures and they are
 In registers
 In dedicated memory location accessed by name
 With pointers passed in registers
 With the stack
Recursive Procedure
 Procedure which calls itself

Page 28 of 55
LAB NO: 7

Solved Exercise
Write a procedure to convert a BCD number to binary where the operands will be passed using
pointers. Write a program that uses this procedure.
data segment
bcd db 25h
binary db ?
data ends
stack_seg segment stack
dw 100 dup(0)
top_stack label word
stack_seg ends
code segment
assume cs:code, ds:data, ss:stack_seg
start: mov ax, data
mov ds,ax
mov ax,stack_seg
mov ss,ax
mov sp, offset top_stack
mov si, offset bcd
mov di, offset binary
call bcd_bin
mov ah,4ch
int 21h
bcd_bin proc near
push ax
push bx
push cx
mov al, [si]
mov bl, al
and bl,0fh
and al ,0f0h
mov cl, 04
ror al, cl
mov bh,0ah
mul bh
add al,bl
mov [di], al
pop cx
pop bx
pop ax
ret
bcd_bin endp
code ends
end start

Page 29 of 55
LAB NO: 7

Sample Output

Lab Exercises:

1. Write a macro to display the string on the screen. Use this macro to display the file contents in the
console.
2. Write a recursive procedure to calculate factorial of a number.
3. Write a procedure named multiply that computes the product of two unsigned 16-bit operands. The
operands will be passed through the stack. The procedure should return the result in AX and DX.
Write a program that uses the multiply procedure.
4. Write a procedure to generate Fibonacci series for a given value of n. Write a program to use this
procedure.
5. Define two macros to add and subtract two 8-bit numbers. Pass the input values as parameters to
the Macro. With the help of macros, evaluate the expression x+y-z.

Additional exercises:
1. Write a procedure to sort elements in an array. Use this procedure to sort an array input from the user.
2. Write a procedure to find the cube of a single digit number. Use this procedure to find the sum of the
Cubes of the digits of a multi-digit number.
------------------------------------------------------------------------------------------------------------------

Page 30 of 55
LAB NO: 8

LAB NO: 8 Date:

DISPLAY INTERFACING

Objectives:
In this lab, student will be able to:

1. Understand the different LED displays


2. Write 8086 ALP to interface display interface with 8086
3. Infer the applications of seven segment LED display

INTRODUCTION TO 8255A:

What is 8255A?
It is a Programmable Peripheral Interface used with 8086 microprocessors to control peripheral I/O devices.
It contains three 8-bit ports which can be programmed. Ports if programmed, can operate in different modes
and hence can do different types of parallel data transfer.

Internal architecture of 8255A:

Fig 8.1 Internal Architecture of 8255A[1]

Page 31 of 55
LAB NO: 8

The internal architecture of 8255A is shown in Fig 8.1.


 It has 24 input/output lines
 Three ports Port A, Port B, Port C
 Port A:
 PA0 to PA7
 It can be used as an 8-bit input port/8-bit output port
 Port B:
 PB0 to PB7
 It can be used as an 8-bit input/output port
 Port C:
 Lower half - PC0 to PC3
 Upper half - PC4 to PC7
 It can be used as 8-bit input/output port, two 4-bit ports, to produce handshake signals for
ports A and B
 8-bit data bus
 Bytes can be written to a port or control register or bytes can be read from a port under the control
of RD and WR control lines.
 Address lines A1and A0 allow we to selectively access ports/control register. The below table
Table 8.1 shows the selective access.
Table 8.1 Selective Access of Ports/Control Register in 8255A[1]
A1 A0 Port/ Control register
0 0 Port A
0 1 Port B
1 0 Port C
1 1 Control register

 RESET input--- When the system is reset, all port lines were initialized as input lines to prevent
circuitry damage

Operational modes of 8255A:


There are three operational modes viz., mode 0, mode 1, and mode 2.

Mode 0:
 A port must be initialized in this mode if we want to use it for simple input/output operations.
 If ports A and B are initialized in Mode 0, then port C can be used as 8-bit port or two 4-bit ports
 2 halves of port C are independent.
 For example, let us say that the thermostat is connected to port A and we want to read
temperature 24/7. In this case, port A will be initialized in mode 0 and will be used as 8-bit input
port.

Constructing and sending mode definition control word:


The following diagram, Fig 8.2 can be used to construct mode definition control word

Page 32 of 55
LAB NO: 8

Fig 8.2 Mode Set Control Word Format[1]


 For instance, if we want to configure port A as mode 0 output, port B as mode 1 input, port C
upper as inputs, port C bit 3 as output, then the mode definition control word will be 10001110
(binary).

Sending constructed mode definition control word to control register:


Step 1: Determine the actual address of control register
 Find out the base address of 8255A
 Add 11 in binary or 03 in hex to the base address of 8255A to get the actual address
 For example, if the base address of 8255A is e0c0h then the actual address of control register is
e0c3h
Step 2: Sending the control word to control register

IN and OUT instructions:


IN:
Description: Input from port into AL or AX
Syntax:
MOV DX, port_no
IN destination, DX where destination can be AL or AX, and port_no is the absolute address
of the port
First we need to move the port_no to DX register
Second we input from port to AL or AX register depending whether the port is 8-bit or 16-bit
Example:
MOV DX, 0e0c1h
IN AL, DX

Page 33 of 55
LAB NO: 8

No flags are affected.


OUT:
Description: Output from AL or AX to port.
Syntax:
MOV destination, data
MOV DX, port_no
OUT DX, destination where destination can be AL or AX, and port_no is the absolute
address of the port.
 We need to load the data into AL or AX register depending on whether the port is 8-bit or 16-bit.
 Then move the port_no to DX register.
 Finally, send the data to the output port device.
Example:
MOV AL,01101111b
MOV DX, 0e0c0h
OUT DX, AL
No flags are affected

Example code snippet to send 10001110 (binary) mode definition control word to control register at
address e0c3h is shown below

MOV AL, 10001110B


MOV DX, 0E0C3H
OUT DX, AL

Knowing the command “allowio”:


This command allows the user to access the 8255A installed in the computer. This is the very first
command that a user will type in the command window for any interfacing program.
Syntax: allowio <base address of 8255A> <filename.exe> . For example, “allowio 0xa400
filename.exe” where a400 is the base address of 8255A in hexa-decimal number system.

INTRODUCTION TO DISPLAY INTERFACE:


Information processed by the microprocessor has to be displayed in intelligible form to the user. There are
two types of displays (i) non-multiplexed displays (ii) multiplexed displays. The display interface we work
with is a 6-digit multiplexed interface wherein each digit is turned ON for a short interval of time after
latching the data and is repeated at a rate of at least 70 to 80 HZ to ensure that the display is stationary.
Seven segment LED display is the basic unit in our display interface. There are six such units in the display
interface. Seven segment LED display is the cheapest of LED displays. Another commonly used LED
display is 5 by 7 dot matrix display which we can find in the elevators of AB5 of MIT. What we see in Fig
8.4 is a display interface with four seven segment LED displays.

Fig 8.4 Four Seven Segment LED Display[2]

Page 34 of 55
LAB NO: 8

A Seven segment LED display:

Fig 8.5 Individual Segments of a Seven Segment Display[2]


The seven segments are labelled in a certain fashion as we see in Fig 8.5. The decimal point that follows
the seven segment display is labelled “H”.

How ports of 8255A are connected to the display interface?


Port A line of 8255A is connected to the segments of the seven segment displays as shown in Table 8.2

Table 8.2 Connection of 8255A Port A Lines to Individual Segments[1]


Port line Segment
PA0 A
PA1 B
PA2 C
PA3 D
PA4 E
PA5 F
PA6 G
PA7 H

Port lines PC2, PC1, PC0 are connected to the seven segment displays in such a manner that they can be
used to turn on the desired seven segment display. Table 8.3 lists the possible port line combinations and
seven segment display enabled
Table 8.3 Connection of 8255A Port C Lines to Individual Units of Seven Segment Display[2]

PC2 PC1 PC0 Seven segment


display
0 0 0 D1
0 0 1 D2
0 1 0 D3
0 1 1 D4
1 0 0 D5
1 0 1 D6

In short, Port C lines are used to select or turn ON the desired seven segment display and Port A lines are
used to display something on the turned ON seven segment display. Normally for a 6-digit display, this is
done for about 2msec. For the next 2msec, the data for the next seven segment is sent to Port A and the
corresponding code to Port C. This is repeated till all the seven segment displays on the display interface
are serviced, at which time the whole sequence is again restarted.

Page 35 of 55
LAB NO: 8

How to generate binary pattern for a character to be displayed on a seven segment display?

Not all the characters can be displayed on seven segment display. For instance, characters like M, V, W, X
and so on cannot be displayed. This is a major drawback of seven segment display. Yet, we will see how
to generate binary pattern for some of the displayable characters.

Referring Fig 8.6, first write the segment labels in the descending order as shown below

Fig 8.6 Encoding format for a seven-segment display unit

Second, for the character, whichever segments need to be turned on, go to those segment labels in Fig 8.6
and put binary value ‘1’. For example, if ‘S’ is the character to be displayed on seven segment display, then
binary value ‘1’ will be put in segment labels ‘A’, ‘F’, ‘G’, ‘C’, ‘D’ and hence the binary pattern would be
01101101b or 6Dh.Table 8.4 tells seven segment code for some of the displayable characters.

Table 8.4 Hexadecimal Code for Displayable Characters[2]


Character Code
0 3F
1 06
2 5B
3 4F
4 66
5 6D
6 7D
7 07
8 7F
9 6F
A 77
B 7C
C 39
D 5E
E 79
F 71

Connections details:
Connect the bus from 8255A to the display interface.

Sample data segment definition:


To display “fire” message on the display interface, the following data segment definition would do the job.

Page 36 of 55
LAB NO: 8

data segment
base_address equ 0a400h
control equ base_address + 03h
porta equ base_address+00h
portb equ base_address+01h
portc equ base_address+02h
fire DB 0,0,79H,50H,06H,71H
data ends

The above data segment definition format can be used to write data segment definition for any interfacing
experiment. It is advisable to use EQU directive to equate the values to variables. An 8255A will have a
base address. In Fig 8.7, it is a400H and is equated to variable base_address. The ports of 8255A can be
selectively accessed as shown in Table 8.1.

It is to be noted that in the above definition porta, portb, portc are used to refer to ports Port A, Port B, Port
C of 8255A respectively. In the data segment definition, the absolute addresses of ports are to be written.
Absolute address is the sum of base address and offset. For example, base_address+00h is the absolute
address of Port A of 8255A and hence equated to variable porta. This is applicable for other ports and
control register as well.

Some code snippets for reference:


1. The following snippet can be used to flush the content of display interface

mov dx,portc
mov al,06h
out dx,al

2. The following snippet can be used to insert a delay between messages

delay proc near


mov cx,0fff1h
l1:dec cx
jnz l1
ret
delay endp

Page 37 of 55
LAB NO: 8

Lab Exercises
With the brief introduction of display interface and knowledge on 8255A,
Write 8086 ALP for the following:
1. Display a 4-digit BCD number on the display interface.
2. Display messages FIRE and HELP alternately with flickering effects on the display
interface for a given duration. Ensure a flashing rate that makes both the messages readable.

Additional Exercises
1. Convert a 16-bit binary number (assumed an unsigned integer) to BCD and display it on the
display interface.
2. Define a string of six characters in the memory. Display the string and its reversed string
alternately with flickering effects on the display interface for a given duration.
-------------------------------------------------------------------------------------------------------------

Page 38 of 55
LAB NO: 9

LAB NO: 9 Date:

KEYBOARD INTERFACING

Objectives:
In this lab, student will be able to:

1. Write 8086 ALP to interface matrix keyboard with 8086


2. Infer the applications of matrix keyboard interface

INTRODUCTION TO MATRIX KEYBOARD:


A keyboard provides a uniform input to the microprocessor. This increases design flexibility since changes
in control functions do not affect the interfaces.

In Fig 9.1,the keyboard consists of 24 keys having calculator type keycaps. It consists of numerals 0 to 9,
the special characters +, -, *, ÷, %, C, CE, and five blank keys. The keys are organized as three rows of
eight keys each in a sense matrix of three rows and eight columns. Hence matrix keyboard.

Fig 9.1 Keyboard Interface[2]

The three rows of matrix keyboard are connected to port lines PC0, PC1 and PC2 and the eight columns
are connected to port lines PA0 to PA7 of 8255. The key code corresponding to each key may be assigned
as shown in the following Table 9.1
Table 9.1 Key codes corresponding to each key in the keyboard interface[2]
Key Code Key Code
0 00 * 0C
1 01 ÷ 0D
2 02 = 0E
3 03 % 0F
4 04 C 10
5 05 CE 11
6 06 BLANK 12

Page 39 of 55
LAB NO: 9

7 07 BLANK 13
8 08 BLANK 14
9 09 BLANK 15
+ 0A BLANK 16
- 0B BLANK 17

So PC0, PC1 and PC2 can be used to select a row. Refer Table 9.2 for port C lines combinations and row
selections.

Table 9.2 Row Selection Using Port C of 8255A


PC2 PC1 PC0 Row Row
selection selected
code
0 0 1 01 01
0 1 0 02 02
1 0 0 04 03

Port A lines act as data lines since they can be used to read the selected row.

Connection Details
 Connect the master/slave bus cable to the keyboard

Sample code snippet:


1.The following snippet may be used to find out which key pressed in the selected row.

Page 40 of 55
LAB NO: 9

l:
mov ah,0
mov cx,3
mov bl,1
l1:
mov al,bl
mov dx,portc
out dx,al
mov dx,porta
in al,dx
cmp al,0
jnz l2
add ah,8
rol bl,1
loop l1
jmp l
l2:
ror al,1
jc l3
inc ah
jmp l2

3. The following snippet may be used to pass row number to disp procedure for display (where bl has row
number)

cmp bl,04h
jne l5a
dec bl
l5a:
mov res,bl

4. The following snippet (disp procedure) may be used to display row value, column value, and key value
of the key pressed on screen (where dl has the value). M1 is a macro that prints the ASCII character for
the ASCII code passed, M2 is a macro that prints a blank character.

Page 41 of 55
LAB NO: 9

disp proc near

cmp dl,0ah
jnae l6
cmp dl,10h
jnc l7
M2
add dl,37h
M1 dl
ret

l7: mov cl,04h


and dl,0f0h
ror dl,cl
add dl,30h
M1 dl
mov dl,res
and dl,0fh
add dl,30h
M1 dl
ret
l6:
M2
add dl,30h
M1 dl
ret
disp endp

Lab Exercises
With the brief introduction of matrix keyboard and knowledge on 8255A,
Write 8086 ALP for the following:
1. Scan the keyboard for a key press and store the code of the key pressed in a memory location. Display
the same on the screen.
2. Scan the keyboard for a key closure. Get the row number and column number of the key pressed and store
it in some memory locations and display the same.

Additional Exercises
1. Simulate matrix keyboard for a calculator with the basic operations addition, subtraction.
multiplication and division.

---------------------------------------------------------------------------------------------------------------

Page 42 of 55
LAB NO: 11

LAB NO: 10 Date:


ELEVATOR INTERFACING

Objectives:
In this lab, students will be able to,

1. Write 8086 ALP to interface elevator interface with 8086.


2. Infer the applications of elevator interface.

INTRODUCTION TO ELEVATOR INTERFACE:


Description:
An elevator presents a number of situations like the occurrence of simultaneous requests, the assignment
of priorities and the logging of requests. The control and operation of an elevator functioning within a
building, having four floors, is simulated by this interface. A key and corresponding LED indicator serve
as a request button and request status indicator. A row of LEDs indicates the position of the elevator within
the shaft.

A four line to ten-line decoder is used to specify the location of the elevator within the shaft. When the four
inputs to the decoder are sequenced through the binary codes corresponding to 0 to 9 with a time interval
between transitions, the elevator is seen to move. Two D flip-flops are used to sense requests and output
the status. A request is cleared because of servicing through the clear inputs of the flip-flops.
The Green LED’s goes ON, indicating the floor, while the Amber LED’s goes ON, indicating the elevator
to be in between 2 floors. The Red LED’s and Push button switch (SW0 to SW3) are used as floor request
status indicators and floor request buttons respectively.

Fig 10.1 Elevator Interface[2]


In Fig 10.1, the signal flow in elevator interface is depicted. Port A of 8255A is to be configured as output
port, the lower order port lines (PA3-PA0) being used as input to the decoder to specify the position of

Page 43 of 55
LAB NO: 11

elevator, the higher order port lines (PA7-PA4) being used to clear a request via the corresponding D flip
flop. Port B of 8255A is to be configured as input port, the lower order port lines (PB3-PB0) being used to
read the output from D flip flop so as to serve the current request (moving the elevator up or down), the
higher order port lines are left unused. A decoder is a combinational circuit with n inputs and 2 n outputs.
The BCD to Decimal decoder in Fig 10.1 has 4 inputs and 16 outputs of which only 10 outputs are used.
The other 6 outputs are left unused. It accepts inputs in the range 0000b to 1001b. Only one among the 10
outputs is active at a given point of time. A D flip flop is a sequential circuit with one input ‘D’ and two
outputs ‘Q and Q+’. If D is 1, Q+ is 1, else Q+ is 0.
Connection details:
Connect the bus from 8255A to the elevator interface.
Sample code snippet:
1. The following code snippet may be used for upward, downward movement of the elevator

BASE_ADDRESS EQU 0Ce00H


PORTA EQU BASE_ADDRESS + 0
PORTB EQU BASE_ADDRESS + 1
PORTC EQU BASE_ADDRESS + 2
CNTRL EQU BASE_ADDRESS + 3
ST0: MOV AL,82H
MOV DX,CNTRL
OUT DX,AL
EL00: MOV BL,0FFH
EL09X: INC BL
MOV AL,BL
JZ EL10X
OR AL,0F0H
EL10X: MOV DX,PORTA
OUT DX,AL
CALL DELAY
CMP BL,09H
JNZ EL09X
EL05: MOV BL,0AH
EL09: DEC BL
MOV AL,BL
JZ EL00
EL95: OR AL,0F0H
EL10: MOV DX,PORTA
OUT DX,AL
CALL DELAY
MOV AH,0BH
INT 21H
OR AL,AL
JNZ QUIT
JMP EL09

Page 44 of 55
LAB NO: 11

2. The following code snippet may be used to generate the required delay

DELAY:
MOV DX, 0FFFH
LP05:
MOV CX, 0FFFFH
HERE: LOOP HERE
DEC DX
JNZ LP05

3. Steps to adjust the elevator and clear a request


i. The input to the decoder for the current elevator position may be compared with the value of PB3-
PB0 to decide upon the direction of the elevator motion and also the value by which the elevator
has to move.
ii. Two procedures UP and DOWN may be defined to take care of upward motion and downward
motion of the elevator
iii. Value 0 may be sent on port B lines to clear the corresponding flip flop thereby clearing the request

Lab Exercises
With brief introduction of elevator interface and knowledge on 8255A,
Write 8086 ALP for the following:
1. Simulate the elevator motion by interfacing the elevator kit
i. Initially the elevator should be in the ground floor, with all requests in OFF state.
ii. When a request is made from a floor, the elevator should move to that floor, wait there
for a couple of seconds, and then come down to ground floor and stop. If any requests
occur during going up or coming down, they should be ignored.
Additional Exercises
1. Repeat the above exercise including the following additives
i. A particular request is cleared as soon as the elevator reaches the desired floor
ii. Change the speed of the movement of the elevator
---------------------------------------------------------------------------------------------------------------

Page 45 of 55
LAB NO: 11

LAB NO: 11 Date:


DAC AND ADC INTERFACING

Objectives:
In this lab, students will be able to

i) Write 8086 ALP to interface DAC Kit , ADC Kit with 8086
ii) Learn and generate digital waveform patterns.
iii) Infer the applications of Digital-to-Analog converter, Analog-to-Digital converter

INTRODUCTION TO DAC INTERFACE:


Microprocessor based systems very often require interface that can convert on board digital signals to
equivalent analog signals for specific applications. Digital-to-Analog converters are used for a range of
applications which include waveforms generation, pattern generation, signal filtering and correlation. The
DAC interface consists of two digital to analog converters, the outputs of which are converted to voltages.

Data flow:

Fig 11.1 Data flow from 8086 to CRO[2]


Let’s see the meaning of 1, 2, 3 numbering in Fig 11.1
1. Digital binary patterns to generate the waveform are sent to microprocessor 8086 in the form of ALP
2. 8086 forwards the binary patterns to DAC interface
3. DAC interface converts digital signal to the required analog waveform, which can be viewed using the
oscilloscope (CRO)

How to do calibration with CRO?


1. With the help of the hook end of connector of the CRO probe, calibration can be done. If the outcome
is a square wave, then calibration is successful.
2. If channel 1 in CRO is the active channel then the button meant for channel 1 should be turned on.
This feature applies for channel 2 if channel 2 is the active channel.
3. Make the necessary adjustments in the CRO to obtain the required waveform.

Connection details:
1. Connect the power supply
2. Connect the bus from 8255A to the DAC interface.
3. From DAC interface to CRO:
(i) Connect the 2 wires of OUT (DAC) to the hook end of the connector to the probe that comes with
CRO.
PORTA: black wire
PORTB: orange wire
(ii) Connect the 2 wires of GND (DAC) to the opposite end of the hook of the connector of the cable
that comes with CRO.

Page 46 of 55
LAB NO: 11

How to generate digital waveform pattern?


1. Use logic value FFH to generate the high end of the waveform
2. Use logic value 00H to generate the low end of the waveform
3. Use necessary delay procedure to generate delay between the two ends of the waveform

Sample code snippet:


1. That generates triangular wave
UP2: MOV DX,PORTB
MOV AL, 00H

UP: OUT DX, AL


INC AL
CMP AL, 0FFH
JNZ UP

UP1: DEC AL
OUT DX, AL
CMP AL, 00H
JNZ UP1

INTRODUCTION TO ADC INTERFACE:


The ADC (Analog-to-Digital Converter) is an 8-bit converter. The features of ADC are listed below

 Resolution—8bits
 Conversion time—100 micro sec
 Single supply—5V dc
 8 channel multiplexer
 0 to 5V analog input voltage range with single 5V supply
 Low power consumption—15mW
 Latched tristate output

Steps involved in Analog-to-Digital conversion in ADC interface:

 Channel selection is done


 Conversion is initiated
 Converted digital output is read by ADC
 End of conversion is done by polled or interrupt mode

How ports of 8255A are connected to ADC interface?

Page 47 of 55
LAB NO: 11

In ADC interface, channel selection is done using port lines PC0, PC1 and PC2, START (conversion) and
ALE (Address Latch Enable) is controlled by port line PC7. Converted digital output is read by ADC
through Port A lines PA0-PA7 by enabling OE (Output Enable). Table 11.1 summarizes it.

Table 11.1
Port lines Description
PA0-PA7 Connected to data line D0-D7
PC0-PC2 Channel selection
PC7 Start & ALE
PC6 OE

Applications of ADC:

 Process control to consumer applications


 Machine control to automotive applications

Operation of ADC:

The required channel is selected by sending the channel number on PC0-PC2 lines. When ALE is toggled
through port line PC7, the channel number is latched on to ADC and since PA7 line is connected to START
pin, conversion will begin at the same time. After conversion, EOC (End Of Conversion) of ADC will go
high. Now that EOC can be realized via one of the operating modes of ADC.

Operating modes of ADC:

i. Polled mode: In this mode, 8086 continuously monitors or polls EOC line. When this line goes
high, OE should be made high and then low. This will put the digital equivalent of analog voltage
of the given channel on data lines of ADC. The digital data through port lines PA0-PA7 is read
and is displayed on data field of seven segment display.
ii. Interrupt mode: In this mode, connect JP6 pin1 to interrupt pin. When EOC goes high processor
is interrupted. In the ISR (Interrupt Service Routine), the digital output is read through port lines
PA0-PA7 after OE is toggled. The data is displayed on data field of seven segment display.

Connection details:
Connect the bus from 8255A to the ADC interface.

Sample code snippet:


1. The following delay procedure may be used to generate the required delay

Page 48 of 55
LAB NO: 11

MOV CX, 035H


; other lines
DELAY PROC NEAR
LUP2: PUSH CX
MOV CX, 0FFFFH
LUP1: LOOP LUP1
POP CX
LOOP LUP2
RET
DELAY ENDP

2. The following snippet may be used to get channel number

; get channel number


MOV AH, 1
INT 21H
SUB AL, 30H
MOV CH_ADDR, AL
CMP AL, 08
; other lines
;Where CH_ADDR has channel
number

3. The following snippet may be used to output channel number

; output channel number


MOV AL, CH_ADDR
MOV DX, PORTC
OUT DX, AL

4. The following snippet may be used for conversion

; start conversion
; PC7 (START/ALE) set
MOV AL, 0FH
MOV DX, CONTROL
OUT DX, AL
MOV CX, 20H
CALL DELAY
; PC7 reset
MOV AL, 0EH
MOV DX, CONTROL
OUT DX, AL

5. The following snippet may be used for polling EOC line

Page 49 of 55
LAB NO: 11

; look for EOC


; reset PC6 to read EOC
MOV AL, 0CH
MOV DX, CONTROL
OUT DX, AL
; poll the EOC line which is connected to PA7 line
AD01: MOV DX, PORTA
IN AL, DX
AND AL, 80H
CMP AL, 80H
JNZ AD01

Lab Exercises
With brief introduction of DAC interface, ADC interface and knowledge on 8255A,
Write 8086 ALP for the following:

1. To generate the waveform in Fig 11.2 using the DAC interface. The output of the DAC is to be
displayed on the CRO.

Fig 11.2
2. To generate
(i) Square wave
(ii) Triangular wave
3. To store the converted ADC value in a memory location

Additional Exercises
1. Generate rectangular wave using DAC interface
2. Generate saw-tooth wave using DAC interface
--------------------------------------------------------------------------------------------------------------------------

Page 50 of 55
LAB NO: 12

LAB NO: 12 Date:

STEPPER MOTOR AND LOGIC CONTROLLER INTERFACING

Objectives:
In this lab, students will be able to
(i) Understand the different parts of stepper motor and the working of the same
(ii) Write 8086 ALP to interface stepper motor, logic controller with 8086
(iii) Infer the applications of stepper motor, logic controller

INTRODUCTION TO STEPPER MOTOR:


A stepper motor is a typical motor designed to rotate by very small angles like 0.2, 0.1 degrees. Stepper
motors are very useful electromechanical transducers for position control as shown in Fig 12.1. They are
used in a number of industrial control applications. They find applications in printers, in CD/DVD drives
to rotate the read/write head by very small angles.

Fig 12.1 Stepper Motor[2]

Like a typical motor, a stepper motor also has 2 essential parts (i) stator (ii) rotor. Stator does not move
whereas rotor moves. For the rotor to move stator is essential. Stator usually has electromagnets and the
number of electromagnets is variable. Likewise rotor usually has teeth and the number of teeth is variable.
In fact the angle by which the rotor rotates depends on the number of teeth in the rotor determined by the
formula
Angle of rotation= 360/number of teeth in the rotor

As shown in Fig 12.2, our stepper motor has four electromagnets in its stator and six teeth in its rotor. So
the angle by which rotor rotates is 60 degrees. Practically speaking real time stepper motors will have lots
of teeth.

Fig 12.2 Internal View of Stepper Motor[2]


Page 51 of 55
LAB NO: 12

Working:
Stepper motor works based on the concept of electromagnetic force. When current passes through an
electromagnet it becomes a magnet. Hence it will have two poles north and south poles. Each teeth in the
rotor is a permanent magnet. Hence each teeth will have two poles north and south poles. By the principle
“Opposite poles attract each other” the rotor rotates.

Connection details:
Connect the bus from 8255A to the stepper motor interface. It is to be noted that stepper motor is different
from stepper motor interface. Stepper motor is operated via the stepper motor interface.

How to generate binary patterns to rotate the motor clockwise and anticlockwise?
For clockwise or anticlockwise motion we need to excite the electromagnets in proper order

Clockwise motion:
For clockwise motion Table 12.1 or Table 12.2 may be followed to excite the electromagnets in order
Table 12.1 Truth Table for clockwise rotation in active high representation[2]
A B C D Code
1 0 0 0 08h
0 1 0 0 04h
0 0 1 0 02h
0 0 0 1 01h

Table 12.2 Truth Table for clockwise rotation in active low representation[2]
D C B A Code
1 1 1 0 0eh
1 1 0 1 0dh
1 0 1 1 0bh
0 1 1 1 07h

Anti-clockwise motion:
For anti-clockwise motion Table 12.3 or Table 12.4 may be followed to excite the electromagnets in order
Table 12.3 Truth Table for anti-clockwise rotation in active high representation[2]

A B C D Code
0 0 0 1 01h
0 0 1 0 02h
0 1 0 0 04h
1 0 0 0 08h

Table 12.4 Truth Table for anti-clockwise rotation in active low representation[2]
D C B A Code
0 1 1 1 07h
1 0 1 1 0bh
1 1 0 1 0dh
1 1 1 0 0eh

Page 52 of 55
LAB NO: 12

Sample data segment definition:


For anti-clockwise motion:
dataseg segment
PHASE_A EQU 07h
PHASE_B EQU 0bh
PHASE_C EQU 0dh
PHASE_D EQU 0eh
dataseg ends

Sample code snippet:

1. The following delay procedure may be used to create delay between the rotations. It is to be noted that
Port C of 8255A drives the stepper motor.

DLY PROC NEAR


MOV CX, 03FH
ABVE: MOV BX, 0FFFFH
DLY1: DEC BX
JNZ DLY1
LOOP ABVE
RET
DLY ENDP

INTRODUCTION TO LOGIC CONTROLLER INTERFACE:

Logic controller is used in industry to exercise programmed control over a process. The nature of control
could range from a simple ON/OFF type of control to complex systems implementing sophisticated control
algorithms while accepting multiple inputs and actuating multiple outputs. The logic controller interface
consists of three 8-bit ports viz., port A, port B and port C wherein Port A is Output port , Port B is Input
port, Port C lower half is Output port and Port C upper half is Input port. The input and output lines of the
interface are hardwired accordingly. The input lines are connected to Port B and Port C upper half. The
output lines are connected to Port A and Port C lower half. Hence, there are 12 input and 12 output lines in
the interface. The inputs are connected to 12 DIP switches. The three ports are represented by means of
LEDs in the interface. 12 LEDs are reserved for inputs and the remaining 12 LEDs are reserved for outputs.

Connections details:
Connect the bus from 8255A to the logic controller interface.

Sample code snippet:


Sample code that reads 8-bit data from logic controller interface:

Page 53 of 55
LAB NO: 12

Lab Exercises
With brief introduction of stepper motor, logic controller and knowledge on 8255A,
Write 8086 ALP for the following:

1. To rotate stepper motor clockwise.


2. Implement a ring counter using logic controller interface.
3. Read the status of eight inputs from the logic controller interface, find 2’s complement of the 8-
bit input and display the result using the same interface.
4. Read the status of eight inputs from the logic controller interface, display FF if any input is high,
and display 00 otherwise.

Additional Exercises
1. To rotate stepper motor anti-clockwise..
2. Read the status of 3 inputs from the logic controller interface. Assuming that these three inputs
represent a binary number X ( 0«X«7), display 2*X using the same interface.
3. Using logic controller interface, simulate Johnson counter.

Page 54 of 55
LAB NO: 12

REFERENCES
1. Douglas V.Hall, “Microprocessors and Interfacing”, Tata McGraw Hill Publications Ltd., Revised
Third edition.
2. Respective user manual for interfacing kit.

Page 55 of 55
[OBSERVATION SPACE]

Vous aimerez peut-être aussi