Introduction
Program control instruction :
direct the flow of a program, allow the flow to change
jumps, calls, returns, interrupts, machine control
instructions
Change in flow :
CMP, TEST followed by conditional jump
Relational assembly language statements :
.IF, .ELSE, .ELSEIF, .WHILE, .ENDW, .REPEAT,
.UNTIL
MASM, TASM Ver.6X ~
allow to develop control flow portions of program with
C/C++ language efficiency
Program Control Instructions 2
The Jump Group
JMP(jump) : allow to skip sections of a program and
blanch to any part of memory for next instruction
unconditional jump, conditional jump
three type unconditional jump : Fig. 6-1
3
Unconditional Jump(JMP)
Intrasegment jump : short, near jump
Short jump(2-byte): 1 byte disp.(within +127~-128 byte)
Near jump(3-byte) : 2 byte disp.(within 32K bytes or
anywhere in current code segment)
Segments : cyclic in nature
Intersegment, far jump(5-byte) :
any memory location within the real memory system
80386~ (in protected mode)
Near(5-byte) : 4 byte displacement(within 2G bytes)
Far(7-byte) : 4 byte(EIP), 2 byte(CS)
Program Control Instructions 4
Short Jump
Short jump : relative jump
distance or displacement : follow the opcode
One-byte signed number(+127~-128) :
sign-extended and added to IP/EIP
to generate the jump address within current code segment
EX. 6-1 :
Label : symbolic name for memory address
SHORT directive : force a short jump
most assembler : choose best form of jump instruction
JMP START : assemble as a short jump
Program Control Instructions 5
Short Jump
1st jump : 0020H – 0009H = 0017(disp. = 17H)
2nd jump : 0002H – 0024H = FFDEH(disp. = DEH)
Ch.6 Program Control
Instructions 6
Fig. 6-2
Fig. 6-2
Program Control Instructions 7
Near, Far Jump
Near jump : relocatable because relative jump
signed displacement : added to IP/EIP to generate
the jump address
2 byte : 32K bytes in current code segment
4-byte(386~ in protected mode) : 2G bytes
Far jump : 5(7, 80386~) byte instruction
new offset address(IP/EIP) : byte 2,3(2~5)
new segment address(CS) : byte 4,5(6,7)
80286~ in protected mode : CS access a descriptor
that contain base address of far jump segment
Program Control Instructions 8
Fig. 6-3
Fig. 6-3
Program Control Instructions 9
Example: Near Jump
E9 0200 R JMP NEXT : only list file
R : denote a relocatable jump address of 0200H
actual machine code : E9 F6 01
0200H - 000AH = 01F6H
Program Control Instructions 10
Fig. 6-4
Fig. 6-4
Program Control Instructions 11
Example
Far jump : FAR PTR directive, far label
Far label : external to current code segment
EXTRN UP:FAR directive
a global label as a double colon(LABEL::)
----E : external. filled in by linker when links program files
12
Indirect Jump
Jump with 16-, 32-bit reg. operand : indirect jump
contents of reg. : transferred directly into IP/EIP
JMP AX : IP ← AX, JMP EAX : EIP ← EAX
EX. 6-4 : how JMP AX access jump table
read a key, converted ASCII to binary, doubled
jump table : 16-bit offset address
Indirect Jumps using Index : double-indirect jump
[ ] form of addressing to directly access jump table
near jump JMP TABLE[SI] : IP ← [SI+TABLE]
far jump JMP FAR PTR [SI], JMP TABLE [SI] with
TABLE data defined DD directive
Program Control Instructions 13
EX. 6-4
EX. 6-4
14
EX. 6-5
EX. 6-5
15
Conditional Jumps
Conditional jump : short jump
~ 80286(short jump) : +127 ~ -128
80386 ~(short, near jump) : 1, 4 bytes
Test one flag bit or some more : S, Z, C, P, O
if condition under test is true : branch to the label
if condition is false : next sequential instruction
Relative magnitude comparisons :
require more complicated conditional jump instructions
that test more than one flag bit
Table 6-1 : conditional jump instructions
Program Control Instructions 16
Table 6-1
Table 6-1
17
Fig. 6-5 : order of signed, unsigned 8-bit no.s
Program Control Instructions 18
Conditional Jumps
Unsigned : FFH is above 00H, above, below, equal
Signed : FFH less than 00H, greater, less, zero
Alternate form :
JE = JZ
JA(if above) = JNBE(if not below or equal)
JCXZ(jump if CX = 0), JECXZ(jump if ECX=0)
if CX/ECX = 0 : jump occur
if CX/ECX <> 0 : no jump occur
EX. 6-6 : search table for 0AH using SANSB, JCXZ
Program Control Instructions 19
Example: Conditional Jump
EX. 6-6
Program Control Instructions 20
Conditional Set Instructions
Conditional set instructions :
80386~
set a byte to either a 01H or clear a byte to 00H
useful where a condition must be tested at a point much
later in the program
SETNC MEM :
places a 01H into memory location MEM if carry is
cleared and
a 00H into MEM if carry is set
Table 6-2 :
Program Control Instructions 21
Table 6-2
Table 6-2
22
LOOP, Conditional LOOP
LOOP : combination of decrement CX and JNZ
~ 80286 : DEC CX ; if CX <> 0, jump to label if
CX = 0, execute next sequential instruction
80386 ~ : CX/ECX depending on instruction mode
LOOPE(loop while equal, LOOPZ) :
jump if CX <> 0 while equal condition exist
exit the loop if CX = 0 or condition is not equal
LOOPNE(loop while not equal, LOOPNZ) :
jump if CX <> 0 while not-equal condition exist
exit the loop if CX = 0 or condition is equal
LOOPEW/LOOPED,LOOPNEW/LOOPNED:override mode
Program Control Instructions 23
EX. 6-7
EX. 6-7 :
24
Controlling the Flow of an Assembly
Language Program
Relational statements
.IF, .ELSE, .ELSEIF, ENDIF, .REPEAT-
.UNTIL, .WHILE-.ENDW :
easier to control the flow than conditional jump
EX. 6-8 : testing system for version of DOS
DOS INT 21H, function no. 30H : read DOS ver.
(a) : source program, (b) fully expended assembled
* : assembler-generated and -inserted statements
&& : logical AND
Table 6-3 : relational operator
Program Control Instructions 25
Table of Operators and their Functions
Table 6-3
Program Control Instructions 26
Example
EX. 6-10 : read a key, convert to hexadecimal
`a`(61H), `A`(41H) : 61H(41H)-57H(37H)=0AH
27
DO-WHILE Loops
.WHILE statement : used with a condition to
begin the loop
EX. 6-11 : read a key, store into array called BUF
until enter key(0DH) is typed
DOS 21H, fn no. 09H
Program Control Instructions 28
EX. 6-11
EX. 6-11
29
REPEAT-UNTIL Loops
.REPEAT : defined start of loop
.UNTIL : defined end of loop, contained condition
EX. 6-14 : EX. 6-11,12
Program Control Instructions 30
EX. 6-14
EX. 6-14
31
Questions
Q1: Contrast the operation of JMP DI with JMP [DI].
Q2: What is the purpose of .BREAK directive?
Q3: Explain how the LOOPE instruction operates.
Q4: What happens if the .WHILE instruction is placed in a
program?
Q5:When does JCXZ instruction jump?
Q6: Write a program that reads the keyboard and converts all
lowercase data to uppercase before displaying it.
Q7: Develop a short sequence of instruction that uses
DO-WHILE Loop
REPEAT-UNTIL Loop
Program Control Instructions 32