Professional Documents
Culture Documents
Ics312 Set 8: Flow Control Instructions
Ics312 Set 8: Flow Control Instructions
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.
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
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
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
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
MOV BL, 0
REPEAT: MOV AH, 1
INT 21H
CMP AL, 0DH
JE OUT
INC BL
JMP REPEAT
OUT: …
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
JCXZ OUT
place: MOV AH, 2
MOV DL, ‘*’
INT 21H
LOOP place
OUT: …
Textbook Reading (Jones):
Sections 5.1-5.4