MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE6 CONTROL FLOW INSTRUCTIONS
MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE-6 -CONTROL FLOW INSTRUCTIONS MUHAMMAD HAFEEZ DEPARTMENT OF COMPUTER SCIENCE GC UNIVERSITY LAHORE
TODAY’S AGENDA l l Conditional Jump Unconditional Jump
CONDITIONAL JUMP l l l The control is transferred to another location if certain condition is met Flags in the flag register indicate the condition Syntax. Jxxx destination_label
CONDITIONAL JUMP l If jump condition is false, IP is not altered, thereby, execute next instruction after jump l Range of conditional jump is -128 -+127 relative to current IP Conditional jumps are sometimes referred to short jumps l
CONDITIONAL JUMP l Three Categories of Conditional Jumps: l l l Singed Jumps [singed interpretation to the result] Unsigned Jump [unsigned interpretation to the result] Single flag jump [based on the value of single flag]
CMP INSTRUCTION l 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 l Syntax, l l CMP Destination, Source
CMP INSTRUCTION l Works like SUB instruction and define the result by subtracting source from destination, however, Destination is not change in CMP l Source and Destination must not be memory locations, destination must not be constant
SINGLE FLAG JUMP l Works like SUB instruction and define the result by subtracting source from destination, however, Destination is not change in CMP l Source and Destination must not be memory locations, destination must not be constant
SIGNED JUMP
UNSIGNED JUMP
SINGED AND UNSIGNED JUMPS l l l 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 l l AX = 7 FFFH , BX=8000 H CMP AX, BX JA someotherlocation Even though AX > BX in singed way, jump will not be taken, as its unsigned jump
EXAMPLE OF CONDITIONAL JUMP
UNCONDITIONAL JUMP l JMP instruction take an unconditional jump l Syntax l l JMP Destination Used to get around limit problem of conditional jump
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
UNCONDITIONAL JUMP l Three Types of Unconditional Jumps l l l NEAR JUMP SHORT JUMP FAR JUMP
UNCONDITIONAL JUMP l SHORT JUMP, Syntax is JMP SHORT label l In this jump the address of target location is within -128 to +127 bytes of memory relative to the address of current IP. l Opcode is EB and operand is 1 byte in the range 00 to FF l Coding SHORT makes jump more efficient
UNCONDITIONAL JUMP l l l NEAR JUMP, Types of Near Jump Direct Jump, Syntax JMP NEAR label l l Register In-Direct Jump, Syntax JMP BX l l IP takes the value in BX register Memory In-Direct Jump, Syntax JMP [DI] l l In this jump the address of target location is within 32768 to +32767 bytes of memory relative to the address of current IP, within current code segment. IP takes the value in DI AND DI+1 FAR JUP, Syntax JMP FAR PTR label l Takes control out of current code segment, CS, IP both are changed
HIGH LEVEL LANGUAGE EQUIVALENCE IN ASSEMBLY l If-Then in HLL l If condition then l l some statement End if
HIGH LEVEL LANGUAGE EQUIVALENCE IN ASSEMBLY EX: If AX contains a number less than 0 then place 41 H in AX Sol: ; If AX < 0 CMP AX, 0 JNL END_IF ; THEN NEG AX INC AX END_IF:
HIGH LEVEL LANGUAGE EQUIVALENCE IN ASSEMBLY l If-Then-Else in HLL l If condition then Then l l Else l l statement for true case statement for false case End if
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
HIGH LEVEL LANGUAGE EQUIVALENCE IN ASSEMBLY l Multi-Branch Structure of HLL in assembly l Switch Statement l IF THEN ELSE END IF l l l
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
HIGH LEVEL LANGUAGE EQUIVALENCE IN ASSEMBLY l Compound Statements Using ‘AND’ ‘OR’ l IF cond 1 AND Condi 2 THEN l statements l END IF l IF cond 1 OR Condi 2 THEN l l statements END IF
HIGH LEVEL LANGUAGE EQUIVALENCE IN ASSEMBLY Example of OR compound statement MOV AH, 1 ; CHARACTER INPUT FUNCTION INT 21 H ; TAKE INPUT INTO AL CMP AL, 'Y' JE THEN CMP AL, 'y' JE THEN JMP ELSE_ THEN: MOV DL, AL MOV AH, 2 INT 21 H ELSE_:
HIGH LEVEL LANGUAGE EQUIVALENCE IN ASSEMBLY Example of AND compound statement MOV AH, 1 INT 21 H CMP AL, 'A' JNGE END_IF CMP AL, 'Z' JNLE END_IF MOV DL, AL MOV AH, 2 INT 21 H END_IF:
ITERATIVE STRUCTURE l Looping/ Iterative structure is the most important in any programming language l We can implement loops by using jump instructions and labels, until some specific conditions is met l Besides implementing loops with jumps and labels we also have LOOP instruction in 8086/8088 assembly
WHILE LOOP l While Loop l While condition is true Do l statement for true case l End While
WHILE LOOP l EX: Input Number of Character, count them, untill Enter is pressed l Initialize count to 0 Read a Character While character <> CR do l l l Count = count + 1 Read a character End While
WHILE LOOP –EXAMPLE MOV DX, 0 MOV AH, 1 ; READ CARACTER FUNCTION INT 21 H ; READ CHARACTER INTO AL WHILE_: CMP AL, 0 DH JE END_WHILE INC DX MOV AH, 2 MOV DL, AL INT 21 H MOV AH, 1 INT 21 H JMP WHILE_: END_WHILE:
DO –WHILE LOOP l Do-While Loop l Do l statement l While (condition)
DO –WHILE LOOP l EX: Read text while spacebar is pressed l Do l l Read Character While character <> spacebar
DO –WHILE EXAMPLE: MOV AH, 1 ; DO DO: INT 21 H CMP AL, 20 H ; WHILE CHARACTER IS NOT A SPACEBAR JNE DO
LOOP INSTRUCTION l l 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. l Syntax: LOOP destination l First loop instruction subtracts 1 from CX, then if CX is greater than zero, control is transferred to destination. l The destination operand must be a short label, in range -128 to +127 bytes from the current location.
LOOP INSTRUCTION - EXAMPLE l What if CX is zero, even before the loop begins? l How to check CX value ? l Figure out how to implement nested loop
QUESTIONS l ? ? ? ? ? ? ?
- Slides: 36