Chapter 3-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

CHAPTER THREE (Lecture one)

8086 ADDRESSING MODE


8086 Addressing Modes
• Addressing mode refers to the specification of the location of
data required by an operation
• The addressing mode depends upon the operands and
suggests how the effective address may be computed for
locating the operand, if it lies in memory.
• Classification of Addressing Modes:
• Data-Addressing Modes
• Program Memory addressing Modes
• Stack memory addressing Modes
Cont..
• In this chapter, MOV (move data) instruction is used to describe
the addressing modes.
• Since the MOV instruction is common, it provides a basis for the
explanation of the addressing modes.
• The MOV instruction transfers bytes/words of data between
registers or between registers and memory in the 8086.
Cont..
 MOV instruction always copies the source data and into the
destination.
 Also, note that the flag registers remain un-affected by most data
transfer instructions.
 Notice that a comma always separates the destination from the
source in an instruction.
Data addressing modes
 This mode is related to data transfer operation, that is, data is
transferred either from the memory to internal registers of 8086
processors or from one register to another register.
 Data addressing modes include:
 Immediate
 Direct
 Register
 Register Indirect
 Indexed
 Register Relative
 Based Indexed
 Relative Based Indexed
Immediate Addressing

 Immediate data is a part of instruction.


 The operand can be either 8-bit or 16-bit in length.
 Transfer the source-immediate byte or word of data into the
destination register or memory location.
e.g. MOV AX, 0005H (AX←0005H)
Here, 0005H is the immediate data.
 Other examples are:
 MOV AL, 83 H; the hex number 83 is copied to AL register
 ADD AX, 1284 H ; the hex number 1284 is copied to AX
register.
 MOV BL, 26H
 MOV CX, 4567H
Direct Addressing
 In the direct addressing mode, a 16-bit memory address (offset)
is directly specified in the instruction
 16-bit effective address (EA) is taken directly from the
displacement field of the instruction.
e.g1. MOV AX, [5000H ] (AX←[DS:5000H])
 Here, data resides in a memory location in the data segment,
whose effective address may be computed using 5000H as the
offset address and content of DS as segment address.
e.g2. MOV CL, [4321H]
 This instruction moves data from location 4321H in the
data segment into CL.
 The physical address(PA) is calculated as
DS * 10H + 4321
Assume DS = 5000H
∴PA = 50000 + 4321 = 54321H
∴CL ← [54321H]
Register Addressing
 Operands are located in registers
 In this mode, either an 8-bit or a 16-bit general purpose register
contains the operand field holds data.
 All the registers, except IP, may be used in this mode
e.g. MOV AX, BX (AX←BX)
 Here, data is transferred from register BX to register AX
 Other examples are:
 MOV AX, BX ; copy the contents of BX register to AX register.
 MOV CX, DX; copy contents of DX to CX
 ADD AL, DH; add the contents of AL to DH and place results in
AL
 SUB DX, CX ; subtract contents of CX from DX, result in DX
Register Indirect Addressing
 Transfers a byte or word between a register and memory
location addressed by an index or base register.
 The registers used for this purpose are SI, DI, and BX
 They must be combined with DS (sometimes with ES)in order to
