Вы находитесь на странице: 1из 7

Kingdom of Saudi Arabia !"‫د‬$%&'‫*)! ا‬+%'‫! ا‬,-..

'‫ا‬
Royal Commission at Yanbu /01" !),-.'‫! ا‬2)3'‫ا‬
Yanbu University College (Boys' and Girls Campus) ( 8)10'‫م ا‬+:) /01"-!)%456'‫)! ا‬-,'‫ا‬
Department of CS / MIS 116ext 3932961 :Tel
Yanbu Al-Sinaiyah !)=51>'‫ ا‬/01"

BACHELOR OF SCIENCE IN COMPUTER SCIENCES


Lab Work

Computer Organization and Assembly ( CS203)


Second Semester - Academic Year 2007 – 08
Lab Sheet 5

Practicing JMP, CMP AND LOOP instructions for flow control

Objective:
The objective of this lab is to make students practice

1. CMP instruction
2. Various jump instructions
3. Loop instructions in assembly

Outline:
Using CMP, Jump and Loop instructions for flow control

1
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Introduction:

In this experiment you will practice how to control the flow of an assembly language program using
the compare instruction, the different jump instructions and the loop instructions.

Objectives:

1- Compare Instruction.
2- Jump Instructions.
3- Loop Instructions.

Compare instruction:

The compare instruction is used to compare two numbers. At most one of these numbers may reside in
memory. The compare instruction subtracts its source operand from its destination operand and sets
the value of the status flags according to the subtraction result. The result of the subtraction is not
stored anywhere.

Instruction Example Meaning


CMP CMP AX, BX If (AX = BX) then ZF  1 and CF  0
If (AX < BX) then ZF  0 and CF  1
If (AX > BX) then ZF  0 and CF  0

The Compare Instruction of the 8086 Microprocessor

Jump Instructions:

The jump instructions are used to transfer the flow of the process to the indicated operator. When the
jump address is within the same segment, the jump is called intra-segment jump. When this address is
outside the current segment, the jump is called inter-segment jump.

2
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Instruction Meaning (jump if) Condition
Type
Unconditional JMP unconditional None
Comparisons JA jnbe above (not below or equal) CF = 0 and ZF = 0
JAE jnb above or equal (not below) CF = 0
JB jnae below (not above or equal) CF = 1
JBE jna below or equal (not above) CF = 1 or ZF = 1
JE jz equal ( zero) ZF = 1
JNE jnz not equal (not zero) ZF = 0
JG jnle greater (not lower or equal) ZF = 0 and SF = OF
JGE jnl greater or equal (not lower) SF = OF
JL jnge lower (not greater or equal) (SF xor OF) = 1 i.e. SF ≠ OF
JLE jng lower or equal (not greater) (SF xor OF or ZF) = 1
JCXZ loop CX register is zero (CF or ZF) = 0
Carry JC Carry CF = 1
JNC no carry CF = 0
Overflow JNO no overflow OF = 0
JO overflow OF =1
Parity Test JNP jpo no parity (parity odd) PF = 0
JP jpe parity (parity even) PF = 1
Sign Bit JNS no sign SF = 0
JS sign SF = 1
Zero Flag JZ zero ZF = 1
JNZ non-zero ZF = 0

Jump Instructions of the 8086 Microprocessor

Label Range Addressing Specified By Directive


Pointer Mode
Short +127/-128 bytes Immediate Word SHORT
IP  IP + Offset
Near Intra-segment Immediate Word
IP  Address
Register Word NEAR PTR

Memory Word

Far Inter-segment Immediate Double Word


IP  Address Memory Double Word FAR PTR
CS  Segment
*
Differentially = Difference between current and next address.

Jump Instructions and Addressing Modes

3
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Instruction Example Meaning
JMP FAR PTR [BX] IP  [BX], CS [BX+2]
JMP
JNZ JNZ END If (ZF=0) Then IP  Offset of END
JE JE FIRST If (ZF=1) Then IP  Offset of FIRST
JC JC SECOND If (CF=1) Then IP  Offset of SECOND

Examples of Jump Instructions of the 8086 Microprocessor

The LOOP Instructions:

The LOOP instruction is a combination of a DEC and JNZ instructions. It causes execution to branch
to the address associated with the LOOP instruction. The branching occurs a number of times equal to
the number stored in the CX register.

Instruction Example Meaning


LOOP Label1 If (CX≠0) then IP  Offset Label1
LOOP
LOOPE LOOPE Label1 If (CX≠0 and ZF = 0) then IP  Offset Label1
LOOPZ
LOOPNE LOOPNZ Label1 If (CX≠0 and ZF = 0) then IP  Offset Label1
LOOPNZ

Summary of the LOOP Instructions.

The Loop Program Structure, Repeat-Until and While-Do:

Like the condionnal and unconditionnal jump instructions which can be used to simulate the IF-Then-
Else structure of any programming language, the Loop instructions can be used to simulate the Repeat-
Until and While-Do loops.

Structure Repeat-Until While-Do


; Repeat until CX = 0 ; While (CX ≠ 0) Do
- -
MOV CX, COUNT MOV CX, COUNT
Again: - Again: JZ Next
- -
- -
- -
- -
- -
LOOP Again LOOP Again
Code - Next: -
-
The Loop Program Structure.

4
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Exercise Program 1

Type in the following program and use CodeView to see the execution of the program.

.TITLE A program to calculate the factorial of a number


.MODEL SMALL
.STACK 200
.DATA

DW N 5

.CODE
.STARTUP

MOV CX, N
MOV AX, 1
JCXZ DONE
THELOOP: MUL CX
DEC CX
JNZ THELOOP
DONE: .EXIT
END

Exercise Program 2

Type in the following program and use CodeView to see the execution of the program.

.TITLE A program to calculate sum of five numbers


.MODEL SMALL
.STACK 200
.DATA
DATAIN DB 5, 6, 7, 8, 9
SUM DB ?
.CODE
.STARTUP
MOV CX, 05H
MOV CX, OFFSET DATAIN
MOV AL, 0
AGAIN: ADD AL, [BX]
INC BX
DEC CX
JNZ AGAIN
MOV SUM, AL

.EXIT
END

5
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Exercise Program 3

Type in the following program and use CodeView to see the execution of the program.

.TITLE A program that finds the number of ones in a byte and save it in BL register
.MODEL SMALL
.STACK 200
.DATA
DATA1 DB 97
.CODE
.STARTUP
SUB BL, BL
MOV DL, 8
MOV AL, DATA1
AGAIN: ROL AL, 1
JNC NEXT
INC BL
NEXT: DEC DL
JNZ AGAIN
.EXIT
END

Exercise Program 4

Type in the following program and use CodeView to see the execution of the program.

.TITLE A program that finds the highest number in a list of numbers and stores in DL register
.MODEL SMALL
.STACK 200
.DATA
DATA1 DB 2, 6, 1, 3, 5
.CODE
.STARTUP

MOV CX, 5
MOV BX, OFFSET DATA1
SUB AL, AL
AGAIN: CMP AL, [BX]
JA NEXT
MOV AL, [BX]
NEXT: INC AX
LOOP AGAIN
MOV DL, AL

.EXIT
END

6
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Exercise Program 5
Write a program which counts the number of zeros in a byte stored in AL register.

Exercise Program 6

Write a program, which searches for a value 3 in a list of given five numbers, suppose, 4, 3, 1, 9, 6
If the program can find the number present in the list then it subtracts 2 from it and saves in BL
register, otherwise it adds 3 in it and stores in BL register.

Pseudocode

If 3 present in (4, 3, 1, 9, 6) then


BL = 3 – 2
Else
BL = 3 +3

7
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC

Вам также может понравиться