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

ECEg-5172: Microprocessors and Interfacing

(Lecture Notes)
Compiled by: Bushra KM

Chapter 3: Assembly Language Programming


Chapter 3: Part 2

3.5. Instruction Set of 8086


3.5.0. Introduction/Overviews
 A Computer Program is a sequence of commands used to tell a microcomputer what to do.
 Each command in a program is an instruction.
 An instruction is a binary pattern designed inside the microprocessor to perform a specific
function. In other words, it is actually a command to the microprocessor to perform a given
task on a specific data.
 An instruction set is the complete set of all the instructions in machine code that can be
recognized and executed by a central processing unit.
 The 8086/88 Instruction Set:
 The 8086 and 8088 execute exactly the same instructions.
 The 8086/88 instruction set includes equivalents of the 8085 instructions plus many new ones.
 The 8086/88 instructions treat different types of operands uniformly.
 The 8086/8088 instruction set can be viewed as existing at two levels:
 the assembly level and
 the machine level
 To the assembly language programmer, the 8086 and 8088 appear to have a repertoire of about
100/117 instructions, and there are about 300 in all-forms of machine-level instructions.
 The assembly-level instructions simplify the programmer's view of the instruction set.
 This section presents the 8086/8088 instruction set from the assembly level instructions
perspectives.
 The instruction set of 8086 can be categorized into the following six main groups:
1. Data Transfer Instructions
2. Arithmetic Instructions
3. Bit Manipulation Instructions
4. String Instructions
5. Program Transfer Instructions
a. Unconditional Transfers Instructions
b. Conditional Transfers Instructions
c. Iteration/Loop Control Instruction
d. Interrupt Instructions
6. Processor Control Instructions
a. Flag Operations
b. External Synchronization
c. No Operation

DBU, Department of Electrical and Computer Engineering, 2024 1


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

3.5.1. Data Movement Instructions


 The 8088 microprocessor has a group of data-transfer instructions that are provided to move/copy
values from one location to another.
 The value can be either a data(bytes or words) or address
 These instructions are used to transfer data from the source operand to the destination operand. We
may use these instructions to:
 Load immediate values into registers or memory locations/variables
 Transfer data between registers
 Transfer data between registers and memory
 Transfer data between accumulator(AL or AX) and I/O ports
 load segment registers with address object
 Transfer data into/from the stack
 Transfer data between the flag and registers(AH)
 Transfer data between the flag and the Stack
 The 8086 has 14 data transfer instructions which can be grouped into four/ main groups, as shown in
the table below.

General Purpose Address Object


MOV Move byte or word LEA Load effective address
PUSH Push word on to stack LDS Load pointer using OS
POP Pop word off stack LES Load pointer using ES
XCHG Exchange byte or word Flag Transfer
XLAT Translate byte LAHF Load AH register from flags
Input/Output SAHF Store AH register in flags
IN Input byte or word PUSHF Push flags onto stack
OUT Output byte or word POPF Pop flags off stack

 Let us now discuss some of data movement instructions in detail.

1. MOV Instruction
 This instruction copies a word or a byte of data from some source to a destination. Both source
and destination operands must be the same size, which can be a byte or a word.
 Syntax: MOV destination, source
 Operands: - Source operands can be immediate, register or memory location
- Destination operands can be register, memory location
 Where:
- REG = AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP
- Memory: [BX], [BX+SI+7], 16 bit variable, etc...
- Immediate: 5, -24, 3Fh, 10001101b, symbolic constants, expressions etc...
 Modified flag: no flags are modified by this instruction

 Note:

- The MOV instruction cannot be used to set the value of the CS and IP registers.
- We can’t use segment registers with an immediate value.

DBU, Department of Electrical and Computer Engineering, 2024 2


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

-The MOV instruction cannot copy value of one segment register to another segment
register (should copy to general register first).
- The source and destination operands in a MOV instruction can’t both be memory
location.
 Some Examples:

2. PUSH Instruction
 This instruction is used to load data to the stack memory. That is, it copies a word from some
source to the location in the stack where the stack pointer (SP) points.
 Whenever data is pushed on to the stack:
