0% found this document useful (0 votes)
25 views31 pages

MP02 - Insruction Set 1

This document provides an introduction to the 8086 instruction set, detailing various addressing modes and the instruction categories such as data transfer, arithmetic, and logical operations. It explains how to write assembly language programs and the effects of instructions on the flag register. Additionally, it includes examples and references for further reading on microprocessors and digital logic.

Uploaded by

asmeissas942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views31 pages

MP02 - Insruction Set 1

This document provides an introduction to the 8086 instruction set, detailing various addressing modes and the instruction categories such as data transfer, arithmetic, and logical operations. It explains how to write assembly language programs and the effects of instructions on the flag register. Additionally, it includes examples and references for further reading on microprocessors and digital logic.

Uploaded by

asmeissas942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to Microprocessors

Chapter 2 - 8086 Instruction Set

Dr. Adnan Ismail Al-Sulaifanie

Department of Electrical and Computer Engineering


University of Duhok, Iraq

Outline

ä List and explain 8086 addressing modes

ä List 8086 instruction set.

ä Explain effect of instructions on flag register.

ä List other data representation methods such as BCD and ASCII.

ä Learn how to write an assembly language program.

References

1. Brey, The Intel Microprocessors, chapters 3 - 6

2. Rafiquzzaman, Fundamentals of Digital Logic and Microcomputer Design


, chapter 9.

3. Kani, 8086 microprocessor and its applications, chapter 3

1
1 8086 Addressing Modes

ä When the 8086 executes an instruction, it performs a specific function on data.


These data, called operands.

ä Addressing modes are the various methods used to specify the operand. It
define how the processor calculates the effective address of an operand.

ä The 8086 processor supports several addressing modes, including:

1. Immediate.
2. Register.
3. Memory.
4. Implied

ä Immediate Addressing Mode

ã Source operand is data which is part of the instruction.


MOV AL, 4E
MOV DX, 3A21
ã Operand is constant and its size is either 8 bit or 16 bit.
ã Operand can be accessed quickly because it resides in instruction queue,
and no extra memory access to data segment is required.

ä Register Addressing Mode

ã Both operand are register .


ADD DH, BL
MOV AX, CS
ã This mode is fast because no extra memory access is required.

ä Memory Addressing Mode

ã One operand resides in memory (data or extra segment).


ã No instruction can use two memory operands in 80x86 family (except string
instructions).
ã The BIU accesses data segment to fetch/write operand during execution of
the instruction.

ä Direct addressing mode

ã The operand is stored in memory at a specific address. The address of


memory is part of the instruction.

2
ã Example
MOV DH, [21F3]
MOV [0100], AX
ã it is not flexible.

ä Register indirect addressing mode

ã The address of memory operand is stored in one of the index or base regis-
ters (SI, DI, BX).
MOV CX, [BX]
ADD [SI], AL
ã This mode is not efficient for variables having a single element.

ä Indexed addressing mode

ã Use SI/DI with 8/16 bit offset.


MOV CX, [SI+05]
ADD [SI + 1200], DX

ä Based addressing mode

ã Use BX/BP with 8/16 bit offset.


MOV AH, [BX+05]
MOV [BX + 0120], CX

ä Based-Indexed addressing mode

ã This mode generates the address by taking the sum of Base register + Index
register + offset.

Base + Index + offset


BX SI 8 bit
BP DI 16 bit

MOV CH, [BX+SI+06]


MOV [BP+SI+1006], BX
ã This addressing mode can be used to access complex data structures such
as two-dimensional arrays.

ä When memory addressing mode is used, the execution time of the instruction
is longer because one/more memory access is required. If memory operand is
destination, it requires two memory access; one to read operand and another to
save the result.
ADD AL, [100] ; requires one memory access
ADD [100], AL ; requires two memory access

3
ä The default segment register is DS for direct,SI,DI,BX

ä BP is used to access stack segment (SS:BP)

Mode Direction Example

Immediate R/M ⇐ data MOV AL, 05


Register R⇐R MOV AX, CX
Direct R ⇐⇒ M MOV AL, [200]
MOV [500], AX
M ⇐ data MOV byte ptr [200], 1A
Register indirect [SI/DI/BX/BP] ⇐⇒ R MOV AL, [SI]
MOV [BX], DX
[SI/DI/BX/BP] ⇐ data MOV word ptr [DI], 1C00
Based [BX/BP + offset] ⇐⇒ R MOV [BX+05], AL
Indexed [SI/DI + offset] ⇐⇒ R MOV AL, [SI+05]
Based-Indexed [BX/BP+SI/DI + offset] ⇐⇒ R MOV AL, [BX+SI+05]
[BX/BP+SI/DI + offset] ⇐ data MOV byte ptr [BX+DI+05], 8B

2 8086 Instruction set

ä Every microprocessor can understand and execute a specific number of com-


mands. Each command is called instruction.

ä The complete set of instructions is called instruction set.

ä Instruction set of 8086 can be divided into a number of groups depending on the
type of operation as shown in the table below.

Group Instructions

Data transfer MOV, XCHG, XLAT, PUSH, POP, IN , OUT


Arithmetic ADD, ADC, INC, SUB, SBB, DEC, CMP, MUL, IMUL, DIV, IDIV
Logical AND, OR, XOR, NOT
Shift and rotate SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR
Jump JMP, Jcc
Subroutine call CALL, RET, RETF
String MOVSX, LODSX, STOSX, CMPS, SCAS
Interrupt INT n , IRET
Miscellaneous STD, CLD, CLI, STI, CBW, CWD

4
ä The effect of these instructions on the flag register is not the same. The instruc-
tion may affect some or all flags, while others do not affect flags at all.

x affected the value depends on the result


not affected the previous value remains unchanged
? undefined changes randomly
1 set always 1 and doesn’t depend on the result
0 reset always 0 and doesn’t depend on the result

3 Data transfer instructions

MOV D, S copy the content of the source to the destination, D =


S

R ⇐ data
M ⇐ data
R ⇐⇒ M

ä No flags are affected.

ä The operands must be of identical size.

ä Both operands cannot be located in memory.

ä Loading data directly into the segment register is not allowed.


MOV AX, 1F00
MOV CS, AX

Example 1 - write a program to copy the content of memory locations 0200h into
0201h.

MOV AL, [200]


MOV [201], AL

Example 2 - write a program to exchange the content of memory locations 0200h


and 0201h with each other

MOV AX, [200]


MOV [200], AL
MOV [201], AH

5
Or

MOV SI, 200


MOV AX, [SI]
MOV [SI], AL
MOV [SI+1], AH

XCHG D, S exchanges the content of register with the content of


another register or memory location.

Instruction AX DX
1111 FFFF
XCHG AX, DX FFFF 1111

Example 3 - two words are stored consecutively in memory locations 250h - 253h.
Write a sequence of instructions to exchange their contents with each other.

MOV SI, 250


MOV AX, [SI]
XCHG AX, [SI+2]
MOV [SI], AX

XLAT [BX+AL] ⇒ AL

ä XLAT instruction performs the direct table lookup technique often used to convert
one code to another.

ä The primary purpose of the XLAT instruction is to perform a one-byte table lookup
using the AL register as an index.

ä It is designed to simplify the conversion of values in a lookup table, often used


for character translation or encoding. For example, code conversion, 7-segment
decoder, ...

Example 4 - write a program to determine the square of any numbers in the


range of 0 - 9, using XLAT instruction.

2000:500 00H, 01H, 04H, 09H, 10H, 19H, 24H, 31H, 40H, 51H

MOV BX, 500


MOV AL, 03
XLAT ; AL = 09

6
Stack memory

ä The stack is a memory area indexed by SS:SP.

ä It is used by processor and programmer for storing information temporarily.

ä The stack is viewed as 32 K words (16 bit data can be read or written).

ä It operate in last-in first-out

ä The memory location indexed by SP called top of stack (TOS).

ä The bottom of stack is the last memory location in the stack (SS:FFFE)

PUSH S S ⇒ [SP]

Example 5 - PUSH AX ; assume AX = 1234 and SP = F008

1. SP = SP - 2 ; SP = F008 - 2 = F006

2. AX ⇒ [SP] ; [F006] = 34, [F007] = 12

