86MICRO
86MICRO
86MICRO
– Computer interfacing
<--- Keypad
EEE/CSE 226
EV80C186EB
I-3
EEE/CSE 226
80C186EB
\C:
\DOS
\WINDOWS
POWERPOINT VIEWER
APBUILDER
80C186EB MANUAL
\226LAB
ASM86.EXE
LINK86.EXE
LOC86.EXE
EEE/CSE 226
8086 Features
The address refers to a byte in memory. In the 8088, these bytes come in on
the 8-bit data bus. In the 8086, bytes at even addresses come in on the low
half of the data bus (bits 0-7) and bytes at odd addresses come in on the upper
half of the data bus (bits 8-15).
The 8086 can read a 16-bit word at an even address in one operation and at an
odd address in two operations. The 8088 needs two operations in either case.
I-8
EEE/CSE 226
8086 Architecture
• The 8086 has two parts, the Bus Interface Unit (BIU) and the
Execution Unit (EU).
• The BIU fetches instructions, reads and writes data, and computes the
20-bit address.
• The EU decodes and executes the instructions using the 16-bit ALU.
The BIU fetches instructions using the CS and IP, written CS:IP, to contruct
the 20-bit address. Data is fetched using a segment register (usually the DS)
and an effective address (EA) computed by the EU depending on the
addressing mode.
I-9
EEE/CSE 226
8086 Block Diagram
I-10
EEE/CSE 226
8086 Architecture
The EU contains the following 16-bit registers:
AX - the Accumulator
BX - the Base Register
CX - the Count Register
DX - the Data Register
SP - the Stack Pointer \ defaults to stack segment
BP - the Base Pointer /
SI - the Source Index Register
DI - the Destination Register
The AX, BX, CX, and DX registers can be considers as two 8-bit registers, a
High byte and a Low byte. This allows byte operations and compatibility with
the previous generation of 8-bit processors, the 8080 and 8085. 8085 source
code could be translated in 8086 code and assembled. The 8-bit registers are:
AX --> AH,AL
BX --> BH,BL
CX --> CH,CL
DX --> DH,DL
I-11
EEE/CSE 226
8086 Architecture
I-12
EEE/CSE 226
8086 Programmer’s Model
ES Extra Segment
BIU registers CS Code Segment
(20 bit adder) SS Stack Segment
DS Data Segment
IP Instruction Pointer
AX AH AL Accumulator
BX BH BL Base Register
CX CH CL Count Register
DX DH DL Data Register
SP Stack Pointer
BP Base Pointer
SI Source Index Register
EU registers
DI Destination Index Register
16 bit arithmetic
FLAGS
I-13
EEE/CSE 226
Segments
Address
0H
Segment Starting address is segment
CODE
64K Data
STACK Segment
DATA
EXTRA CS:0
64K Code
Segment
Segment
Registers
Memory Segments
Segment 0H
Registers
01000H
DS: 0100H DATA
10FFFH
0B2000H
SS: 0B200H STACK
0C1FFFH
CS: 0FF00H
0FF000H
CODE
0FFFFFH
4000H
CS: 0400H
IP 0056H 4056H
CS:IP = 400:56
Logical Address
Memory
0400 0
Segment Register
Offset + 0056
Physical or 04056H 0FFFFFH
Absolute Address
The offset is the distance in bytes from the start of the segment.
The offset is given by the IP for the Code Segment.
Instructions are always fetched with using the CS register.
0A00 0A000H
SS:
0A100H
SP 0100 SS:SP
Memory
0A00 0
Segment Register
Offset + 0100
I-17
EEE/CSE 226
The Data Segment
0H
05C00H
DS: 05C0
05C50H
EA 0050 DS:EA
Memory
05C0 0
Segment Register
Offset + 0050
I-18
EEE/CSE 226
Addressing
Modes
Assembler directive
SAM DW 25H SAM is defined as a word (16-bit) variable, i.e., a
memory location that contains 25H.
Direct Addressing
MOV AX,SAM [SAM] AX, the contents of SAM is put into AX.
The cpu goes to memory to get data.
25H is put in AX.
Immediate Addressing
I-19
EEE/CSE 226
Addressing
Modes
Memory MOV AX,SAM
0H
[200ABH] = 25H
DS [200ACH] = 00H
Direct Addressing
SAM
Data is at DS:SAM
25H
0FFFFFH
I-20
EEE/CSE 226
Addressing
Modes
EA = BX + DI + offset SAM
I-21
EEE/CSE 226
Addressing
Modes
Data usually comes from the data segment. However, data can
be fetched from any segment by use of the segment override prefix.
I-22
EEE/CSE 226
Addressing
Modes
FAR
Intersegment Direct -- new CS and IP are encoded in
(CS changes) the instruction.
I-23
EEE/CSE 226
Assembly Language
The Assembler is a program that reads the source program as data and
translates the instructions into binary machine code. The assembler
outputs a listing of the addresses and machine code along with the
source code and a binary file (object file) with the machine code.
Most assemblers scan the source code twice -- called a two-pass assembler.
• The first pass determines the locations of the labels or identifiers.
• The second pass generates the code.
To locate the labels, the assembler has a location counter. This counts the
number of bytes required by each instruction.
• When the program starts a segment, the location counter is zero.
• If a previous segment is re-entered, the counter resumes the count.
• The location counter can be set to any offset by the ORG directive.
In the first pass, the assembler uses the location counter to construct a
symbol table which contains the offsets or values of the various labels.
The offsets are used in the second pass to generate operand addresses.
I-24
EEE/CSE 226
Assembly Language
In addition to the offset of the variable, the symbol table also contains
the type of variable, e.g.,
etc.
I-25
EEE/CSE 226
Pseudo
Code
Programming with no rules of syntax.
Example
I-26
EEE/CSE 226
Functional Level Flow
Chart AX = A
START A = BC
DX = B
CX = C
A 0
B Multiplicand
C Count
A A+B
C C-1
NO
C=0?
YES
END
I-28
EEE/CSE 226
List
File
1 NAME MULBYADD
2 ;
3 ;This program does multiplication by repeated addition
4 ;
5 ASSUME CS:SEG0,DS:SEG0,SS:SEG0,ES:SEG0 ; Segments overlap.
---- 6 SEG0 SEGMENT AT 10H ; Segment registers assumed to be 10H.
0100 7 ORG 100H ; Program origin within the segment.
0100 B81000 8 START: MOV AX,SEG0 ; Load segment value.
0103 8ED8 9 MOV DS,AX ; Set up data segment.
0105 B80000 10 MOV AX,0 ; Initialize the product.
0108 BA0600 11 MOV DX,6 ; Number to be added.
010B B90700 12 MOV CX,7 ; Loop count or number of repeats.
010E 03C2 13 MLOOP: ADD AX,DX
0110 49 14 DEC CX
0111 75FB 15 JNZ MLOOP
0113 CC 16 INT 3 ; Return to monitor
---- 17 SEG0 ENDS
18 END START
I-29
EEE/CSE 226
More Assembler Directives
DW Define word
DB Define byte.
I-30
EEE/CSE 226
Assembly Example
I-31
EEE/CSE 226
Symbol
Table
Symbol Offset Segment Type
DSEG 0 Segment
COUNT 10H DSEG Word variable
ARRAY 12H DSEG Byte variable
.
.
.
CSEG 0 Segment
START 0 CSEG Near label
REPEAT 9H CSEG Near label
.
.
.
The program source should be organized in the following order.
Data segments
Stack
Code
Procedures (subroutines)
Main routine
Therefore, labels and variables are defined before they are used
by the code, except for forward jumps.
I-32
EEE/CSE 226
Debuggin
g
When developing a program,write and test small sections or modules. You can develop you own
library of tested routines.
Check that you have not accidentally reversed source and destination operands.
Assuming that you have assembled, linked, located, and downloaded your program without any
errors, what do you do when your program
does not work. You use a debugger and two standard techniques, single stepping and breakpoints.
You should know what is the result of executing each instruction. In single stepping through a
program, you execute one instruction at a time and review the registers and contents any relevant
memory location after each step. This is like watching a slow motion replay of your program
execution.
Obviously you cannot single step through a long program every time you need to debug it. You can
set a breakpoint so that the program can run through code that has previously been checked and
stop at the point in the program where the problem is. This allows you to run through procedures
that have been debugged. You can run through a number of instructions before stopping to
examine registers and memory.
EEE/CSE 226
Instruction
Encoding
Machine code (the encoded instruction) specifies things such as
1. The operation to be performed.
2. The operand or operands to use.
3. Byte or word operations.
4. Whether data is in a register or memory.
5. If data is in memory, how the address is generated.
In the 8086, the machine-code instructions can vary from one to six bytes
with additional bytes for prefixes such as segment override.
Single-Byte Instructions
1. No operand, e.g., the clear carry instruction CLC has the code 0F8H.
Example
[1111111 0] [11 000 001] INC CL
INC byte reg INC CL
I-35
EEE/CSE 226
Instruction
Encoding
A086
+005C
A0E2 = EA
I-36
EEE/CSE 226
Examples of 8086 Instruction Formats
Opcode dw
00 reg r/m Register Indirect
Direct addressing is the special case where mod = 00 and r/m = 110.
[ 0 0 0 0 0 0 1 1 ] [0 0 0 0 0 1 1 0] [0 0 1 1 0 1 0 0] [0 0 0 1 0 0 1 0 ] = 03 06 34 12
AX is AX 3 4 1 2
destination
Word operation
Due to the special case, the instruction ADD AX,[BP] is encoded as
ADD AX,Disp[BP] where Disp = 0.
I-38
EEE/CSE 226
Loops and Conditional
Jumps
All loops and conditional jumps are SHORT jumps, i.e., the target must
be in the range of an 8-bit signed displacement (-128 to +127).
The displacement is the number that, when added to the IP, changes the
IP to point at the jump target. Remember the IP is pointing at the next
instruction when this occurs.
The loop instructions perform several operations at one time but do not
change any flags.
I-40
EEE/CSE 226
Loops and
Strings name sholoop
dseg segment at 200H
string db 'This is a string.'
endstr equ $ - offset string ; label whose offset is the
dseg ends ; length of the string
sseg segment at 100H
dw 200H dup(?)
stktop label word ; location after stack
sseg ends
extrn outchar:near
cseg segment at 4000H
assume cs:cseg,ds:dseg,ss:sseg
start: mov ax,dseg
mov ds,ax ; set up data segment
mov ax,sseg
mov ss,ax ; set up stack segment
mov sp,offset stktop ; sp is decremented first
mov cx,offset endstr ; length of string
mov bx,offset string ; points at string
back: mov al,[bx] ; get a character
call outchar ; output one character
inc bx ; point at next character
loop back ; cx=cx-1, back if cx not 0
cseg ends
end start
I-41
EEE/CSE 226
Procedures
Two assembler directives are used, the PROC and ENDP directives are
used to begin and end the procedure. For example:
A near CALL pushes the IP onto the STACK before changing the IP to start
executing the procedure. The RET restores the IP so the program can
pick up from where it left off, i.e., the instruction right after the CALL.
The far CALL pushes both CS and IP onto the STACK and the RET
restores them to their values before the call (assuming the SP is pointing
at the IP on the stack when the RET is executed).
I-42
EEE/CSE 226
Parameter Passing
3. Use the STACK. This also uses memory, but only temporarily. Since
the 8086 has a limited set of registers, parameter passing on the stack
is widely used, especially with compiled code.
The primary instructions for dealing with the stack are the PUSH and POP
instructions. These instructions only have word operands and manipulate
the stack pointer.
The PUSH instruction first decrements the SP by two before pushing the data,
i.e., SP <---- SP - 2 and push word to SS:SP.
The POP instruction first pops the word to the desired location and then
increments SP by 2, i.e. push the word and then SP <----- SP + 2.
The next example shows how to use the stack in a procedure that computes
c = A - B.
I-43
EEE/CSE 226
Stack Example
AMB PROC FAR
A DW xxxx PUSH BP ;Index into stack
B DW xxxx MOV BP,SP ;using BP
C DW ? MOV SI,[BP+10] ; offset of A
... MOV AX,[SI]
MOV AX,offset A MOV SI,[BP+8] ;offset of B
MOV BX,offset B SUB AX,[SI]
MOV CX,offset C MOV SI,[BP+6] ;offset of C
PUSH AX MOV [SI],AX
PUSH BX POP BP
PUSH CX RET 6 ;discard parameters, i.e
CALL AMB AMB ENDP ; add 6 to SP after return.
... low memory
BP <---SP
IP <---SP+2
CS <---SP+4
offset C <---SP+6
offset B <---SP+8
offset A <---SP+10
Stack after first instruction
of AMB has been executed.
I-44
EEE/CSE 226
Bus Structure
Address Bus
Control Bus
I-45
EEE/CSE 226
Pinout
s
I-46
EEE/CSE 226
8086 Pins
The 8086 comes in a 40 pin package which means that some pins have
more than one use or are multiplexed. The packaging technology of time
limited the number of pin that could be used.
In particular, the address lines 0 - 15 are multiplexed with data lines 0-15,
address lines 16-19 are multiplexed with status lines. These pins are
The 8088 does not have the upper 8 data lines so the pins are A8 - A15.
The 8086 has one other pin that is multiplexed and this is BHE’/S7.
BHE stands for Byte High Enable. This is an active low signal that is
asserted when there is data on the upper half of the data bus. There is no
need for this signal on an 8088.
The 8086 has two modes of operation that changes the function of some pins.
The SDK-86 uses the 8086 in the minimum mode with the MN/MX’ pin tied to
5 volts. This is a simple single processor mode. The IBM PC uses an 8088
in the maximum mode with the MN/MX” pin tied to ground. This is the mode
required for a coprocessor like the 8087.
I-47
EEE/CSE 226
8086 Pins
HOLD When this pin is high, another master is requesting control of the
local bus, e.g., a DMA controller.
I-48
EEE/CSE 226
8086 Pins
The following are pins are available in both minimum and maximum modes.
GND Ground
READY Acknowledgement from wait-state logic that the data transfer will
be completed.
RESET Stops processor and restarts execution from FFFF:0. Must be high
for 4 clocks. CS = 0FFFFH, IP = DS = SS = ES = Flags = 0000H, no
other registers are affected.
TEST’ The WAIT instruction waits for this pin to go low. Used with 8087.
CLK Clock: 33% duty cycle, i.e., high 1/3 the time.
I-49
EEE/CSE 226
Timin
g