- The stack pointer (SP) is decremented by two. i.e., SP= SP-2
- The first (most significant) data byte moves into the stack segment memory location
addressed by sp-1
- The second (least significant) data byte moves into the stack segment memory
location addressed by sp-2
 Syntax: PUSH Source

 Where:

- Source can be general purpose registers, segment registers, memory location, or


immediate.

 REG: AX, BX, CX, DX, DI, SI, BP, SP.


 SREG: DS, ES, SS, CS.
 Memory: [BX], [BX+SI+7], 16 bit variable, etc...
 Immediate: 5, -24, 3Fh, 10001101b, etc...
 Note:
- The source operand must be a word (16 bit).
- Push immediate works only on the 80186 microprocessor and later, (not work for
8086).
- You can push segment registers
3. POP Instruction
 This instruction is used to receive data from the stack memory. That is, it copies a word from the
location in the stack where the stack pointer (SP) points to some destination.
 Whenever data is popped/removed from the stack:
 The byte from the stack segment memory location addressed by SP moves into the most
significant byte of the destination register.
 The byte from the stack segment memory location addressed by SP+1 moves into the least
significant byte of the destination register.
 The stack pointer (SP) is incremented by two. i.e., SP= SP+2
 Syntax:
POP destination
 Where:
 Destination can be general purpose registers, segment registers, or memory location/16-
bit variables.
- REG: AX, BX, CX, DX, DI, SI, BP, SP.
- SREG: DS, ES, SS, (except CS).

DBU, Department of Electrical and Computer Engineering, 2024 3


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

- Memory: [BX], [BX+SI+7], 16 bit variable, etc...


 Note:
 The destination operand must be a word (16 bit).
 8-bit registers & 8-bit variables cannot be used as destination for POP instruction
 CS & IP cannot be used as destination operand
 As well; any immediate values or Symbolic constants cannot be destination
 Examples:
4. IN Instruction
 The 8086 I/O System (Overview):
 Before reading about IN and OUT instructions, please revise I/O Port Addressing
Modes in chapter 2.
 In 8086 system, I/O devices can be placed as Standard I/O devices, Memory-Mapped
I/O, or Direct Memory Access(DMA) controllers
 The 8086 provides a large I/O space that is, separate from the memory space, and
instructions that transfer data between the CPU and devices located in the I/O space.
 There are Two different addressing modes that can be used to access ports located in
the I/O space
 IN Instruction:
 This instruction transfers a byte or a word from an input port to the AL register or the
AX register, respectively.
 Syntax:
IN destination, source
 Where:
 Destination can be either register AL (for 8-bit data) or register AX (for a 16-
bit data).
 Source operand is the port number.
- The port number may be specified as either with an immediate byte
constant for 8-bit port number, or in the DX register for 16-bit port
number.
 Note:
 Other registers than (AH and AX), any variables or memory operands are note
used for IN and OUT instructions.
 8-bit port number can be used as source operand either in direct mode or
indirect port addressing mode. But 16-bit port must be taken from DX
register.
 Examples:

5. OUT Instruction
 This instruction transfers a byte or a word from AL register or AX register,
respectively, to an out-put port number.
 Syntax:
OUT destination, source
 Where:
 Destination operand is the port number, it may be specified either as an
immediate number or in the DX register for 16-bit port number.
 Source operand can be either register AL (for 8-bit data) or register AX (for a
16-bit data).

DBU, Department of Electrical and Computer Engineering, 2024 4


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

 Examples:

6. LEA Instruction
 The LEA, LDS, and LES are Address Object Transfers instructions. They manipulate the
addresses of variables rather than the contents or values of variables. They are most useful
for determining the offset address of a variable, list processing, based variables, and string
operations.
 LEA: Load Effective Address
 This instruction determines the offset of the variable or memory location and loads this
address in the specified 16 bit register.
 Syntax:
LEA destination, source
 Where:
- Destination operand can be any 16-bit general register (except segment registers and
IP).
- The source operand must be a memory operand or a variable.
- Operation: destination =offset address of source
 Note:
 8-bit registers cannot be used as destination
 LEA does not affect any flags.