POP D [SP] ⇒ D

Example 6 - POP DX ; assume SP = F006

1. MOV DX, [SP]

2. SP = SP + 2 ; SP = F008

To access operands in the stack segment, the BP register should be utilized.

4 Arithmetic Instructions

Addition instructions ADD, ADC, and INC,


Subtraction instructions SUB, SBB, DEC, and NEG
Multiplication instructions MUL and IMUL
Division instructions DIV, IDIV

7
4.1 Addition and Subtraction Instructions

Instruction O S Z A P C
ADD, ADC, SUB, SBB, NEG, CMP x x x x x x
INC, DEC x x x x x
MUL, IMUL x ? ? ? ? x
DIV, IDIV ? ? ? ? ? ?

ADD D, S D = D + S
ADC D, S D = D + S + CF
INC D D=D+1
SUB D, S D=D-S
SBB D, S D = D - S - CF
CMP D, S performs D - S without saving the result in D. Only update flags
NEG D D = 0 - D = -D
DEC D D=D-1

Example 7 - write a program to add four numbers in memory locations 0050h -


0053h. Store the result in memory locations starting at address 0055h

MOV SI, 0050

MOV AL, [SI]


MOV AH, 00

ADD AL, [SI+1]


ADC AH, 00

ADD AL, [SI+2]


ADC AH, 00

ADD AL, [SI+3]


ADC AH, 00

MOV [SI+5], AX

HLT

Example 8 - write a program to compute the result of adding 8 bit with 16 bits
numbers. The numbers are stored in memory locations 0100h - 0102h respectively.
Store the result in memory locations starting at address 0103h.

MOV SI, 0100


MOV AX, [SI+1]

8
MOV BL, 00
ADD AL, [SI]
ADC AH, 00
ADC BL, 00
MOV [SI+1], AX
MOV [SI+3], BL

Example 9 - write a program to subtract two numbers stored in memory locations


03F0h and 03F1h. Store the result in memory locations starting at address 03F2h

MOV SI, 03F0

MOV AL, [SI]

MOV AH, 00

SUB AL, [SI+1]

SBB AH, 00

MOV [SI+2], AX

4.2 Multiplication and Division Instructions

(8 bit) MUL S AX = Al ∗ S

(16 bit) MUL S DXAX = AX ∗ S


AX
(8 bit) DIV S ⇒ AL = quotient AH = reminder
S
DXAX
(16 bit) DIV S ⇒ AX = quotient DX = reminder
S

ä IMUL and IDIV are used for signed operands.

ä The source is either register or memory. Immediate operand is not allowed.

ä MUL and IMUL affect only CF and OF flags. Other flags are undefined.
CF = OF = 0 if high order of the result is zero.

ä There are two type of division operations: unsigned (DIV) and signed (IDIV).

ä DIV and IDIV change flags randomly (undefined)

ä In the case of memory operand, memory pointer (byte ptr and word ptr) should
be used to specify the size of operand.

9
MUL byte ptr[SI] 8 bit AL * M8
MUL word ptr[SI] 16 bit AX * M16
DIV byte ptr[SI] 8 bit AX/M8
MUL word ptr[SI] 16 bit DX AX / M16

Example 10 - assume AL = 03 and BL = 05

MUL BL

AX = 000F

CF = OF = 0 because AH = 0

Example 11 -
unsigned signed
AL = FF 255 -1
BL = FE 254 -2

AX CF = OF
Unsigned MUL BL FD02h = 64770 1
Signed IMUL BL 0002h 0

Example 12 - write a program to multiply two bytes stored in memory locations


0200h and 0201h. Store the result in memory locations starting at 0300h.

MOV SI, 0200

MOV AL, [SI]

MUL byte ptr [SI+1]

MOV [SI+100], AX

Example 13 - write a program to compute the result of following arithmetic


expression:

Y = X 2 − 3X + 5

Store the result in memory locations starting at address 201h. Assume X is stored in
memory location 200h

MOV SI, 0200

MOV BL, [SI] ; BL = X

10
MOV AL, 03

MUL BL ; AX = 3X

XCHG AX, BX ; AH = ?, AL = X, BX = 3X

MUL AL ; AX = X 2

SUB AX, BX ; AX = X 2 - 3X

ADD AX, 0005 ; AX = X 2 - 3X + 5

MOV [SI+1], AX

Homework 1 - write a program to multiply 8 bit number with 16 bit number.


They stored in memory locations 400h - 402h receptively. Store the result in memory
locations starting at address 0450h.

Number extension

ä Sometimes, it is required to perform 8 bit / 8 bit or 8 bit * 16 bit. In such cases,


one of the operands should be converted to 16 bit.

ä Converting 8 bit operand to 16 bit is performed differently for signed and un-
signed numbers.

ä Examples

16 bit-unsigned 16 bit-signed
AL = FF AX = 00FF AX = FFFF
AX = FFFF DX AX = 0000 FFFF DXAX = FFFFFFFF

ä The instruction CBW is used to convert signed byte to word.

ä The instruction CWD is used to convert signed word to double.

ä CBW and CWD are used for signed numbers.

CBW AL = 80h ⇒ AH = FF
AL = 0F ⇒ AH = 00

CWD AX = F000 ⇒ DX = FFFF


AX = 0FFF ⇒ DX = 0000

16
Example 14 - write a program to compute
−3

11
MOV AL, 10 ; AL = 16

CBW ; AX = 0016h

MOV BL, 03

NEG BL BL = -3 = FDh

IDIV BL AL = FB = -5 , AH = 1

Example 15 - write a program to compute the result of following arithmetic


expression

3X 2 + X
X +2
Assume X is stored in memory location 200h. Store the result in memory locations
starting at address 201h.

MOV AL, [200] ; AL = X

MOV BL, AL ; BL = X

MOV BH, 00

MUL AL ; AX = X 2

MOV DX, 0003

MUL DX ; DX AX = 3 X 2

ADD AX, BX

ADC DX, 0000 ; DX AX = 3X 2 + X

ADD BX, 0002 ; BX = X + 3

3X 2 + X
DIV BX ; AX =
X +2
MOV [201], AX

Homework 2 - write a program to compute the average of three bytes stored in


memory starting at address 051Fh.

Homework 3 - the speed and time of a car is shown below. Write a program to
compute the average speed of the car.

Time (hours) Speed (Km/hour)


3 80

12
5 60
2 100

5 Logic instructions

ä Instructions: AND, OR, XOR, and NOT

ä NOT instruction finds 1’s complement of the operand.

Instruction O S Z A P C
AND, OR, XOR, TEST 0 x x ? x 0
NOT

Assume AL = 1111 0000b


Instruction Function Example AL Description
AND reset certain bits AND AL, EF 1110 0000 reset b4 in AL
OR set certain bits OR AL, 01 1111 0001 set b0 in AL
XOR complement certain bits XOR AL, 30 1100 0000 complement b5 − b4 in A
NOT complement all bits NOT AL 0000 1111 complement AL

Example 16 - write a set of instructions to do the following:

1. Clear bits 0 and 1 of register AL

2. Set bits 6 and 7 of register BH

3. Invert bit 5 of register CL

Example 17 - write a program to compute the result of following expression:

y = A + B⊕C

where A, B, and C are stored in memory locations 1A00h - 1A02h.

MOV SI, 1A00

MOV AL, [SI+1] ; AL = B

XOR AL,[SI+2] ; AL = B ⊕ C

OR AL, [SI]

NOT AL

13
6 Shift and Rotate Instructions

Assume AL = 1000 1111 and CF = 0

Group Instruction Description Example AL C


Shift SHL shift left SHL AL, 1 0001 1110 1
SHR shift right SHR AL, 1 0100 0111 1
Arithmetic shift SAL = SHL shift arithmetic left SAL AL, 1 0001 1110 0
SAR shift arithmetic right SAR AL, 1 1100 0111 1
Rotate ROL rotate left ROL AL, 1 0001 1111 1
ROR rotate right ROR AL, 1 1100 0111 1
Rotate through carry RCL rotate through carry left RCL AL, 1 0001 1110 1
RCR rotate through carry right RCR AL, 1 0100 0111 1

ä Each instruction can shift/rotate the content of operand by one or by the count
specified in CL register
SHL AL, 1
ROR BX, CL

