You are on page 1of 13

PROGRAM CONTROLS

5.0 Outcomes
At the end of this chapter, you should be able to:

1. Describe the three programming constructs

2. Write assembly programs that performs selection

3. Write assembly programs that performs Iteration

5.1 Flowchart
Flowchart is a graphical representation of the program logic. It is one of the oldest methods of
program design. Flowchart uses graphical shapes to represents different actions that the
computer will perform. Arrows that indicate the flow of control connect these shapes. Figure
5.1 shows the common flowchart symbols.

PROCESS INPUT/
DECISION START/END
OUTPUT

DIRECTION
PROCEDURE OF
FLOW
CONNECTOR OFF-PAGE CONNECTOR
Figure 5.1 Common Flowchart Symbols

5.2 Program Constructs


Any computer program can be written using only few constructs (building blocks). These
constructs are:
1. Sequence
2. Selection
3. Iteration
5.2.1 Sequence
The simplest construct is the sequence. In a sequence, all the statements are executed one
after another in the same order they are found in the program. Figure 5.2 shows a flowchart for
sequence of two statements. Statement 1 (Deduction = EPF + Income Tax) will be executed
first then statement 2 (Net = Gross – Deduction) will be executed following the same order in
which they appear in the program.

S
Deduction = EPF+ Income Tax
STATEMENT 1

STATEMENT 2 Net = Gross - Deduction

Figure 5.2 A Sequence of Two Instructions

5.2.2 Selection
There are two constructs that perform selection:
• The IF construct
• The CASE construct

5.2.2.1 The IF Construct


This construct has two forms: IF <condition> THEN <true action> ELSE <false action> and IF
<condition> THEN <true action>. The condition is evaluated. If the condition evaluates to a
logical True then the <true action> is executed and the <false action> is skipped. However, if
the condition evaluates to logical False then the <false action> is executed and the <true
action> is skipped as shown in Figure 5.3a. However, for the IF <condition> THEN <true
action> as shown in Figure 5.3b, no action is performed when the condition evaluate to a false.
False False
True Decision
Decision

True
True Action False Action
True Action

a) IF….THEN….ELSE b) IF….THEN

Figure 5.3 The IF Construct

5.2.2.2 The Case Construct


The case construct can be thought of as a group of nested IF constructs. In this construct,
several conditions are evaluated for the same variable. If condition1 evaluates to true then
action1 is executed and all other actions are skipped. If, however, condition2 is true then
action2 is executed and all other actions are skipped. This process continues until all conditions
are evaluated. If no condition evaluates to true then the false action (if there is one) is
executed. Like the IF construct, only one action or no action in the case construct will be
executed as shown in Figure 5.4
True Action
Case 1 1

Case 2 True Action


2

Case 3 True Action


3

True Action
Case 4
4

Default
Action

Figure 5.4 The Case Construct


5.2.3 Iteration
There are basically two types of Iterations: the While Loop and the Repeat until loop

5.2.3.1 The While Loop


A condition is evaluated at the beginning of the loop. If the condition is true, all the statements
in the loop are executed and the condition is evaluated again. These processes continue until
the condition evaluates to false then the loop will terminate as shown in Figure 5.5.

5.2.3.2 The Repeat-Until Loop


All the statements in this loop are executed before the condition placed at the end of the loop is

evaluated. If it evaluates to true the loop is terminated. However, if it evaluates to false, the

statements are executed again until the condition is true as shown in Figure 5.6.

Body of the loop


False
Decision

True True
Decision

Body of the loop

False

Figure 5.5 While Loop Figure 5.6 Repeat until Loop

5.3 Implementing Program Constructs in Assembly Language


Unlike high level language, there are no single instructions that can be used to implement

selection or iteration. However, a combination of instructions can be used to implement them.

5.3.1 The Jump Commands


• The jump commands can be used to control of the flow of statements execution within a
program
• There are two main groups of jump commands: Conditional and Unconditional jumps

5.3.1.1 The Unconditional Jump


• The general syntax of the unconditional jump is: JMP [short/near/far] label
• The distance between the JMP instruction and the label can be: Short, Near and Far jumps
• For short jump the label must be within +127 or -128 bytes from the JMP label instruction

• For near jump the label must be within 32 Kbytes from the JMP label instruction
• For far jump the label can be anywhere in the real memory. The far jump can be obtained
by using the FAR PTR directive or by defining the label as an external label using the EXTRN
directive

5.3.1.2 Conditional Jumps