7. LDS Instruction
 Load register and DS or (load pointer using DS)
 This instruction copies a word from two memory locations into the 16 bit register specified in the
instruction. It then copies a word from the next two memory location in to the DS register.
 Syntax:
LDS destination, source
 Where:
 Destination operand can be any 16 bit register except segment registers and IP register.
 Source operand must be a memory operand/variable.
 Operation:
 destination = first word data from source, and
 DS = second word data from source
 Note:
8. LES Instruction
 Load register and ES.
 The instruction LES is similar to the instruction LDS except that it load the Extra Segment (ES)
Register instead of Data Segment (DS) Register.

3.5.2. Arithmetic Instructions


3.5.2.0. Intro/Overviews
 Arithmetic/Arithmetics:

DBU, Department of Electrical and Computer Engineering, 2024 5


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM


is the oldest and most elementary branch of mathematics

It consists in the study of numbers, especially the properties of the traditional operations
between them — addition, subtraction, multiplication and division.
 Arithmetic is an elementary part of number theory.
 In 8086/88 arithmetic operations may be performed on four types of numbers:
 Unsigned binary,
 Signed binary (integers),
 Unsigned packed decimal, and
 Unsigned unpacked decimal
 Binary numbers:
 Signed or Unsigned
 may be 8 or 16 bits long
 Unsigned binary numbers:
 may be either 8 or 16 bits long
 all bits are considered in determining a number's magnitude
 The value range of an 8-bit unsigned binary number is 0-255
 The value range of a 16-bit unsigned binary number is 0-65,535
 Arithmetic operations available for unsigned binary numbers are Addition,
subtraction, multiplication and division.
 Signed binary numbers (integers):
 may be either 8 or 16 bits long
 The high-order (leftmost) bit is interpreted as the number's sign: 0 = positive and 1 =
negative
 Negative numbers are represented in standard two's complement notation
 The value range of an 8-bit signed binary number is -128 to +127
 The value range of a 16-bit signed binary number is - 32,768 to +32,767
 The value zero has a positive sign
 Multiplication and division operations are provided for signed binary numbers.
Addition and subtraction are performed with the unsigned binary instructions.
Conditional jump instructions, as well as an "interrupt on overflow" instruction, can
be used following an unsigned operation on an integer to detect overflow into the
sign bit.

3.5.2.1. Addition Instructions


 The table below shows the form of each of the instructions in the addition(binary addition instruction)
subgroup
Mnemonic Meaning Syntax/Form Operation
ADD Add/Addition ADD destination, source destination = destination + source
ADC Add with carry ADC destination, source destination = destination + source + CF
INC Increment by 1 INC destination destination = destination + 1

 Where:
 Destination can be a register, or a memory/variable operand.
 Source can be an immediate number, a symbolic constant, a register, or a memory/variable
operand.

DBU, Department of Electrical and Computer Engineering, 2024 6


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

 CF: is the current value of Carry Flag(it is either 1 or 0)


 Flags affected: AF, CF, OF, PF, SF, and ZF. (But for INC instruction CF is not affected!)
 Note:
 Segment registers (CS, DS, ES, and SS) and IP register cannot be used.
 Immediate values or Symbolic constants cannot be used as destination operand.
 The source and destination both must be a word or byte.
 The source and the destination in an instruction cannot both be memory locations.
 For INC Instruction, when the content of the destination operand is FFH for 8-bit, or FFFFH for
16 bit operand, then after INC instruction the contents will be zero.
 Examples:

3.5.2.2. Subtraction Instructions


 The table below shows the form of each of the instructions in the subtraction(binary subtraction
instruction) subgroup
Mnemonic Meaning Syntax/Form Operation
SUB Subtract SUB destination, source destination = destination - source
SBB Subtract with SBB destination, source destination = destination - source - CF
borrow
DEC Decrement by 1 DEC destination destination = destination - 1
NEG Negate NEG destination destination= 2’s complement of
destination
CMP Compare CMP destination, source destination-source

 Where:
 Destination can be a register, or a memory/variable operand.
 Source can be an immediate number, a symbolic constant, a register, or a memory/variable
operand.
 CF: is the current value of Carry Flag(it is either 1 or 0)
 For subtraction the carry flag CF acts as borrow flag
 The NEG instruction evaluate the 2’complement of an operand
 The CMP instruction compares a byte/word from the specified source with a byte/word from
the specified destination.
 The comparison is done by subtracting the source byte or word from the destination
byte or word.
 But the result is not stored in the destination or anywhere.
 Source and destination remain unchanged, only flags are updated
 Flags affected: AF, CF, OF, PF, SF, and ZF. (But for DEC instruction CF is not affected!)
 Note:
 Segment registers (CS, DS, ES, and SS) and IP register cannot be used.
 The source and destination both must be a word or byte.
 Immediate values or Symbolic constants cannot be used as destination operand.
 The source and the destination in an instruction cannot both be memory locations.
 For DEC Instruction, when the content of the destination operand is zero, then after DEC
instruction the content will be FFH for 8-bit, or FFFFH for 16 bit operand
 Examples:

DBU, Department of Electrical and Computer Engineering, 2024 7


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

3.5.2.3. Multiplication and Division Instructions


 Syntax:
MUL, source
IMUL, source
DIV, source
IDIV, source
 Where, source can be register or memory
 Register: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
 Memory: [BX], [BX+SI+7], variable, etc....

Instructions Meaning Operation

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).

 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
 Examples:
1. Assume that AL contains FFH (the 2’complement of the number 1), CL contain FEH (the
2’complement of the number 2). Then, what is the result of executing the following
instructions?
a. MUL CL
b. IMUL CL

Solution:

DBU, Department of Electrical and Computer Engineering, 2024 8


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

3.5.3. Bit Manipulation & Logical Instructions


3.5.3.1. Basic logic instructions (AND, OR, XOR, NOT, TEST)
The 8086 processor has instructions to perform bit by bit logic operation on the specified source and
destination operands. These basic logic instructions include AND, OR, XOR, and NOT. This group
also include TEST instruction which is a special form of the AND instruction.
Instruction Meaning Syntax Operation Flags affected
CF, OF, PF, SF ZF
AND Logical AND AND d, s d = d .s - AF undefined
- CF, OF=0, after AND
CF, OF, PF, SF ZF
OR Logical inclusive OR OR d, s d=d+s - AF undefined
- CF, OF=0, after AND
CF, OF, PF, SF ZF
XOR Logical exclusive OR XOR d, s d = d ⨁ s AF undefined
CF, OF=0, after AND
NOT Logical NOT NOT d d = not d none
Where:

DBU, Department of Electrical and Computer Engineering, 2024 9


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

- d is destination. It can be register or memory


- s is source. It can be register, memory, or immediate.
Note:
- Uses any addressing mode except memory-to-memory and segment registers
- Destination cannot be an immediate value.
- Both source and destination operands must be the same size, which can be a byte or a word.
Example: assume,
 AL= 1001 0011 =93h,
 BL = 0111 0101 = 75h,
 CX = 0110 1011 1001 1110 = 6B9E, and
 DX = 1110 1010 1001 0110 = EA96h. then

no Sample instruction result


1 AND AL, BL AL= 0001 0001
2 AND CX, DX CX= 0110 1010 1001 0110
3 AND AL, 09H AL= 0000 0001
4 OR AL, BL AL= 1111 0111
5 OR CX, DX CX= 1110 1011 1001 1110
6 OR AL, 11h AL= 1001 0011
7 XOR AL, BL AL= 1110 0110
8 XOR CX, DX CX= 1000 0001 0000 1000
9 XOR CX, 1234h CX= 0111 1001 1010 1010
10 NOT AL AL= 0110 1100
11 NOT CX CX= 1001 0100 0110 0001
12 AND AL, DX Invalid, because size doesn’t match
13 OR DS, CX Invalid, because segment register can’t be used
14 NOT AL, BL Invalid, Not instruction has only one operand

AND: used to clear certain bits in the operand (this is called masking).
Example, clear the high nibbles of BL register
 AND BL, 0FH ; (xxxxxxxx AND 0000 1111 = 0000 xxxx)
Example: Clear bit 5 of DH register
 AND DH, DFH ; (xxxxxxxxAND1101 1111 = xx0xxxxx)
OR: used to set certain bits in the operand
Example: Set the lower three bits of BL register
 OR BL, 07H ; (xxxxxxxxOR 0000 0111 = xxxx x111)
Example: Set bit 7 of AX register
 OR AH, 80H ; (xxxxxxxxOR1000 0000 = 1xxxxxxx)
XOR:
 Used to invert certain bits (toggling bits)
 Used to clear a register by XORed it with itself
DBU, Department of Electrical and Computer Engineering, 2024 10
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

Example: Invert bit 2 of DL register


 XOR BL, 04H ; (xxxxxxxx OR 0000 0100 = xxxx xxxVxx)
Example: Clear DX register
 XOR DX, DX ; (DX will be 0000H)

TEST Instruction:
This instruction performs the AND operation. The difference is that the AND instruction changes the
destination operand, while the TEST instruction does not. A TEST instruction only affects the
condition of the flag register, which indicates the result of the TEST. These flags are effected: ZF, SF,
PF. Result is not stored anywhere.

3.5.3.2. Shift Instructions


- The four shift instructions of the 8086 can perform two basic types of shift operations: the
logical shift, the arithmetic shift
- Shift instructions are used to
 Align data
 Isolate bit of a byte of word so that it can be tested
 Perform simple multiply and divide computations
- The source can specified in two ways
 Value of 1: Shift by One bit
 Value of CL register: Shift by the value of CL register
- Note that the amount of shift specified in the source operand can be defined explicitly if it is
one bit or should be stored in CL if more than 1.

-
- Where D is destination, it can be register or memory. Count is number of shift, it can be 1 or
CL register
- The SHL and SAL are identical: they shift the operand to left and fill the vacated bits to the
right with zeros.
DBU, Department of Electrical and Computer Engineering, 2024 11
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

- The SHR instruction shifts the operand to right and fill the vacated bits to the left with zeros.
- The SAR instruction shifts the operand to right and fill the vacated bits to the left with the value
of MSB (this operation used to shift the signed numbers)

3.5.3.3. Rotate Instructions

- Where D is destination, it can be register or memory. Count is number of shift, it can be 1 or


CL register

DBU, Department of Electrical and Computer Engineering, 2024 12


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

3.5.4. String Manipulation Instructions


String Instructions

There are five basic/primitive instructions for processing strings. They are:

 MOVS - This instruction moves 1 Byte or Word of data from memory location to another.
 LODS - This instruction loads from memory. If the operand is of one byte, it is loaded into
the AL register, and if the operand is one word, it is loaded into the AX register.
 STOS - This instruction stores data from register (AL or AX) to memory.
 CMPS - This instruction compares two data items in memory. Data could be of a byte size or
word.
 SCAS - This instruction compares the contents of a register (AL or AX) with the contents of
an item in memory.
Each of the above instruction has a byte and word version and string instructions can be repeated by
using a repetition prefix. These instructions use the ES: DI and DS: SI pair of registers, where DI and
SI registers contains valid offset addresses that refer to bytes stored in memory. SI is normally
associated with DS (data segment) and DI is always associated with ES (extra segment).

The DS: SI and ES: DI registers point to the source and destination operands respectively. The source
operand is assumed to be at DS: SI and the destination operand at ES: DI in memory. The following
table provides various versions of string instructions and the assumed space of the operands.

Basic Instruction Operands at Byte Operation Word Operation

MOVS ES:DI, DS:EI MOVSB MOVSW


LODS AX, DS:SI LODSB LODSW
STOS ES:DI, AX STOSB STOSW
CMPS DS:SI, ES: D CMPSB CMPSW
DBU, Department of Electrical and Computer Engineering, 2024 13
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

SCAS ES:DI, AX SCASB SCASW

MOVS:

The MOVS instruction is used to copy a data item (byte, or word) from the source string to the
destination string. The source string is pointed by DS: SI and the destination string is pointed by ES:
DI.