generate the 20-bit physical address.
e.g. MOV AX, [BX ] (AX←[DS:BX]
 Physical address can be calculated as DS * 10H + BX.
 Other Examples:
MOV CL, [SI] ; move contents of DS:SI into CL
MOV [DI], AH ; move contents of AH into DS:DI
Indexed Addressing
 Offset of the operand is stored in one of the index registers (SI
and DI).
 DS and ES are the default segments for SI and DI respectively
 This mode is a special case of register indirect addressing
mode
e.g. MOV AX, [SI ] (AX←[DS:SI])
 Here, data is present in a memory location in DS whose offset
address is in SI.
Register Relative Addressing
 In this mode, the operand address is calculated using one of the base
registers and an 8 bit or a 16 bit displacement.
 The data in a segment of memory are addressed by adding the
displacement to the contents of a base or an index register (BP, BX,
DI,or SI).
 Effective address formed by adding an 8-bit or 16-bit displacement
with the content of any one of the registers BX, BP, SI and DI in the
default segment (DS or ES)
e.g1. MOV AX, 50H[BX ] (AX←[DS:BX+50H])
e.g2. MOV CL, [BX + 04H]
 This instruction moves a byte from the address pointed by BX + 4 in
data segment to CL.
CL ← DS: [BX + 04H]
 Physical address can be calculated as DS * 10H +BX + 4H
Based Indexed Addressing
 Transfers a byte or word between a register and the memory
location addressed by a base register (BP or BX) plus index
register ( DI or SI).
 Here, operand address is calculated as base register plus an index
register.
 The effective address of data is formed by adding content of a
base register (BX or BP) to the content of an index register (SI
or DI)
 Combining based and indexed addressing modes but One base
register and one index register are used.
e.g1. MOV CL, [BX + SI] (CL ← DS: [BX + SI])
 This instruction moves a byte from the address pointed by BX +
SI in data segment to CL.
 Physical address can be calculated as DS * 10H +BX + SI.
e.g2 MOV AX, [BX ][SI] (AX←[DS:BX+SI])
CONT…
e.g3 MOV ALPHA [SI] [BX], CL
 If BX = 0200H, value of ALPHA = 08H SI = 1000H and DS = 3000H
then 8-bit contents of CL is moved to 20 bit physical address (DS+EA)
 The 20 bit physical address (DS+EA)=31208H, is calculated as below ,

EA 0200H + BX
1000H + SI
08H displacement
---------------
1208H

Now, DS + EA 30000H +
1208H
---------------
31208H
Relative Based Indexed Addressing
 In this mode, the address of the operand is calculated as the sum
of base register, index register and 8 bit or 16 bit displacement.
 The effective address is formed by adding an 8 or 16-bit
displacement with the sum of contents of any one of the base
registers (BX or BP) and any one of the index registers (SI or
DI).
 e.g. 1. MOV AX, 50H[BX ][SI] (AX←[DS:BX+SI+50])
 e.g2. MOV CL, [BX + DI + 20] (CL ← DS: [BX + DI + 20H])
 This instruction moves a byte from the address pointed by BX +
DI + 20H in data segment to CL.
 Physical address can be calculated as DS * 10H +BX + DI +
20H
Program-Memory Addressing Modes
 This mode involves program memory addresses during various
operations.
 Program memory-addressing modes, used with the JMP
instructions
 Example: JMP AX,
 In this instruction, the code execution control jumps to the
current code segment location addressed by the contents of AX
register.
Stack Addressing Modes
 This mode involves stack registry operations.
 It holds data temporarily and stores return addresses
for procedures.
 The stack memory is a LIFO (last-in, first-out) memory,
which describes the way that data are stored and
removed from the stack.
 Data are placed onto the stack with a PUSH instruction
and removed with a POP instruction.
 Example: PUSH AX, this instruction copies the contents of AX
register to the stack.
CHAPTER THREE (Lecture Two)

8086 MICROPROCESSOR ‘S INSTRUCTION SETS


Introduction
 There are three language levels that can be used to write a program for a
microcomputer
 Machine language
 Assembly Language
 High-Level Language
CONT…
 Machine Language
 These are the binary codes for the instructions you want the
microcomputer to execute

11101001000000000101000

 It is hard or impossible for a programmer to write code in


machine language, because it requires memorizing all the
instructions in binary form and soon the program will get out
of control!
CONT…
 Assembly Language
 Uses two, three, or four letter mnemonics to represent each
instruction type
 The letters in an assembly language mnemonic are usually
initials or a short form of the English word(s) for the operation
performed by the instruction
 e.g., SUB for subtract , XOR for Exclusive OR , etc
 Assembly language program has to be translated to
machine language so that it can be loaded into memory
and run – This is done by the assembler
CONT…
 High-Level Languages
 These languages use program statements which are even more
English-like than those of assembly language
 e.g. BASIC, C, C++, Java, ...
 Each high-level statement may represent many machine code
instructions
 An interpreter (compiler) program is used to translate higher
level language statements to machine codes, which can be
loaded into memory and executed.
Instructions Set
 Listing all the instructions that the microprocessor can execute.
 8086 instructions do not permit memory-memory operations.
 Some instruction set contains instructions with
 No Operand,
 Single Operand
 Two Operands
CONT…
 Each statement in a program consists of 4 parts or fields
 Label
 The leftmost field .
 Used to store a symbolic name for the memory location that
it represents.
 Must begin with a letter or one of the special character like
@,$,-.?
 May of any length from 1 to 35 character
CONT…
 Opcode field.
 Operand/s field.
 Comment field
 Contains a comment about an instruction or a group of
instructions.
 A comment always begins with a semicolon(;)
 Examples:
label opcode operand/s comment
DATA DB 23H ; define DATA as a byte of 23H
START: MOV AL,BL ; copy BL into AL
MOV CX,200 ; copy 200 decimal into CX.
8086 Instruction Types
 The 8086 instructions can be grouped in to six categories
 Data transfer instructions
 Arithmetic instructions
 Bit manipulation instructions
 String manipulation instructions
 Control transfer instructions
 Processor control instructions
Data Transfer Instructions
 This type of instructions are used to transfer data from
source operand to destination operand.
 Memory to register e.g. MOV AX, [0005h]
 Register to memory e.g. PUSH AL
 Immediate to memory/register e.g. MOV AH, 09h
 I/O device to register e.g. IN AX, 4
 Register to I/O device e.g. OUT AL, 2
 All the MOV , PUSH, POP , XCHG, IN, OUT, LEA,
instructions belong to this category
The MOV instruction

 This data transfer instruction transfers data from one


register/memory location to another register/memory location.
 MOV destination, source
 Destination: can be a register or a memory location.
 Source: can be a register, a memory location, or an immediate
number.
 The source and destination must both be of type byte, or they
must both be of type word.
 The source may be any one of the segment registers, general
purpose registers, or a memory location and….
 The destination may be another registers (except code segment
register), or memory locations.
CONT…
 In the case of immediate addressing mode, a segment register can’t be
a destination register.
 In other words, direct loading of the segment registers with immediate
data is not permitted.
 To load the segment registers with immediate data, one will have to
load any general purpose register with the data and then it will be have
to be moved to that particular segment register as follows.
 MOV DS, 5000H ; is not permitted (invalid)
 The correct procedure is
 MOV AX, 5000H
 MOV DS, AX
CONT
 Some invalid MOV statements
 MOV DL, CX ; Different operand sizes
 MOV DS, 1075H ; Immediate value cannot be moved to a
segment register
 MOV CS, DX ; Destination register cannot be CS
 MOV ES, DS ; Both registers can’t be segment registers
 MOV 75H, AX; Immediate value cannot be a destination
operand
 MOV BEGIN, START ; Both operands cannot be located in
memory
The XCHG instruction
 This instruction exchanges the contents of the specified source
and destination operands, which may be registers or one of them
may be a memory location.
 However, data exchange between two memory locations is not
permitted.
 Examples:
 XCHG [5000H], AX ; will exchange data between AX and a
memory location [5000H] specified by the DS.
 XCHG AX, BX ; will exchange data between registers AX &
BX
CONT…
 The XCHG instruction is convenient because we don’t need a
third register to hold a temporary value in order to swap two
values.
 To perform XCHG AX, DX, we need three MOV instructions
MOV CX, AX
MOV AX, DX
MOV DX, CX
 This instruction is especially useful in sorting applications.
PUSH And POP Instructions
 PUSH instruction decrements the stack pointer by twice and
copies a word from a specified source to the location in the stack where
the stack is pointing at.
 Syntax: PUSH Source
 Source may be GPR,SR and MOL
 E.g., PUSH AX
 Say AX =2255H, and
 SP= FFFFH, SS=5000H
CONT…
 POP instruction increments the stack pointer by twice
and copies a word from a specified source to the location in the stack
where the stack pointer is pointing at.
 Syntax: POP Source
 Source may be GPR,SR and MOL
 E.g., POP BX
 Say BX = 2255, and
 SP= FFFDH,
 SS=5000H
IN And OUT Instructions
 IN accumulator, port
 OUT port, accumulator
 Port: 8 or 16-bit I/O port address. Can be fixed or variable
 For the fixed-port type, the 8-bit address of a port is specified
directly in the instruction.
 Example1:
 IN AX,34H ; Input a word from port 34H to AX
 OUT 3BH,AL ;Copy the contents of AL to port 3BH
 For the variable port, the port address is loaded into the DX
register before the IN/OUT instruction.
 Example1:
 MOV DX, 0FF78H ; Initialize DX to point to port
 IN AL, DX ;Input a byte from 8-bit port 0FF78H to AL
LEA (load effective address) Instructions
 LEA register, source
 This instruction determines the offset of the variable or memory
location named as the source and puts this offset in the indicated
16-bit register.
 Example:
 LEA BX, PRICES ;Load BX with offset of PRICES in DS
Arithmetic instructions
 These instructions usually perform the arithmetic operations, like
addition, subtraction, multiplication and division.
 The increment and decrement operations also belong to this
type of instructions.
 The operands are either the registers or the memory locations or
immediate data, depending upon the addressing mode.
 Arithmetic instructions may affect the flags.
The ADD instruction
 ADD Dest,Src
 After the ADD instruction, the result will be in the
destination operand.
 However, both the source and destination operands cannot be
memory operands.
 That means memory to memory addition is not possible.
 Also, the contents of the segment registers cannot be
added using this instruction.
CONT…
 Any ADD instruction may modify the contents of the sign, zero ,
carry, auxiliary carry, parity and overflow flags.
 For example:
 MOV DL,12H (DL will be loaded with a 12H by using an
immediate MOV instructions.)
 ADD DL, 33H(33H is added to the 12H in DL by using an
immediate ADD instruction)
 After the addition the sum(45H)moves into register DL and
the flags will change as follows.
 Z =0 (Result Not Zero).
 C =0 (No Carry)
 A =0 (No Half-carry)
 S =0 (Result Positive)
 P =0 (Odd Parity)
 O=0 (No Overflow)
Addition with Carry(ADC)
 This instruction performs the same operation as ADD instruction,
but adds the carry flag bit( C ).
 All the condition flags are affected by this instruction.
 ADC AL,AH ;AL = AL + AH + carry
 ADC CX,BX ;CX = CX + BX + carry
Increment Addition (INC)
• INC adds a 1 to the contents of the specified register or a
memory location (except segment registers).
• Immediate data cannot be an operand for this instruction
• All the condition flags may or may not be affected
• INC BL  BL=BL+1
• INC SP  SP=SP+1
• INC 55H (is an illegal instruction)
• Example:
• MOV AL, 4
• INC AL ; AL = 5
Decrement Subtraction(DEC)
• Subtracts a 1 from the contents of the specified register or a
memory location.
• Immediate data cannot be operand of the instruction.
• DEC BH  BH=BH-1
• DEC CX CX =CX-1
• Example:
• MOV AL, 255 ; AL = 0FFh (255 or -1)
• DEC AL ; AL = 0FEh (254 or -2)
Subtraction and Subtraction With Borrow(SBB)
• The subtract instruction subtracts the source operand from the
destination operand and the result is stored in the destination operand.
• The types of subtraction not allowed are Memory-to-Memory &
between Segment Registers
• Destination operand cannot be an immediate data.
• All the condition code flags are affected by this instruction.
• SUB destination, source
• SUB CX, BX ;CX - BX. Result in CX
• SUB AX,3427H ;Subtract immediate number 3427H from AX
• SBB instruction functions as a regular subtraction except that the borrow
from the carry flag (which holds the borrow) also subtracts from the
difference.
• SBB destination, source
• The result is stored in the destination operand.
• SBB AH,AL ;AH = AH – AL – carry
• SBB AX,BX ;AX = AX – BX – carry
NEG and CMP Instructions
 NEG AL ;Replace number in AL with its 2’s complement
 Examples: Suppose (AX) =0015 and AL=35H=0011 0101
 NEG AX; (AX ) = -(AX)
 NEG AL ;replace number in with 2’s complement
; AL=1100 1011 =CBH
 The CMP source, dest is used to compare two numbers
 CMP BH, CL compare a byte in BH with a byte in CL
 Example
 MOV AL, 5
 MOV BL, 5
 CMP AL, BL
 It is not possible to compare segment registers
MUL INSTRUCTIONS
 MUL source
 Source: can be memory or registers.
 one of the operands is in AL if source contains a byte, else in
AX if source holds a word.
 when operand is a byte: AX = AL * operand.
 when operand is a word: DX = AX * operand.
 Example1:
 MUL BH ;AL times BH, result in AX
 MUL CX ;AX times CX, result in DX.
 Example2:
 MOV AL, 200 ; AL = 0C8h
 MOV BL, 4
 MUL BL ; AX = 0320h (800)
 Suppose, (BL) =11H =17d, (AL) =B4H, then
MUL BL ; (AX) = (AL)*(BL) =180d*17d=3060d=OBF4H
DIV INSTRUCTIONS
 DIV source
 Source: contains the divisor. It can be in memory or registers.
 when operand is a byte: AL = AX / operand, AH = remainder
 when operand is a word: AX = (DX) / operand ,DX = remainder.
 Example1:
 DIV BL ;Divide word in AX by byte in BL. Quotient in AL,
remainder in AH.
 MOV AX, 203 ; AX = 00CBh
 MOV BL, 4
 DIV BL ; AL = 50 (32h), AH = 3
 Example2: Suppose, (AX) = 0400H, (BL) = B4H, then
DIV BL ; (AH) =7CH =124d = remainder, (AL)=05H=5d=Quotient
Bit-oriented Instructions
 Logical (AND, OR, XOR, NOT)
 AND/OR/XOR dest, source
 NOT dest
 Do the indicated logic operation on the given source and
destination operands and keep the result in destination
 Example1
 MOV AL, 'A' ; AL = 01000001b
 OR AL, 00100000b ; AL = 01100001b ('a')
 Example2:
 MOV AL, 00011011b
 NOT AL ; AL = 11100100b
 Example3:
 MOV AL, 00000111b
 XOR AL, 00000010b ; AL = 00000101b
CONT…
 SHIFT INSTRUCTIOS
 SHR
 SHR dest, count
 Shift operand1 Right. The number of shifts is set by operand2.
 Shift all bits right or Shifting right n bits divides the operand by 2n
 Zero bit is inserted to the left-most position.
 Example1: Example2:
 MOV AL, 00000111b Mov dl,80 ;
 SHR AL, 1 ; AL = 00000011b, SHR dl,1 ;dl=40
 SHL SHR DL,2 ;dl=10
 SHL dest, count
 Shift operand1 Left. The number of shifts is set by operand2.
 Shift all bits left,
 Zero bit is inserted to the right-most position or Shifting left n bits
multiplies the operand by 2n Example2:
 Example1: Mov dl,5
 MOV AL, 11100000b SHL dl,4 ;dl=20
 SHL AL, 1 ; AL = 11000000b,
CONT…
 ROR
 ROR dest,count
 Rotate operand1 right. The number of rotates is set by operand2.
 shift all bits right, the bit that goes off is set to CF .
 Example1:
Example2:
 MOV AL, 1Ch ; AL = 00011100b
• MOV DL,3Fh
 ROR AL, 1 ; AL = 00001110b, CF=0. • ROR DL,4 ; DL = F3h
 ROL
 ROL dest, count
 Rotate operand1 left. The number of rotates is set by operand2.
 shift all bits left.
 Example1: Example2:
 MOV AL, 1Ch ; AL = 00011100b • MOV Dl,3Fh
 ROL AL, 1 ; AL = 00111000b • ROL Dl,4 ; DL = F3h

String Instructions
 A string is a series of bytes or a series of words in sequential
memory locations.
 A string often consists of ASCII character codes.
 In the list, a “/" is used to separate different mnemonics for the
same instruction.
 Use the mnemonic which most clearly describes the function of
the instruction in a specific application
CONT…
 CMPS Destination, Source
 It compares the string bytes or words.
 REP (Repeat): This is an instruction prefix.
 It causes the repetition of the instruction until CX becomes zero.
 MOVS: copies string in data segment to a string in extra segment
 LODS: copies a string in memory pointed by SI in to accumulator
 STOS: Copies a string in accumulator to memory at [DI]
Program Execution Instructions
 These instructions cause change in the sequence of the execution of
instruction.
 CALL : This instruction is used to call a subroutine/procedure from a
main program.
 The address of the procedure may specified directly or indirectly
depending upon the addressing mode.
 RET: It returns the control from procedure to calling program.
 Every CALL instruction should have a RET.
 LOOP: This instruction executes the part of the program from the label
or address specified in the instruction up to loop instruction, CX number
of times
 JMP : This instruction unconditionally transfers the control of execution
to the specified address using an 8-bit or 16-bit displacement
 This instruction is used for unconditional jump from one place to
another
CONT….
 Interrupt instruction
 INT type
 Type: a number between 0 and 255 which identifies the interrupt.
 INT 21 is used for basic I/O functions, like display, print…
 Examples of INT 21h are
 INT 21h / AH=1 read character from standard input, result is
stored in AL. If there is no character in the keyboard
buffer, the function waits until any key is pressed.
 INT 21h / AH=2 write character to standard output. DL =
character to write
 INT 21h / AH=9 printout a string from DS:DX. String must be
terminated by '$'
Processor control Instructions
 These instructions control the processor itself.
 8086 allows to control certain control flags that causes the
processing in a certain direction
 STC: It sets the carry flag to 1.
 CLC: It clears the carry flag to 0.
 CMC: It complements the carry flag.
 STD: It sets the direction flag to 1.
 If it is set, string bytes are accessed from higher memory
address to lower memory address.
 CLD: It clears the direction flag to 0.
 If it is reset, the string bytes are accessed from lower
memory address to higher memory address.
 HLT Stops the processor
 WAIT puts the processor in idle state (no processing)
Assignment ONE
 Explain BCD(Packed and unpacked) and ASCII Arithmetic
Instructions and write at least three examples for each based on
the following instructions.
 DAA instruction
 DAS instruction
 AAA instruction
 AAS instruction
 AAM instruction
 AAD instruction

You might also like