Professional Documents
Culture Documents
5.0 Outcomes
At the end of this chapter, you should be able to:
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
S
Deduction = EPF+ Income Tax
STATEMENT 1
5.2.2 Selection
There are two constructs that perform selection:
• The IF construct
• The CASE construct
True
True Action False Action
True Action
a) IF….THEN….ELSE b) IF….THEN
True Action
Case 4
4
Default
Action
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.
True True
Decision
False
• 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
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
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
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.
Start
age”
Input Age
No
Is Age > Display “Sorry”
21?
Yes
Display
“Congratulation”
End
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
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
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
No = 255
Display Char(No)
No = No - 1
Yes
Is No =0?
End
No
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”.