Professional Documents
Culture Documents
List of Experiments:
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
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.
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)
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.
Where to start?
Start Emu8086 by selecting its icon from the start menu, or by running Emu8086.exe.
7
Click “new”
Example: Create COM file and write the following 8086 assembly language
Example on the emulator to see how it works
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.
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:
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.
Memory Map:
Address Memory Description
00000H ~ 0FFFFH RAM PROGRAM & DATA MEMORY
F0000H ~ FFFFFH ROM MONITOR ROM
10000H ~ EFFFFH USER'S RANGE
Function of Keys:
Basic Operation:
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.
Serial monitor is the basic monitor program to do data communicates between MDA-Win8086
and your computer. Adjust the P1 switch as following figure.
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
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
Terminal Window: Terminal window is that you can use to connect the MDA-Win8086 kit.
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 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 6 If you click "Yes" button, your source program will be download from PC into the
target.
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
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:
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.
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;
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 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.
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
Similarly SUB command is used for subtraction of two numbers. For example,
x = 1236H
y = 1438H
z=x–y
x = 146H
y = 5898H
z = x*y
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,
This code takes BX value and then complements all the bits and stores the new value to BX.
AND operation performs bit by bit AND operation and then stores the value in first operand. In
upper code CX holds the final result.
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,
Test operation is a little different from AND operation. It performs bit by bit AND operation but
it does not change any operands value.
Do not confuse yourself with semicolon given after each line in assembly codes above.
Comments are written after semi colon ‘;’ in assembly language.
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
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).
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.
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:
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
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
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
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
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
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
JNZ Back
Factorial: MOV BX, AX ; results stored in AX
HLT
CODE ENDS
END
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.
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
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.
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 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.
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.
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,
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.
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.
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.
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:
STC ; Set Carry (CF = 1)
MOV AL, 5AH ; AL = 01011010B
MOV CL, 03H
ROR AL, CL ; AL = 01011010B ; CF = 0
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.
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
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,
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,
Here the loop is executed until CX is zero. If 1 loop takes 1ms, the program will wait for 100ms.
Observe the operation of this code. What happens when the loop is executed again and again?
Assignments:
Objectives:
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.
The PUSH and POP instructions, as also the allowed operands are shown in (a) and (b)
respectively.
CODE ENDS
END
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.
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).
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)
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:
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:
Base-Plus-Index-Addressing Mode:
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
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
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:
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
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
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)
In MDA_WinIDE8086 Environment:
83
Now start assembling your target application with the " Assemble and Link" button.
If you click "Yes" button, your source program will be download from PC into the target.
After executing the LED.ASM following operation will happen on MDA- Win8086 Trainer Kit
Assignment:
Seven Segment display (FND) and its interfacing with 8086 microprocessor on the MDA-
Win8086 Trainer Kit using Port A of PPI 8255A:
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
Seven Segment
Display (FND)
In order to say the compiler about data type, these prefixes should be used:
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
;*****************************************
; 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: