Chapter Three
Addressing Modes
Addressing Modes in 8086 Programming
When the microprocessor executes an instruction, it
performs the specified function on data.
These data called operands, may be part of the instruction,
may reside in one of the internal registers, may be stored at
an address in memory, or may be held at an I/O port.
To access these different types of operands, the 8086
provided with various addressing
….
The addressing modes are categorized in to the following types:
1. Immediate Operand Addressing Mode.
2. Register Operand Addressing Mode.
3. Memory Operand Addressing Mode:
a. Direct Addressing Mode.
b. Register Indirect Addressing Mode.
c. Index Addressing Mode.
d. Based Addressing Mode.
e. Based & Index Addressing Mode.
f. Based & Index with Displacement Addressing Mode
Immediate Operand Addressing Mode
If an operand is part of the instruction instead of the content of a
register or a memory location, it represents what is called an
immediate operand and is accessed using the immediate addressing
mode
Example:
Write an instruction that will move the immediate value 1234h into
the CX register.
Solution:
The instruction must use immediate operand addressing for the
source operand and register operand addressing for the destination
operand:
MOV CX, 1234H
Immediate addressing mode
In this type of mode, immediate data is part of
instruction and appears in the form of successive byte
or bytes
10 ABH
MOV AX,10ABH AX
Register Operand Addressing Mode
In this mode, the operand to be accessed is specified as
residing in an internal register of the 8086.
Both source and destination operands are internal registers.
Example:
Write an instruction that will move the contents of DX register to
the CX register.
Solution:
The instruction must use register operand addressing for the source
operand and register operand addressing for the destination operand:
MOV CX, DX
Register addressing mode
In this type of addressing mode, the data is stored in the
register and it can be a 8-bit or 16-bit register. All the
registers, except IP, may be used in this mode.
10 AB BX
MOV AL,BLH BH BL
MOV AX,BXH FF 33 AX
AH AL
Direct Addressing Mode
This mode is similar to immediate addressing in that
information is encoded directly into the instruction. However,
in this case the instruction opcode is followed by an effective
address, instead of the data.
Example:
Write an instruction that will move the contents of memory location
(1230) to the AX register.
Solution:
The instruction must use memory location for the source operand and
register operand addressing for the destination operand:
MOV AX, [1230h]
Direct addressing mode
In this type of addressing mode a 16-bit memory address
is directly specified in the instruction as a part of it.
Memory
22 5000
33 5001
MOV AX,[5000H] 5002
AX
Register Indirect Addressing Mode
This mode is similar to the direct addressing in that an
effective address is combined with the contents of DS to
obtain a physical address.
However, it differs in the way of offset is specified such that
EA (Effective Address) resides in either a base register or in
an index register.
The base register can be either base register (BX) or base
pointer register (BP), and the index register can be source
index register (SI) or destination index register (DI).
Register Indirect addressing mode
The address of the memory location which contains
data or operand is determined in a indirect way, using
the offset register.
Memory
22 5000
AX
33 5001
MOV AX,[BX] 5002
50 00 BX
Index Addressing Mode
This mode uses the value of the displacement as a pointer to
the starting point of an array of data in memory and the
contents of the specified register as an index that selects the
specific element in the array to be accessed
Based Addressing Mode
In this mode, the effective address of operand is obtained
by adding a direct or indirect displacement to the contents of
either base register (BX) or base pointer register (BP)
Indexed addressing mode
In this addressing mode, offset of the operand is
stored in one of the index registers. DS is the default
segment for index register SI and DI.
Memory
22 5000
AX
33 5001
MOV AX,[SI] 5002
50 00 SI
Based & Index Addressing Mode
Combining the based addressing mode and the indexed
addressing mode results in new, more powerful mode known
as based-indexed addressing mode.
This mode can be used to access complex data structures
such as two dimensional array
Base plus index addressing mode
In this mode the effective address is formed by adding
content of a base register (any one of BX or BP) to the
content of an index register (SI or DI). Default segment
register DS.
12 3000
AX
MOV AX, [BX] [SI] 34 3001
3002
10 00 + 20 00 = 3000H
Final
BX SI Index
Address
Based & Index with Displacement Addressing Mode
This mode is similar to Based & Index Addressing Mode
with adding a displacement value.
Base relative plus index addressing mode
In the effective address is formed by adding an 8 or
16-bit displacement with sum of contents of any one
of the base registers (BX or BP) and any one of the
index registers, in a default segment.
12 3050
MOV AX,50H[BX] AX
34 3051
[SI]
3052
50H + 10 00 20 00 = 3050H
Final
BX SI Index
Address
Summary Addressing Modes
•Implied - the data value/data address is implicitly associated with the instruction.
•Register - references the data in a register or in a register pair.
•Immediate - the data is provided in the instruction.
•Direct - the instruction operand specifies the memory address where data is located.
•Register indirect - instruction specifies a register containing an address, where data
is located. This addressing mode works with SI, DI, BX and BP registers.
•Based:- 8-bit or 16-bit instruction operand is added to the contents of a base register
(BX or BP), the resulting value is a pointer to location where data resides.
•Indexed:- 8-bit or 16-bit instruction operand is added to the contents of an index
register (SI or DI), the resulting value is a pointer to location where data resides
•Based Indexed:- the contents of a base register (BX or BP) is added to the contents
of an index register (SI or DI), the resulting value is a pointer to location where
data resides.
•Based Indexed with displacement:- 8-bit or 16-bit instruction operand is added to
the contents of a base register (BX or BP) and index register (SI or DI), the
resulting value is a pointer to location where data resides.
Reflection Spot
MOV [7000H],CX
Q) Which addressing does instruction above
belong, and why?
Reflection Spot
MOV [7000H],CX
Q) Which addressing does instruction above
belonging and why?
Memory
Ans) Direct addressing mode 22 7000
33 7001
7002
CX 43 56
Exercises:
Distinguish each of the following instructions as correct or incorrect. In the
case of the correct instructions, state the type of the used addressing
mode.In the case of the incorrect instruction, justify the reason
Chapter Four
Data movement, ALU and
program control instructions
Data Movement Instruction.
Data movement instructions indicate the amount of data to be
moved
When we say that the contents of memory location 2000 have
been moved to some register, we always mean that an identical
copy has been created there and that the original is still
undisturbed in location 2000.
Data movement instructions would better be called ‘‘data
duplication’’ instructions, but the term ‘‘data movement’’ is
already established.
The data movement instructions copy values from one location
to another. These instructions include mov, xchg, lds, lea, les,
lfs, lgs, lss, push, pusha, pushad, pushf, pushfd, pop, popa,
popad, popf, popfd, lahf, and sahf.
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
The following types of operands are supported:
MOV REG, memory
MOV memory, REG
MOV REG, REG
MOV memory, immediate
MOV REG, immediate
Where;
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP,
SP.
memory: [BX], [BX+SI+7], variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
…
for segment registers only these types of MOV are supported:
MOV SREG, memory
MOV memory, SREG
MOV REG, SREG
MOV SREG, REG
Where;
SREG: DS, ES, SS, and only as second operand: CS.
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: [BX], [BX+SI+7], variable, etc...
NOTE
The MOV instruction cannot be used to set the value of the CS and IP
registers.
There is no memory to memory move operation.
you cannot move immediate data into a segment register. (should copy to
general register first )
You cannot copy value of one segment register to another segment register
(should copy to general register first)
…
Example: Load DS with 5000H.
MOV DS, 5000H; Not permitted (invalid)
Thus to transfer an immediate data into the segment register, the correct
procedure is given below.
MOV AX, 5000H
MOV DS, AX
It may be noted, here, that both the source and destination operands cannot be
memory locations (Except for string instructions).
Other MOV instruction examples are given below with the
corresponding addressing modes.
MOV AX, 5000H; Immediate
MOV AX, BX; Register
MOV AX, [SI]; Indirect
MOV AX, [200 OH]; Direct
MOV AX, 50H [BX]; Based relative, 50H Displacement
XCHG: Exchange
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, exchange of data contents of two memory locations is
not permitted.
Algorithm: operand1 < - > operand2
Example
XCHG [5000H], AX ; This instruction exchanges data between AX and a ;
memory location [5000H] in the data segment.
XCHG BX ; This instruction exchanges data between AX and BX
Arithmetic and Logic Instructions
These instructions usually perform the arithmetic operations, like addition,
subtraction, multiplication and division instructions.
The increment and decrement operations also belong to this type of instructions.
The operands are either the registers or memory locations or immediate data
depending upon the addressing mode.
Most Arithmetic and Logic Instructions affect the processor status register
(or Flags). 16 bits in this register, each bit a flag can take a value of 1 or 0.
Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow.
Zero Flag (ZF) - set to 1 when result is zero. For none 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. Actually this flag take
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).
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. Even if result is a word only 8 low bits are analyzed!
Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low nibble (4 bits).
Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to interrupts from external devices.
Direction Flag (DF) - this flag is used by some instructions to process data chains, when this flag is set
to 0 - the processing is done forward, when this flag is set to 1 the processing is done backward.
ADD:
This instruction adds an immediate data or contents of a memory location
specified in the instruction or a register ( source ) to the contents of another
register (destination) or memory location. The result is in the destination
operand.
However, both the source and destination operands cannot be memory
operands. Also the contents of the segment registers cannot be added using
this instruction.
The examples of this instruction are given along with the corresponding
modes.
ADD AX, 0100H Immediate
ADD AX, BX Register
ADD AX, [SI] Register indirect
Example
ADD AL,BL AL = AL + BL
ADD CX,DI CX = CX + D
ADD CL,44H CL = CL + 44H
ADD CL,[BP] The byte contents of the stack segment memory location addressed by BP
add to CL with the sum stored in CL
ADD BX,[SI+2] The word contents of the data segment memory location addressed by SI +
ADC: Add with Carry
This instruction performs the same operation as ADD instruction,
but adds the carry flag bit (which may be set as a result of the
previous calculations) to the result.
Example
ADC AL,AH AL = AL + AH + carry
ADC CX,BX CX = CX + BX + carry
Example: Program
STC ; set CF = 1
MOV AL, 5 ; AL = 5
ADC AL, 1 ; AL = 7
RET
INC and DEC
INC: Increment
This instruction increments the contents of the specified register or memory location by 1.
All the condition code flags are affected except the carry flag CF.
This instruction adds 1 to the contents of the operand.
The examples of this instruction are as follows: Program
INC AX Register Example:
INC [BX] Register indirect MOV AL, 4
INC [5000H] Direct INC AL ; AL = 5
Example:
RET
DEC: Decrement
The decrement instruction subtracts 1 from the contents of the specified register or memory
location.
All the condition code flags except carry flag are affected depending upon the result.
The examples of this instruction are as follows:
DEC AX Register
DEC [500OH] Direct Program Example:
MOV AL, 255 ; AL = 0FFh (255 or -1)
DEC AL ; AL = 0FEh (254 or -2)
RET
SUB: Subtract
The subtract instruction subtracts the source operand from the
destination operand and the result is left in the destination
operand.
Source operand may be a register, memory location or immediate
data and the destination operand may be a register or a memory
location, but source and destination operands both must not be
memory operands.
All the condition code flags are affected by this instruction.
examples
SUB AX, BX Register
SUB AX, [5000H] Direct
SUB [5000H], 0100 Immediate
SUB CL,BL CL = CL – BL
SUB AX,SP AX = AX – SP
SBB: Subtract with Borrow
The subtract with borrow instruction subtracts the source operand
and the borrow flag (CF) which may reflect the result of the
previous calculations, from the destination operand.
Subtraction with borrow, here means subtracting 1 from the
subtraction obtained by SUB, if carry (borrow) flag is set.
The result is stored in the destination operand. All the flags are
affected (Condition code) by this instruction.
Example
SBB O 10 0H Immediate [destination AX]
SBB AX.BX Register
SBB AX, [5000H] Direct
Example Program:
STC
MOV AL, 5
MUL , IMUL, DIV and IDIV
These types of operands are supported:
REG
Memory
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: [BX], [BX+SI+7], variable, etc...
MUL and IMUL instructions affect these flags only CF, OF
When result is over operand size these flags are set to 1, when result fits in operand
size these flags are set to 0.
For DIV and IDIV flags are undefined.
MUL - Unsigned multiply:
when operand is a byte: AX = AL * operand.
when operand is a word: (DX AX) = AX * operand.
IMUL - Signed multiply:
when operand is a byte: AX = AL * operand.
when operand is a word: (DX AX) = AX * operand.
…
DIV - Unsigned divide:
when operand is a byte:
AL = AX / operand
AH = remainder (modulus). .
when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus). .
IDIV - Signed divide:
when operand is a byte:
AL = AX / operand
AH = remainder (modulus).
.when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus). .
Logical Instructions
These type of instructions are used for carrying out the bit by
bit shift, rotate, or basic logical operations.
All the condition code flags are affected depending upon the
result.
Basic logical operations available with 8086 instruction set are
AND, OR, NOT, and XOR
AND
Logical AND between all bits of two operands.
Result is stored in operand1.
These rules apply:
1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0
Example:
MOV AL, 'a' ; AL = 01100001b
AND AL, 11011111b ; AL = 01000001b ('A')
RET
OR
Logical OR between all bits of two operands. Result is stored
in first operand.
These rules apply:
1 OR 1 = 1
1 OR 0 = 1
0 OR 1 = 1
0 OR 0 = 0
Example:
MOV AL, 'A' ; AL = 01000001b
OR AL, 00100000b ; AL = 01100001b ('a')
RET
…
NOT
invert each bit of the operand.
Algorithm:
if bit is 1 turn it to 0.
if bit is 0 turn it to
Example:
MOV AL, 00011011b
NOT AL ; AL = 11100100b
RET
Logical XOR (Exclusive OR) between all bits of two operands.
Result is stored in first operand.
These rules apply:
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0
Example:
MOV AL, 00000111b
XOR AL, 00000010b ; AL = 00000101b
RET
program flow control
controlling the program flow is a very important thing, this is
where your program can make decisions according to certain
conditions.
unconditional jumps
The basic instruction that transfers control to another point in the program is JMP.
The basic syntax of JMP instruction: JMP label
To declare a label in your program, just type its name and add ":" to the end,
label can be any character combination but it cannot start with a number,
for example here are 3 legal label definitions:
label1:
label2:
a:
Label can be declared on a separate line or before any other instruction,
for example:
x1:
MOV AX, 1
x2: MOV AX, 2
…
Of course there is an easier way to calculate the some of two numbers, but it's
still a good example of JMP instruction.
As you can see from this example JMP is able to transfer control both forward
and backward.
example of JMP instruction: