Lecture 4 Jump Loop instructions Chapter Outline Jump

  • Slides: 18
Download presentation
Lecture 4 Jump & Loop instructions

Lecture 4 Jump & Loop instructions

Chapter Outline ØJump Instruction Ø IF-THEN-ELSE ØLoop instruction Ø For Loop Ø While Loop

Chapter Outline ØJump Instruction Ø IF-THEN-ELSE ØLoop instruction Ø For Loop Ø While Loop Ø REPEAT Loop

IF-THEN-ELSE • Example: Suppose AL and BL contain extended ASCII characters. Display the one

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

IF-THEN-ELSE It can be coded as follows: ; if AL <= BL CMP AL,

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 ; AL <= BL? ; no, display char in BL ; AL <= BL ; move char to be displayed ; go to display ; BL < AL ; prepare to display ; display it

Branches with compound Conditions • Sometimes the branching condition in an IF or CASE

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.

AND Condition • An AND condition is true if and only if all conditions

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

AND Condition It can be coded as follows: ; read a character MOV AH,

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:

OR Condition • An OR condition is true if at least one of the

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

OR Condition It can be coded as follows: ; read a character MOV AH,

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: ; DOS exit

Loop Instruction • The LOOP instruction can be used to implement a for loop.

Loop Instruction • The LOOP instruction can be used to implement a for loop. • Syntax: LOOP SHORT address • 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.

Loop Instruction • Using the instruction LOOP, a FOR loop can be implemented as

Loop Instruction • Using the instruction LOOP, a FOR loop can be implemented as follows: TOP: ; initialize CX to loop_count ; body of the loop LOOP TOP • In for loop the loop statements are repeated a known number of times.

FOR Loop • Example: Write some code to display a row of 80 stars.

FOR Loop • Example: Write some code to display a row of 80 stars. • Solution: Pseudocode: ; what if CX =0? FOR 80 times DO display '*' MOV CX, 80 END_IF MOV AH, 2 It can be coded as follows: MOV DL, '*' MOV CX, 80 JCXZ SKIP ; jump if CX=0 MOV AH, 2 TOP: MOV DL, '*' INT 21 h TOP: LOOP TOP INT 21 h SKIP: LOOP TOP

WHILE Loop • This loop depends on a condition. • The condition is checked

WHILE Loop • This loop depends on a condition. • The condition is checked at the top; if true the statements are executed; if false program goes into whatever follow. • Pseudocode: WHILE condition DO statements END_WHILE

WHILE Loop • Example: Write some code to count the number of characters in

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 continue

WHILE Loop It can be coded as follows: MOV INT DX, 0 AH, 1

WHILE Loop It can be coded as follows: MOV INT DX, 0 AH, 1 21 h ; DX counts characters ; prepare to read ; character in AL CMP JE INC INT JMP AL, 0 Dh END_WHILE DX 21 h WHILE_ ; CR? ; yes, exit ; not CR, increment count ; read a character ; loop back WHILE_: END_WHILE:

REPEAT Loop • This loop depends on a condition. • the statements are executed

REPEAT Loop • This loop depends on a condition. • the statements are executed then the condition is checked. If true the loop terminates; if false, control branches to the top of the loop. • Pseudocode: REPEAT Statements UNTIL conditions

REPEAT Loop • Example: write code to read characters until a blank is read

REPEAT Loop • Example: write code to read characters until a blank is read • Pseudocode: REPEAT Read character UNTIL character is blank The code is: REAPEAT: MOV AH, 1 INT 21 H CMP AL, ’ ‘ JNE REAPEAT

WHILE Versus REPEAT • Use of a WHILE loop or a REPEAT loop is

WHILE Versus REPEAT • Use of a WHILE loop or a REPEAT loop is a matter of personal preference. • A WHILE loop can be bypasses if the terminating condition is initially false. (a REPEAT loop must be done at least once) • The code for a REPEAT loop is likely to be a little shorter because there is only one jump. (WHILE loops has two jumps)