ä Carry flag is loaded with the last bit shifted.

14
Example 18 -

1. Multiply AX by 4 using shift instructions

2. Divide AX by 16 using shift instructions

7 Control Transfer Instructions

ä These instructions transfer program control to a different address instead of next


memory address indexed by CS:IP

ä There is two types of transfer:

1. Intrasegment the transfer is within the same segment (only change IP)
2. Intersegment transfer to another segment (change IP and CS)

ä There is two set of instructions:

1. JUMP instructions
2. CALL instruction

7.1 Jump instructions

ä Jump instructions allow the programmer to skip a section of the program and
jump to any part of the memory to execute next instruction.

ä There are two type of jump instructions

15
1. Uncondition jump execute next instruction indexed by the addr
JMP addr
JMP 1000 intrasegment jump
JMP 2000:0500 intersegment jump
2. Conditional jump execute next instruction indexed by the addr if some
condition is verified

Jcc addr

ä A conditional jump instruction allows the programmer to make a decision based


on the result of previous operation.

ä The control will be transferred to a new address if a particular flag satisfies the
condition.

ä Conditional jump is short type (only IP is changed).

Example 19 - write a program to find a larger of two words stored in memory


locations starting at 400h. Store the result in memory address 404h.

MOV SI, 0400


MOV AL, [SI]
CMP AX, [SI+2]
JNC NEXT ; jump to next if X ≥ Y
MOV AX,[SI + 2] ; AX = Y
NEXT: MOV [SI+04], AX
HLT

Example 20 - write a program to compute the sum of numbers in memory range


0500h - 0520h. Store the result in memory locations starting at the address 600h

16
MOV CX, 21
XOR AX, AX ; AX = 0000
MOV DI, 500
NEXT: ADD AL, [DI] ; AL = AL + X
ADC AH, 00 ; AH = AH + CF
INC DI
DEC CX
JNZ NEXT
MOV [600], AX
HLT

LOOP instruction

ä The LOOP instruction replaces the following instructions to speed up program


execution:
DEC CX
JNZ addr

ä LOOP decrements CX

CX >0 jump to addr


=0 execute next instruction

Example 21 - write a program to count positive numbers in memory range 0500h


- 0520h.

MOV CX, 21
MOV BL, 00
MOV SI, 500
NEXT1: MOV AL, [SI]
TEST AL, 80
JS NEXT
INC BL
NEXT2: INC SI
LOOP NEXT1
HLT

Example 22 - write a program to count zero numbers in memory range 0400h -


0450h.

MOV CX, 51

17
MOV DL, 00
MOV SI, 400
NEXT1: MOV AL, [SI]
AND AL, AL
JNZ NEXT2
INC DL
NEXT2: INC SI
LOOP NEXT1
HLT

Example 23 - write a program to count numbers those divisible by 8 in the range


01 - FF.

MOV CX, 00FF


XOR DL, DL
MOV BL,08
NEXT: MOV AX, CX
DIV BL
OR AH, AH
INZ NEXT2
INC DL
NEXT2 LOOP NEXT
HLT

Example 24 - write a program to find out the largest number in an array of


sixteen bytes stored in memory locations starting at address 500h.

MOV CX, 0010


MOV AL, 00 ; AL = minimum number (unsigned)
MOV SI, 500
NEXT: CMP AL, [SI] ; compare AL with Xn
JAE NEXT2 ; jump to NEXT2 if AL < Xn
MOV AL, [SI] ; AL = Xn
NEXT2 INC SI
LOOP NEXT
HLT

Example 25 - write a program to shift the content of a table of 10 bytes one byte
down.

18
MOV SI, 0108
MOV CX, 0009
NEXT: MOV AL, [SI] AL = x
MOV [SI+1], AL ; load first location in the table with 00
INC SI
LOOP NEXT
MOV byte ptr [SI] , 00
HLT

Assume the content of the table before executing program is 01 02 03 04 05 06 07


08 09 0A

After executing program, the content of the table will be 00 01 02 03 04 05 06 07 08


09

Example 26 - convert the following segment of program written in C to Assembly


