ICS 312 SET 8 Flow Control Instructions Flow

  • Slides: 21
Download presentation
ICS 312 SET 8 Flow Control Instructions

ICS 312 SET 8 Flow Control Instructions

Flow Control Instructions Flow control, or branching, instructions for jumps, loops and conditional jumps

Flow Control Instructions Flow control, or branching, instructions for jumps, loops and conditional jumps allow us to control the execution of program statements and to build high-level programming structures such as if-else statements, case statements, and loops in assembler programs

Jump Instructions (1) n The unconditional jump instruction, branches to a new address in

Jump Instructions (1) n The unconditional jump instruction, branches to a new address in the program code. JMP NEXT ; jumps to address of "NEXT" n Conditional jump instructions test the condition of one or two FLAGS in the flags register, then "jump" to a new location in the program based on the result of the test. Example: TOP: SUB AX, BX ; AX = AX - BX JC EXIT: ; IF AX < BX then jump to EXIT JMP TOP ; else, repeat until AX < BX EXIT: MOV AX, 4 C 00 H INT 21 H n

Jump Instructions (2) CMP Instruction The CMP instruction sets the flags the same as

Jump Instructions (2) CMP Instruction The CMP instruction sets the flags the same as if a subtraction had been performed, but the destination operand is NOT changed. Example (simulating the LOOP instruction): MOV CX, 5 TOP_2: CMP CX, 0 ; CX = 0 ? JE EXIT_: ; IF CX = 0, then EXIT_ SUB AX, BX ; AX = AX - BX DEC CX JMP TOP_2 ; else, repeat until CX = 0 EXIT_: MOV AX, 4 C 00 H INT 21 H

Signed versus Unsigned Conditional Jumps Note: In this course we will only make use

Signed versus Unsigned Conditional Jumps Note: In this course we will only make use of signed conditional jumps. n Unsigned Conditional Jumps: JA and JB n Signed Conditional Jumps: JG or JL Example 1. Will the JG instruction in the following code result in a jump? MOV AL, 80 h MOV BL, 7 FH CMP AL, BL JG EXIT_ . . . Example 2. Will the JA instruction in the following code result in a jump? MOV AL, 80 h MOV BL, 7 FH CMP AL, BL JA EXIT_ . . .

Examples of Flow Control Using Jump Instructions (1) • IF-THEN-ELSE Structures START: MOV AH,

Examples of Flow Control Using Jump Instructions (1) • IF-THEN-ELSE Structures START: MOV AH, 1 INT 21 H ; input AL CMP AL, 'Q' ; if AL = 'Q' JE EXIT ; then, branch to EXIT MOV AH, 2 ; else output AL MOV DL, AL INT 21 H JMP START ; repeat EXIT: MOV AX, 4 C 00 H INT 21 H

Examples of Flow Control Using Jump Instructions (2) • IF-THEN-ELSE If AL ≤ BL,

Examples of Flow Control Using Jump Instructions (2) • IF-THEN-ELSE If AL ≤ BL, display BL else display AL CMP AL, BL ; AL ≤ BL? JG ELSE_ ; no, go to "else" MOV DL, BL ; display BL JMP DISPLAY ELSE_: ; else, AL > BL MOV DL, AL ; display AL DISPLAY: ; display result MOV AH, 2 INT 21 H

Examples of Flow Control Using Jump Instructions (3) • CASE Structure MOV AH, 1

Examples of Flow Control Using Jump Instructions (3) • CASE Structure MOV AH, 1 INT 21 H CMP AL, 'A' JE CASE_A CMP AL, 'B' JE CASE_B CMP AL, 'C' JE CASE_C CMP AL, 'D' JE CASE_D CASE_A: ; code for case A goes here JMP NEXT_ CASE_B: ; code for case B goes here JMP NEXT_ CASE_C: ; code for case C goes here JMP NEXT_ CASE_D: ; code for case D goes here NEXT_: ; code continues here

Examples of Flow Control Using Jump Instructions (4) • AND Conditions: Is AL >=

Examples of Flow Control Using Jump Instructions (4) • AND Conditions: Is AL >= 30 H and <= 39 H? • I. e. is it the code for a decimal digit? MOV AH, 1 INT 21 H CMP AL, 30 H ; AL >= 30 h? JL ERROR ; no, error CMP AL, 39 H ; AL <= 39 h? JG ERROR ; no, error . . . ; continue: AL >= 30 h and AL <= 39 h ERROR: . . . ; code for handling an error

Examples of Flow Control Using Jump Instructions (5) • OR Conditions Input until the