• Conditional jumps are either short or near jumps
• The Carry (C), Sign (S), Zero (0), Parity (P), and Overflow (O) flags are tested to see if they
are set (equal 1) or reset (equal 0)
• There are conditional jumps commands for unsigned and signed numbers as shown in Table
5.1 and 5.2 respectively
• Since TASM, and assembly language in general, do not enforce type checking, it is the job
of the programmer to select the appropriate conditional jump command.

Table 5. 1 Unsigned Conditional Transfers

Mnemonic Condition Tested "Jump If..."

JA/JNBE (CF or ZF) = 0 Above/not below nor equal


JAE/JNB CF = 0 Above or equal/not below
JB/JNAE CF = 1 Below/not above nor equal
JBE/JNA (CF or ZF) = 1 Below or equal/not above
JC CF = 1 Carry
JE/JZ ZF = 1 Equal/zero
JNC CF = 0 Not carry
JNE/JNZ ZF = 0 Not equal/not zero
JNP/JPO PF = 0 Not parity/parity odd
JP/JPE PF = 1 Parity/parity even

Table 5. 2 Signed Conditional Transfers

Mnemonic Condition Tested "Jump If..."


JG/JNLE ((SF xor OF) or ZF) = 0 Greater/not less nor equal
JGE/JNL (SF xor OF) = 0 Greater or equal/not less
JL/JNGE (SF xor OF) = 1 Less/not greater nor equal
JLE/JNG ((SF xor OF) or ZF) = 1 Less or equal/not greater
JNO OF = 0 Not overflow
JNS SF = 0 Not sign (positive, including 0)
JO OF = 1 Overflow
JS SF = 1 Sign (negative)
Since the conditional jump commands depend on the state of the flags, the question is how to
set or reset the values of these flags to make the jump instruction transfers control to the
specified location within the program so that we can make selection and iteration? There are
several ways of setting the values of these registers. The most commonly used command for
performing program control is the CMP (compare) command.

5.3.2 The CMP Command


The general syntax of this command is: CMP destination, source
Where: destination can be a register or a memory variable. Source can be a register, a memory
variable, or an immediate value.

1. The CMP instruction compares two binary data items by subtracting the source from the
destination.
2. Neither the value of the source nor the destination is changed by this command. Only
the status of the flags will be changed.
3. This command is normally used before a conditional jump to check whether a condition
is true or false
5.3.3 Selection
The program vote.asm shown in Figure 5.7 shows how to use the compare (CMP) command

and the jump instructions to perform a selection. The flowchart of the program is shown in

Figure 5.8. As you can see from Figure 5.8, the condition stated (is age >=21) this is

accomplished by CMP AX, ‘21’ and JAE Yes instructions. Then the true action (Display yes

message) is accomplished partially by mov dx,offset yesmessage instruction. The false

action is partially accomplished by mov dx,offset nomessage and jmp dispmessage

instructions. The jmp dispmessage instruction is required to jump over the true action.
; vote.asm
.MODEL SMALL
.STACK 200h
.DATA
Prompt db 13,10,"Type your age please (0 to 99) $"
age db 3, 4 dup(?)
yesmessage db 10,13, "Congratulation, you are eligible to vote $"
nomessage db 10,13, "Sorry, you are under age $"
.CODE
START:
;display prompt
mov ax, seg prompt ;
mov ds,ax
mov ah,9
mov dx,offset Prompt
int 21h ;print a message
;input the age
mov ah,0ah
mov dx,offset age
int 21h ;get the age
;move age to ax
mov bx, offset age
mov ah,[bx+2] ;MSB in ah
mov al,[bx+3] ;LSB in al
; check if age is more than 21
cmp ax,'21' ; is age > 21
jae yes ;yes display yesmessage
mov dx,offset nomessage ; no display nomessage
jmp dispmessage
yes:
mov dx,offset yesmessage
dispmessage:
mov ah,9 ; display message
int 21h
;return to dos
mov ax,4c00h ;Returns control to DOS
int 21h ;
END START

Figure 5.7 The Source Code for the Vote.asm Program

5.3.3 Iteration
There are two ways of making iterations in Assembly language. The first method is by using the
CMP and JMPs instructions and the second method is by using the Loop instructions.

5.3.3.1 Using CMP and JMP Commands


The CMP and JMPs commands can be used to make all types of loops. The program
ASCII.asm shown in Figure 5.9 shows how to use the Compare (CMP) and the conditional
jump statements to perform iteration. The program makes a loop that repeats itself 256 times
to print out the entire ASCII code. The flowchart of the program is shown in Figure 5.10. This
loop is called For … next loop in high level language and is not supported by all programming
language. It is a special case of the repeat until loop where the number of times the loop is
executed is known in advance

Start

Display “Enter your

age”

Input Age

No
Is Age > Display “Sorry”
21?

Yes

Display
“Congratulation”

End

Figure 5.8 Flowchart of the Vote.asm Program

5.3.3.1 Using the Loop Commands


The loop instructions are conditional jumps that use a value placed in the ECX register to
specify the number of repetitions of the loop. All loop instructions automatically decrement ECX
and terminate the loop when ECX=0. Four of the five loop instructions specify a condition
involving ZF that terminates the loop before ECX reaches zero. The general syntax is:
Loop/Loope/Loopz/Loopne/Loopnz label
Table 5. 3 The Loop Commands

Instruction Termination Description


Condition(s)
Loop ECX = 0 Repeat all the instructions between the Loop Label
command and the Label until ECX = 0
Loope/Loopz ECX=0 or ZF=0 Like the loop command but the loop will terminate when
either ECX = 0 or ZF = 0
Loope/Loopz ECX=0 or ZF=1 Like the loop command but the loop will terminate when
either ECX = 0 or ZF = 1

The program ASCII2.asm shown in Figure 5.11 shows how to use the Loop command to

perform iteration. The program makes a loop that repeats itself 256 times to print out the entire

ASCII code in reverse order. The flowchart of the program is shown in Figure 5.12.

;ascii.asm
.MODEL SMALL
.STACK 200h
.CODE
START:
mov cx,0 ; first number in the ASCII table
mov ah,6 ; DOS function to display a single character
begin:
mov dl,cl ; ascii code of first character
int 21h ; display it
cmp cx,255
jae done
inc cx
jmp begin
done:
.exit
END START

Figure 5.9. The Source Code for the ASCII.asm Program

5.3.3.2 Executing a Loop Zero Times

JCXZ (Jump if ECX Zero) branches to the label specified in the instruction if it finds a value of
zero in ECX. JCXZ is useful in combination with the LOOP instruction and with the string scan
and compare instructions, all of which decrement ECX. Sometimes, it is desirable to design a
loop that executes zero times if the count variable in ECX is initialised to zero. Because the
LOOP instructions (and repeat prefixes) decrement ECX before they test it, a loop will execute
2^(32) times if the program enters the loop with a zero value in ECX. A programmer may
conveniently overcome this problem with JCXZ, which enables the program to branch around
the code within the loop if ECX is zero when JCXZ executes. When used with repeated string
scan and compare instructions, JCXZ can determine whether the repetitions terminated due to
zero in ECX or due to satisfaction of the scan or compare conditions.
Syntax: JCXZ Label

5.4 Review Questions

Start

;ascii2.asm
No =
.MODEL 0
SMALL
.STACK 200h
.CODE
START:
Display Char(No)
mov cx,255 ; last number in the ASCII table
mov ah,6 ; DOS function to display a single character
begin:
mov dl,cl ; ascii code of first character
int 21h ;Yes
display it
loop
Is begin
done:
No>255?
.exit
END START End
No

No = No + 1
Figure 5.11 The Source Code for the ASCII2.asm Program

Figure 5.10 Flowchart of the ASCII Program


Start

No = 255

Display Char(No)

No = No - 1

Yes
Is No =0?

End
No

Figure 5.12 Flowchart of the ASCII2 Program

1. The jump commands can be classified into 2 groups: _____________ jumps and
_______________________ jumps
2. The number of bytes between the jmp Short label instruction and the label should be
between ________________________ and ______________________ bytes
3. The conditional jump can only be ____________ and _____________
4. The conditional jump statement JA is used with unsigned number, the equivalent
conditional jump statement used with signed number is ________________
5. For the Repeat – until loop, the condition that controls the loop is placed at _________
_________________________ of the loop
6. Draw a flowchart and then write the source code for an assembly language program that
displays the EVEN numbers from 0 to 9 on the screen
7. The ASCII program shown in Figure 6.3 uses the Compare and conditional jumps
commands to display the ASCII codes starting from 0 to 255. You are required to modify
the ASCII program so that it uses the Loop command to display the ASCII code
8. Draw a flowchart and then write the source code for an assembly language program that:
a. Request the user to enter his or her sex
b. If the user is a male then the message “Hi Handsome Guy” is displayed. If the
user is a female, then the message “Hi Beautiful Lady” is displayed.
9. Draw a flowchart and then write the source code for an assembly language program that:
a. Request the user to enter the name of a file.
b. If the file is found on the current directory, the file is open for reading and the
message “File is found is displayed” else the message “File is not found is
displayed”.

You might also like