C.O.A.L Chapter6
C.O.A.L Chapter6
1
Overview
• Jump and loop instructions transfer control to another part
of the program
• The transfer can be unconditional or can depend on a
particular combination of status flag settings.
• Study PGM 6_1.ASM on pp. 93-94
2
Conditional Jumps
• Conditional Jumps
– Conditional Jumps have the syntax:
Jxxx destination_label
– The destination-label, a symbolic name, refers to the instruction that
follows it
– If the condition for the jump is true, the next instruction to be executed
is specified in the destination label
– If the condition for the jump is false, the next sequential instruction in
your code will be executed
– The structure of the machine code requires that the destination label
must precede the jump instruction by no more than 126 bytes and
cannot follow it by more than 127 bytes. ((We will see a way to get
around this restriction later !!)
3
A note on addresses in PC Assembler Language
• There are three types of addresses in PC Assembler Language
– short, near, and far
• A short address is reached by an offset and is limited to a
distance of -126 to 127 bytes.
• A near address is reached by an offset and is limited to a
distance of -32,766 to 32,767 bytes within the same segment.
• A far address is in another segment and is reached by a
segment address and offset.
Distance Short Near Far
Instructions -126 to 127 -32,766 to Another
Same Segment 32,767 segment
Same Segment
JMP YES YES YES
Jxxx YES 386/486/Pentium NO
LOOP YES NO NO
CALL N/A YES YES
4
How the CPU Implements a Conditional Jump
• The CPU checks FLAGS register
• If the proper combination of status FLAGS are set, the
CPU adjusts the IP to point to the destination label
5
Conditional Jumps
Signed Conditional Jumps:
Op Code Description Condition for Jumps
JG or JNLE Jump if greater than ZF=O and SF=OF
Jump if not less than
or equal to
JGE or JNL Jump if greater than SF=OF
or equal to
Jump if not less than
JL or JNGE Jump if less than SF<>OF
Jump if not greater
than or equal to
JLE or JNG Jump if less than ZF=1 or SF<>OF
or equal
Jump if not greater than
6
Conditional Jumps (cont’d)
Unsigned Conditional Jumps:
Op Code Description Condition for Jumps
JA or JNBE Jump if above CF=O or ZF=O
7
Conditional Jumps (cont’d)
Single-Flag Conditional Jumps:
Op Code Description Condition for Jumps
JE or JZ Jump if equal ZF=1
Jump if zero
JNE or JNZ Jump if not equal ZF=O
Jump if not Zero
JC Jump if carry CF=1
JNC Jump if no carry CF=O
JO Jump if overflow OF=1
JNO Jump if no overflow OF=O
JS Jump if sign negative SF=1
JNS Jump if sign nonnegative SF=O
JP or JPE Jump if parity even PF=1
JNP or JPO Jump is parity odd PF=0
8
CMP Instruction
• Compares the destination field to the source field by
subtracting the source field from the destination field. The
result is not stored but the flags are affected
• The sole purpose of this instruction is to set the status flags
• Syntax: CMP destination, source
• Example 1:
AX:0020 BX:0001
CMP AX,BX
JG AX_GT_BX
9
CMP Instruction (cont’d)
• Example 2
AX:0001 BX:000A
CMP AX,BX
JE AX_EQ_BX
ZF=0 so the next sequential instruction will be executed. No jump
will be taken to AX_EQ_BX (Since AX was not equal to BX, this
is exactly what you would expect!)
• Restrictions:
– Both operands cannot be memory locations
– The destination field may not be a constant
10
Interpreting Conditional Jumps
• Programmers don't really have to think about the actual flag
settings - thank heavens !!!!
• Just choose the proper jump instruction
• Example
SUB AX,BX
JG STOP Means jump to STOP if result > 0
JGE STOP Means jump to STOP if result >=0
JL STOP Means jump to STOP if result <0
JLE STOP Means jump to STOP if result <=0
JZ STOP Means jump to STOP if result =0
JNZ STOP Means jump to STOP if result <>0
JO STOP Means jump to STOP if result caused
Signed Overflow to occur
JNO STOP Means jump to STOP if result did not
cause Signed Overflow to occur
11
Interpreting Conditional Jumps (cont’d)
• Consider using these conditional jump instructions after the
compare instruction
CMP AX,BX
JG STOP Means jump to STOP if AX > BX
JGE STOP Means jump to STOP if AX >= BX
JL STOP Means jump to STOP if AX < BX
JLE STOP Means jump to STOP if AX <= BX
JE STOP Means jump to STOP if AX = BX
JNE STOP Means jump to STOP if AX <> BX
12
Signed Conditional Jumps vs Unsigned
Conditional Jumps
• Each signed jump is analogous to an unsigned jump
JG is analogous to JA
JGE is analogous to JAE
JL is analogous to JB
JLE is analogous to JBE
13
Example on Wrong Jumps
• Suppose AX=0001 BX=FFFF
• You issue:
CMP AX,BX
JA Stop
• Control will not pass to Stop because in an unsigned sense 0001 < FFFF
• On the other hand if you issue:
CMP AX,BX
JG Stop
• Control will pass to Stop because in a signed sense, 0001>FFFF
• Notes:
– When working with standard ASCII Characters (00-7F) either signed or
unsigned jumps may be used.
– When working with extended ASCII characters (80-FF) UNSIGNED
JUMPS should be used. 14
JMP Instruction
• JMP instruction causes an unconditional transfer of control.
• Can be used to get around -126 byte / 127 byte range
restriction on a conditional jump
• Syntax: JMP Destination_label
• Destination_label is usually a label in same segment as JMP
itself
• Example 1:
JMP STOP
means GO TO STOP now!
15
Examples on JMP Instruction
• Assume you code the following and assume that the label TOP precedes the
JNZ instruction by more than 126 bytes but you didn't know that when you
coded this!
TOP:
;lots of other instructions !!
ADD AX,BX
DEC CX
JNZ TOP
MOV ANSWER,AX
• When you try to assemble the program, the assembler indicates that the label
TOP is too far away from the JNZ instruction! It is a problem!
• JMP instruction can be used to solve the problem. But how?
16
Examples (cont’d)
• Here is how:
First thought: Better code:
TOP: TOP:
ADD AX,BX ADD AX,BX
DEC CX DEC CX
JNZ CONTINUE JZ GO_ON
JMP GO_ON
CONTINUE:
JMP TOP JMP TOP
GO_ON: GO_ON:
MOV ANSWER,AX MOV ANSWER,AX
• Note: you don’t have to worry about how far away an instruction is when
you need to use a conditional jump instruction. If the Assembler flags your
conditional jump instruction as an error then you should go back and use the
JMP instruction instead, as described in the above example. 17
High-Level Language Decision Structures
• IF .. THEN structure
IF condition is true
THEN execute true statements
END_IF
• Consider:
IF AX > 0
Then ADD 1 to BX
END_IF
• In PC-ALC we would write:
; If AX>0
CMP AX,0
JLE END_IF ;Jump on reverse condition
; Then add 1 to BX
INC BX
; Endif
END_IF: 18
IF ... THEN ... ELSE Structure
• Consider: IF AX >= 0
THEN Add 1 to BX
ELSE Add 1 to CX
END_IF
• In PC-ALC we would write
; IF AX >= 0
CMP AX,0
JL ELSE_ ;Jump on reverse
; then add 1 to BX
INC BX
JMP CONT ;Doesn't happen automatically
; else add 1 to CX
ELSE_:
INC CX
; endif
CONT:
19
NOTES
• You can't use the label ELSE: by itself because ELSE is a
RESERVED WORD
• Other reserved words you can not use for labels are:
END LOOP
ENDIT ELSE
EVEN ENDIF
IF LABEL
IF1
IF2
20
CASE Structure
• CASE structure is a multi-way branch instruction:
CASE expression
Values_1: Statements to perform if
expression = values_1
Values_2: Statements to perform if
expression = values_2
.
.
.
Values_N: Statements to perform if
expression = values N
END_CASE
21
Conversion of Case Structure
• Consider:
CASE AX
1 or 2: Add 1 to BX
3 or 4: Add 1 to CX
End_CASE
22
Conversion of Case Structure (cont’d)
• FIRST THOUGHT BETTER CODE
;CASE AX ; CASE AX
;1 or 2 ; 1 or 2
CMP AX,1 CMP AX,1
JE ADD_BX JE ADD_BX
CMP AX,2 CMP AX,2
JE ADD_BX JE ADD_BX
;3 or 4 ; 3 or 4
CMP AX,3 CMP AX,3
JE ADD_CX JE ADD_CX
CMP AX,4 CMP AX,4
JE ADD_CX JNE END_CASE_AX
JMP END_CASE_AX ADD_CX:
ADD_BX: INC CX
INC BX JMP END_CASE_AX
END_CASE_AX ADD_BX:
ADD_CX: INC BX
INC CX END_CASE_AX:
END_CASE_AX: 23
Branches with Compound Conditions
• AND conditions (TRUE IFF both conditions are true)
• How do you implement:
IF AX=1 AND BX=1
THEN ADD 1 TO CX
END_IF
• In PC ALC we could code:
;IF AX=1 and BX=1
CMP AX,1
JNE END_IF
CMP BX,1
JNE END_IF
;Then add 1 to CX
INC CX
END_IF:
24
Branches with Comp. Conditions (cont’d)
• OR conditions (True if either condition is true, false IFF both conditions are
false)
• How do you implement:
IF AX=1 or BX=1
Then Add 1 to CX
END_IF
• In PC ALC we could code:
; IF AX=1 or BX=1
CMP AX,1
JE ADD_CX
CMP BX,1
JNE END_IF
ADD_CX:
INC CX
END_IF:
25
Looping Structures
• FOR loop
– A looping structure in which the loop statements are executed a known
number of times
FOR loop_count times DO
statements
END_FOR
• Refer figure 6.5 in the text (page 105)
• The FOR loop is a trailing decision loop. The condition is checked at the
bottom of the loop so a FOR loop will always be executed at least once
• To implement a FOR loop in PC -ALC
; Initialize CX TO Loop-count
TOP:
; code the statements in the body of the loop
LOOP TOP
26
LOOP Instruction
• The LOOP instruction can be used to implement a FOR loop
• SYNTAX:
LOOP destination _label
27
LOOPE and LOOPZ Instructions
• The LOOPE and the LOOPZ instructions are similar to the
LOOP instruction except that they allow you to execute a loop
and terminate it under not one but two different conditions
• Each instruction automatically subtracts 1 from CX
• LOOPE and LOOPZ both cause a branch to the destination_label
if CX <> 0 and ZF = 1.
• If either CX = 0 or ZF = 0, the instruction immediately following
the loop is executed
• See example 7-a p. 115 in Yu's book
• SYNTAX: LOOPE destination_label or
LOOPZ destination-label
28
LOOPNE and LOOPNZ Instructions
• Also similar to LOOP instruction except that they also
allow you to execute a loop and terminate it under not one
but two different conditions
• Each instruction automatically subtracts 1 from CX
• LOOPNE and LOOPNZ both cause a branch to the
destination_label if CX <> 0 and ZF = 0. If either CX = 0
or ZF = 1, the instruction immediately following the loop
is executed.
• See example 7-b p. 115 in Yu's book
• SYNTAX:
LOOPNE destination_label or
LOOPNZ destination-label
29
Problem 7-a on page 115 in Yu’s Book
• Write instructions to read characters until
either a non-blank character is typed or 80
characters have been typed. Use LOOPE.
MOV CX,80
MOV AH,1
TOP_LOOP:
INT 21H
CMP AL,' '
LOOPE TOP_LOOP
30
Problem 7-b on page 115 in Yu’s Book
• Write instructions to read characters until either a carriage
return is typed or 80 characters have been typed. Use
LOOPNE.
MOV CX,80
MOV AH,1
TOP_LOOP:
INT 21H
CMP AL,0Dh
LOOPNE TOP_LOOP
31
JCXZ Instruction
• This instruction transfers control to a designated label if the
contents of register CX = 0.
• It can be used to prevent a "FOR LOOP" from being executed
if the CX register is zero.
• Code it as the instruction immediately preceding your FOR
loop.
32
While Loop
• A looping structure whose loop statements are executed as long
as a specific condition is true
• The while loop is a leading decision loop. The condition is
checked at the top of the loop, so it may not be executed at all.
• To implement in PC-ALC:
WHILE:
CMP ___,___ ; set up the condition check
J ___ END_WHILE ;use the reverse condition
; write all the statements
; to execute if the condition
; is TRUE
JMP WHILE
END_WHILE: 33
REPEAT Loop
• A looping structure whose loop statements are executed until
specific condition becomes true
• REPEAT loop is a tailing decision loop
• The condition is checked at the bottom of the loop so a
REPEAT loop will always be executed at least once
• To implement in PC-ALC:
REPEAT:
; code the statements in the body of
; the loop
CMP _____,______
J___ REPEAT ; check for reverse condition
34
Examples: FOR, WHILE, and REPEAT Loops
• Write the code to sum the first 100 integers.
FOR loop: SUM = 0
For I = 1 to 100 DO
SUM = SUM + I
End of For
35
Examples (cont’d)
WHILE Loop SUM = 0
I= 1
While I <= 100 DO
SUM = SUM + I
I=I+1
End of While
1. IF CX >= BX
THEN MOVE 1 TO AX
END-IF.
2. IF BX = DX
THEN ADD 1 TO AX
SUB 10 FROM DX
ELSE DISPLAY A '?' ON THE SCREEN
END-IF.
3. CASE DL
'A': READ ANOTHER CHARACTER FROM THE KEYBOARD
'B': DISPLAY 'b' ON THE SCREEN
'?': DISPLAY THE '?' ON THE SCREEN
END-CASE. 38
Homework (cont’d)
4. IF CX = 0 AND BX = 0
THEN ADD 1 TO AX
ELSE SUBTRACT 1 FROM AX
END-IF.
5. IF AH = 1 OR BH = 3
THEN READ A CHARACTER FROM THE KEYBOARD
ELSE IF AH = 2
THEN DISPLAY A LINE FEED AND AS CARRIAGE RETURN ON
THE
SCREEN BEFORE YOU DISPLAY A '?'
ELSE DISPLAY A '.' ON THE SCREEN
END-IF.
6. MOVE 0 TO AX
FOR I = 10 TO 1 IN STEPS OF -1 DO:
AX = AX + I
END-FOR.
39
Homework (cont’d)
7. MOVE 0 TO AX
MOVE 1 TO DX
WHILE DX < 300 DO
AX = AX + DX
ADD 3 TO DX
END-WHILE.
8. MOVE 0 TO AX
MOVE 1 TO DX
REPEAT
AX = AX + DX
ADD 3 TO DX
UNTIL DX >= 300
END-REPEAT.
40
Answers
1. Method 1
CMP CX,BX
JL END_IF
MOV AX,1
END_IF:
Method 2:
CMP CX,BX
JNGE END_IF
MOV AX,1
END_IF:
41
Answers (cont’d)
2. CMP BX,DX
JNE ELSE_
ADD AX,1
SUB DX,10
JMP END_IF
ELSE_:
MOV AH,2
MOV DL,'?'
INT 21h
END_IF:
42
Answers (cont’d)
3. CMP DL,'A'
JE READ_CHAR
CMP DL,'B'
JE DISPLAY_b
CMP DL,'?'
JNE END_CASE
MOV AH,2
INT 21h
JMP END_CASE
READ_CHAR:
MOV AH,1
INT 21h
JMP END_CASE
DISPLAY_b:
MOV AH,2
ADD DL,20h ;CHANGE B(42) TO b(62)
INT 21h
END_CASE:
43
Answers (cont’d)
4. CMP CX,0
JNE ELSE_
CMP BX,0
JNE ELSE_
ADD AX,1 ;CX=0 and BX=0
JMP END_IF
ELSE_:
SUB AX,1
END_IF:
44
Answers (cont’d)
5. CMP AH,1
JE THEN_1
CMP BH,3
JNE ELSE_1
THEN_1:
MOV AH,1
INT 21h
JMP END_IF
ELSE_1:
CMP AH,2
JNE ELSE_2
THEN_2:
MOV DL,0Dh
INT 21h ;AH already has a 2 in it
MOV DL,0Ah
INT 21h
MOV DL,'?'
INT 21h
JMP END_IF
ELSE_2:
MOV DL,'.'
MOV AH,2
INT 21h
END_IF:
45
Answers (cont’d)
6. MOV AX,0
MOV CX,10
FOR_LOOP:
ADD AX,CX
LOOP FOR_LOOP
7. MOV AX,0
MOV DX,1
WHIL_LOOP:
CMP DX,300
JGE END_WHILE
ADD AX,DX
ADD DX,3
JMP WHIL_LOOP
END_WHILE:
46
Answers (cont’d)
8. MOV AX,0
MOV DX,1
REPEAT:
ADD AX,DX
ADD DX,3
CMP DX,300
JL REPEAT
END_REPEAT:
47