Академический Документы
Профессиональный Документы
Культура Документы
'ا
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"
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.
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
Memory Word
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
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.
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.
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.
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.
.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
7
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC