310 likes | 391 Views
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization & Assembly Language Lecture 16 Flow Control & Looping. Outline. Study from Chapter 6 Introduction Branching structure
E N D
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization & Assembly Language Lecture 16 Flow Control & Looping
Outline • Study from Chapter 6 • Introduction • Branching structure • IF-Then • IF-Then-Else • Case • Conditional jumps • Looping structure • FOR loop • While loop • Repeat loop Flow control & looping
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. Flow control & looping
Branching Structures • In high-level languages, branching structures enable a program to take different paths, depending on conditions. • Branching structures: • IF-THEN • IF-THEN-ELSE • CASE Flow control & looping
true or false IF-Then False condition True True-branch statements • Pseudocode: IF condition is true THEN execute true-branch statements END_IF Flow control & looping
IF-Then (cont.) • 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 ; then • CMP AX, 0 ; AX < 0 • JNL END_IF ; no, exit • NEG AX ; yes, change sign • END_IF: Flow control & looping
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 & looping
Conditional Jumps • Syntax • Jxxxdestination_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 & looping
How CPU imlpements 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 & looping
Unconditional Jump-JUMP Instruction • The JMP (jump) instruction causes an unconditional transfer of control (unconditional jump). • Syntax: JMP destination a label in the same segment as the JMP itself Flow control & looping
TOP: ; body of the loop DEC CX JNZ BOTTOM JMP EXIT BOTTOM: JMP TOP EXIT: MOV AX, BX If the loop body contains so many instructions that label top is out of range for JNZ Unconditional Jump-JUMP 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 & looping
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 Flow control & looping
IF –Then – Else (Ex.) It can be coded as follows: ; if AL <= BL ; then CMP AL, BL ; AL <= BL? JNBE ELSE_ ; no, display char in BL ; AL <= BL MOV DL, AL ; move char to be displayed JMP DISPLAY ; go to display ELSE_: ; BL < AL MOV DL, BL DISPLAY: MOV AH,2 ; prepare to display INT 21h ; display it Flow control & looping
AND condition OR condition 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 where condition_1 and condition_2 are either true or false. Flow control & looping
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 Flow control & looping
AND condition EX It can be coded as follows: ; read a character ; if ('A' <= char) and (char <='Z') ; then display char MOV AH,1 ; prepare to read INT 21h ; char in AL CMP AL, 'A' ; char >= 'A'? JNGE END_IF ; no, exit CMP AL, 'Z' ; char <= 'Z'? JNLE END_IF ; no, exit MOV DL,AL ; get char MOV AH,2 ; prepare to display INT 21h ; display char END_IF: Flow control & looping
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 Flow control & looping
OR condition EX It can be coded as follows: ; read a character ; if (char = 'y') or (char = 'Y') MOV AH,1 ; prepare to read INT 21h ; char in AL CMP AL, 'y' ; char = 'y'? JE THEN ; yes, go to display it CMP AL, 'Y' ; char = 'Y'? JE THEN ; yes, go to display it JMP ELSE_ ; no, terminate THEN: MOV DL,AL ; get char MOV AH,2 ; prepare to display INT 21h ; display char JMP END_IF ; and exit ELSE_: MOV AH, 4Ch INT 21h ; DOS exit END_IF: Flow control & looping
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. Example 6.1 Read from Book Flow control & looping
Conditional Jumps • Signed Jumps: used for signed interpretations. • Symbol Description Condition for Jumps • JG/JNLE jump if grater than ZF = 0 & SF = OF • jump if not less than or equal • JGE/JNL jump if grater than or equal SF = OF • jump if not less than • JL/JNGE jump if less than SF <> OF • jump if not greater than or equal • JLE/JNG jump if less than or equal ZF = 1 or SF <> OF • jump if not grater than Flow control & looping
Conditional Jumps -- 1 • Unsigned Jumps: used for unsigned interpretations. • Symbol Description Condition for Jumps • JA/JNBE jump if above CF = 0 & ZF = 0 • jump if not below or equal • JAE/JNB jump if above or equal CF = 0 • jump if not below • JB/JNAE jump if below CF = 1 • jump if not above or equal • JBE/JNA jump if below or equal CF = 1 or ZF = 1 • jump if not above Flow control & looping
Conditional Jumps -- 2 • Single Flag Jumps: operates on settings of individual flags. • Symbol Description Condition for Jumps • JE/JZ jump if equal/ jump if equal to 0 ZF = 1 • JNE/JNZ jump if not equal/ jump if not 0 ZF = 0 • JC jump if carry CF = 1 • JNC jump if no carry CF = 0 • JO jump if overflow OF = 1 • JNO jump if no overflow OF = 0 • JS jump if sign negative SF = 1 • JNS jump if nonnegative sign SF = 0 • JP/JPE jump if parity even PF = 1 • JNP/JPO jump if parity odd PF = 0 Flow control & looping
Loops • A loop is a sequence of instructions that is repeated. • The number of times to repeat may: • Be known in advance, or • Depend on conditions. • Looping structures: • FOR loop. • WHILE loop. • REPEAT loop. Flow control & looping
loop counter loop loop control instruction Example of Jump TITLE P1: IBM CHARACTER DISPLAY .MODEL SMALL .STACK 100H .CODE MAIN PROC MOV AH,2 ;display char function MOV CX,256 ;no. of chars to display MOV DL,0 ;DL has ASCII code of null char PRINT_LOOP: INT 21h ;display a char INC DL ;increment ASCII code DEC CX ;decrement counter JNZ PRINT_LOOP ;keep going if CX not 0(jump !0) ;DOS exit MOV AH, 4CH INT 21h MAIN ENDP END MAIN Flow control & looping
Initialize count Statements Count = count -1 Count=0 False True FOR Loop • This is a loop structure in which the loop statements are repeated a • known number of times. • Pseudocode: • FOR loop_count times DO • statements • END_FOR Flow control & looping
FOR Loop -- 2 • The LOOP instruction can be used to implement a for loop. • Syntax: • LOOP destination_label • Destination_label must precede the LOOP instruction by no more • than 126 bytes. • The counter for the loop is the register CX, which is initialized to • loop_count. • Execution of the LOOP instruction causes CX to be decremented • automatically. • If (CX < > 0) control transfers to destination_label • else the next instruction after LOOP is done. Flow control & looping
FOR Loop -- 3 • Using the instruction LOOP, a FOR loop can be implemented as • follows: • ; initialize CX to loop_count • TOP: • ; body of the loop • LOOP TOP Flow control & looping
; what if CX =0? MOV CX, 80 MOV AH, 2 MOV DL, '*' JCXZ SKIP ;jump if CX=0 TOP: INT 21h LOOP TOP SKIP: FOR Loop - EX • Example: Write some code to display a row of 80 stars. • Solution: Pseudocode: FOR 80 times DO display '*' END_IF It can be coded as follows: MOV CX, 80 MOV AH, 2 MOV DL, '*' TOP: INT 21h LOOP TOP Flow control & looping
WHILE Loop • Example: Write some code to count the number of characters in an input line. • Solution: Pseudocode: initialize count to 0 read a character WHILE character <> carriage_return DO count = count + 1 read character END_WHILE Flow control & looping
While Loop - EX It can be coded as follows: WHILE_: END_WHILE: MOV DX, 0 ; DX counts characters MOV AH, 1 ; prepare to read INT 21h ; character in AL CMP AL, 0Dh; CR? JE END_WHILE ; yes, exit INC DX ; not CR, increment count INT 21h ; read a character JMP WHILE_ ; loop back Flow control & looping
WHILE Loop • Example: Write some code to count the number of characters in an input line. • Solution: Pseudocode: initialize count to 0 read a character WHILE character <> carriage_return DO count = count + 1 read character END_WHILE Flow control & looping