language. Assume x and y are stored in consecutive memory locations and indexed
by SI register.

if (x < y) {
x=x+1;
y=y-1;
}
...

MOV AL, [SI] ; AL = x


CMP AL, [SI+1]
JNC NEXT
INC byte ptr [SI] ; x = x + 1
DEC byte ptr [SI+1] ; y = y - 1
NEXT: ...

Homework 4 - repeat previous example if the source code is modified as follows:


if (x < y) {
y=y-1;
x=x-1;
}
else {
y=y-2;
x=x-2;
}
......

19
Homework 5 - write a program to count positive numbers in memory range
0500h - 0520h.

Homework 6 - write an 8086 assembly language program to compute the result


of
y
5x + 6y +
8
where X is an unsigned 8-bit number stored at 0100H and Y is a 16-bit signed number
stored at offsets 0200H and 0201H. Store the result in BX and BP. BX holds the low
word of the result and BP holds the high word.

Homework 7 - (optional)

1. A class contain 30 students. Write a program to compute the average of suc-


cessful students.

2. Write a program to compute the sum of numbers greater than 31h in a table of
30 bytes.

3. Write a program to find the maximum number in an array of 10 bytes.

4. Write a program to compute the sum of numbers in the range 00 - FF.

5. Write a program to compute the sum of odd numbers in the range 00 - FF.

6. Write a program to count positive numbers in memory range 1000h - 1070h.

7.2 CALL Instruction

ä A procedure is a group of instructions that performs a specific task.

ä A procedure is a reusable program (written once and executed many).

ä It is called from main program and return back after the processor executes last
instruction in the procedure.

ä There are three instruction.

Instruction Type Last instruction


CALL offset intrasegment RET
CALL segment:offset intersegment RETF

ä How CALL instruction is executed?


CALL addr

1. The processor executes PUSH IP IP is the return address

20
2. The starting address of the procedure (addr) is loaded into IP.
3. The processor starts the execution of the procedure until it reaches RET
instruction.
4. The processor executes POP IP return back to the main program

ä Far CALL usually used for global procedures, while local procedure uses near call.

ä Advantages of procedures: saves memory space and makes it easier to de-


velop software.

ä Disadvantage of procedures: more time is needed to link to the procedure


and return from it.

Example 27 - A table of 10 bytes stored in memory locations starting at address


300h. Write a program using a subroutine to construct another table (B) according
to following expression:

y = x2 − 5x + 3

Store table B at memory address 400h.

MOV SI, 0300


MOV DI, 0400
MOV CX, 000A
NEXT: MOV AL, [SI]
CALL COMPUTE
MOV [DI], AX
INC SI
ADD DI, 0002
LOOP NEXT
HLT

COMPUTE: MOV BL, AL


MOV BH, 00
MOV AH, 05
MUL AH ; AX = 5X
XCHG AX, BX ; AL = X, BX = 5X
MUL AL ; AX = X2
SUB AX, BX ; AX = X2 - 5X
ADD AX, 0003 ; AX = X2 - 5X + 3
RET

21
8 String Instructions

ä A string is a sequence of bytes or words that are stored in contiguous locations


in memory as a one-dimensional array.

ä String instructions perform the same operation on each element repetitively.


Thus, long strings is processed much faster than if done in a software loop.

ä 8086 processor supports 5 different string instructions which are divided into two
groups: move and compare.

ä Source operand is either AL/AX or memory location indexed by [DS:SI]

ä Destination operand is either AL/AX or memory location indexed by [ES:DI]

ä Operand size: byte or word (the letter X is replaced by B or W).

ä If both arrays reside in the same segment, then ES = DS.

ä Direction of processing: can be either upward (increment) or downward (decre-


ment). The direction is controlled by the direction flag (DF) in flag register. Two
instructions are available to control this flag:

1. CLD ( clear direction flag DF = 0) ⇒ Auto increment index registers (from


first address to last address)
2. STD ( set direction flag DF = 1) ⇒ Auto decrement index registers (from
last address to first address)

Operand size DF Registers updated


8 bit 0 SI = SI + 1 , DI = DI + 1
1 SI = SI - 1 , DI = DI - 1
16 bit 0 SI = SI + 2 , DI = DI + 2
1 SI = SI - 2 , DI = DI - 2

