Microprocessor 8086
Microprocessor 8086
CHAPTER THREE
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 1 of 38
8086 Assembly Language Programming and Instruction Sets
MOV [DI], BX ; the instruction copies the 16-bit content of BX into a memory
location ; offset by the value of EA specified in DI from the current
contents in DS. ; Now, if [DS] =7205H, [DI] =0030H, and [BX] =8765H,
then after MOV ; [DI], BX content of BX (8765H) is copied to memory
locations 72080H ; and 72081H.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 3 of 38
8086 Assembly Language Programming and Instruction Sets
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 4 of 38
8086 Assembly Language Programming and Instruction Sets
Example: MOV AL, LAST [SI+2]; This instruction copies the content of the 20-bit
address computed from the displacement LAST, SI+2 and DS into AL.
The following example shows how to address data element within the array with register
relative addressing.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 5 of 38
8086 Assembly Language Programming and Instruction Sets
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 6 of 38
8086 Assembly Language Programming and Instruction Sets
Figure 8 Two dimensional array accessing using base relative plus index addressing
In direct port addressing mode, the port number is an 8-bit immediate number operand.
This allows fixed access to ports numbered from 0-255.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 7 of 38
8086 Assembly Language Programming and Instruction Sets
Example:
OUT 05H, AL ; sends the contents of AL to 8-bit port 05
In indirect port addressing mode, the port number is taken from DX allowing 64K 8-bit
ports or 32K 16-bit ports.
Example:
IN AL, DX ; if [DX] =7890H, then this command copies 8-bit content of
; port 7890H into AL.
Note that the 16 and 8-bit I/O transfer must take place through AX and AL respectively.
In our previous discussion of the data addressing modes, we always used the DS register
to hold the 16-bit current memory location. Similarly, here in program memory
addressing, we use the CS register to hold this same value of the next instruction to be
executed. However, in case of direct intersegment jump the address could be any memory
segment. What this instruction does is it loads the CS with the immediate 16-bit address.
For that matter, this type of jump is also called far jump.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 8 of 38
8086 Assembly Language Programming and Instruction Sets
Like JMP instruction, CALL instruction also uses direct program addressing with
intersegment or far CALL instruction. Usually in both instructions, (JMP and CALL) the
name of the memory address, called a label is specified in the instruction instead of
address.
In JMP instruction, opcode takes one byte and displacement may take one or two bytes.
When displacement is one byte (8-bit), it is called short jump. When displacement is two
bytes (16-bit), it is called near jump. In both (short and near) cases only contents of IP
register are modified; contents of CS register are not modified. Such jumps are called
intrasegment jumps because jumps are within the current code segment.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 9 of 38
8086 Assembly Language Programming and Instruction Sets
Opcod
e
20000H EB
JMP [03]
20001H 05
20002H --
20003H -- Offset
20004H --
20005H
20006H
The relative JMP and CALL instructions can have either an 8-bit or 16-bit signed
displacement that allows a forward memory reference or a reverse memory reference.
PUSH Operations
This instruction decrements stack pointer by two and copies a word from the source to
the location where the stack points. The source must be a 16-bit (a word). The source
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 10 of 38
8086 Assembly Language Programming and Instruction Sets
can be a general purpose register, a segment register or memory. The way PUSH behaves
has been demonstrated in the following figure (PUSH AX, PUSH CX).
POP Operation
The POP instruction copies a word from the stack location pointed by the stack pointer to
the destination. The destination can be a general purpose register, a segment register, or
a memory location. After the word is copied to the specified location, the stack pointer is
automatically incremented by 2. The next figure shows the map of the stack after and
before the execution of POP DX and POP BX.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 11 of 38
8086 Assembly Language Programming and Instruction Sets
CALL Operation
The call instruction is used to transfer execution to a subprogram or procedure. There
are two basic types of CALL instructions, the near and far CALL. In the near CALL, the
next procedure resides in the same memory segment as the CALL instruction. When the
8086 executes a near call, the stack pointer is decremented by two and then copies the
offset the next instruction after the CALL on the stack segment.
A far CALL is a call to a procedure which is in a different segment from that which
contains the CALL instruction. When the 8086 executes the far CALL it decrements the
stack pointer by two and copies the content of the CS register to the stack. It then
decrements the stack pointer by two again and copies the offset of the instruction after
CALL to the stack. Finally, it loads CS with the segment base of the code which contains
the procedure and IP with the offset of the instruction of the procedure in that segment.
RET Operation
You may have a question how a program will return back to its previous instruction after
finishing a procedure block after the instruction CALL in the calling program. The answer
to this is by the help of an instruction RET which exactly does the opposite task of the
CALL. If the procedure is a near procedure, (in the same code segment as in the CALL
instruction) then the return will be done by replacing the instruction pointer with a word
from the top of the stack.
If the procedure is a far procedure (in a different code segment from the CALL instruction
which calls it), then the instruction pointer will be replaced by the word at the top of the
stack. The stack pointer will then be incremented by two. The code segment register is
then replaced with a word from the new top of the stack. After the code segment word is
popped off the stack, the stack pointer is again incremented by two. These words/ word
are the offset of the next instruction after CALL. So 8086 will fetch the next instruction
after the CALL.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 12 of 38
8086 Assembly Language Programming and Instruction Sets
MOV BX, 67F2H ; Load the immediate number 67F2H in the BX register
MOV CL, [375AH] ; Copy the contents of the memory location, at a displacement of 375AH
; from the data segment into the CL register
MOV [734AH], BX ; Copy the contents of the BX register to two memory locations in the
; data segment. Copy the contents of the BL register to memory location
; at displacement of 734AH and the content of the BH register into the
; memory in the data segment at the displacement 734BH
MOV DS, CX ; Copy a word from the CX register into the data segment register
MOV TOTAL [BP], ; Copy AX to two memory locations. AL into the first and AH into the
AX ; second. The EA is represented by TOTAL and contents of BP.
; Physical address=EA+SS
MOV CS: TOTAL ; same as the above immediate instruction except the physical
[BP], AX ; address=EA+CS, because the segment override prefix is CS
After the execution of the above instruction, the SP=0032 and this is the top of the stack.
PUSHF
This instruction pushes the flag register contents onto the stack. Whenever this command
is executed the most significant byte of flag register moves into the stack segment memory
location addressed by SP-1. The least significant bytes moves into the memory location SP-
2.
POP destination
This instruction copies a word from the stack location pointed by the stack pointer to the
memory location. After the word copied the stack pointer is automatically incremented by
2. Whenever data is removed from a stack, the data addressed at SP moves into the most
significant byte of the destination register and the byte from the stack segment memory
addressed by SP+1 moves into the least significant byte of the destination register.
Example
POP CX ; Copy a word from top of stack to CX and increment
SP by 2
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 14 of 38
8086 Assembly Language Programming and Instruction Sets
POPF
Removes word from top of stack to the flag register.
Initializing the stack
Before going to use any instruction which uses stack for its operation we have to initialize
stack segment and we have to reverse the memory area required for the stack. The stack
can be initialized by including the following sequence of instructions in the program.
Method 1
ASSUME CS: CODE, DS: DATA, SS: STACK
STACK SEGMENT ; Starts stack segment
S_DATA DB 100 DUP (?) ; fixes the stack data to be 100 bytes (DB is a
;directive for byte and DUP is Generate Duplicate
STACK ENDS
Method 2
Syntax: .Stack [size]
Example: .Stack 100
The .stack is a directive which provides shortcut in definition of the stack segment. The
default segment is 1024.
• LEA
• LDS
• LES
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 15 of 38
8086 Assembly Language Programming and Instruction Sets
This instruction determines the offset of the variable or memory location named as the
source and loads this address in the specified 16-bit register. This instruction does not the
affect flag register.
Example:
Meaning:- load the specified register and DS with words from memory. This instruction
copies a word from two memory locations to the specified register and copies the next two
bytes to the DS.
Example:
LDS CX, [3218H] ; Copy the content of memory location at the specified location
; (3218H and 3219H) into CL and CH and the content of the
; 321AH and 321BH into the DS register
LES Instruction:
Syntax: LES Register, Immediate Register address this instruction does operate exactly as
the LDS however loads the ES register instead of the DS.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 16 of 38
8086 Assembly Language Programming and Instruction Sets
LODS/LODSB/LODSW
This instruction copies a byte from a string location pointed to by SI to AL, or a word from a
string location pointed by SI into AX. LODS does not affect any flags.
STOS/STOSB/STOSW
These instructions copy a byte from AL or a word from AX to a location in the extra
segment. DI is the offset storage that is used to give the location of the memory where the
byte or word is going to be written.
3.2.1.5 Miscellaneous Data Transfer Instructions
This group contains the following instructions.
▪ XCHG
▪ LAHF
▪ SAHF
▪ XLAT
▪ IN and OUT
XCHG Instruction
Example
XCHG AL, SUM [BX] ; exchange bytes in AL with the byte in memory
; at EA=SUM+[BX]
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 17 of 38
8086 Assembly Language Programming and Instruction Sets
This instruction copies the contents of the lower byte of 8086 flag register to AH register.
SAHF INSTRUCTION: Meaning---copy AH register content to the lower byte of the flag
register
This instruction copies the content of the AH register into the lower bytes of the flag register
The XLAT instruction replaces a byte in AL register with a byte from a lookup table in
memory. BX register stores the offset of the starting address of the lookup table and AL
register stores the byte number from the lookup table.
AL [BX+AL]
These two instructions are used to access ports of the 8086. IN is used to input data from
the port address specified byte address in IN. The instruction OUT is used to send data to a
port specified by the address in the instruction itself. There are two basic port addressing
modes related to these instructions. These are direct and indirect port addressing modes.
In direct port addressing mode the port address is readily provided in the instruction while
in indirect it is a content of the specified register that will give out the address of the port in
mind.
Example
INC Instruction:
Syntax Increment destination
This instruction increment the value in the destination by one (adds one to the content of
the specified destination). The destination could be a memory location, or a register. This
instruction affects the flags AF, OF, PF, SF and ZF flags.
Examples:
INC AL
AL AL + 1
INC BX
BX BX + 1
INC WORD PTR [BX] ; Increment word at offset BX in DS.
▪ SUB: subtraction
▪ SBB: Subtraction with borrow
▪ DEC: Decrement (Subtract one)
▪ NEG: 2’s Complement of a number
SUB/SBB Instructions:
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 19 of 38
8086 Assembly Language Programming and Instruction Sets
This instruction subtracts one (1) from the specified destination. The destination may be a
register or a memory location. The flags affected by this instruction are AF, OF, PF, SF, and
ZF.
DEC AL
AL AL - 1
DEC BX
BX BX - 1
DEC WORD PTR [BX] ; Decrement a word at offset BX in the DS
This instruction replaces the number in the destination with the 2’s complement of that
number. The destination can be a register or a memory location.
This instruction can be implemented by inverting each bit and adding 1 to the inversion.
This instruction affects the flags AF, CF, SF, PF, ZF and OF.
This instruction multiplies the unsigned number in the source with the number in AL or
AX. Remember that the register AX is used as an accumulator. When a word is multiplied
by the content of AX, the most significant word of result is stored in DX and least
significant word of the result is stored in AX. MUL affects the flags AF, PF, SF and ZF.
This instruction multiplies the signed number in the source with the number in AL or AX.
When a word is multiplied by the content of AX, the most significant word of result is
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 20 of 38
8086 Assembly Language Programming and Instruction Sets
stored in DX and least significant word of the result is stored in AX. MUL affects the flags
AF, PF, SF and ZF.
If the upper byte of the 16-bit result or upper word of 32-bit result contains only copies of
the sign bit (all 0’s or all 1’s), then the CF and the OF flags will both be 0’s.
3.2.2.4 Division
This group contains the
• DIV
• IDIV
DIV Instruction:
When a double word is divided by a word, the most significant word of the double word
must be in DX and the least significant word must be in AX. After the division the AX will
contain a 16-bit quotient and the DX a 16-bit remainder.
If dividing of a byte by a byte is desired it is important to put the byte in AL and pad AH
with all zeroes. The following example demonstrates how binary division is carried out.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 21 of 38
8086 Assembly Language Programming and Instruction Sets
Example
3.2.2.5 Comparison
Syntax CMP Destination, Source
The comparison instruction (CMP) compares a byte/word from specified source with a
byte/word from the specified destination. The source and destination must both be a byte
or word. The source could be an immediate number, a register, or a memory location. The
destination can be a register or a memory location, but both the destination and the source
cannot be memory places at the same time. The comparison is done by subtracting the
source byte or word from the destination byte or word. The result is not stored in the
destination. Rather only the flag registers are affected. Both the source and the destination
remain unchanged. The flags updated include,
The flags affected are AF, OF, SF, ZF, PF, and CF
Segment registers are not comparable; therefore the register cannot be the segment
registers
Examples
CMP BL, 01H ; compare an immediate number 01H with byte in BL
CMP CX, BX ; compare word in BX with word in CX
CMP CX, TOTAL ; compare word at the displacement TOTAL in DS with the
; word in CX
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 22 of 38
8086 Assembly Language Programming and Instruction Sets
This sub instruction set contains two instructions. They are DAA (Decimal Adjust After
Addition) and DAS (Decimal Adjust after Subtraction). Both are used to adjust the results
after the specific operation.
DAA Instruction
This instruction is used to make sure the result of adding two packed BCD numbers is
adjustable to be a formal BCD number. To demonstrate the function of this instruction let
us pay attention to the following example,
1001 + 1000 = 10001
9 + 8 = 17
The sum of the two binary numbers has just generated another binary number of five bits.
This is not any of the BCD format. We here understand that addition of BCDs may results
into non-BCD format that has to be re-arranged into BCD again. (Note that 10001 is not 17
in BCD). This sum can be corrected and changed into BCD by adding 6 in binary to the
sum calculated.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 23 of 38
8086 Assembly Language Programming and Instruction Sets
Adjusting of the BCD sum into BCD in 8086 can be achieved using the following algorithm.
1. If the lower order four bits (D3-D0) in AL is greater than 9 or if AF is set the
instruction adds 6 (06) to the low order four bits.
2. If the value of the high-order four bits (D7-D4) in the AL is greater than 9 or if
carry flag is set, the instruction adds 6 (60) to the high-order four bits.
Example
This instruction updates the AF, CF, PF, and ZF. The OF is understand after DAA. Note
that the instruction DAA only works for AL (Register A)
The instruction is used after subtracting packed BCD numbers to make sure the result is
correct packed BCD.
1. If the lower order four bits (D3-D0) in AL is greater than 9 or if AF is set the
instruction subtracts 6 (06) to the low order four bits.
2. If the value of the high-order four bits (D7-D4) in the AL is greater than 9 or if
carry flag is set, the instruction subtracts 6 (60) to the high-order four bits.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 24 of 38
8086 Assembly Language Programming and Instruction Sets
The DAS instruction updates the AF, CF, PF, and ZF. The OF flag is unaffected after DAS
instruction. The same was as DAA, DAS only affects the AL register.
The numbers from 0-9 are represented as 30H-39H in ASCII code. Whenever adding of two
numbers is necessary in ASCII code, it is important to mask upper nibble (3) from the code
before addition.
register or a memory location. The flags affected include PF, SF and ZF. Both CF and OF
are zero after AND operation.
; AL=0011 1001 = 39 H
; CL=0001 0010 = 12 H
AND AL, CL ; AL=0001 0000 = 10 H
One of the most important application of the AND operations is masking. In masking we
clear bits. Any value AND ed with zero is zero.
3.2.3.2 OR Instructions
It is known that OR operation produces logic 1 whenever one of the two inputs is one.
broadly speaking if any one of the inputs to an OR gate is one, then the output will be one.
X Y Z
0 0 0
0 1 1
1 0 1
1 1 1
Table 2 Truth Table for OR gate
The instruction OR logically ORs all bits in the destination with bits in the source and
stores the result in the destination. The source may be an immediate number, a register or
a memory location. The destination can only be a memory location or a register.
; AL=0011 1001 = 39 H
; CL=0001 0010 = 12 H
OR AL, CL ; AL=0011 1011 = 3B H
The OR instruction has a special function. It is used to set bits. Remember that any bit
ORed with one is one. Flags affected are PF, SF and ZF. Both CF and OF are both 0 after
OR instruction.
X Y Z
0 0 0
0 1 1
1 0 1
1 1 0
The source may be an immediate number, a register or a memory location. The destination
may be a register or a memory location. Flags affected are PF, SF and ZF. Both CF and OF
are both 0 after OR instruction.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 26 of 38
8086 Assembly Language Programming and Instruction Sets
; AL=0011 1001 = 39 H
; CL=0001 0010 = 12 H
XOR AL, CL ; AL=0010 1011 = 2B H
XOR instruction has a special function. If an unknown number is XORed with all ones it
gives all toggled bits. Therefore XOR is used to toggle even unknown values.
NOT Instruction:
Syntax: NOT Destination
The NOT inverts each bit of a byte or a word. The destination can be register or a memory
location.
; AL=0011 1001 = 39 H
NOT AL ; AL=1100 0110 = C6 H
NOT does not affect any flags
Refer the following topics from Microprocessors and Interfacing, first Edition, 2009. A.P Douglas and
D.A Douglas page 3-32 to 3-50
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 27 of 38
8086 Assembly Language Programming and Instruction Sets
• Specify the problem: this is a point at which the programmer understands the
problem and decides what is to be done. This is the most important step that is used
to initiate the next steps.
• Designing the solution for the problem: this step involves coming up with the exact
step by step procedure that must be followed in order to come up with the solution.
The design of the same program could vary. This step requires designing and writing
down of the designs.
• Coding: once the problem is specified and designed, the next step would be coding
the design. Coding is also known as implementing the problem. Coding is telling the
processor what to do in which order. This requires skill so that the programmer may
select appropriate instructions to do the task. Coding for the same design could vary
depending on the experience and knowledge of the coder.
• Debugging: once the program is developed and implemented (coded), the next step is
debugging the code. Debugging is the process of testing the code if it does exactly
what it is supposed to according to the design procedure. During this process errors
are found and fixed.
There is a conventional way of providing what to be done. This is known as flow charts.
Flow charts give the actions to be performed in step by step fashion. This is a graphical way
of representing the actions.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 28 of 38
8086 Assembly Language Programming and Instruction Sets
There are important steps in coming up with a 100 percent functional program. This
involves understanding the various types of languages that machines (microprocessors)
understand. Even though there are high level programs that can take the filthy works of
assembling and linking, understanding the assembler language brings in great
understanding how processors work. A program which has simply a sequence of binary
coeds for an instruction is called machine level language program. This takes a name
machine language just because it is made up of series of zeroes and ones that machines
directly understand. However, writing machine language for humans is extremely
complicated. Therefore, there must be another way of presenting these codes to humans by
compromising the level the machines understand it. Getting one more stage above the
machine language is the assembly language. The assembly language is relatively much
easier for a programmer to understand and write. We have already studied instructions in
their assembly form. AND is one of these. However, it is important to note that the
instructions AND shall finally be converted in its machine equivalent (zeroes and one). In
assembly language we have opcodes or mnemonics that represent the specific zeroes and
ones in machine languages.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 29 of 38
8086 Assembly Language Programming and Instruction Sets
A mnemonic is an instruction itself. An instruction could address one or two operands. The
comment is an optional field used to indicate what exactly the code does. The mnemonic
generate the control signals while when the operands are data to be processed.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 30 of 38
8086 Assembly Language Programming and Instruction Sets
• Come up with a minimum solution to the problem: this is very important as there
is a requirement of memory spaces. Minimum code requires less memory and is
highly preferable. The design should come up with the minimum solution for the
problem.
• Use proper instruction and advanced instructions: there are more than one set of
instructions that do the same task. Going from one instruction to the other should be
based on the fact that it results in better performance for the intended application
rather than a programmer being familiar with it. Therefore, knowledge of instructions
and instruction sets is mandatory. Here falls addressing modes too. Remember that
addressing modes can indirectly specify the type of the data and addressing modes
result in different execution clock cycle for the instruction which directly affects the
performance of the program. For example, the following explains two possible ways of
implementing a code for moving blocks of data from one place to another.
One can see that the second approach requires less number of instructions and is
therefore more efficient, meaning that it occupies less amount of memory space.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 31 of 38
8086 Assembly Language Programming and Instruction Sets
An assembler translates a source file that was created using the editor into machine
language such as binary or object code. The assembler usually produces two different types
of files. The one is called the object file and the other assembler list file. The object file
contains the binary code for the instructions and the information the addresses of the
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 32 of 38
8086 Assembly Language Programming and Instruction Sets
instructions. The list file contains the assembly language statements, the binary code for
each instruction, and the offset for each instruction. The following figure illustrates this.
Linking is a process of joining several object files into single larger object file. Linking aids
in designing one big program. Usually programs can be developed separately in a logical
fashion and linked together to form one large program. A set of instruction which does one
specific task is designed and separately saved. When there is a need to use the module, one
can easily call the routine and add it into the code. The linker process produces a link file
that contains the binary codes for all the combined modules. The linker also produces a
map which contains the address information about the linked files. The linker however,
does not assign absolute addresses to the program; it only assigns relative addresses
starting from zero. This kind of program is said to be re-locatable because it can be placed
anywhere in the memory.
Debugging a program requires loading the object file of the program into the memory of the
target processor and executes it to see if it is possible to come up with the desired output. A
debugger is usually a software program or a hardware kit. In case of software applications,
the debugger stands on the behalf of the processor and simulates it fully. Debugger
software allow you to
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 33 of 38
8086 Assembly Language Programming and Instruction Sets
1. To look into the contents of register and memory locations after the program runs
2. Allows the users to change the content of registers and memory locations
3. Some debuggers allow users to execute sets of instructions step by step
4. Allows users to set breakpoints and execute lines up to the points.
In this section we shall see the possible ways of providing delays in 8086. All are software
delays
There is a special instruction in 8086 that does nothing. This instruction is NOP
instruction. It is a mnemonic for no operation. 8086 requires 3 clock cycles to finish
executing NOP. Therefore one can at least get three clock cycles by executing it in between
two instructions.
Another way to delay a processor is by making it count up to certain desired number. One
should load the number to be loaded onto the CX register so that counting it will produce
the desired amount of delay in the program. Look at the following example
= 2692 cycles
If the processor is running on 10 MHz, we see that one clock frequency is 0.1µsec, therefore
using the above routine one can get 269.2µseconds.
In this approach once or more external loop is added to execute the internal loop multiple
times so that we can get larger delays. The inner loop is nothing but the program we have
seen in the above immediate section.
For count = 150 and multiplier=50, the number of clock cycles required are,
= 134600 cycles
If the processor is running on 10 MHz, we see that one clock frequency is 1µsec; therefore
using the above routine one can get 13.46ms.
Exercise: Write an 8086 ALP to generate a delay of 100ms, if the processor is running
on 10 MHz frequency.
8086 can be configured to work in two different modes. These are minimum and maximum
modes. In minimum mode, the processor is alone and is required to take care of all the
coordination. In maximum mode, two or more processors work in conjunction to each other
and therefore must have another third party to coordinate them. The minimum mode is
often used for small systems and the maximum mode for larger tasks requiring complex
systems. There is no literal physical difference between the two modes. There difference is
only setting of the MN/MX (MX bar). Let us see the special pins that will affect the mode in
8086.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 36 of 38
8086 Assembly Language Programming and Instruction Sets
4 DT/R (Data transmit/Receive) Output: this controls the data flow direction.
High on this pin indicates that the processor is transmitting while the low is for
receiving.
5 M/IO Output: is used to differentiate between memory (M/IO=HIGH) or IO
(M/IO=LOW) data transfer.
6 WR: Write Output: when 8086 writes data to either external IO or memory device
this pin goes LOW.
7 HOLD input, HLDA Output: A HIGH on HOLD pin indicates that another master
(DMA) is requesting to take over the system bus. When a HOLD is received the
processor outputs HLDA signal HIGH as an acknowledgement. At the same time
the processor tristates the system bus. A LOW on HOLD gives the system bus
control back to the processor. Processor then output LOW signal on HLDA.
2 S2, S1, So (Active Low) (outputs): these three status signals indicate the type of
transfer to take place during the current bus cycle.
S2 S1 So Machine Cycle
Interrupt
0 0 0
Acknowledge
0 0 1 I/O Read
0 1 0 I/O Write
0 1 1 Halt
1 0 0 Instruction fetch
1 0 1 Memory Read
1 1 0 Memory Write
1 1 1 Inactive-Passive
3 LOCK (Active Low): signifies that an instruction with a LOCK prefix is being
executed and the bus is not to be used by any other processor.
4 RQ/GT1 and RQ/GT0 (Active Low) in the maximum mode, HOLD and HLDA pins
are replaced by the RQ (Bus Request) and GT0 (Bus Grant), and RQ/GT1 signals.
By using bus request, another master can request for the system bus and
processor communicate that the request is granted to the requesting master by
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 37 of 38
8086 Assembly Language Programming and Instruction Sets
using bus granted signals. Both signals are similar except the RQ/GT0 has higher
priority than RQ/GT1.
Arba Minch Institute of Technology, Faculty of Electrical & Computer Engineering Page 38 of 38
Chapter-4 Interfacing
4 Interfacing
Interfacing is a process of joining different equipment to a microprocessor so that a specific task shall be
worked out. A microprocessor is a controller that coordinates tasks of various devices connected to it. In this
section, we shall see interfacing of IOs, timers, serial devices, interrupts and memory. In each section, we shall
also examine the special hardware that will be integrated into the 8086 system to provide appropriate functions.
Page 1 of 30
Chapter-4 Interfacing
FFFF
1Mx8
64Kx8
0000 0000
Figure 1 Isolated I/O
FFFF
I/O
0000
Figure 2 Memory-mapped I/O
In isolated I/O, the address for the I/O devices called PORTS are separated from the memory. This implies
that only the instructions IN and OUT are used to access I/O devices. This is one disadvantage. A separate
control signal for I/O space are developed using the (M/IO and W/R). The address for isolated IO ranges
from 0000 to FFFF in 8086 (remember that 8086 has 16 bit IO addressing capability).
Page 2 of 30
Chapter-4 Interfacing
These can be programmed in two groups of 12 pins. These groups again can be made to operate in three distinct
modes. 8255 is a TTL compatible device. Therefore, when one needs to interface some higher voltage or current
valued devices, it is important to device driver circuits.
The ports are usually divided into three labels. Port A (PA7-PA0), port B (PB7-PB0) and port C (PC7-PC0). The
8255 is enabled or selected by its CS (Active low) when it is programmed, read or written.
Page 3 of 30
Chapter-4 Interfacing
Ports can also be arranged into groups. One can form two main groups of the 8255 as follows.
Group A: port A including the upper nibble of the port C
Group B: port B including the lower nibble of the port C
The following figure shows the possible way of interfacing the 8255 to a microprocessor and address bit
combinations for three port selections.
• Command Byte A
• Command Byte B
Command byte A is used to program the port groups as defined above as
▪ Inputs or outputs
▪ In either modes 0, 1 and 2
The command byte B is used to set or clear specific bits when configured in modes 1 and 2.
Page 4 of 30
Chapter-4 Interfacing
▪ Mode 0
▪ Mode 1
▪ Mode 2
Mode 0: This mode is used to set the ports into group A. Both group A and group B have 12 ports. This mode
is the most commonly used and all pins are simple latched input or output.
Mode 1: This is an occasionally used mode in which most pins of port C are used to provide handshaking
signals to an IO device. This mode enables IOs to operate asynchronously.
Mode 2: In this mode, only group A is used. The mode sets port A into a bidirectional port while port C is used
for handshaking signals.
To program the 8255, the command register mode and the operation mode of command Byte A must be
selected.
For example, to program all the ports as an output in mode 0 (this is the most common used configuration). We
can write an ASP as follows
Page 5 of 30
Chapter-4 Interfacing
The timer is interfaced to a microprocessor through bits A1 and A0. These bits therefore define the immediate
task as follows.
A1 A2 Function
0 0 Counter 0
0 1 Counter 1
1 0 Counter 2
1 1 Control Word (Command Byte)
Page 6 of 30
Chapter-4 Interfacing
Page 7 of 30
Chapter-4 Interfacing
• CS (Active Low)=0
• A1A0=11
• RD (Active Low)=1
• WR (Active Low)=0
After these bits are set as stated, selection of one of the three counters, loading initial value and specifying count
type must be provided in the data bits coming. (D0 to D7). The bits D0 to D7 can be used as follows during
programming.
D7 D6 D5 D4 D3 D2 D1 D0
SC1 SC0 RW1 RW0 M2 M1 M0 BCD
Bit definitions
M2 M1 M0 MODE
0 0 0 Mode 0
0 0 1 Mode 1
X 1 0 Mode 2
X 1 1 Mode 3
1 0 0 Mode 4
1 0 1 Mode 5
Bits RW1 and RW0 are defined as
BCD FUNCTION
0 Binary Counter
1 Binary Decoded Decimal (BCD) Counter [4 Decades]
The initial count for the currently selected counter must be sent after the control word. The initial count values
are written into the IO addresses of the specified counter. Upon programming all the three counters, one must
follow certain sequences otherwise it will result in invalid settings. The following four sequences are all possible
followed by the initial counts.
Page 8 of 30
Chapter-4 Interfacing
1. Simple Read: inhibit clocking by clearing G (disrupt future counts) to ensure stable counter at a proper
level and then simply read the counters at their address (for example A1A0=01 for counter one)
2. Issue a counter latch command by writing appropriate byte into the control word register. This is
achieved by setting RW1RW0=00 and selecting the appropriate counter using SC1 SC0. This command
latches the counter’s output parallaly and the count value is available in the Output Latch (OL) and
remains unchanged till the output is read by the microprocessor or the counter is re-programmed.
After the timers are programmed, the next step will be reading their count values. Reading operation
happens while when the counters count.
• A1A0=11
• CS (Active Low)=0
• RD (Active Low)=0
• WR (Active Low)=1
D7 D6 D5 D4 D3 D2 D1 D0
SC1 SC0 0 0 X X X X
Page 9 of 30
Chapter-4 Interfacing
3. Using the Read Back Command:- this is a command word (A1A0=11)written into the control word to
latch status and or count of any of the three counters. All the three counters can be specified
simultaneously. The command word looks like the following.
Counter modes
Page 10 of 30
Chapter-4 Interfacing
Mode 1
This mode is hardware triggered one-shot. Gate G is used as the monostable hardware trigger input. This
application loads the counter on the next falling edge after the trigger goes high and the output remains low till
the terminal count. The duration of the output will be nTclk. If the Gate goes high again, the monostable is
retriggered and the count will be postponed for another duration.
Mode 2
This mode is used to divide a frequency by a specified number. (it is a programmable frequency clock). The gate
G is always enabled to count. The out frequency fOUT=Clock frequency/n.
Page 11 of 30
Chapter-4 Interfacing
Mode 3
This mode is similar to mode 2 except it only produces square wave outputs.
Mode 4
This will give an active low OUT signal when the count finishes. The count remains for n+1 pulses. This is used
to trigger external devices.
Page 12 of 30
Chapter-4 Interfacing
Mode 5
This is hardware triggered strobe output (Active Low). Gate G is used as hardware trigger. Upon receiving a
high on the gate the OUT gives an active low of one pulse (for the duration of one period). This is a similar
mode of counting to that of mode 1.
Mode Specifications
M2 M1 M0 Function
# (n = pre-loaded initial count, Tclk = clock interval)
X 1 1 3 Square-Wave Generator fout = fclk / (n) Duty cycle 50% (for all n)
Interfacing examples
In the following schematic, it is desired to generate 100KHz and 200KHz frequency from counters 0 and 1. The
wiring diagram and the assembly language program would be as follows.
TIME PROC NEAR USES AX DX
MOV DX, 706H ; address Control register
MOV AL, 00110110B ; program counter 0 for mode 3
; Counter Load/Read format is 2 bytes (1 for mode 2)
OUT DX, AL
MOV AL, 01110100B ; program counter 1 for mode 2
OUT DX, AL
Page 13 of 30
Chapter-4 Interfacing
OUT DX, AL
MOV DX, 702H ; Address counter 1; A procedure that programs the 8254 timer to function as illustrated in Fig. 18 LS 1st)
MOV AL, 40 ; Load initial count 40d into counter 1
OUT DX, AL
MOV AL, 00 ; Then MS byte of initial count
OUT DX, AL
RET
TIME ENDP
Depending on the communication channel interface between the receiver and transmitter, one can have three
types of data transfers between a receiver and transmitter.
Page 14 of 30
Chapter-4 Interfacing
Page 15 of 30
Chapter-4 Interfacing
The 3 IO address bits from the microprocessor can refer to various functions as follows.
A2 A1 A0 Function
0 0 0
Receiver buffer (read data from RX) and transmitter holding (write data to TX). Also write LS byte of baud rate divisor
0 0 1
Interrupt enable. Also write MS byte of baud rate divisor
0 1 1
Line control Register (Write into the line control register to program asynchronous communication at initialization)
1 0 0
Modem control
1 0 1
Line status LSTAT (Read the line status register to see if TX or RX are ready and to check for errors )
1 1 0
Modem status
1 1 1
Scratch
Page 16 of 30
Chapter-4 Interfacing
ST P PE Function
0 0 0 No parity
0 0 1 Odd parity
0 1 0 No parity
0 1 1 Even parity
1 0 0 Undefined
1 1 0 Undefined
The baud rate is programmed by loading a 16-bit divisor for the crystal oscillator or the external input frequency
into the I/O port addresses. To write the 16-bit divisor value, the operation will be done in two steps. First,
write the LSB into Byte of the divisor and then the MSB of the divisor. This can be achieved as follows
The divisor value is dependent on the oscillator frequency and the baud rate required. The baud rates supported
are
Baud Rate
110
300
1200
2400
4800
9600
19,200
38,400
57,600
115,200
Divisor value can be calculated using the following formula
𝑂𝑠𝑐𝑖𝑙𝑙𝑎𝑡𝑜𝑟 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦
𝐷𝑖𝑣𝑖𝑠𝑜𝑟 =
(16 ∗ 𝐵𝑎𝑢𝑑 𝑟𝑎𝑡𝑒)
For a certain clock frequency, one can calculate divisor values of each baud rate.
The address of this control register is at A2A1A0=010. The 8-bit exposition of the FIFO control register is as
follows
Page 17 of 30
Chapter-4 Interfacing
Interfacing Example:
Interface 16550 to 8086 having a clock frequency of 18.432MHz. The desired baud rate is 9600 with frame
format of 7 bit data, odd parity and one stop bit.
Page 18 of 30
Chapter-4 Interfacing
SENDER PROGRAM
;//////////////////////////////////////////////////////////////////////////////////////
;A procedure that transmits the byte in AH serially
;via the 16650 UART
LSTAT EQU 0F5H ; The Line status register (LSTAT) (A2 A1 A0 = 101)
DATA EQU 0F0H ; TX/RX Data Register at (A2 A1 A0 = 000)
SEND PROC NEAR USES AX
.REPEAT ;test the TH bit (bit 5) in to see if TX is available
IN AL,LSTAT
Page 19 of 30
Chapter-4 Interfacing
4.5 Interrupts
An interrupt is an event which informs the CPU that its service (action) is needed. The possible sources of
interrupts are:
• Internal fault (e.g... divide by zero, overflow)
• Software
• External hardware:
Hardware interrupts can further be categorized as maskable and non-maskable interrupt. A reset is also one type
of hardware interrupt.
When a CPU is requested to service an interrupt, it will stop or finish (depending on the priority of the
interrupt) the current job. If it is stopping an execution, it will PUSH the current flag register, IP and CS register
and jumps to a fixed memory location known as Interrupt Service Routine (ISR). Upon finishing the ISR block
it will get back to its previous instruction by POPing the aforementioned registers. 8088 has the following
hardware interrupts.
The interrupt flag in the flag register is used to mask any hardware interrupt that may arrive on the INTR pin. It
this bit is 0, all incoming interrupts will be masked and shall not be serviced. However, this has no effect on the
interrupts arriving on the NMI pin as the pin is not maskable. The CLI sets the interrupt flag (IF) to 0 and the
STD will set it to 1.
Page 20 of 30
Chapter-4 Interfacing
▪ Increases the CPU’s throughput(the processor will not waste busy waiting sequential executions)
▪ Will prioritize services
▪ Improves response time
n Interrupt type
1 Single trap (will be enabled if Trap flag is set)
3 INT3 (Breakpoint)
4 INTO (Overflow) the OF bit in the flag
5 BOUND (Check limits)
Page 21 of 30
Chapter-4 Interfacing
Each interrupt has its own interrupt handler. The number of hardware interrupts is limited by the number of
interrupt request (IRQ) lines to the processor, but there may be hundreds of different software interrupts.
Interrupts are a commonly used technique for computer multitasking, especially in real-time computing. Such a
system is said to be interrupt-driven. Special instruction IRET is used at the end of the ISR for both types of
interrupts.
Page 22 of 30
Chapter-4 Interfacing
The interrupt vector contains the starting address (both the offset and segment) of the ISR. The first two bytes
of the vector are the OFFSET address while the second two bytes contain the segment address of the ISR.
Page 23 of 30
Chapter-4 Interfacing
1. One or more of the interrupt Request lines (IR7-IR0) are set high that in turn will set the corresponding
Interrupt Request Register (IRR) bits.
2. The 8259 will then evaluate these requests and then sends an INT to the CPU, if appropriate
3. The CPU will acknowledges with an INTA pulse
4. When an INTA pulse is received from the CPU, the high priority IRS bit will be set and the
corresponding IRR bit is set.
5. The 8086 will initiate the second INTA pulse. During this pulse, the 8259A releases an 8-bit pointer
onto the data bus where it is ready by the CPU.
6. This completes the interrupt cycle. The End of Interrupt command will be executed at the end of an
ISR.
The 8259A accepts two types of command words generated by the CPU:
Page 24 of 30
Chapter-4 Interfacing
1. Initialization Command Words (ICWs): Before normal operation can begin, each 8259A in the system
must be brought to a starting point-by a sequence of 2 to 4 bytes timed by WR (Active low) pulses.
2. Operation Command Words (OCWs): These are the command words which command the 8259A to
operate in various interrupt modes. Theses modes are:
a. Fully nested mode
b. Rotating priority mode
c. Special mask mode
d. Polled mode
The OCWs can be written into the 8259A any time after initialization.
Refer to the 8259A PROGRAMMABLE INTERRUPT CONTROLLER application note for the rest of
the information.
For example, a hard disk may boast a transfer rate of 5 M bytes per second, i.e. 1 byte transmission every 200 ns.
To make such data transfer via the CPU is both undesirable and unnecessary.
The basic idea of DMA is to transfer blocks of data directly between memory and peripherals. The data don’t go
through the microprocessor but the data bus is occupied. “Normal” transfer of one data byte takes up to 29
clock cycles. The DMA transfer requires only 5 clock cycles. Nowadays, DMA can transfer data as fast as 60 M
byte per second. The transfer rate is limited by the speed of memory and peripheral devices.
Page 25 of 30
Chapter-4 Interfacing
1 Peripheral asserts one of the request pins, e.g. RQ/GT1 or RQ/GT0 (RQ/GT0 has higher priority)
2 8088/8086 completes its current bus cycle and enters into a HOLD state
3 8088/8086 grants the right of bus control by asserting a grant signal via the same pin as the request
signal.
4 DMA operation starts
5 Upon completion of the DMA operation, the peripheral asserts the request/grant pin again to
relinquish bus control.
However the later is associated only with bulk data movement and is designed to function with other interfaces.
DMA controlled IOs must follow certain procedure in order to get successful data transfer. The followings are
features of DMA protocol
▪ The direct memory access (DMA) I/O technique provides direct access to the memory while the
microprocessor is temporarily disabled.
▪ A DMA controller temporarily borrows the address bus, data bus, and control bus from the
microprocessor and transfers the data bytes directly between an I/O port and a series of memory
locations.
▪ The DMA transfer is also used to do high-speed memory-to memory transfers.
▪ Two control signals are used to request and acknowledge a DMA transfer in the microprocessor-based
system.
▪ The HOLD signal is a bus request signal which asks the microprocessor to release control of the buses
after the current bus cycle.
▪ The HLDA signal is a bus grant signal which indicates that the microprocessor has indeed released
control of its buses by placing the buses at their high-impedance states.
▪ The HOLD input has a higher priority than the INTR or NMI interrupt inputs.
Page 26 of 30
Chapter-4 Interfacing
The 8237 can be cascaded to produce Master-Slave architecture. This is used to interface priority based IO
devices. The 8237 DMA controller supplies the memory and I/O with control signals and memory address
information during the DMA transfer. 8237 is a four-channel device that is compatible to the 8086/8088
microprocessors and can be expanded to include any number of DMA channel inputs.
The 8237 is capable of DMA transfers at rates of up to 1.6M bytes per second and each channel is capable of
addressing a full 64K-byte section of memory and can transfer up to 64K bytes with a single programming.
▪ The current address register (CAR) is used to hold the 16-bit memory address used for the DMA
transfer.
▪ The current word count register (CWCR) programs a channel for the number of bytes (up to 64K)
transferred during a DMA action.
▪ The base address (BA) and base word count (BWC) registers are used when auto-initialization is
selected for a channel. In this mode, their contents will be reloaded to the CAR and CWCR after the
DMA action is completed. Each channel has its own CAR, CWCR, BA and BWC.
▪ The command register (CR) programs the operation of the 8237 DMA controller
▪ The mode register (MR) programs the mode of operation for a channel.
▪ The request register (RR) is used to request a DMA transfer via software, which is very useful in
memory-to-memory transfers.
▪ The mask register set/reset (MRSR) sets or clears the channel mask to disable or enable particular
DMA channels.
▪ The mask register (MSR) clears or sets all of the masks with one command instead of individual
channels as with the MRSR.
▪ The status register (SR) shows the status of each DMA channel.
Page 27 of 30
Chapter-4 Interfacing
The following table shows the operation along with address lines of some of the listed registers.
Page 28 of 30
Chapter-4 Interfacing
Page 29 of 30
Chapter-4 Interfacing
Page 30 of 30