You are on page 1of 91

1

Dhaka University of Engineering and Technology, Gazipur


Department of Electrical and Electronic Engineering
Course No: EEE-3604
Course Title: Microprocessor and Peripheral Sessional
3rd year 2nd semester

List of Experiments:

Sl. No. Name of the experiment


1 Introductory Idea of 8086 Microprocessor, EMU8086 Emulator and exploring MDA-
Win8086 Trainer Kit with MDA-WinIDE8086 software
2 Basic Data Transfer Instruction, Arithmetic Instructions, Logical Instructions, Jump
Commands and Flag Registers in 8086 Assembly Language
3 Shift, Rotate and Loop Commands in 8086 Assembly Language
4 Application of Stack Instructions, CALL & RET and Arrays in 8086 Assembly
Language
5 Experiments based on I/O Device Interface to 8086 Microprocessor
(a) Interfacing LED, 7 Segment and DOT Matrix display with 8086 using 8255 PPI in
MDA- Win8086 Trainer Kit
(b) Interfacing Stepper Motor with 8086 using 8255 PPI in MDA- Win8086 Trainer
Kit
2

Dhaka University of Engineering and Technology, Gazipur


Department of Electrical and Electronic Engineering
Course No: EEE-3604
Course Title: Microprocessor and Peripheral Sessional
3rd year 2nd semester
............................................................................................................................................................
Experiment No.: 01
Name of Experiment: Introductory Idea of 8086 Microprocessor, EMU8086 Emulator and
exploring MDA-Win8086 Trainer Kit with MDA-WinIDE8086 software

Objective:
(i) To get an overview of 8086 microprocessor (µP) and its architecture.
(ii) To know about assembly language programming and familiarization with EMU8086
emulator and learn how to write and emulate 8086 assembly language program.
(iii) To familiarize the students with MDA-Win8086 Trainer Kit and to give an overview
about how to write, assemble and link a program in MDA-Win8086 Trainer Kit using
MDA-WinIDE8086 software.

Learning Outcome:
(i) Explain a brief overview and its architecture of 8086 µP.
(ii) Crete a program in assembly language and compile it by EMU8086 emulator.
(iii) Upload an assembly language program to the MDA-Win8086 Trainer Kit and run the
program.

Theory:

Microprocessor is a silicon chip that contains a CPU. In the world of personal computers, the
terms microprocessor and CPU are used interchangeably. A microprocessor (sometimes
abbreviated as μP) is a digital electronic component with miniaturized transistors on a single
semiconductor integrated circuit (IC).

8086 Microprocessor:
• It is a 16 bit μp. It’s a HMOS microprocessor. The term HMOS is used for high-speed MOS.
• 8086 has a 20 bit address bus can access up to 2^20 memory locations (1 MB).
• It can support up to 64K I/O ports.
• It provides 14, 16-bit registers.
• It has multiplexed address and data bus AD0- AD15 and A16 – A19.
• 8086 is designed to operate in two modes, Minimum and Maximum.
• It requires +5V power supply.
• A 40 pin dual in line package.

To write the assembly language programs for 8086 microprocessor, we are required to know the
internal organization at least programming model.
3

8086 Internal Architecture:


8086 internal Architecture contains mainly following two units :
• BIU (Bus Interface Unit) : BIU contains Instruction queue, Segment registers, Instruction
pointer, etc.
• EU (Execution Unit) : EU contains Control circuitry, Instruction decoder, ALU, Pointer and
Index register, Flag registers, etc.

8086 Pin Diagram:

8086’s fourteen 16-Bit Registers:


4

General Purpose Registers:


There are four 16-bit general purpose registers: AX, BX, CX and DX.

AX (Accumulator Register): AX register is also known as accumulator register that stores


operands for arithmetic operation like divided, rotate.
 It is the preferred register to use in the arithmetic, logic and data transfer instructions
because its use generates the shortest machine code.
 In multiplication and division operations, one of the numbers involved must be in AX or
AL.
 Input and output operation also requires the use of AX and AL.
BX (Base Register): This register is mainly used as a base register. It holds the starting base
location of a memory region within a data segment.
 It is used to store the data also it serves as an address register.
CX (Count Register): It is defined as a counter. It is primarily used in loop instruction to store
loop counter.
 Program loop instructions are facilitated by the use of CX register, serves as a loop
counter.
 Also used as a counter in the string operations.
 CL is used as count in instructions that shift and rotate bits.
DX (Data Register): DX register is used to contain I/O port address for I/O instruction.
 It is used in multiplication and division operations.
 It is used in IO operation like DL in character output and DX in string output functions.

Segment Registers:
Segment registers points to the starting address of a memory segment. For e.g.: The code
segment registers points to the starting address of the code segment. The data segment register
points to the starting address of the data segment, and so on. The maximum capacity of a
segment may be up to 64 KB.

There are four segment registers in Intel 8086:


 Code Segment Register (CS)  Data Segment Register (DS)
5

 Stack Segment Register (SS)  Extra Segment Register (ES)

Code Segment:
Program instructions are placed in the code segment. Instructions are actually organized into
units called procedures. Every procedure starts with a line.

Example:
Main Proc;
Main is the name of procedure and PROC is the directive identify the start of the procedure
Main Endp;
Main is again the name of the procedure and Endp is the directive, identifies the end of the
procedure

Data Segment:
By default, the processor assumes that all data referenced by general registers (AX, BX, CX,
DX) and index register (SI, DI) is located in the data segment. Variables are declared in the data
segment. Each variable is assigned space in memory and may be initialized.

Example:
 A DW 3501H
It sets memory for a variable called A, and initialize it to 3501H.
DW - Define Word (16 bits = 2 memory locations)

Stack Segment: The stack segment is used for temporary storage of addresses and data. If no
stack segment is declared, an error message is generated, so there must be a stack segment even
if the program doesn’t utilize the stack.

Extra Segment: By default, the processor assumes that the DI register references the ES
segment in string manipulation instructions.
Pointer & Index Registers:

The registers SP, BP, SI and DI normally points to (contain the offset address of) memory
location. These registers can be used in arithmetic and other operations. Following four registers
are under this category:
• Stack Pointer (SP) • Base Pointer (BP)

• Source Index (SI) • Destination Index (DI)

Stack Pointer (SP): It is a 16-bit register pointing to program Stack, also contains 16-Bit offset
address.
 The SP register contains the offset of the top of stack.
 The SP and SS registers combine to form the complete address of the top of the stack.
6

Base Pointer (BP): It is a 16-bit register pointing to data in stack segment. BP register is usually
used for based indexed or register indirect addressing.

Source Index (SI): It is a 16-bit register. SI is used for indexed, based indexed and register
indirect addressing, as well as a source data addresses in string manipulation Instructions.
 The SI register is used to point to memory locations in the data segment addressed by DS.

Destination Index (DI): It is a 16-bit register. DI is used for indexed, based indexed and register
indirect addressing, as well as a destination data addresses in string manipulation instructions.
 The DI register performs the same operation as the SI register.

Control Register:
IP (Instruction Pointer): The Instruction Pointer (IP) in 8086 acts as a Program Counter. It
points to the address of the next instruction to be executed. Its content is automatically
incremented when the execution of a program proceeds further.
 The IP register contains the offset of the next instruction to be executed within the current
code segment. The IP and CS registers combine to form the complete address of the next
instruction.
Flags (Flag Register): This is a special register with individual bit positions assigned to show
the status of the CPU or the results of arithmetic operations. Each relevant bit is given a name
and others are undefined.

Emu8086 Microprocessor Emulator Overview:


Emu8086 Microprocessor Emulator has an integrated 8086 assembler, runs on virtual machine. It
emulates real hardware, screen, memory and I/O devices. It combines an advanced source editor,
assembler, disassembler, software emulator (Virtual PC) with debugger. This program is
extremely helpful for those who just begin to study assembly language. It compiles the source
code and executes it on emulator step by step. Visual interface is very easy to work with. You
can watch registers, flags and memory while your program executes. Emu8086 has a much easier
syntax than any of the major assemblers, but will still generate a program that can be executed on
any computer that runs 8086 machine code; a great combination for beginners! Another
advantage in this emulator, the program can be executed step by step to get the error position or
can be executed altogether.

Emu8086 Emulator Full Version Click to Download

Where to start?
 Start Emu8086 by selecting its icon from the start menu, or by running Emu8086.exe.
7

 Click “new”

 Choose COM template and then click Ok


8

 Example: Create COM file and write the following 8086 assembly language
 Example on the emulator to see how it works

CODE SEGMENT ; Program instructions are placed in the code segment


