ics312 set 8 flow control instructions. flow control, or branching, instructions for jumps, loops...

Post on 21-Dec-2015

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ICS312 SET 8

Flow Control Instructions

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) The unconditional jump instruction, branches to a new address in

the program code.    JMP NEXT        ; jumps to address of "NEXT"

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, 4C00H INT 21H

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, 4C00H INT 21H

Note: the above code simulates the effect of the LOOP instruction

Signed versus Unsigned Conditional Jumps

Note:In this course we will only make use of signed conditional jumps. Unsigned Conditional Jumps:  JA and JB Signed Conditional Jumps: JG or JL

Example 1. Will the JG instruction in the following code result in a jump? MOV AL, 80h MOV BL, 7FH CMP AL, BL JG EXIT_ ... Example 2. Will the JA instruction in the following code result in a jump? MOV AL, 80h MOV BL, 7FH CMP AL, BL JA EXIT_ ...

Examples of Flow Control Using Jump Instructions (1)

•IF-THEN-ELSE Structures

START: MOV AH, 1 INT 21H ; input AL CMP AL, 'Q' ; if AL = 'Q' JE EXIT ; then, branch to EXIT MOV AH, 2 ; else output AL MOV DL, AL INT 21H JMP START ; repeat

EXIT: MOV AX, 4C00H INT 21H

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 21H

Examples of Flow Control Using Jump Instructions (3)

•CASE Structure MOV AH, 1 INT 21H 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 >= 30H and <= 39H? •I.e. is it the code for a decimal digit?

MOV AH, 1 INT 21H CMP AL, 30H ; AL >= 30h? JL ERROR ; no, error CMP AL, 39H ; AL <= 39h? JG ERROR ; no, error

... ; continue: AL >= 30h and AL <= 39h

ERROR: ... ; code for handling an error

Examples of Flow Control Using Jump Instructions (5)

•OR Conditions Input until the character is Q or q.

START: MOV AH, 1 INT 21H 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, 4COOH INT 21H

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 21H MOV CX, 10 TOP_: JCXZ EXIT_ ; If CX = 0, exit MOV DL, AL MOV AH, 2 INT 21H INC AL LOOP TOP_ ; repeats for 10 times EXIT_: MOV AX, 4C00H INT 21H

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 21H CMP AL, 'Q' JE EXIT_ ; repeat while AL != 'Q' MOV DL, AL MOV AH, 2 INT 21H JMP start EXIT_: MOV AX, 4C00H INT 21H

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 5Bh)

MOV AH, 2 MOV DL, 'A' TOP_: INT 21H INC DL CMP DL, '[' JNE top_ ; repeat until DL = '[' EXIT_: MOV AX, 4C00H INT 21H

Loop Instruction 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 21H SUB AL, 30H MOVSX CX, AL ; sign extend into CX register MOV AH, 2 TOP_: INT 21H ; display asterisks LOOP TOP_ ; repeats CX times EXIT_: MOV AX, 4C00H INT 21H

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 For signed arithmetic J[N] {G | L | Z } [E]

eg: JG, JGE, JNZ, JLE

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 (0DH), don’t count CR. Use BL to store that number.

MOV BL, 0REPEAT: MOV AH, 1 INT 21H CMP AL, 0DH JE OUT INC BL JMP REPEATOUT: …

Example on JGE

To put the absolute value of AX into AX.

CMP AX, 0 JGE OUT ; jump if greater than or equal NEG AX

OUT: …

Example on JE & JL

If AX < 0, output – ; If AX = 0, output 0; If AX > 0, output +

CMP AX, 0 JE zerocase JL lesscase MOV DL, ‘+’ JMP display zerocase: MOV DL, 0 JMP display lesscase: MOV DL, ‘-’ display: MOV AH, 2 INT 21H

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

Assume that CX contains some number and you want toOutput this number of asterisks

JCXZ OUT place: MOV AH, 2 MOV DL, ‘*’ INT 21H LOOP place

OUT: …

Textbook Reading (Jones):

Sections 5.1-5.4

Homework: Ex 5.1, p. 109, nos. 1-4 Ex 5.3, p. 115, no. 1

top related