a. MOVSB:
-
Copy byte at DS: [SI] to ES: [DI] and then update SI and DI.
-
Syntax: MOVSB (No operands)
-
Algorithm:
ES: [DI] = DS: [SI]
if DF = 0 then
SI = SI + 1
DI = DI + 1
Else
SI = SI – 1
DI = DI – 1
b. MOVSW:
- Copy word at DS: [SI] to ES: [DI] and then update SI and DI.
- Syntax: MOVSW (No operands)
- Algorithm:
ES: [DI] = DS: [SI]
if DF = 0 then
SI = SI + 2
DI = DI + 2
Else
SI = SI – 1
DI = DI – 1
LODS:

It loads strings to registers from memory. If the operand/data is of one byte, it is loaded into the AL
register, if the operand is one word; it is loaded into the AX register.

a. LODSB:
- Load byte at DS: [SI] into AL and then Update SI.
- Algorithm:
AL = DS: [SI]
if DF = 0 then
SI = SI + 1
Else
SI = SI – 1
b. LODSW:
DBU, Department of Electrical and Computer Engineering, 2024 14
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

- Load word at DS: [SI] into AX and then Update SI.


- Algorithm:
AX = DS: [SI]
if DF = 0 then
SI = SI + 2
Else
SI = SI – 2
STOS:

The STOS instruction copies the data item from AL (for bytes - STOSB) or AX (for words - STOSW)
to the destination string, pointed to by ES: DI in memory.

CMPS:

The CMPS instruction compares two strings. This instruction compares two data items of one byte or
word, pointed to by the DS: SI and ES: DI registers and sets the flags accordingly. You can also use
the conditional jump instructions along with this instruction. It also has two versions; CMSB and
CMSW.

SCAS:

The SCAS instruction is used for searching a particular character or set of characters in a string. The
data item to be searched should be in AL (for SCASB) or AX (for SCASW) registers. The string to be
searched should be in memory and pointed by the ES: DI register. It is similar to CMPS instruction
except the operands of CMPS are both memory locations.

Repetition Prefixes

The REP prefix, when set before a string instruction, for example - REP MOVSB, causes repetition
of the instruction based on a counter placed at the CX register. REP executes the instruction, decreases
CX by 1, and checks whether CX is zero. It repeats the instruction processing until CX is zero.

The REP prefix also has the following variations:

- REP: it is the unconditional repeat. It repeats the operation until CX is zero.


- REPE or REPZ: It is conditional repeat. It repeats the operation while the zero flag indicate
equal/zero. It stops when the ZF indicates not equal/zero or when CX is zero.
- REPNE or REPNZ: It is also conditional repeat. It repeats the operation while the zero flag
indicate not equal/zero. It stops when the ZF indicates equal/zero or when CX is decremented
to zero.
The Direction Flag (DF) determines the direction of the operation.

- Use CLD (Clear Direction Flag, DF = 0) to make the operation left to right.
- Use STD (Set Direction Flag, DF = 1) to make the operation right to left.
Examples:

DBU, Department of Electrical and Computer Engineering, 2024 15


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

1. What will be the value of y, and the content of SI & DI, after executing the following program?
(Assume that offset of x = 0102h and offset of y = 0107h)
a b c
org 100h org 100h org 100h
.data .data .data
x db "hello" x db "hello" x db "hello"
y db ? y db ? y db ?
.code .code .code
mov ax, @data mov ax, @data mov ax, @data
mov ds, ax mov ds, ax mov ds, ax
cld std cld
mov si, offset x mov si, offset x mov si, offset x
mov di, offset y mov di, offset y mov di, offset y
movsb movsb movsw
ret ret ret
d e f
org 100h org 100h org 100h
.data .data .data
x db "hello" x db "hello" x db "hello"
y db ? y db ? y db ?
.code .code .code
mov ax, @data mov ax, @data mov ax, @data
mov ds, ax mov ds, ax mov ds, ax
std mov cx, 3 mov cx, 3
mov si, offset x cld std
mov di, offset y mov si, offset x mov si, offset x
movsw mov di, offset y mov di, offset y
ret rep movsb rep movsb
ret ret
g h ANSWERS
org 100h org 100h Y= SI= DI=
.data .data a h 0103h 0108h
x db "hello" x db "hello" b h 0101h 0106h
y db ? y db ? c he 0104h 0109h
.code .code d h 0100h 0105h
mov ax, @data mov ax, @data e hel 0105h 010ah
mov ds, ax mov ds, ax f h 00ffh 0104h
mov cx, 2 mov cx, 2 g hell 0106h 010bh
cld std h h 00FEh 0103h
mov si, offset x mov si, offset x
mov di, offset y mov di, offset y
rep movsw rep movsw
ret ret

