You are on page 1of 8

8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H.

ALDOURI

Conditional Jump

Cond. Unsigned Signed


JE : Jump Equal JE : Jump Equal
= ZF = 1 ZF = 1
JZ : Jump Zero JZ : Jump Zero
JNZ : Jump Not Zero JNZ : Jump Not Zero
≠ ZF = 0 ZF = 0
JNE : Jump Not Equal JNE : Jump Not Equal
JA : Jump above CF = 0 JG : Jump Greater ZF = 0
>
and ZF = 0 and SF = OF
< JB : Jump Below CF = 1 JL : Jump Less SF ≠ OF
JAE : Jump Above or Equal JGE : Jump Greater or
≥ CF = 0 SF = OF
Equal
JBE : Jump Below or Equal CF = 1 JLE : Jump Less or Equal SF ≠ OF

or ZF = 1 or ZF = 1

JC : Jump if Carry CF=1


Carry
JNC: Jump if No Carry CF=0
JZ: Jump if Zero ZF=1
Zero
JNZ: Jump if No Zero ZF=0
JPE : Jump if Parity Even PF=1
Parity
JPO: Jump if Parity Odd PF=0
JS: Jump if Signed (if negative) SF = 1
Sign
JNS: Jump if Not Signed (if positive) SF = 0
JO: Jump if Overflow OF = 1
Overflow
JNO: Jump if Not Overflow OF = 0

Ex: Write a piece of code that transfers a block of 256 bytes stored at
locations starting at 34000H to locations starting at 36000H.
Ans.
360FF ← 340FF
33 ←

.
.
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

MOV AX , 3000H
MOV DS , AX
MOV BX , 0000H
MOV CX , 100H
NEXT: MOV AL , [BX + 4000H]
MOV [BX + 6000H] , AL
INC BX
DEC CX
JNZ NEXT
HLT
Ex : Write an ALP that adds an array of integers of size 256 byte stored at
locations starting at 54000H with another array starting at address 56000H,
store the result at locations starting at 58000H.
Ans.
MOV AX , 5000H 540FF 560FF 580FF
MOV DS , AX . .
MOV BX , 0000H . .
MOV CX , 100H . .
NEXT: MOV AL , [BX + 4000H] . + . =
. .
ADD AL , [BX + 6000H]
. .
MOV [BX + 8000H] , AL . .
INC BX 54001 56001 58001
DEC CX 54000 56000 58000
JNZ NEXT
HLT
Ex: Write an ALP to find the absolute subtraction of two arrays of size
1024 bytes starting at 82000H and 84000H respectively. Store the results at
locations starting at 86000H.
Ans.
MOV AX , 8000H
MOV DS , AX
XOR SI , SI
MOV CX , 400H
NEXT: MOV AL , [SI + 2000H]
CMP AL , [SI + 4000H]
JB N1
SUB AL , [SI + 4000H]
JMP N2
N1: MOV BL , [SI + 4000H]
XCHG AL , BL
SUB AL , BL
N2: MOV [SI + 6000H] , AL

34
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

INC SI
DEC CX
JNZ NEXT
HLT
Ex: Write an ALP that counts the number of 1’s in a byte stored in 54000H
and stores the result in 54001H
Ans.
XOR BL , BL ; clear BL to keep the number of 1s
MOV CL , 8 ; rotate total of 8 times
MOV AX , 5000H
MOV DS , AX
MOV SI , 4000H
MOV AL , [SI]
AGAIN: ROL AL,1 ; rotate it once
JNC NEXT ; check for 1
INC BL ; if CF=1 then add one to count
NEXT: DEC CL ; go through this 8 times
JNZ AGAIN ; if not finished go back
INC SI
MOV [SI] , BL
HLT
Ex : Write a piece of code to find the number of negative integers in an
array of size 1024 byte contain signed numbers stored at addresses starting
at 21000H, store the result in a location 51000H.
Ans.
MOV AX , 2000H
MOV DS , AX
MOV CX , 400H
MOV DL , 00H
MOV SI , 0000H
AGAIN: MOV AL , [SI+1000H]
ROL AL , 1
JNC NEXT
INC DX
NEXT: INC SI
DEC CX
JNZ AGAIN
MOV AX , 5000H
MOV [1000H] , DX
HLT
Ex: Write an ALP to find the maximum byte of a block of 256 bytes
starting at 53000H. Store the result at 56000H.

35
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

MOV AX , 5000H
MOV DS , AX
MOV SI , 3000 H
MOV CX , 0100 H
MOV AH , 00 H
NEXT: CMP AH , [SI]
JAE PASS
MOV AH, [SI]
PASS: INC SI
DEC CX
JNZ NEXT
MOV DI , 6000
MOV [DI] , AH
HLT

Ex : Write a program finds the factorial of 08H .


Ans.
MOV BX , 08
MOV AX , 0001
LOOP: MUL BX
DEC BX
JNZ LOOP
HLT

H.W :
 Write a piece of code to find the number of odd integers in an array
of size 1024 byte stored at addresses starting at 21000H, store the
result in location 51000H.
 Write a piece of code that exchanges a block of 256 bytes stored at