ASSUME CS:CODE, DS:CODE ; "ASSUME" directive select segment register(s) to be the
default for all symbol in segment(s)
ORG 100h
MOV AL, 10001100B ; Copy 10001100B = 8CH to AL register
MOV BL, 5 ; Copy decimal number 5 to BL
ADD AL, BL ; Add AL and BL and result is stored in AL register
ADD BL, 0FBH ; Add 0FBH with BL and result is stored in BL
HLT ; Stop here
CODE ENDS ; Mark end of segment or structure
END ; Terminates assembly

ORG 100h is a compiler directive (it tells compiler how to handle the source code). This
directive is very important when you work with variables. It tells compiler that the executable
file will be loaded at the offset of 100h (256 bytes), so compiler should calculate the correct
address for all variables when it replaces the variable names with their offsets.

The symbol (;) acts like (//) in C++, writing a comment. After writing the previous program, it
should appear like the following:
9

 Start emulation by clicking the “emulate” button on the toolbar. A new emulator
window will appear.

You can see in it the general registers file (AX, BX…), instruction pointer (IP), segment register
file (CS, DS…), flags, stack, and memory locations. You can also see the Arithmetic and Logic
10

Unit (ALU) inputs and output. You can see your code in the most right yellow column and the
machine code of the program in the next yellow column.
Code Example: This 8086 assembly language program gets a character from a user, and displays it out.

CODE SEGMENT ; Program instructions are placed in the code segment


ASSUME CS:CODE, DS:CODE ; "ASSUME" directive select segment register(s) to be the
default for all symbol in segment(s)
ORG 100h
MOV AH, 1h ; Read character function (Character saved in AL)
INT 21h ; DOS interrupt "do it"
MOV DL, AL ; Copy character to DL
MOV AH, 2h ; Display character function (from DL)
INT 21h ; Character will be displayed
HLT ; Stop here
CODE ENDS ; Mark end of segment or structure
END ; Terminates assembly

From Emu8086 emulator:

Short Notes:
MOV instruction:
 Copies the second operand (source) to the first operand (destination).
 The source operand can be an immediate value, general-purpose register or memory location.
 The destination register can be a general-purpose register, or memory location.
 Both operands must be the same size, which can be a byte or a word.
Assembly Language:
Assembly language is a machine specific programming language with a one-to-one
correspondence between its statements and the computer’s native machine language. IBM-PC
assembly language refers to instruction recognized by a number of different microprocessors in
the Intel family: 8086, 8088, 80186, 80286, 80386, 80486, and Pentium. Assembly language is
used most often when either communicating with the operating system or directly accessing
computer hardware. An Assembler is a program that converts source code programs from the
11

assembly language into machine language. The most popular assemblers for the Intel family are
MASM (Microsoft Assembler), TASM (Turbo Assembler).
MDA-Win8086 Overview:

MDA-Win8086 System Configuration:

 CPU (Central processing unit): Using Intel 8086, Using 14.7456㎒.


12

 ROM (Read Only Memory): It has program to control user's key input, LCD display,
user's program. 64K Byte, it has data communication program. Range of ROM Address
is F0000H~FFFFFH.
 SRAM (Static Random Access Memory): Input user's program & data. Address of
memory is 00000H~0FFFFH, totally 64K Byte.

 DISPLAY: Text LCD Module, 16(Characters)×2(Lines)


 KEYBOARD: It is used to input machine language. There are 16 hexadecimal keys and
8 function keys.
 SPEAKER: Sound test.
 RS-232C: Serial communication with IBM compatible PC.
 DOT MATRIX LED: To understand & test the dot matrix structure and principle of
display. It is interfaced to 8255A (PPI).
 A/D CONVERTER: ADC0804 to convert the analog signal to digital signal.
 D/A CONVERTER: DAC0800 (8-bits D/A converter) to convert the digital signal to the
analog signal and to control the level meter.
 STEPPING MOTOR INTERFACE: Stepping motor driver circuit is designed.
 POWER: AC 110~220V, DC +5V 3A, +12V 1A, -12V 0.5A SMPS.
13

MDA-Win8086 Address Map:

Memory Map:
Address Memory Description
00000H ~ 0FFFFH RAM PROGRAM & DATA MEMORY
F0000H ~ FFFFFH ROM MONITOR ROM
10000H ~ EFFFFH USER'S RANGE

I/O Address Map:


ADDRESS I/O PORT DESCRIPTION
00H ~ 07H LCD & KEYBOARD LCD Display
00H : INSTRUCTION REGISTER
02H : STATUS REGISTER
04H : DATA REGISTER
KEYBOARD
01H : KEYBOARD REGISTER (Only
read)
01H : KEYBOARD FLAG (Only
write)
08H ~ 0FH 8251 / 8253 8251(Using to data communication)
08H : DATA REGISTER
0AH : INSTRUCTION / STATUS
REGISTER
8253(TIMER/COUNTER)
09H : TIMER 0 REGISTER
0BH : TIMER 1 REGISTER
0DH : TIMER 2 REGISTER
0FH : CONTROL REGISTER
10H ~ 17H 8255A-CS1/ 8255A-CS1(DOT & ADC
8255A-CS2 INTERFACE)
18H : A PORT DATA REGISTER
1AH : B PORT DATA REGISTER
1CH : C PORT CONTROL
REGISTER
8255-CS2(LED & STEPPING
MOTOR)
19H : A PORT DATA REGISTER
1BH : B PORT DATA REGISTER
1DH : C PORT CONTROL
REGISTER
1FH : CONTROL REGISTER
20H ~ 2FH I/O EXTEND CONNECTOR
30H ~ FFH USER'S RANGE
14

Function of Keys:

The following is a simple description of the key functions.

Basic Operation:

On a power-up, following message will be displayed on a LCD.

Whenever RES is pressed, the display becomes Figure 1-1 or Figure 1-2.
15

To select the Machine Code and Serial monitor mode with P1 switch.

How to setup the serial monitor:

Serial monitor is the basic monitor program to do data communicates between MDA-Win8086
and your computer. Adjust the P1 switch as following figure.

How to connect MDA‐Win8086 to your PC:

 Connect the MDA-Win8086 Kit to a spare serial port on your PC.

MDA‐WinIDE8086 Installation:
16

 Insert the CD in the CD-ROM driver, and double click the file "SETUP.EXE".
 The installation begins.

 Click the Start button in the task bar, then click All Programs and MIDAS ENG. Then click
the MDA-WinIDE8086 program icon

 The MDA-WinIDE8086 window will be displayed.


17

About MDA-WinIDE8086:
18

Tool bar: The tool bar provides button s for the most useful commands on the MDA-
WinIDE8086 menus.

Editor Window: Source file is displayed in the editor window. The MDA-WinIDE8086 editor
automatically recognizes the syntax of C program and Assembly program.
19

Editor Window: C program Editor Window: Assembly program

Terminal Window: Terminal window is that you can use to connect the MDA-Win8086 kit.

Assembling and Compiling the source (C or Assembly Program):

Click / button / for assembling/compiling to generate an ABS file.


20

Quick Start with MDA-WinIDE8086:

STEP1 Click the Start button in the task bar, then click All Programs and MIDAS ENG.
21

Then click the "MDA_WinIDE8086" program icon, and the "MDA-WinIDE8086" window will
be displayed.
22

STEP 2 Open a source file from the File-Open menu.


23

STEP 3
(1) In the Files of types, select a source type from the drop-down list, as an assemble file ②
Select "Led.ASM" file ③ Click "Open" button
24

STEP 4 Start assembling your target application with the " Assemble and Link" button.

STEP 5 If there is no error, following window will be displayed


25

STEP 6 If you click "Yes" button, your source program will be download from PC into the
target.

STEP 7 If you click "Run" button, the program will be executed.


26

Discussion:
A short discussion should be prepared by students after finishing their Experiment # 1

Prepared by:
Md. Maruful Islam, MESc, MIEEE, MIEB
Faculty, Department of Electrical and Electronic Engineering (EEE)
Dhaka University of Engineering & Technology (DUET)
Email: maruful.islam@duet.ac.bd

Dr. Md. Monirul Kabir


Associate Professor
Department of Electrical & Electronic Engineering
Dhaka University of Engineering & Technology (DUET)
Email: munir@duet.ac.bd, mmkdmunir@gmail.com
27

Dhaka University of Engineering and Technology, Gazipur


Department of Electrical and Electronic Engineering
Course No: EEE-3604
Course Title: Microprocessor and Peripheral Sessional
3rd year 2nd semester
............................................................................................................................................................
Experiment No.: 02
Name of Experiment: Basic Data Transfer Instruction, Arithmetic Instructions, Logical
Instructions, Jump Commands and Flag Registers in 8086 Assembly Language

Objectives:
1. To get an overview of 8086 assembly language instruction set
2. To apply basic data transfer instructions, arithmetic instructions, logical instructions in
8086 assembly language
3. To apply jump commands in 8086 assembly language
4. To write assembly language programs with data transfer, arithmetic, logical and Jump
instructions.
5. To gain knowledge about effects of different arithmetic instructions on flag registers.

Learning Outcomes:
1. Write a set of programs in assembly language using data transfer instructions, arithmetic
instructions and logical instructions.
2. Write a set of programs in assembly language using data transfer, arithmetic, logical and
Jump instructions.
3. Show the different effects on flag registers for arithmetic instructions.

Theory:

Instruction: An instruction is a binary pattern designed inside a microprocessor to perform a


specific function.
28

Opcode: It stands for operational code. It specifies the type of operation to be performed by
CPU. It is the first field in the machine language instruction format.
Operand: We can also say it as data on which operation should act. Operands may be register
values or memory values. The CPU executes the instructions using information present in this
field. It may be 8-bit data or 16-bit data.
Mnemonics: These are the symbolic codes for either instructions or commands to perform a
particular function. E.g. MOV, ADD, SUB etc.

Basic Data Transfer Instruction:

MOV instruction: MOV Destination, Source


 Copies the second operand (source) to the first operand (destination).
 The source operand can be an immediate value, general-purpose register or memory location.
 The destination register can be a general-purpose register, or memory location.
 Both operands must be the same size, which can be a byte or a word.
These types of operands are supported:
MOV REG, memory
MOV memory, REG
MOV REG, REG REG = Register
MOV memory, immediate
MOV REG, immediate

More about MOV:

MOV is a basic command to assign a number to a variable. For example following word can be
converted to a MOV command as,
x = 33;

MOV AX, 33d ; d stays for decimal

Here AX stands for x. In this way we can declare variables. MOV can also be used to assign
another registers value.
x = 33
y=x

MOV AX, 33
MOV BX, AX

MOV can also be used to assign some value to a memory location or from a memory location.

MOV AX, [1000H] ; Storing value to AX from memory location 1000H


MOV [1000H], AX ; Storing value to memory location 1000H from AX
MOV [BX], AX ; Storing value of memory location that has address given by the value
of BX, to AX
29

MOV command must be used with registers of equal size. MOV AX, BL is not correct as
AX is of 16bits and BL is of 8 bits. Similarly MOV BL, 1652h is not correct as 1652h is 16 bit
number and available space for BL is only 8 bits.

Remember if you write ‘h’ after a number then it is hexadecimal number. If nothing is written
then default type is decimal.

Basic Arithmetic Instructions:

Mnemonic Addition Mnemonic Multiplication


ADD Add byte or word DIV Divide byte or word (unsigned)
ADC Add byte or word with carry IDIV Integer divide byte or word
INC Increment byte or word by 1 (signed)
Mnemonic Subtraction
SUB Subtract byte or word
SBB Subtract byte or word with borrow
DEC Decrement byte or word by 1
NEG Negate byte or word
Mnemonic Multiplication
MUL Multiply (unsigned)
IMUL Integer multiply (signed)

ADD, SUB, DIV, MUL are all basic arithmetic instructions. ADD is used to add two numbers.
For example following lines can be converted to,

x = 1236H
y = 1438H
z=x+y

MOV AX, 1236H


MOV BX, 1438H
ADD AX, BX ; This command adds AX and BX content and stores it to AX register.

Similarly SUB command is used for subtraction of two numbers. For example,

x = 1236H
y = 1438H
z=x–y

MOV AX, 1236H


MOV BX, 1438H
SUB AX, BX ; AX holds the subtraction result.

MUL command is used to multiply two operands.


30

x = 146H
y = 5898H
z = x*y

MOV AX, 1456H


MOV BX, 5898H
MUL BX ; AX holds the lower 16 bit and DX holds the upper 16 bit result. For example
result of multiply operation is 0709A310. So, AX holds A310 and DX holds
0709.

DIV is used to perform division operation.


x = 5327H
y = 15F2H
z = x/y

MOV AX, 5327H


MOV BX, 15F2H
DIV BX ; AX holds the result and DX holds the remainder. For example, for upper
operation AX holds 3H and DX holds 1151H.

NEG command is used to get the 2’s complement of any number.

MOV AL, 30h


NEG AL ; Now, AL will have the value D0h

Basic Logical Instructions:

Mnemonic Meaning Format


AND Logical AND AND D, S
OR Logical Inclusive —OR OR D, S
XOR Logical Exclusive —OR XOR D, S
NOT Logical NOT NOT D
Bit by bit logical AND operation for flags only (i.e. only
TEST TEST D, S
flags will be affected).

Logical instructions include NOT, AND, OR, XOR, TEST etc. instructions. There job is to
compare the data values and make results according to logic specified. For example,

MOV BX, 30H ; In binary 110000


NOT BX ; In binary 001111

This code takes BX value and then complements all the bits and stores the new value to BX.

MOV BX, 70H ; In binary 1110000


31

MOV CX, 40H ; In binary 1000000


AND CX, BX ; In binary 1000000

AND operation performs bit by bit AND operation and then stores the value in first operand. In
upper code CX holds the final result.

MOV BX, 70H ; In binary 1110000


MOV CX, 40H ; In binary 1000000
OR CX, BX ; In binary 1110000

OR operation performs bit by bit OR operation and then stores the value in first operand. In
upper code CX holds the final result. Similar case happens for XOR and it is given below,

MOV BX, 70H ; In binary 1110000


MOV CX, 40H ; In binary 1000000
XOR CX, BX ; In binary 0110000

Test operation is a little different from AND operation. It performs bit by bit AND operation but
it does not change any operands value.

MOV BX, 70H ; In binary 1110000


MOV CX, 40H ; In binary 1000000
TEST CX, BX ; In binary CX value is 1000000

Do not confuse yourself with semicolon given after each line in assembly codes above.
Comments are written after semi colon ‘;’ in assembly language.

Characteristics of CMP Instruction:

 The following are the characteristics of CMP instruction:


 Can compare two 8-bit or two 16-bit numbers
 Results of comparison is reflected in the status of the six status flags: CF, AF, OF, PF, SF and
ZF
 CMP is a subtraction method—it uses 2’s complement for this
 Result of CMP is not saved—but based on CMP result, appropriate flags are either set/reset

Mnemonic Meaning Format Operation Flags Affected


CMP Compare CMP D, S (D) – (S) is used in setting CF, AF, OF, PF, SF,
or resetting the flags ZF

Jump Commands:

Sometimes it is necessary to go from one line of program to another line without executing some
intermediate lines. For this Jump commands are used. There are two types of Jump commands.
These are (i) Conditional Jump and (ii) Unconditional Jump.
32

Mnemonic Meaning Format Operation Flags


Affected
JMP Unconditional JMP Operand Jump is initiated to the None
Jump address specified by the
operand

Conditional Different Types of Jump Instructions:

Mnemonic Meaning Mnemonic Meaning


JA Jump if above JE Jump if equal
JAE Jump if above or equal JG Jump if greater
JB Jump if below JGE Jump if greater or equal
JBE Jump if below or equal JL Jump if less
JE Jump if equal JLE Jump if less or equal
JNA Jump if not above JNG Jump if not greater
JNAE Jump if not above or JNGE Jump if not greater or equal
equal JNL Jump if not less
JNB Jump if not below JL Jump if less
JNBE Jump if below or equal JNLE Jump if not less or equal
JN Jump if not equal JNO Jump if not overflow
JNS Jump if not sign JNZ Jump if not zero
JNZ Jump if not zero JO Jump if overflow
JS Jump if sign JPO Jump if parity odd
JZ Jump if zero JP Jump if parity
JC Jump if carry JNS Jump if not sign
JNC Jump if no carry JPE Jump if parity even
JNP Jump if not parity

Flag Registers:

8086 has 9 flags and they are divided into two categories:
(1) Condition Flags
(2) Control Flags
33

Flag Registers: It determines the current state of the processor. They are modified automatically
by CPU after mathematical operations; this allows determining the type of the result, and to
determine conditions to transfer control to other parts of the program. Generally you cannot
access these registers directly.

Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow. For example when
you add bytes 255 + 1 (result is not in range 0...255). When there is no overflow this flag is set to
0.

Parity Flag (PF) - this flag is set to 1 when there is even number of one bits in result, and to 0
when there is odd number of one bits.

Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low nibble (4 bits).

Zero Flag (ZF) - set to 1 when result is zero. For non-zero result this flag is set to 0.

Sign Flag (SF) - set to 1 when result is negative. When result is positive it is set to 0. (This flag
takes the value of the most significant bit.)

Overflow Flag (OF) - set to 1 when there is a signed overflow. For example, when you add
bytes 100 + 50 (result is not in range -128...127).

MOV BX, 70H ; In binary 1110000


MOV CX, 40H ; In binary 1000000
AND CX, BX ; In binary 1110000

After this operation Zero Flag is 0 (ZF = 0; as the value of CX is not 0), Carry Flag is 0 (CF = 0;
as there is no carry), Parity Flag is 0 (PF = 0; as there are odd number of 1’s), Sign Flag is 1 (SF
= 1; as the last bit of MSB is 1), Overflow Flag is 0 (OF = 0; as there is no overflow). Thus, all
the flags can be determined.

Example of MOV instruction:


34

DB, DW, DD, DQ and DT:

These directives are used to define different types of variables or to set aside one or more storage
locations of corresponding data type in memory. Their definitions are as follows:

An 8086 assembly program that displays ‘AAAAAA’ on screen:

CODE SEGMENT ;Program instructions are placed in the code segment


ASSUME CS:CODE, DS:CODE ;"ASSUME" directive select segment register(s) to be the
default for all symbol in segment(s)
ORG 100h
MOV CX , 6 ;Initialize a counter by CX
MOV AH , 02h ;Function to display a single character
35

MOV DL , 'A' ;Transfer A to register DL


L1: INT 21H ;DOS routine interrupt
DEC CX ;Decrement CX by 1
JNZ L1 ;Using conditional Jump JNZ to display A six times
HLT ;Stop here
CODE ENDS ;Mark end of segment or structure
END ;Terminates assembly

An 8086 assembly program that displays A to Z on screen:

CODE SEGMENT ;Program instructions are placed in the code segment


ASSUME CS:CODE, DS:CODE ;"ASSUME" directive select segment register(s) to be
the default for all symbol in segment(s)
ORG 100h
MOV CX , 1Ah ;Initialize a counter by CX 1A for 26 Alphabet
MOV AH , 02h ;Function to display a single character
MOV DL , 'A' ;Transfer A to register DL
Alphabet: INT 21H ;DOS routine interrupt
INC DL ;increment DL for next Alphabet
DEC CX ;Decrement CX by 1
JNZ Alphabet ;Using conditional Jump JNZ to display A-Z
HLT ;Stop here
CODE ENDS ;Mark end of segment or structure
END ;Terminates assembly

An 8086 assembly program to display ‘ELECTRICAL & ELECTRONIC ENGINEERING


(EEE)’ on screen:

DATA SEGMENT ;message to be displayed in the output


;can also be declared in the data segment
;using the keyword MSG
MSG DB 'ELECTRICAL & ELECTRONIC ENGINEERING (EEE) $' ;ensure the string is terminated with the '$'
character
DATA ENDS
CODE SEGMENT ; start of the code segment
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA ; code for initializing DS
MOV DS, AX ; move content AX to DS
MOV DX, OFFSET MSG ;moves the offset of the msg to the register
MOV AH, 09h ; reads from offset and display on output
INT 21h ; interrupt call
CODE ENDS
END START
36

ADDITION OF 2 8-BIT HEXADECIMAL NUMBERS:

DATA SEGMENT
NUM1 DB 03H ;define 1st 8 bit number
NUM2 DB 08H ;define 2nd 8 bit number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ;initialize AX by address of data segment
MOV DS, AX ;transfer AX to DS
MOV AX,00h ;clear accumulator
MOV AL, NUM1 ;transfer NUM1=03H to AL
MOV BL, NUM2 ;transfer NUM2=08H to BL
ADD AL, BL ;add that two 8 bit numbers
MOV DL, AL ;transfer sum to DL register
HLT
CODE ENDS
END START

ADDITION OF 2 16-BIT HEXADECIMAL NUMBERS:

DATA SEGMENT
NUM1 DW 1234H ;define 1st 16 bit number
NUM2 DW 4321H ;define 2nd 16 bit number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ;initialize AX by address of data segment
MOV DS, AX ;transfer AX to DS
MOV AX,00h ;clear accumulator
MOV AX, NUM1 ;transfer NUM1=1234H to AX
MOV BX, NUM2 ;transfer NUM2=4321H to BX
ADD AX, BX ;add that two 16 bit numbers
MOV DX, AX ;transfer sum to DX register
HLT
CODE ENDS
END START

SUBTRACTION OF 2 8-BIT HEXADECIMAL NUMBERS:


37

DATA SEGMENT
NUM1 DB 4AH ;define 1st 8 bit number
NUM2 DB 3FH ;define 2nd 8 bit number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ;initialize AX by address of data segment
MOV DS, AX ;transfer AX to DS
MOV AX,00h ;clear accumulator
MOV AL, NUM1 ;transfer NUM1=4A H to AL
MOV BL, NUM2 ;transfer NUM2=3F H to BL
SUB AL, BL ;subtract that two 8 bit numbers
MOV DL, AL ;transfer result to DL register
HLT
CODE ENDS
END START

SUBTRACTION OF 2 16-BIT HEXADECIMAL NUMBERS:

DATA SEGMENT
NUM1 DW 5678H ;define 1st 16 bit number
NUM2 DW 4321H ;define 2nd 16 bit number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ;initialize AX by address of data segment
MOV DS, AX ;transfer AX to DS
MOV AX,00h ;clear accumulator
MOV AX, NUM1 ;transfer NUM1=5678H to AX
MOV BX, NUM2 ;transfer NUM2=4321H to BX
SUB AX, BX ;subtract that two 16 bit numbers
MOV DX, AX ;transfer result to DX register
HLT
CODE ENDS
END START

MULTIPLICATION OF 2 8-BIT HEXADECIMAL NUMBERS:

DATA SEGMENT
NUM1 DB 4FH ;define 1st 8 bit number
NUM2 DB 8CH ;define 2nd 8 bit number
DATA ENDS
38

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ;initialize AX by address of data segment
MOV DS, AX ;transfer AX to DS
MOV AX,00h ;clear accumulator
MOV AL, NUM1 ;transfer NUM1=4F H to AL
MOV BL, NUM2 ;transfer NUM2=8C H to BL
MUL BL ;multiply that two 8 bit numbers
MOV DX, AX ;transfer result to DX register
HLT
CODE ENDS
END START

MULTIPLICATION OF 2 16-BIT HEXADECIMAL NUMBERS:

DATA SEGMENT
NUM1 DW 3A6BH ;define 1st 16 bit number
NUM2 DW 8ECDH ;define 2nd 16 bit number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ;initialize AX by address of data segment
MOV DS, AX ;transfer AX to DS
MOV AX,00h ;clear accumulator
MOV AX, NUM1 ;transfer NUM1=3A6B H to AX
MOV BX, NUM2 ;transfer NUM2=8ECD H to BX
MUL BX ;multiply that two 16 bit numbers
HLT
CODE ENDS
END START

DIVISION OF 2 8-BIT HEXADECIMAL NUMBERS:


DATA SEGMENT
NUM1 DB 8FH ;define 1st 8 bit number
NUM2 DB 1AH ;define 2nd 8 bit number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
39

START: MOV AX, DATA ;initialize AX by address of data segment


MOV DS, AX ;transfer AX to DS
MOV AX,00h ;clear accumulator
MOV AL, NUM1 ;transfer NUM1=8FH to AL
MOV BL, NUM2 ;transfer NUM2=1AH to BL
DIV BL ; Division of that two 8 bit numbers(AL by
BL) see the result in AL for quotient and in
AH for remainder
HLT
CODE ENDS
END START

DIVISION OF TWO 16-BIT HEXADECIMAL NUMBERS:


DATA SEGMENT
NUM1 DW 9FFFH ;define 1st 16 bit number
NUM2 DW 1A3EH ;define 2nd 16 bit number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ;initialize AX by address of data segment
MOV DS, AX ;transfer AX to DS
MOV AX,00H ;clear accumulator
MOV DX,00H ;clear DX
MOV AX, NUM1 ;transfer NUM1=9FFFH to AX
MOV BX, NUM2 ;transfer NUM2=1A3EH to BX
DIV B ; division of that two 16 bit numbers(AX by BX)
see the result in AX(quotient) and in
DX(remainder) register
HLT
CODE ENDS
END START

Write a program to find factorial of a number


CODE SEGMENT
ASSUME CS:CODE, DS:CODE
MOV AX, 05
MOV CX, AX
Back: DEC CX
CMP CX,0
JE Factorial
MUL CX
CMP CX,0
40

JNZ Back
Factorial: MOV BX, AX ; results stored in AX
HLT
CODE ENDS
END

Example of AND instruction:


CODE SEGMENT
ASSUME CS:CODE, DS:CODE
ORG 100H
MOV BX, 3256H
MOV CX, 1554H
AND CX, BX
HLT
CODE ENDS
END

Observe content of CX register. What operation happened here?

Example of XOR instruction:


CODE SEGMENT
ASSUME CS:CODE, DS:CODE
ORG 100H
MOV BX, 3256H
MOV CX, 1554H
XOR CX, BX
HLT
CODE ENDS
END

Observe content of CX register. What operation happened here?

Example of Logical instructions

CODE SEGMENT
ASSUME CS:CODE, DS:CODE
ORG 100H
MOV AX, 1027H
MOV BX, 5A27H
MOV CX, 54A5H
41

OR AX, BX
XOR AX, CX
NOT AX
TEST CX, BX
AND CX, AX
HLT
CODE ENDS
END

Perform this operation in single step mode and write the values of registers
for every step. Obtain binary values for upper hexadecimal values and
perform bit by bit operation for every step. Compare your hand calculation
with obtained result.

Example of Bit by Bit operation by Logical instructions

CODE SEGMENT
ASSUME CS: CODE, DS:CODE
ORG 100H
MOV AL, 01011010B
AND AL, 11110000B
OR AL, 00001010B
XOR AL, 01011111B
NOT AL
INC AL
MOV BX, 39ABH
MOV AL, BL
OR AL, 0FH
AND AL, BH
AND BL, 0FH
OR AL, BL
HLT
CODE ENDS
END

Example of JMP instruction

CODE SEGMENT
ASSUME CS: CODE, DS: CODE
ORG 100H
MOV AX, 3254H
MOV BX, 1F4BH
MOV CX, 412AH
ADD AX, CX
42

JMP L3T2
SUB AX, BX
L3T2: AND AX, BX
HLT
CODE ENDS
END

In this example L3T2 is a level. As we can see in fifth line JMP command is
used. It makes the program to go from fifth line to L3T2 level that is seventh
line. So, sixth line is not executed.

Example of conditional jump command


CODE SEGMENT
ASSUME CS: CODE, DS: CODE
ORG 100H
MOV AX, 125BH
MOV BX, 125BH
MOV CX, 412AH
SUB AX, BX
JZ L3T2
DIV BX
L3T2: AND AX, CX
HLT
CODE ENDS
END

Application of data transfer, logical, jump instructions


CODE SEGMENT
ASSUME CS: CODE, DS: CODE
ORG 100H
MOV AX, 7A24H
MOV BX, 15A3H
SUB AX, BX
JMP Y3S2
EEE3604: DIV BX
JMP Last
Y3S2: MOV CX, 45B1H
AND AX, CX
TEST AX, BX
JMP EEE3604
Last: HLT
CODE ENDS
END
43

Perform this operation in single step mode and write the values of registers for every step.
Explain why we need ‘Last’ termed level? What will happen if it is not used?
44

Multiplication and Division:

Multiplication and division can be performed on signed or unsigned numbers. For unsigned
numbers, MUL and DIV instructions are used, while for signed numbers IMUL and IDIV are used.

The format of the multiplication and division instructions does not specify the multiplicand as it is
implicitly specified depending on the size of the source. As shown in the following tables.

For the unsigned division, if the quotient is larger than FF in the division by a byte and FFFF in the
division by a word type 0 interrupt occurs.

For the signed division, if the quotient is positive and exceeds 7F for the byte division or 7FFF for
the word division type 0 interrupts occurs. However, if the quotient is negative and less than 81H
for byte division and 8001H for word division, then type 0 interrupt occurs.
45

The following examples explain the steps to perform multiplication and division:
46

 CF (carry flag)/OF (overflow flag) = 1 means that the product is too big to fit in the lower
half of the destination (AL for byte 00multiplication and AX for word multiplication). The
effect of MUL on the SF, ZF are undefined.

 Divide overflow: It is possible that the quotient will be too big to fit in the specified
destination (AL or AX). This can happen if the divisor is much smaller than the dividend.
47

When this happened the program terminates and the system displays the message “Divide
Overflow”. The effect of DIV on the flags is that all status flags are undefined.

 Unsigned and Signed Integers:


An unsigned integer is an integer that represents a magnitude, so it is never negative. So far in
this laboratory we have dealt with unsigned integers. A signed integer can be positive or
negative. In a signed integer the most significant bit is reserved for the sign: 1 means negative
and 0 means.

For example,
If FFFFh is handled as an unsigned integer it will represent +65536 in decimal. On the other
hand, If FFFFh is handled as a signed integer it will represent -1 in decimal.

 The following points for signed multiplication and division are to be noted:
1. For both the instructions all the operands the considered signed integers.
2. The product of signed multiplication is also a signed integer.
3. For signed division the remainder has the same sign as the dividend.

 CF/OF = 0, if upper half of the product is the sign extension of the lower half (this means the
bits of the upper half are the same as the sign bit og the lower half)
= 1, otherwise

 To divide an 8-bit dividend by and 8-bit divisor by extending the sign bit of Al to fill all bits of
AH. This can be done automatically by executing the Instruction (CBW).

 In a similar way 16-bit dividend in AX can be divided by 16-bit divisor. In this case the sign
bit in AX is extended to fill all bits of DX. The instruction CWD performs this operation
automatically.

 Note that CBW extends 8-bit in AL to 16-bit in AX while the value in AX will be equivalent to
the value in AL. Similarly, CWD convert the value in AX to 32-bit. In (DX,AX) without
changing the original value.

CBW and CWD Instruction:

CWD stands for convert word to double word. When using IDIV, DX should be made the sign
extention of AX. CWD will do this extension. For example,

MOV AX, -1250 ; AX = FB1Eh

CWD will make DX sign extension of AX, so that DX:AX = FFFF:FB1E h.


Examples for illustrating the differences of MUL and IMUL
48

Examples illustrate the differences between DIV and IDIV


49
50

Assignments:
8. Write complete assembly language for the following operation
1. MOV AX, 30h
and verify it in EMU 8086 software.
MOV [2010], AX x = 30D
MOV BX, [2010] y = 50D
Observe content of BX register. z=x+y
w = z*y
2. MOV SI, 1256h x = w – 20D
y = x/5
MOV [SI], 3251h
u = x%5
MOV AX, [SI]
Observe content of AX register.
9. Write a assembly code that will put 1265H to memory location
2100H and 4512H in memory location 1287H. Access these
3. MOV AL,87h
values via registers and perform subtraction operation.
NEG AL
NEG AL
Write value of AL for every step. 10. Perform Multiplication of 1254H and 4512H. Store the higher 16
bit of the result to CX result.
4. MOV AX, 1782H
MOV BX, 1278H 11. Perform Division operation of 4512H by 1254H. ADD 03H with
ADD AX, BX remainder value.
Examine register contents.
12. Write an assembly code that will determine whether a number is
5. MOV AX, 1782H greater than 5 or equal of less, and put 0 or 1 or 2 for the
MOV BX, 1278H conditions in DX.
SUB AX, BX
Examine register contents. 13. Subtract 86B1H from 3F42H and store 0 in CX if overflow
occurs and 1 if no overflow occurs.

14. Take 2 arbitrary numbers x and y. If x>1000H perform x+y. If


y<1000H perform x-y. If x>1000H and y<100H perform x = x’.
51

6. MOV AX, 2782H


MOV BX, 1278H
MUL BX
Examine register contents.

7. MOV AX, 57F2H


MOV BX, 1375H
DIV BX
Examine register contents.

Dhaka University of Engineering and Technology, Gazipur


Department of Electrical and Electronic Engineering
Course No: EEE-3604
Course Title: Microprocessor and Peripheral Sessional
3rd year 2nd semester
............................................................................................................................................................
Experiment No.: 03
Name of Experiment: Shift, Rotate and Loop Commands in 8086 Assembly Language

Objectives:
1. To get an overview of shift and rotate instructions in 8086 assembly language
2. To use shift and rotate instructions in 8086 assembly language
3. To familiar with loop commands in 8086 assembly language
4. To use loops in 8086 assembly language

Learning Outcomes:
1. Write a set of programs in assembly language using shift and rotate instructions.
2. Write a set of programs in assembly language using loop commands.

Theory:

The 8086 provides four rotate instructions, and three shift instructions. This is the second set of
instructions which are used for bit manipulation. The 8086 supports three different shift
instructions (SHL and SAL are the same instructions), SHL (shift left), SAL (shift arithmetic
52

left), SHR (shift right) and SAR (shift arithmetic right). The shift instructions move bits around
in a register or memory locations.

Rotate instructions are the third set of instructions which are used for bit manipulation. The
rotate instructions shift the bits around, just like the shift instructions, except the bits shifted out
of the operand by the rotate instructions recirculate through the operand. They include RCL
(rotate through carry left), RCR (rotate through carry right), ROL (rotate left) and ROR (rotate
right).

These instructions can manipulate bits, convert values, do logical operations and do arithmetic
operations.

SAL/SHL: Arithmetic/Shift Logical Left


Operands memory, immediate
REG, immediate
memory, CL
REG, CL
The SHL (Shift Logically Left) and SAL (Shift Arithmetic Left) are used to shift the content of
the first operand towards left. The encoding of these two mnemonics is the same. The second
operand may be an immediate data or the counter register CL and represent the number of times
the shifting takes place. While each bit of the operand shifts to left, the zeros fill vacated
positions at the lower order bit.

SAR: Arithmetically Shift Right


Operands memory, immediate
REG, immediate
memory, CL
REG, CL

Shift arithmetically the content of the first operand right by the number of times specified by the
second operand. The second operand may be an immediate data or the counter register CL and
represent the number of times the shifting takes place. In SAR while each bit is shifted right and
goes to the carry flag, the sign bit is inserted from the MSB side as shown in the following
figure.
53

After the execution of the SAR instruction, the carry, parity and zero flag modified as per the
result. The sign remain unchanged, overflow flag is always zero and the AC flag is undefined.

Example: MOV AL, 8EH ; AL = 10001110B


MOV CL, 02H
SAR AL, CL ; AL = 11100011B; CF = 1

SHR: Logically Shift Right


Operands memory, immediate
REG, immediate
memory, CL
REG, CL

Shift logically, the content of the first operand right by the number of times specified by the
second operand. The second operand may be an immediate data or the counter register CL. In
SHR while each bit is shifted right and goes to the carry flag, a zero bit is inserted from the MSB
side as shown in the following figure.

Example: MOV AL, 07H ; AL = 00000111B


MOV CL, 02H
SHR AL, CL ; AL = 00000011B ; CF = 1

The various shift instructions and the allowed operands are:


54

ROR: Rotate Right

Operands memory, immediate


REG, immediate
memory, CL
REG, CL
The ROR instruction is used to rotate the operand to right. The second operand may be an
immediate data or the counter register CL. This second operand decides the number of times the
rotation will take place. After performing this instruction the LSB will go to the MSB position as
well as to the carry. Following figure shows the pictorial representation of ROR.

Example:
STC ; Set Carry (CF = 1)
MOV AL, 5AH ; AL = 01011010B
MOV CL, 03H
ROR AL, CL ; AL = 01011010B ; CF = 0

RCR: Rotate Right Through Carry

Operands memory, immediate


REG, immediate
memory, CL
REG, CL

This instruction performs the rotation of the contents of operand to right through the carry flag.
The second operand may be an immediate data or the counter register CL. This second operand
decides the number of times the rotation will take place. In this instruction the D0 bit of the
55

operand is shifted to the carry flag, the carry flag is shifted to the D15 or the D7 bit and all the
other bits are shifted towards the right.

Example: STC ; Set Carry (CF = 1)


MOV AL, 5AH ; AL = 01011010B
MOV CL, 03H
RCR AL, CL ; AL = 10111010B ; CF = 0

ROL: Rotate Left

Operands memory, immediate


REG, immediate
memory, CL
REG, CL
The ROL instruction is used to rotate the operand to left. The second operand may be an
immediate data or the counter register CL. This second operand decides the number of times the
rotation will take place. The ROL instruction moves the D15 or D7 bit to the D0 bit position of the
operand as well as to the carry flag and all other bits shifts towards left, i.e. to the higher bit
position in the operand register. Following figure shows the pictorial representation of ROL
56

Example: STC ; Set Carry (CF = 1)


MOV AL, 5AH ; AL = 01011010B
MOV CL, 03H
ROL AL, CL ; AL = 11010010B ; CF = 0

RCL: Rotate Left Through Carry


Operands memory, immediate
REG, immediate
memory, CL
REG, CL

This instruction performs the rotation of the contents of operand to left through the carry flag.
The second operand may be an immediate data or the counter register CL. This second operand
decides the number of times the rotation will take place. The RCL instruction moves the D15 or
D7 bit to the carry flag and the Carry Bit goes to the D0 bit position and other bits shift towards
left. Following figure shows the pictorial representation of RCL
57

Example: STC ; Set Carry (CF = 1)


MOV AL, 5AH ; AL = 01011010B
MOV CL, 03H
RCL AL, CL ; AL = 11010101B ; CF = 0

Shift and Rotate commands in 8086 assembly language: EMU8086 Environment


1. Shift Logical Left (SHL)

2. Shift Logical Right (SHR)

3. Shift Arithmetic Right (SAR)


58

4. Shift Arithmetic Left (SAL)

5. Rotate Right (ROR)

6. Rotate Left (ROL)

7. Rotate Through Carry Right (RCR) for Rotation Number = 1


59

8. Rotate Through Carry Right (RCR) for Rotation Number = 2

9. Rotate Through Carry Left (RCL) for Rotation Number = 1

LOOP in assembly language:

Loop commands are used to perform same operation again and again. This is like for, while type
instructions in ‘C’ or ‘MATLAB’. A common example can be shown as,

MOV CX, 0100D


MOV AX, 564AH
Lev: DEC AX
Loop Lev
HLT
60

Here CX acts as a count register. Loop Lev instruction leads instruction to go back to Lev level
until CX is zero. Each time Lev level is executed CX is decreased by 1. Loop command can be
used for waiting purposes. Such as,

MOV CX, 0100D


Wt: NOP
Loop Wt
HLT

Here the loop is executed until CX is zero. If 1 loop takes 1ms, the program will wait for 100ms.

LOOP Command in 8086 Assembly language:

How delay is happened by NOP and LOOP:


61

Observe the operation of this code. What happens when the loop is executed again and again?

8086 Assembly Language Program to find GCD of two numbers:


62

Assignments:

1. Suppose x = 20 and y = 28. Add y with x for 30 times.


2. Multiply 12 by 6 until result is below 3000H. If result is greater than this, divide the result by
2 for three times.
3. You can get input into microprocessor via following code.
MOV AH, 1H ; keyboard input subprogram
INT 21H
HLT
Take input from the keyboard until b is pressed.
63

Dhaka University of Engineering and Technology, Gazipur


Department of Electrical and Electronic Engineering
Course No: EEE-3604
Course Title: Microprocessor and Peripheral Sessional
3rd year 2nd semester
............................................................................................................................................................
Experiment No.: 04
Name of Experiment: Application of Stack Instructions, CALL & RET and Arrays in 8086
Assembly Language

Objectives:

1. To be familiar with stack operations.


2. To be familiar with CALL and RET instructions.
3. To get knowledge about Arrays in assembly language programming.

Learning Outcome:
1. Write a set of programs in assembly language using PUSH and POP instructions.
2. Write a set of programs in assembly language using CALL and RET instructions.
3. Describe Array operation with writing programs in assembly language.

Theory:

Stack Memory Addressing Modes: The stack is a portion of read/write memory set aside by
the user for the purpose of storing information temporarily and also it is a segment where some
register values can be used so that it is not lost. When the information is written on the stack the
operation is called PUSH. When the information is read from the stack, the operation is called a
POP.

The microprocessor stores the information, much like stacking plates. Using this analogy of
stacking plates it is easy to illustrate the stack operation. Following figure shows the stacked
plates.

Here, we realize that if is desired to take out the first stacked plate we will have to remove all
plates above the first plate in the reverse order. This means that to remove first plate we will
have to remove the third plate, then the second plate and finally the first plate. This means that,
the first information popped off from the stack. This type of operation is known as a first in, last
out (FILO). This stack is implemented with the help of special memory pointer register.
64

Stack Pointer:
The special pointer register is called the stack pointer. During PUSH and POP operation, stack
pointer register gives the address of memory where the information is to be stored or to be read.
The stack pointer’s contents are automatically manipulated to point to stack top. The memory
location currently printed by stack pointer is called top of stack.

PUSH and POP Operation:


Temporarily stores the contents of 16-bit register or memory location or program status word,
and retrieves when required. When programmer realizes the shortage of the registers, he stores
the present contents of the registers in the stack with the help of PUSH instruction and then uses
the registers for other function. After completion of other function programmer loads the
previous contents of the register from the stack with the help of POP instruction.

The PUSH and POP instructions, as also the allowed operands are shown in (a) and (b)
respectively.

A simple program can clarify this.


CODE SEGMENT
ASSUME CS:CODE
ORG 100H
MOV AX, 3256H ;Stores 3256H in AX. This is AX’s original value
PUSH AX ;Puts AX’s value in stack
MOV AX, 1254H ;AX is assigned to an intermediate value 1254H
DEC AX ;New value of AX is 1253H. Certainly AX’s value is changed.
POP AX ;This restores AX’s previous value and now AX is 3256H
65

CODE ENDS
END

Application of PUSH and POP Instruction


66

CALL & RET Instructions:


The CALL instruction is used to transfer execution to a subprogram or procedure. The RET
instruction will return execution from a procedure to the next instruction after the CALL
instruction in the calling program.

Like ‘C’ or ‘MATLAB’, in assembly language we can create functions that can be accessed from
main program. CALL instruction is needed to access a function. Each function is ended with a
RET to go back to main program. Suppose “Program” is the name of a procedure (in assembly
language functions are called procedures). It is called from the main program. What happens is
stated step by step below,

At the instant of procedure calling “Program” is called, next instruction address 0102H is saved
in stack. IP is transferred to 0202H. At the instant of procedure finishing, “Program” is
finishing, IP is set to 0260H. At the time back in the main program, “Program” is finished IP is
set to 0102H from stack.

At the time back in the main program,


“Program” is finished IP is set to 0102H
from stack.
67

Create a delay using NOP Instruction by CALL and RET Operation:


68

Introduction to Arrays in 8086 assembly language:


Array is nothing but series of data stored in successive locations in memory.
Declare an array:

To declare an array, you will use specify the name of your array, the dimension of your array, the
size of every element and the special system words DUP. Size of element can be DB (for byte)
or DW (for word that means 2 bytes).

Simple command to insert an array in assembly language is,

W DW 10, 20, 30, 40, 50, 60


Or
MSG DB ‘abcde’

Here DW is data word or word array, DB is data byte or byte array. W and MSG are array
names. DUP command is used to create an array of same numbers.

For example,
ALPHA DW 100 DUP(0)

Creates an array containing 100 0s

DUP can also be nested.

GAMMA DB 5, 4, 3 DUP (2, 3 DUP (0), 1)

Creates an array of 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1


Let us define a one-dimensional array of numbers.
NUMBERS DW 34, 45, 56, 67, 75, 89

The above definition declares an array of six words each initialized with the numbers 34, 45, 56,
67, 75, 89. The symbolic address of the first number will be NUMBERS and that of the second
number will be NUMBERS + 2 and so on.

Let us take up another example. You can define an array named inventory of size 8, and initialize
all the values with zero, as:

INVENTORY DW 0 Which can be abbreviated as:


DW 0
DW 0 INVENTORY DW 0, 0 , 0 , 0 , 0 , 0 , 0 , 0
DW 0
DW 0
DW 0
DW 0
DW 0
69

Addressing Modes for Accessing Data in Memory:

Though the Execution Unit (EU) has direct access to all registers and data for register and
immediate operands, but the EU cannot directly access the memory operands. It must use the
BIU segment registers to access memory operands. For example, when the EU needs to access a
memory location, it sends an offset value to the BIU. This offset is also called the Effective
Address (EA). Note that EA is displacement of the desired location from the segment base. The
BIU generates a 20-bit physical address after shifting the contents of the desired segment register
four bits to the left and then adding the 16-bit EA to it.

There are six ways to specify effective address (EA) in the instruction:

 Direct addressing mode


 Register indirect addressing mode
 Based addressing mode
 Indexed addressing mode
 Based indexed addressing mode
 String addressing mode

Here we will discuss only one addressing mode. That is: -

Base-Plus-Index-Addressing Mode:

Base-plus-index addressing is similar to indirect addressing because it indirectly addresses


memory data. This addressing uses one base register (BP or BX) and one index register (DI or
SI) to indirectly address memory. The base register often holds the beginning location of a
memory array, while the index register holds the relative position of an element in the array.
Remember that whenever BP addresses the memory data, the contents of stack segment, BP and
index register are used to generate physical address.

Locating Data with Base-Plus-Index Addressing:


70

Locating Array Data using Base-Plus-Index Addressing:

A main use of the base-plus-index addressing mode is to address elements in a memory array.
Suppose that the array is located in the data segment beginning from memory location ARRAY.
To access a particular element within the array we have to load the BX register (base) with the
beginning address of the array, and the DI or SI register (index) with the element number to be
accessed. This is illustrated in the following figure:
71

Application of ARRAY’s in 8086 Assembly Language:

LEN EQU $ - ARR concept is very useful. It determines the size of the array i.e. length of the
array. What it does is give you the number of bytes between the current address ($) and the
beginning of the ARR variable - thus giving you the size (in bytes) of the ARR variable itself.
Because the ARR is an array of bytes, LEN will be the actual length of the array. If ARR was an
array of WORDS, you'd have to divide LEN by two.
72

Assignment:
Write an 8086 assembly language program that will find the average of a series of numbers
in an array.
73

Assignment:
1. Write an 8086 assembly language program that will find the smallest number from
a series of numbers in an array.
2. Write a program to find the largest and smallest number from an array of n 8 bit
numbers.
74

Dhaka University of Engineering and Technology, Gazipur


Department of Electrical and Electronic Engineering
Course No: EEE-3604
Course Title: Microprocessor and Peripheral Sessional
3rd year 2nd semester
............................................................................................................................................................
Experiment No.: 05
Name of Experiment: Experiments based on I/O Device Interface to 8086 Microprocessor
(a) Interfacing LED, 7 Segment and DOT Matrix display with 8086 using 8255 PPI in MDA-
Win8086 Trainer Kit
(b) Interfacing Stepper Motor with 8086 using 8255 PPI in MDA- Win8086 Trainer Kit

Objectives:
1. To get an overview of MDA- Win8086 trainer kit and its system configuration
2. Familiarization with a Programmable Peripheral Interface (PPI) device, 8255
3. To know about the uses of 8255 PPI for controlling external devices
4. Controlling four LEDs on the MDA- Win8086 trainer kit using port B of PPI 8255A
5. To get familiar with the Seven Segment display and its interfacing with 8086
microprocessor on the MDA- Win8086 trainer kit using port A of PPI 8255A
6. To scroll the DOT Matrix display on the MDA- Win8086 trainer kit using port C of PPI
8255A
7. Controlling a Stepper Motor through 8255A PPI connected to 8086 microprocessor

Learning Outcomes:
1. Connect the PPI with 8086 μP for controlling 4 LEDs, seven segment display and dot
matrix display.
2. Control a stepper motor using 8255A PPI connected to 8086 μP.

Theory:

8255A Programmable Peripheral Interface (PPI) Device:


 The 8255A is a general purpose programmable I/O device designed for use with Intel
microprocessors.
 It consists of three 8-bit bidirectional I/O ports (24I/O lines) that can be configured to meet
different system I/O needs.
 The three ports are PORT A, PORT B & PORT C. Port A contains one 8-bit output
latch/buffer and one 8-bit input buffer. Port B is same as PORT A. However, PORT C can be
split into two parts PORT C lower (PC0-PC3) and PORT C upper (PC7-PC4) by the control
word. The three ports are divided in two groups Group A (PORT A and upper PORT C)
Group B (PORT B and lower PORT C).
75

 The two groups can be programmed in three different modes. In the first mode (mode 0),
each group may be programmed in either input mode or output mode (PORT A, PORT B,
PORT C lower, PORT C upper).
 In mode 1, the second’s mode, each group may be programmed to have 8-lines of input or
output (PORT A or PORT B) of the remaining 4-lines (PORT C lower or PORT C upper) 3-
lines are used for hand shaking and interrupt control signals.
 The third mode of operation (mode 2) is a bidirectional bus mode which uses 8-line (PORT
A only for a bidirectional bus and five lines (PORT C upper 4 lines and borrowing one from
other group) for handshaking.
 The 8255 is contained in a 40-pin package, whose pin out is shown below:
 The 8255 is contained in a 40-pin package, whose pin out is shown below:
76
77

8255A Control Word for I/O Mode:


78

Controlling four LEDs on the MDA- Win8086 Trainer Kit using Port B of PPI
8255A:
Trainer Kit and Software:
 MDA- Win8086 Trainer Kit
 MDA_WinIDE8086

Connection of the LEDs with the 8255A:


8255 is a programmable peripheral interface. It has 3 I/O ports named as port A, B, C. Port B is
for controlling the four LEDs on the board, we can control the four LEDs by keeping high any of
the first four bits as following:
(0001 for the upper RED LED)
(0010 for the GREEN LED)
(0100 for the YELLOW LED)
(1000 for the Second RED LED)

This is simply because PB0 (18-29), PB1 (19-28), PB2 (20-27), PB4 (21-26) are connected to the
LED11, LED12, LED13, LED14 respectively in the MDA- Win8086 Trainer Kit.
79
80

Need a
Critical
Look

Seven Segment
Display (FND)

Interfacing of 8255A with 8086

System Configuration on MDA- Win8086 Trainer Kit


81

A Code for running the 4 LED in series of 1234:


10H ~ 17H 8255A-CS2 8255-CS2 (LED & STEPPING MOTOR)
19H : A PORT DATA REGISTER
1BH : B PORT DATA REGISTER
1DH : C PORT CONTROL REGISTER
1FH : CONTROL REGISTER
;To define constants EQU directive is used
;*****************************************
; MDA-Win8086 EXPERIMENT PROGRAM *
; FILENAME : LED.ASM
; PROCESSOR : I8086
;*****************************************
CODE SEGMENT
ASSUME CS:CODE,DS:CODE,ES:CODE,SS:CODE
;
PPIC_C EQU 1FH ; Control Port 8-bit Address
PPIC EQU 1DH ; Port C 8-bit Address
PPIB EQU 1BH ; Port B 8-bit Address
PPIA EQU 19H ; Port A 8-bit Address
;
ORG 1000H ; Program Effective Address, IP = 1000H
MOV AL,10000000B ; Configure all ports of 8255 as output
OUT PPIC_C,AL ; Send to Control Port 8-bit Address
;
MOV AL,11111111B ; AL = 0FFH
OUT PPIA,AL ; All pins of Port A to HIGH or Disable 7 Segment
;
MOV AL,00000000B ; AL = 00H
OUT PPIC,AL ; All pins of Port C to LOW or Disable DOT Matrix
;
L1: MOV AL,11110001B ; PB0=1, LED11(RED)=ON, PB4toPB7=HIGH(Disable Stepper Motor)
L2: OUT PPIB,AL ; Send to Port B 8-bit Address
CALL TIMER ; CALL TIMER subroutine
SHL AL,1 ; Shift AL left by 1 bit so that next LED will ON
TEST AL,00010000B ; TEST AL with 00010000B, always zero except 5th bit of AL=1
JNZ L1 ; If test result is zero then next line otherwise go to L1
OR AL,11110000B ; Logically OR AL with 11110000B to make PB4toPB7=HIGH
JMP L2 ; Jump L2 to send AL to Port B 8-bit Address
;
INT 3 ; Type 3 interrupt is used for breakpoint. Break points are
; useful for program debugging
TIMER: MOV CX,1 ; Set up counter for delay
TIMER2: PUSH CX
MOV CX,0
TIMER1: NOP ; Dummy instructions to cause time delay
NOP
NOP
NOP
LOOP TIMER1
POP CX
LOOP TIMER2
RET ; Return from subroutine call
CODE ENDS
END ; End of Assembly Program
82

In MDA_WinIDE8086 Environment:
83

Now start assembling your target application with the " Assemble and Link" button.

If there is no error, following window will be displayed


84

If you click "Yes" button, your source program will be download from PC into the target.

If you click "Run" button, the program will be executed.


85

After executing the LED.ASM following operation will happen on MDA- Win8086 Trainer Kit

Assignment:

1. Write an 8086 assembly program on MDA_WinIDE8086 Environment for running the 2


LED (1212) in the 4 LEDs on MDA- Win8086 Trainer Kit.
2. Write an 8086 assembly program on MDA_WinIDE8086 Environment for running the 2
LED (1313) in the 4 LEDs on MDA- Win8086 Trainer Kit.
3. Write an 8086 assembly program on MDA_WinIDE8086 Environment for running the 4
LEDs in reverse of 4321

Seven Segment display (FND) and its interfacing with 8086 microprocessor on the MDA-
Win8086 Trainer Kit using Port A of PPI 8255A:

Trainer Kit and Software:


 MDA- Win8086 Trainer Kit
 MDA_WinIDE8086

Introduction:

Seven segment (FND) is a display which may be either common anode or common cathode.
FND is ON when logic of either 1 or zero. Here on MDA-Win8086 Trainer Kit common anode
configuration is used. Port A is for controlling the seven segment (FND) on the board.
86

Important Information about this experiment:

 To define constants EQU directive is used


 MOV BX, OFFSET DATA ; puts offset address of DATA into BX
 LEA Instruction: stores the address of a memory variable in a
general register
 Default segments: For SI, DI, and BX the DS is the default
base segment for the address. For BP, the SS is the default
base segment for the address.
 Segment Overrides: Use a Segment Override Prefix to
designate
which segment to use when the default segment is not
appropriate:
MOV AL,CS:[SI] ;uses SI as an offset into the CS
MOV DX,DS:[BP] ;uses BP as an offset into the DS,
;instead of the SS.
 The PTR operator can be used to override the default size of
an operand,
or to explicitly state the size of an operand that would
otherwise be
ambiguous.
 The OFFSET operator gives the offset (displacement) of a
variable
or label from the beginning of its segment

Interfacing of Seven segment with 8086 and 8255A:


87
88

Seven Segment
Display (FND)

System Configuration on MDA- Win8086 Trainer Kit

In order to say the compiler about data type, these prefixes should be used:

BYTE PTR - For Byte.


WORD PTR – For Word (Two Bytes)

PTR = Pointer

To access memory we can use these four registers: BX, SI, DI, and BP. Combining these
registers inside [ ] symbols, we can get different memory locations.
89

A Code for displaying 0-9 in sequence on the seven-segment display:

;*****************************************
; MDA-Win8086 EXPERIMENT PROGRAM *
; FILENAME : FND.ASM
; PROCESSOR : I8086
; 8255 TEST
;*****************************************
CODE SEGMENT
ASSUME CS:CODE,DS:CODE,ES:CODE,SS:CODE
PPIC_C EQU 1FH ; Control Port 8-bit Address
PPIC EQU 1DH ; Port C 8-bit Address
PPIB EQU 1BH ; Port B 8-bit Address
PPIA EQU 19H ; Port A 8-bit Address
ORG 1000H ; Program Effective Address, IP = 1000H
MOV AL,10000000B ; Configure all ports of 8255 as output
OUT PPIC_C,AL ; Send to Control Port 8-bit Address
MOV AL,11110000B ; Logical Bit to disable Port B
OUT PPIB,AL ; Send to Port B 8-bit Address
MOV AL,00000000B ; Logical Bit to disable DOT Matrix
OUT PPIC,AL ; Send to Port C 8-bit Address
L2: MOV SI,OFFSET DATA ; Setup Memory Address of DATA
L1: MOV AL,BYTE PTR CS:[SI] ; Transfer DATA (byte) by using segment override
CMP AL,00H ; Compare DATA with 00H for repetition of 0 to 9
JE L2 ; If Al = 00H then jump L2 to load Setup Memory Address of DATA
OUT PPIA,AL ; If AL ≠ 00H then send AL to Port A
CALL TIMER ; Call TIMER subroutine
INC SI ; Increment SI for next Memory Address of DATA
JMP L1 ; Jump to L1 to transfer DATA (byte) by using segment override
INT 3
TIMER: MOV CX,0 ; Set up counter for delay
TIMER1: NOP ; Dummy instructions to cause time delay
NOP
NOP
NOP
LOOP TIMER1
RET ; Return from subroutine call
;
DATA: DB 11000000B dp g f e d c b a Decimal Hex
DB 11111001B Value Value
DB 10100100B 1 1 0 0 0 0 0 0 0 C0
DB 10110000B 1 1 1 1 1 0 0 1 1 F9
DB 10011001B
1 0 1 0 0 1 0 0 2 A4
DB 10010010B
DB 10000010B 1 0 1 1 0 0 0 0 3 B0
DB 11111000B 1 0 0 1 1 0 0 1 4 99
DB 10000000B 1 0 0 1 0 0 1 0 5 92
DB 10010000B 1 0 0 0 0 0 1 0 6 82
DB 00H 1 1 1 1 1 0 0 0 7 F8
CODE ENDS 1 0 0 0 0 0 0 0 8 80
END 1 0 0 1 0 0 0 0 9 90
90

In MDA_WinIDE8086 Environment:
91

After executing the FND.ASM following operation will happen on MDA- Win8086 Trainer Kit

Assignment:

4. Write an 8086 assembly program on MDA_WinIDE8086 Environment for displaying 9-0


in sequence on the seven-segment display on MDA- Win8086 Trainer Kit.
5. Write an 8086 assembly program on MDA_WinIDE8086 Environment for displaying 0-F
in sequence on the seven-segment display on MDA- Win8086 Trainer Kit.

You might also like