no Sample code result


1 org 100h SI= SI+1
DBU, Department of Electrical and Computer Engineering, 2024 16
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

.data AL= ‘h’


x db "hello"
.code
mov ax, @data
mov ds, ax
CLD
mov si, offset x
LODSB
ret
2 org 100h SI= SI+2
.data AX= ‘he’
x db "hello"
.code
mov ax, @data
mov ds, ax
CLD
mov si, offset x
LODSW
ret
3 org 100h DI= DI+1
.data X= ‘A’
x db ?
.code
mov ax, @data
mov ds, ax
mov al, 41h
CLD
mov DI, offset x
stosb
ret

3.5.5. Program Control Transfer Instructions


The sequence of execution of instructions in an 8086/8088 program is determined by the content of the code
segment register (CS) and the instruction pointer (IP).
DBU, Department of Electrical and Computer Engineering, 2024 17
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

The program transfer instructions operate on the instruction pointer and on the CS register; changing the con~
tent of these causes normal sequential execution to be altered.

Four groups of program transfers are available in the 8086/8088 : unconditional transfers, conditional
transfers, iteration control instructions and interrupt-related instructions. Only the interrupt-related
instructions affect any CPU flags.

- Unconditional transfer instructions CALL, RET, JMP


- Conditional transfer instructions J cond
a. CALL Instruction
- It is used to transfer the execution to a sub program or procedure.
- There are two basic types of CALL, near and far:
- Near CALL: is a call to procedure which is in the same code segment as the call
instruction. When it is executed:
 It decrements the SP by two,
 copies the offset of the next instruction after the call on the stack.
 Loads IP with the offset of the first instruction of the procedure in the
same segment.
- far CALL: is a call to procedure which is in a different segment from that which
contains the call instruction. When it is executed:
 it decrements the SP by two,
 Copies the content of CS registers to the stack.
 It then decrements SP by two again and copies the offset of the next
instruction after the call on the stack.
 Finally, it loads CS with the segment base of the segment which contains the
procedure and Loads IP with the offset of the first instruction of the procedure
in that segment.
b. RET Instruction
- This instruction will return execution from a procedure to the next instruction after the
call instruction in the calling program.
- If the procedure is a near procedure (in the same code segment as the call instruction),
then the return will be done by replacing the IP register with a word from the top of
stack.
- If the procedure is a far procedure (in a different code segment from the call
instruction), then the IP will be replaced by the word at the top of stack. The SP will
then be incremented by two. The CS register is then replaced with a word from the
new top of stack. Finally, the SP is again incremented by two.

Iteration Control Instructions (Loops)

DBU, Department of Electrical and Computer Engineering, 2024 18


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

These instructions are used to execute a series of instructions some number of times. The number is
specified in the CX register. The CX register is automatically decremented by one, each time after
execution of LOOP instruction. Until CX =0, execution will jump to a destination specified by a label
in the instruction.

The destination address for the jump must be in the range of -128 bytes to +127 bytes from the address
of the instruction after the iteration control instruction.

Instruction operation and jump condition opposite instruction


LOOP Decrease cx, jump to label if cx not zero. DEC CX and JCXZ
LOOPE Decrease cx, jump to label if cx not zero and equal (zf = 1). LOOPNE
LOOPNE Decrease cx, jump to label if cx not zero and not equal (zf = LOOPE
0).
LOOPNZ Decrease cx, jump to label if cx not zero and zf = 0. LOOPZ
LOOPZ Decrease cx, jump to label if cx not zero and zf = 1. LOOPNZ
JCXZ Jump to label if cx is zero. OR CX, CX and
JNZ

