Flow Control Instructions
Flow Control Instructions
By
Sakifa Aktar
Introduction
• The jump and loop instructions transfer control to another part of the
program
• This transfer can be unconditional or can depend on a particular
combination o status flag settings
• We'll use them to implement high-level language decision and looping
structures
• A program to display the
entire- IBM character set:
• There are 256 characters in the
IBM character set
• Those with codes 32 to 127 are
the standard ASCII display
characters
• IBM also provides a set of
graphics characters with codes
0 to 31 and 128 to 255
• Labels (PRINT_LOOP) are needed
in situations where one instruction
Conditional Jumps
• If the condition for the jump is true, the next instruction to be executed is
the one at destination_label, which may precede or follow the jump
instruction itself
• If the condition is false, the instruction Immediately following the jump is
done next
• To implement a conditional jump, the CPU looks at the FLAGS register
• If the condition for the jump are true, the CPU adjusts the IP to point to
the destination label. so that the instruction at this label will be done next
• If the jump condition is false, then IP is not altered; this means that the
next instruction in line will be done.
Categories of Conditional Jumps
• The signed jumps are used when a signed interpretation is being
given to results
• The unsigned jumps are used for an unsigned interpretation
• The single-flag jumps, which operate on settings/of individual flags
The CMP Instruction
• The jump condition is often provided by the CMP (compare)
instruction:
CMP destination, source
• This instruction compares destination and source by computing
destination contents minus source contents
• The result is not stored, but the flags are affected
• Note: CMP is just like sub. except that destination is not changed
Signed Conditional Jumps
Unsigned Conditional Jumps
Single-Flag Jumps
Signed Versus Unsigned Jumps
• Each of the signed jumps corresponds to an analogous unsigned jump; for example,
the signed jump JG and the unsigned jump JA
• The signed jumps operate on ZF, SF, and OF, while the unsigned jumps operate on ZF
and CF.
• Using the wrong kind of jump can lead to incorrect results
• For example, suppose we're giving a signed interpretation. If AX = 7FFFh, BX =8000h,
and we execute
CMP AX,BX
JA BELOW
then even though 7FFFh > 8000h in a signed sense:, the program does not jump
to BELOW. The reason is that 7FFFh < 8000h in an unsigned sense, and we are
using the unsigned jump JA
Ex: 6.1
• Suppose AX and BX contain signed numbers. Write some code to put
the biggest one in CX:
The JMP Instruction
• The JMP (jump) instruction causes an unconditional transfer of
control (unconditional jump):
JMP destination
where destination is usually a label in the same segment as the JMP
itself
Ex: 6.2: IF-THEN Structure
• Replace the number in AX by its absolute value:
Ex: 6.3: IF-THEN-ELSE Structure
• Suppose AL and BL contain extended ASCII characters. Display the one
that comes first in the character sequence:
Ex: 6.4: CASE
• If AX contains a negative number, put -1 In BX; if AX contains 0, put 0
In BX; if AX contains a positive number, put 1 In BX:
Ex: 6.5
• If AL contains 1 or 3, display "o"; if AL contains 2 or 4, display "e“:
Ex: 6.6: Branches with Compound
Conditions (AND)
• Read a character, and if it's an uppercase letter, display it:
Ex: 6.7: Branches with Compound
Conditions (OR)
• Read a character. If it's "y" or "Y", display it; otherwise, terminate the
program
Looping Structure
• The LOOP instruction can be used to implement a FOR loop. It has
the form:
LOOP destination_label
• The counter for the loop is the register CX which is initialized to
loop_count
• Execution of the LOOP Instruction causes CX to be decremented
automatically and if CX Is not 0, control transfers to destination_label
• If CX = 0, the next instruction after LOOP is done
Ex: 6.8
• Write a count-controlled loop to display a row of 80 stars
Ex: 6.9
• Write some code to count the number of characters in an input line:
Ex: 6.10
• Write some code to read characters until a blank is read: