Assembly Language Lecture 5 6 Flow Control Instructions
Assembly Language Lecture 5 & 6 (Flow Control Instructions)
Lecture Outline • Branching structures: • IF-THEN-ELSE • Unconditional Jumps Flow Control Instructions 1
Branching Structures • In high-level languages, branching structures enable a program to take different paths, depending on conditions. • Branching structures: • IF-THEN-ELSE • CASE High-Level Language Structures 3
Introduction • Jump instructions can be used to implement branches and loops. • Because the jumps are so primitive, it is difficult to code an algorithm with them without some guidelines. • High-level language structures could be used as a guideline when coding in assembly language. High-Level Language Structures 2
IF-THEN • Pseudocode: IF condition is true or false THEN execute true-branch statements END_IF False condition True-branch statements High-Level Language Structures 4
IF-THEN • Example: replace the number in AX by its absolute value. • Solution: Pseudocode: IF AX < 0 THEN replace -AX by AX END_IF It can be coded as follows: ; if AX < 0 CMP AX, 0 JNL END_IF ; then NEG AX END_IF: High-Level Language Structures ; AX < 0 ; no, exit ; yes, change sign 5
The CMP Instruction • The jump condition is often provided by the CMP (compare) instruction • Syntax: CMP destination, source • Compares by computing destination contents minus source contents. • The result is not stored, but the flags are affected. • Destination may not be a constant. • CMP is just like SUB, except that destination is not changed. Flow Control Instructions 10
Conditional Jumps • Syntax Jxxx destination_label • Example JNZ PRINT_LOOP • If the condition for the jump is true, the next instruction to be executed is the one at destinaltion_label (PRINT_LOOP), which may precede or follow the jump instruction itself. • If the condition is false, the instruction immediately following the jump is done next. • For JNZ, the cindition is that the result of the previous operation is not zero. Flow Control Instructions 4
Conditional Jumps • Signed Jumps: used for signed interpretations. Symbol JG/JNLE JGE/JNL JL/JNGE JLE/JNG Description jump if grater than jump if not less than or equal jump if grater than or equal jump if not less than jump if not greater than or equal jump if less than or equal jump if not grater than Flow Control Instructions Condition for Jumps ZF = 0 & SF = OF SF <> OF ZF = 1 or SF <> OF 7
Conditional Jumps • Unsigned Jumps: used for unsigned interpretations. Symbol JA/JNBE JAE/JNB JB/JNAE JBE/JNA Description jump if above jump if not below or equal jump if above or equal jump if not below jump if not above or equal jump if below or equal jump if not above Flow Control Instructions Condition for Jumps CF = 0 & ZF = 0 CF = 1 or ZF = 1 8
Conditional Jumps • Single Flag Jumps: operates on settings of individual flags. Symbol JE/JZ JNE/JNZ JC JNC JO JNO JS JNS JP/JPE JNP/JPO Description jump if equal/ jump if equal to 0 jump if not equal/ jump if not 0 jump if carry jump if no carry jump if overflow jump if no overflow jump if sign negative jump if nonnegative sign jump if parity even jump if parity odd Flow Control Instructions Condition for Jumps ZF = 1 ZF = 0 CF = 1 CF = 0 OF = 1 OF = 0 SF = 1 SF = 0 PF = 1 PF = 0 9
How the CPU Implements a Conditional Jump • The CPU looks at the FLAGS register. • If the conditions for the jump are: • True: the CPU adjusts the IP to point to the destination_label, so that the instruction at this label will be done next. • False: the IP is not altered; this means that the next instruction in line will be done. Flow Control Instructions 6
Unconditional Jumps - The JMP Instruction • The JMP (jump) instruction causes an unconditional transfer of control (unconditional jump). • Syntax: JMP destination Flow Control Instructions a label in the same segment as the JMP itself 15
Unconditional Jumps - The JMP Instruction • JMP can be used to get around the range restriction of a conditional jump. • Example: TOP: ; body of the loop DEC CX JNZ TOP MOV AX, BX Flow Control Instructions TOP: If the loop body ; body of the loop contains so many DEC CX instructions that JNZ BOTTOM label top is out of JMP EXIT range for JNZ BOTTOM: JMP TOP EXIT: MOV AX, BX 16
IF-THEN-ELSE • Pseudocode: IF condition is true THEN execute true-branch statements ELSE execute false-branch statements END_IF False-branch statements High-Level Language Structures condition True-branch statements 6
IF-THEN-ELSE • Example: Suppose AL and BL contain extended ASCII characters. Display the one that comes first in the character sequence. • Solution: Pseudocode: IF AL <= BL THEN display the character in AL ELSE display the character in BL END_IF continue High-Level Language Structures 7
IF-THEN-ELSE It can be coded as follows: ; if AL <= BL CMP AL, BL JNBE ELSE_ ; then MOV DL, AL JMP DISPLAY ELSE_: MOV DL, BL DISPLAY: MOV AH, 2 INT 21 h High-Level Language Structures ; AL <= BL? ; no, display char in BL ; AL <= BL ; move char to be displayed ; go to display ; BL < AL ; prepare to display ; display it 8
Branches with compound Conditions • Sometimes the branching condition in an IF or CASE takes the form: condition_1 AND condition_2 or condition_1 OR condition_2 AND condition OR condition where condition_1 and condition_2 are either true or false. High-Level Language Structures 14
AND Condition • An AND condition is true if and only if all conditions are true. • Example: Read a character, and if it’s an uppercase letter, display it. • Solution: Pseudocode: Read a character (into AL) IF ('A' <= character) and (character <= 'Z') THEN display character END_IF continue High-Level Language Structures 15
AND Condition It can be coded as follows: ; read a character MOV AH, 1 ; prepare to read INT 21 h ; char in AL ; if ('A' <= char) and (char <='Z') CMP AL, 'A' ; char >= 'A'? JNGE END_IF ; no, exit CMP AL, 'Z' ; char <= 'Z'? JNLE END_IF ; no, exit ; then display char MOV DL, AL ; get char MOV AH, 2 ; prepare to display INT 21 h ; display char END_IF: High-Level Language Structures 16
OR Condition • An OR condition is true if at least one of the conditions is true. • Example: Read a character. If it’s 'y' or 'Y', display it; otherwise, terminate the program. • Solution: Pseudocode: Read a character (into AL) IF (character = 'y') or (character = 'Y') THEN display character ELSE terminate the program END_IF continue High-Level Language Structures 17
OR Condition It can be coded as follows: ; read a character MOV AH, 1 INT 21 h ; prepare to read ; char in AL CMP JE JMP AL, 'y' THEN AL, 'Y' THEN ELSE_ ; char = 'y'? ; yes, go to display it ; char = 'Y'? ; yes, go to display it ; no, terminate MOV INT JMP DL, AL AH, 2 21 h END_IF ; get char ; prepare to display ; display char ; and exit ; if (char = 'y') or (char = 'Y') THEN: ELSE_: MOV AH, 4 Ch INT 21 h END_IF: High-Level Language Structures ; DOS exit 18
Working with Characters • In working with the standard ASCII character set, either signed or unsigned jumps may be used. • Why? • Because the sign bit of a byte containing a character code is always zero. Flow Control Instructions 13
- Slides: 23