Download presentation
Presentation is loading. Please wait.
1
Microprocessor and Assembly Language
Lecture-6-Control Flow Instructions Muhammad Hafeez Department of Computer Science GC University Lahore
2
Today’s Agenda Conditional Jump Unconditional Jump
3
Conditional Jump The control is transferred to another location if certain condition is met Flags in the flag register indicate the condition Syntax. Jxxx destination_label
4
Conditional Jump If jump condition is false, IP is not altered, thereby, execute next instruction after jump Range of conditional jump is relative to current IP Conditional jumps are sometimes referred to short jumps
5
Conditional Jump Three Categories of Conditional Jumps:
Singed Jumps [singed interpretation to the result] Unsigned Jump [unsigned interpretation to the result] Single flag jump [based on the value of single flag]
6
CMP Instruction Single Flag jumps are taken by looking at the value of single flag change But singed and unsigned jumps are taken with a comparison based result using CMP instruction Syntax, CMP Destination, Source
7
CMP Instruction Works like SUB instruction and define the result by subtracting source from destination, however, Destination is not change in CMP Source and Destination must not be memory locations, destination must not be constant
8
Single Flag Jump Works like SUB instruction and define the result by subtracting source from destination, however, Destination is not change in CMP Source and Destination must not be memory locations, destination must not be constant
9
Signed Jump
10
Unsigned Jump
11
Singed and Unsigned Jumps
Each singed jump has its counterpart in unsigned like JG has JA Signed and unsigned jumps may be used by programmer based on interpretation of the result When we compare character, use either of singed or unsigned jump, for Extended ASCII use unsinged AX = 7FFFH , BX=8000H CMP AX,BX JA someotherlocation Even though AX > BX in singed way, jump will not be taken, as its unsigned jump
12
Example of Conditional Jump
13
Unconditional Jump JMP instruction take an unconditional jump Syntax
JMP Destination Used to get around limit problem of conditional jump
14
Unconditional Jump TOP:. ;body of the loop DEC CX ; decrement counter
JNZ DOWN ;keep looping if CX > 0 JMP EXIT DOWN: JMP TOP EXIT: MOV AX,BX
15
Unconditional Jump Three Types of Unconditional Jumps NEAR JUMP
SHORT JUMP FAR JUMP
16
Unconditional Jump SHORT JUMP, Syntax is JMP SHORT label
In this jump the address of target location is within -128 to +127 bytes of memory relative to the address of current IP. Opcode is EB and operand is 1 byte in the range 00 to FF Coding SHORT makes jump more efficient
17
Unconditional Jump NEAR JUMP, Types of Near Jump
Direct Jump, Syntax JMP NEAR label In this jump the address of target location is within to bytes of memory relative to the address of current IP, within current code segment. Register In-Direct Jump, Syntax JMP BX IP takes the value in BX register Memory In-Direct Jump, Syntax JMP [DI] IP takes the value in DI AND DI+1 FAR JUP, Syntax JMP FAR PTR label Takes control out of current code segment, CS, IP both are changed
18
High Level Language Equivalence in Assembly
If-Then in HLL If condition then some statement End if
19
High Level Language Equivalence in Assembly
EX: If AX contains a number less than 0 then place 41H in AX Sol: ;If AX < 0 CMP AX, 0 JNL END_IF ;THEN NEG AX INC AX END_IF:
20
High Level Language Equivalence in Assembly
If-Then-Else in HLL If condition then Then statement for true case Else statement for false case End if
21
High Level Language Equivalence in Assembly
EX: Suppose two numbers in AL and BH, put the largest one in CL Sol: ;If AL <= BH ; CMP AL, BH JNLE ELSE_ ;THEN MOV CL,BH ;IF AL >= BH :ELSE_ MOV CL,AL ;END_IF
22
High Level Language Equivalence in Assembly
Multi-Branch Structure of HLL in assembly Switch Statement IF THEN ELSEIF THEN ELSE END IF
23
High Level Language Equivalence in Assembly
EX: If AL contains a positive number, put -1 in BH, if AL contain 0 then put 0 in BH, if AL contains a positive number put +1 in BH? Sol: Use Case structure from HLL
24
High Level Language Equivalence in Assembly
Compound Statements Using ‘AND’ ‘OR’ IF cond1 AND Condi2 THEN statements END IF IF cond1 OR Condi2 THEN
25
High Level Language Equivalence in Assembly
Example of OR compound statement MOV AH,1 ; CHARACTER INPUT FUNCTION INT 21H ;TAKE INPUT INTO AL CMP AL,'Y' JE THEN CMP AL,'y' JMP ELSE_ THEN: MOV DL,AL MOV AH,2 INT 21H ELSE_:
26
High Level Language Equivalence in Assembly
Example of AND compound statement MOV AH,1 INT 21H CMP AL,'A' JNGE END_IF CMP AL,'Z' JNLE END_IF MOV DL,AL MOV AH,2 END_IF:
27
Iterative Structure Looping/ Iterative structure is the most important in any programming language We can implement loops by using jump instructions and labels, until some specific conditions is met Besides implementing loops with jumps and labels we also have LOOP instruction in 8086/8088 assembly
28
While Loop While Loop While condition is true Do End While
statement for true case End While
29
While Loop EX: Input Number of Character, count them, untill Enter is pressed Initialize count to 0 Read a Character While character <> CR do Count = count + 1 Read a character End While
30
While Loop – Example MOV DX,0 MOV AH,1 ;READ CARACTER FUNCTION
INT 21H ;READ CHARACTER INTO AL WHILE_: CMP AL, 0DH JE END_WHILE INC DX MOV AH,2 MOV DL,AL INT 21H MOV AH,1 JMP WHILE_: END_WHILE:
31
Do – While Loop Do-While Loop Do statement While (condition)
32
Do – While loop EX: Read text while spacebar is pressed Do
Read Character While character <> spacebar
33
Do – While Example: MOV AH,1 ;DO DO: INT 21H
CMP AL,20H ;WHILE CHARACTER IS NOT A SPACEBAR JNE DO
34
Loop Instruction The LOOP instruction is the easiest way to repeat a block of statements a specific number of times. CX is automatically used as a counter and is decremented each time the loop repeats. Syntax: LOOP destination First loop instruction subtracts 1 from CX, then if CX is greater than zero, control is transferred to destination. The destination operand must be a short label, in range -128 to +127 bytes from the current location.
35
Loop Instruction - Example
What if CX is zero, even before the loop begins? How to check CX value ? Figure out how to implement nested loop
36
Questions ??????????????????????????
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.