ä String instructions can be executed repeatedly using a repetition prefix. The


instruction is preceded by REP instruction (except the LODS instruction). The
execution is repeated automatically either for a fixed number of iterations or
until some condition is satisfied.

22
Unconditional repeat REP string-instruction repeat while CX ̸= 0
Conditional repeat REPE / REPZ string-instruction repeat while CX ̸= 0 and ZF =
REPNE / REPNZ string-instruction repeat while CX ̸= 0 and ZF =

ä REP STOSB is the fastest way to initialize a large block of memory. Also you can
use REP STOSW which is much faster.

Example 28 - write a program to move a block of data (100 bytes) from memory
address 2000:0100 to 3000: 0500.

MOV AX, 2000


MOV DS, AX
MOV SI, 0100h

23
MOV AX, 3000h
MOV ES, AX
MOV DI, 0500h

CLD
MOV CX, 0064

REP MOVSB
HLT

How we can speed up the execution of previous program?

Example 29 - write a program to reset segment 1200 using string instructions.


MOV AX, 1200
MOV ES, AX
MOV DI, 0
CLD
MOV CX, 0 ; to repeat the LOOP FFFF times
XOR AL, AL ; AL = 0
REP STOSB
HLT

Modify this program to load the segment with the following sequence 00, 01, ... , FF,
00, ....

Example 30 - write a program to count match items in segments 1000h and


2000h.

24
MOV AX, 1000h
MOV DS, AX
MOV SI, 0
MOV AX, 2000h
MOV ES, AX
MOV DI, 0
CLD
MOV CX, 0
MOV DX, 0
NEXT1: CMPSB
JNZ NEXT2
INC DX
NEXT2: LOOP NEXT1
HLT

Example 31 - write a program to copy the content of segment 2000 into segment
3000 in reverse order.

MOV AX, 2000h


MOV DS, AX
MOV SI, 0
MOV AX, 3000h
MOV ES, AX
MOV DI, 0FFFFh
MOV CX, 0
NEXT1: CLD
LODSB
STD
STOSB
LOOP NEXT
HLT

Homework 8 -

1. Write a program to move the first 100 bytes from segment 1000h to segment
2000h after squaring each element. Use string instructions.

2. Write a program to count the number of memory locations that contain 0Fh in
segment 1000 using string instructions.

25
9 BCD Arithmetic Instructions

ä BCD is acronym of Binary Coded Decimal. It is used to represent numbers in


decimal form.

ä Each digit is represented by a binary code of four bits (0 - 9). The letters A - F
are not available.

ä There are two types of BCD representation:

1. Packed BCD stores each digit in four bits. Thus, two decimal digits can be
packed into a byte. This reduces the memory requirement by half compared
to the unpacked BCD.
1234 ⇒ 12 34
2. Unpacked BCD stores one decimal digit in a byte. The most significant 4
bits of each byte is 0.
1234 ⇒ 01 02 03 04

ä The result of arithmetic operation on BCD numbers is in binary form. Therefore,


it should be converted back to BCD format.

9.1 Packed BCD Instructions

DAA - decimal adjust after addition. DAS - decimal adjust after subtraction.
MOV AL, 71 MOV AL, 71
ADD AL, 43 ; AL = B4h SUB AL, 43 ; AL = 2Eh
DAA ; AL = 14H and CF = 1 DAS ; AL = 28h

9.2 Unpacked BCD Instructions

Four instructions are used to correct the result of arithmetic operations applied on
unpacked BCD numbers:

26
AAA ASCII adjust after addition AAM ASCII adjust after multiplication
MOV AX, 0009 MOV AL, 5
ADD AL, 01; AL = 0A MOV CL, 3
AAA ; AX = 0100 MUL CL ; AX = 000F
AAM ; AX = 0105

AAS ASCII adjust after subtraction AAD ASCII adjust before division
MOV AX, 0009 ; AL = 09 MOV BL, 9
SUB AL, 05 MOV AX, 0702
AAS ; AX = 0004 AAD ; AX = 0048h
DIV BL ; AX = 0008

Example 32 - assume BX = 3099 and DX = 1234. Add the numbers and store
the result in BCD format.
MOV BX, 3099
MOV DX, 1234
MOV AL, BL ; AL = 99
ADD AL, DL ; AL = 99 + 34 = CDh
DAA ; AL = 33 , CF = 1
MOV CL, AL
MOV AL, BH ; AL = 30
ADC AL, DH ; AL = 30 + 12 + CF = 43h
DAA ; AL = 43 , CF = 0
MOV CH, AL ; CX = 4333

Example 33 - write a procedure to convert the content of AX register into BCD


form. Store the result in AX and DX registers.

27
MOV AX, 00FFh
MOV DL, 64h ; DL = 100
DIV DL ; AX = 3702h ===> remainder = 37h = 55d, Quote = 02
MOV DL, AH ; DL = 37h
AAM ; AX = 0002
XCHG AX, DX ; AL = 37h , DX = 0002
AAM ; AX = 0505 ==> DX AX = 00 02 05 05
RET

9.3 ASCII Code

ä ASCII is abbreviation of American Standard Code for Information Interchange.

ä ASCII is standard method to represent alphanumeric and symbols in computer


system. Thus, information can be shared among computers.

ä Each number, letter, symbol, or command is represented by a unique code (1


byte).

ä The first 128 items in ASCII table called standard ASCII and the represent
different English letters, number, and keyboard commands like ESC, CTRL, etc.

ä The first 32 codes in standard ASCII are non graphic commands (never printed)
and used for control purpose such as Ins, Esc, Enter, del, shift, etc.

ä The rest of codes (20h - 7Fh) represent printable alphanumeric symbols.

ä The second half of the table is called extended ASCII (80h - FF). They are used
to store the code of non English symbols and letters.

28
ä Each hexadecimal digit is represented by a unique ASCII code as shown in table
below:

Number 0 1 2 3 4 5 ... 9
ASCII code 30h 31h 32h 33h 34h 35h ... 39h

Capital letter A B C D E F ... Z


ASCII code 41h 42h 43h 44h 45h 46h ... 5Ah

Small letter a b c d e f ... z


ASCII code 61h 62h 63h 64h 65h 66h ... 7Ah

ä When a program read a number from the keyboard, each digit is represented by
a unique ASCII code (byte). If you press keys 1 and 2 the program receives two
bytes 31h and 32h from the keyboard.

ä To apply arithmetic operations on these data, they must be converted to their


binary format before applying an arithmetic operation.

ä The result should be converted back into ASCII code before we can display them
on the screen or send them to the printer.

ASCII code Range Conversion to binary


Capital letter 61h - 66h code - 20h - 07 - 30h

29
Small letter 41h - 46h code - 07 - 30h
Numeric 30h - 39h code - 30h

Example 34 - write a procedure to convert the content of AL register from an


ASCII code to hexadecimal digit and store the result in the same register.

CMP AL, 61
JB NEXT1
SUB AL, 57h
JMP, NEXT3
NEXT1: CMP AL, 41
JB NEXT2
SUB AL, 37h
JMP, NEXT3
NEXT2: SUB AL, 30
NEXT3: ...

The following is more efficient program

CMP AL, 61
JAE NEXT1
CMP AL, 41
JAE, NEXT2
JMP, NEXT3
NEXT1: SUB AL, 20
NEXT2: SUB AL, 7
NEXT3: SUB AL, 30

Example 35 - write a procedure to convert a hexadecimal digit into ASCII code


using lookup table.

MOV BX, 500


MOV AL, X ; X = 0 - F
XLAT ; [500+X] ==> AL
XLAT

2000:500 30h, 31h, 32h, 33h, 34h, 35h, 36h, 37h, 38h, 39h, 41h, 42h, 43h, 44h,
45h, 46h

10 Flag Control Instructions

30
STC Set carry flag
CLC Clear the carry flag
CMC Complement the carry flag
CLD Clear the direction flag
STD Set direction flag
LAHF Load flags into AH register
SAHF Store AH register into flags
PUSHF Push FLAGS onto stack
POPF Pop FLAGS from stack
STI Set interrupt flag
CLI Clear the interrupt flag

31

You might also like