Examples of Flow Control Using Jump Instructions (5) • OR Conditions Input until the character is Q or q. START: MOV AH, 1 INT 21 H CMP AL, 'Q' JE EXIT_ ; if AL = 'Q', then exit CMP AL, 'q' JNE START ; if AL != 'Q' and AL != 'q', repeat input EXIT_: ; if AL = 'Q' or AL = 'q' MOV AX, 4 COOH INT 21 H

Examples of Flow Control Using Jump Instructions (6) • FOR Loops Input the value

Examples of Flow Control Using Jump Instructions (6) • FOR Loops Input the value of AL. For I = 1 to 10 output this value+I. START: MOV AH, 1 INT 21 H MOV CX, 10 TOP_: JCXZ EXIT_ ; If CX = 0, exit MOV DL, AL MOV AH, 2 INT 21 H INC AL LOOP TOP_ ; repeats for 10 times EXIT_: MOV AX, 4 C 00 H INT 21 H

Examples of Flow Control Using Jump Instructions (7) • WHILE Loops input Al while

Examples of Flow Control Using Jump Instructions (7) • WHILE Loops input Al while AL != ‘Q’ output AL input Al end while START: MOV AH, 1 INT 21 H CMP AL, 'Q' JE EXIT_ ; repeat while AL != 'Q' MOV DL, AL MOV AH, 2 INT 21 H JMP start EXIT_: MOV AX, 4 C 00 H INT 21 H

Examples of Flow Control Using Jump Instructions (8) • REPEAT-UNTIL Loops Set Dl to

Examples of Flow Control Using Jump Instructions (8) • REPEAT-UNTIL Loops Set Dl to ‘A’ TOP: output DL add 1 to DL repeat top until DL = ‘[‘ (ascii code 5 Bh) MOV AH, 2 MOV DL, 'A' TOP_: INT 21 H INC DL CMP DL, '[' JNE top_ ; repeat until DL = '[' EXIT_: MOV AX, 4 C 00 H INT 21 H

Loop Instruction n Used to implement "for loops" more conveniently than above. Decrements CX

Loop Instruction n Used to implement "for loops" more conveniently than above. Decrements CX register and jumps to specified label as long as CX != 0 after being decremented. Example: Input a number, and display that number of asterisks . 586 ; required for MOVSX or MOVZX instructions . CODE START: MOV DL, '*" MOV AH, 1 INT 21 H SUB AL, 30 H MOVSX CX, AL ; sign extend into CX register MOV AH, 2 TOP_: INT 21 H ; display asterisks LOOP TOP_ ; repeats CX times EXIT_: MOV AX, 4 C 00 H INT 21 H

MOVSX Instruction Move with sign extension. Copies a byte or word from a source

MOVSX Instruction Move with sign extension. Copies a byte or word from a source operand to a register, and sign extends into the upper half of the destination. This is used to copy an 8 -bit or 16 -bit operand into a larger destination.

JMP Instructions n For signed arithmetic J[N] {G | L | Z } [E]

JMP Instructions n For signed arithmetic J[N] {G | L | Z } [E] eg: JG, JGE, JNZ, JLE n For unsigned arithmetic (not used in this course) J[N] {A | B | Z } [E] eg: JA, JAE, JNZ, JBE

Example on JE Count the number of characters entered which terminate in a CR

Example on JE Count the number of characters entered which terminate in a CR (0 DH), don’t count CR. Use BL to store that number. REPEAT: OUT: MOV INT CMP JE INC JMP … BL, 0 AH, 1 21 H AL, 0 DH OUT BL REPEAT

Example on JGE To put the absolute value of AX into AX. CMP JGE

Example on JGE To put the absolute value of AX into AX. CMP JGE NEG OUT: … AX, 0 OUT ; jump if greater than or equal AX

Example on JE & JL If AX < 0, output – ; If AX

Example on JE & JL If AX < 0, output – ; If AX = 0, output 0; If AX > 0, output + zerocase: lesscase: display: CMP JE JL MOV JMP AX, 0 zerocase lesscase DL, ‘+’ display MOV JMP MOV INT DL, 0 display DL, ‘-’ AH, 2 21 H

Example on JCXZ – Jump if cx is zero, cx used as a counter

Example on JCXZ – Jump if cx is zero, cx used as a counter Assume that CX contains some number and you want to Output this number of asterisks place: OUT: JCXZ MOV INT LOOP … OUT AH, 2 DL, ‘*’ 21 H place

Textbook Reading (Jones): Sections 5. 1 -5. 4 Homework: Ex 5. 1, p. 109,

Textbook Reading (Jones): Sections 5. 1 -5. 4 Homework: Ex 5. 1, p. 109, nos. 1 -4 Ex 5. 3, p. 115, no. 1