locations starting at 34000H with another block starting at 36000H.
 By using XCHG instruction.
 By using MOV instruction.
 Write an ALP to find the 2’s complement of a block of 100 bytes
starting at 53000H and store the result in
 Write an ALP to find the minimum value of a byte from a block of
256 bytes starting at 53000H. Store the result at 56000H.
 Write an ALP to find the sum of the following series:
Sum=1 + 2 + 3 + 4 + ………………………..+ 100

36
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Branch Program Structure IF – THEN – ELSE


The High–level language IF–THEN–ELSE structure is expressed by the
following assembly structure:
CMP AX , BX
JNE NEQUAL
--- --- ; Next instruction if (AX) = (BX)
..
NEQUAL: --- --- ; Next instruction if (AX) ≠ (BX)
..
--- ---

Loop Program Structures WHILE – LOOP


A typical WHILE – LOOP shown in the following flow chart can be expressed
by the following assembly structure:

count= final;
while (count !=0)
{
Loop program statements;
--count;
}
….. // next statement

MOV CL , COUNT ; Set loop repeat count


AGAIN: JZ NEXT ; Loop is complete if CL=0 (ZF=1)
--- --- ; First instruction of the loop
--- --- ; Second instruction of the loop
...
--- --- ; last instruction of the loop
DEC CL ; Decrement repeat count by 1
JMP AGAIN ; Repeat from AGAIN
NEXT: --- --- ; First instruction executed after the loop
is complete

38
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Loop Instructions
There are three loop instructions. They can be used in place of certain
conditional jump instructions. These instructions give the programmer a
flexibility in writing programs in a simpler manner. The different loop
instructions and the operations they perform are listed below:

CX = CX - 1
NEXT

CX ≠ 0 ? Yes

No

Ex: Write a piece of code that transfers a block of 256 bytes stored at
locations starting at 34000H to locations starting at 36000H.
Ans.
MOV AX , 3000H
MOV DS , AX
MOV BX , 0000H
MOV CX , 100H
NEXT: MOV AL , [BX + 4000H]
MOV [BX + 6000H] , AL
INC BX
LOOP NEXT
HLT

30
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Ex: Write an ALP to find the maximum byte from a block of 256 bytes
starting at 53000H.
Ans.
MOV AX , 5000H
MOV DS , AX
LEA SI , [3000H]
MOV CX , 0100H
MOV AH , 00H
AGAIN: CMP AH , [SI]
JAE NEXT
MOV AH , [SI]
NEXT: INC SI
LOOPNE AGAIN
MOV [SI] , AH
HLT
Ex : Write an ALP that counts the number of letters M in a string of size
256 bytes starts at 5F600H.
MOV AX , 5000H
MOV DS , AX
LEA SI , [F600H]
MOV CX , 100H
MOV DL , 00H
MOV AL , 4DH ; 4D is the ASCII code of the letter M
AGAIN: CMP AL , [SI]
JNE NEXT
INC DL
NEXT: INC SI
LOOP AGAIN
HLT
Ex: Write an ALP to find the average of an array of size 256 bytes of
unsigned integers starting at 6A000H.
MOV AX , 6000H
MOV DS , AX
XOR BX , BX
XOR AX , AX
MOV CX , 100H
AGAIN: ADD AL , [BX+2000H] H.W: Repeat the previous example
ADC AH , 00H if the array has signed integers
NEXT: INC BX
LOOP AGAIN
XOR DX , DX
DIV BX
HLT

39
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Stack Instructions
The stack in the 8086/8088 µPs, like that in many microprocessors, is a region
of memory that can store information temporarily (‫ )بصىرة مؤقتت‬during the
execution of the program. It is called a stack, because you "stack" things on it.
The stack is a Last-In-First-Out, (LIFO) structure so the last thing stored in
the stack is the first thing retrieved. The philosophy is that you retrieve (‫)يستزجع‬
(pop) data in the opposite order of storing (push) it .
In the 8086/8088µP, the stack pointer is SS:SP (physical address) , which is a 16
bit pointer into a 20 bit address space.
 In a POP operation, the data is retrieved (POPed) from that address
(SS : SP). The SP register is incremented by 2.
(Low byte of the operand) ← SS : SP
(High byte of the operand) ← SS : (SP) + 1
(SP)← (SP) + 2
 In a PUSH operation, the data of the source is stored (pushed) at address
(SS : SP). The SP register is decremented 2.
SS: (SP) – 1 ← (High byte of the operand)
SS: (SP) – 2 ← (Low byte of the operand)
(SP) ← (SP) – 2
 POPF retrieves a word from the stack and places it into the flags register
and increments the stack pointer SP by 2.
(Low Byte of Flags Reg.) ← SS : SP
(High Byte of Flags Reg.) ← SS : (SP) + 1
(SP) ← (SP) + 2
 PUSHF pushes the contents of the flags register onto the stack at address
(SS : SP) and decrements the stack pointer SP by 2.
SS : (SP) – 1 ← (High Byte of Flags Reg.)
SS : (SP) – 2 ← (Low Byte of Flags Reg.)
(SP) ← (SP) – 2

48

You might also like