Interrupt Instructions

Interrupts can be seen as a number of functions. These functions make the programming much easier,
instead of writing a code to print a character you can simply call the interrupt and it will do everything
for you. There are also interrupt functions that work with disk drive and other hardware. We call such
functions software interrupts.

Interrupts are also triggered by different hardware, these are called hardware interrupts.

To make software interrupt there is an INT instruction, it has very simple syntax:

INT value

Where value can be a number between 0 to 255 (or 0 to 0FFh), generally we will use hexadecimal
numbers.

3.5.6. Processor Control Instructions


The following table shows the Processor Control Instructions of 8086 microprocessor

DBU, Department of Electrical and Computer Engineering, 2024 19


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

Following are some of the most important instructions under this group:

- STC: Set Carry flag CF = 1


- CLC: Clear Carry flag
- CMC: Complement Carry flag. Inverts value of CF. if CF = 1 then CF = 0if CF = 0 then CF
=1
- STD: Set Direction flag. SI and DI will be decremented by chain instructions: CMPSB,
CMPSW, LODSB, LODSW, MOVSB, MOVSW, STOSB, and STOSW. DF = 1
- CLD: Clear Direction flag. SI and DI will be incremented by chain instructions: CMPSB,
CMPSW, LODSB, LODSW, MOVSB, MOVSW, STOSB, STOSW. DF = 0
- STI: Set Interrupt enable flag. This enables hardware interrupts. IF = 1
- CLI: Clear Interrupt enable flag. This disables hardware interrupts. IF = 0

3.6. Procedures and Macros


3.6.1. Procedures
Procedure is a part of code that can be called from your program in order to make some specific task.
Procedures make program more structural and easier to understand. Generally procedure returns to the
same point from where it was called.

Procedures or subroutines are very important in assembly language, as the assembly language
programs tend to be large in size. Procedures are identified by a name. Following this name, the body
of the procedure is described, which perform a well-defined job. End of the procedure is indicated by
a return statement.

DBU, Department of Electrical and Computer Engineering, 2024 20


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

Syntax: The syntax for procedure declaration:

Proc_name PROC
; Here goes the code
; of the procedure ...
RET
Proc_name ENDP
name - is the procedure name, the same name should be in the top and the bottom, this is used to check
correct closing of procedures.

Probably, you already know that RET instruction is used to return to operating system. The same
instruction is used to return from procedure (actually operating system sees your program as a special
procedure).

PROC and ENDP are compiler directives, so they are not assembled into any real machine code.
Compiler just remembers the address of procedure.

The procedure is called from another function by using the CALL instruction. The CALL instruction
should have the name of the called procedure as argument as shown below:
CALL Proc_name
The called procedure returns the control to the calling procedure by using the RET instruction.
Example
org 100h
.data
x db "Hello$"
.code Hello
mov ax, @data
mov ds, ax
mov DX, offset x
CALL print
ret
print proc
mov ah, 09h
int 21h
ret
print endp

3.6.2. Macros
Macros are just like procedures, but not really. Macros look like procedures, but they exist only until
your code is compiled, after compilation all macros are replaced with real instructions. If you declared
a macro and never used it in your code, compiler will simply ignore it.

Unlike procedures, macros should be defined above the code that uses it

DBU, Department of Electrical and Computer Engineering, 2024 21


ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM

Syntax for macro definition:

name MACRO [parameters,...]

<instructions>

ENDM

The macro is called from another function by using its name.

name parameters

Example: a macro which display strings

macro_print macro para1


mov dx, offset para1
mov ah, 09h
int 21h Helloworld
endm

org 100h
.data
x db "Hello$"
y db "world$"
.code
mov ax, @data
mov ds, ax

macro_print x
macro_print y

ret

3.7. Timing and Delay


Read about

 INT 15h / AH = 86h - BIOS wait function.


 On Godse, about timing and delay

3.8. Other Topics

DBU, Department of Electrical and Computer Engineering, 2024 22

You might also like