You are on page 1of 10

Microprocessor

and
Assembly Language

RED SEA UNIVERSITY


FACULTY OF COMUPTER SCIENCE & IT
Lecturer: Eng. Muhiyadin Abdikadir
Chapter Seven
Procedures
Procedure is a part of code that can be called from your program in order to make some
specific task. Procedures make program more structural and easier to understand.
Generally procedure returns to the same point from where it was called.
The syntax for procedure declaration:

name - is the procedure name, the same name should be in the top and the bottom, this is
used to check correct closing of procedures. Probably, you already know that RET instruction
is used to return to operating system. The same instruction is used to return from procedure
(actually operating system sees your program as a special procedure). 02
PROC and ENDP are compiler directives, so they are not assembled into any real machine
code. Compiler just remembers the address of procedure.

CALL instruction is used to call a procedure.

Here is an example:

The above example calls procedure m1, does MOV BX, 5, and returns to the next instruction
after CALL: MOV AX, 2.

03
Example
An assembly language code that print the character 'A' five times to the screen using
procedure

04
1. The call p1 instruction is used to call the procedure p1 five times. Each call will
execute the procedure, which prints the character 'A'.

2.The p1 procedure uses the DOS interrupt int 21h with function number 2 in

register AH to print the character in register DL to the screen.


3.After printing the character, the ret instruction returns control to the next

instruction after the call p1 that invoked it.

4.The .data directive is present but not used, as no data is defined. Similarly, the

.code directive indicates the start of the code segment, which contains the

actual executable instructions.


5.This program is a simple demonstration of using DOS interrupts to print

characters to the screen and the use of procedures in assembly language. Each
call p1 instruction results in the character 'A' being printed, so the output will be
"AAAAA" on the screen. 05
There are several ways to pass parameters to procedure.
The easiest way to pass parameters is by using registers, here is another example of a
procedure that receives two parameters in AL and BL registers, multiplies these parameters
and returns the result in AX register:

In the above example value of AL register is update every time the procedure is called, BL
register stays unchanged, so this algorithm calculates 2 in power of 4, so final result in AX
register is 16 (or 10h)
06
Here goes another example, that uses a procedure and loop

1. The mov cl, 5 instruction initializes the CL register with the value 5, indicating that we want to
print 'A' five times.
2. The call PrintA instruction calls the PrintA procedure.
07
3. mov dl, 'A' loads the ASCII code for 'A' into the DL register.
4. The RepeatPrint label marks the beginning of the loop.
5. int 21h prints the character 'A'.
6. dec cl decrements the counter in the CL register.
7. cmp cl, 0 compares the CL register with 0 to check if we've printed 'A' five times.
08
8. jne RepeatPrint jumps back to RepeatPrint if CL is not 0, continuing the loop.
9. Once CL reaches 0, the ret instruction returns control to the main program, which then returns
to the operating system.
10. Assemble and run this code in the EMU8086 emulator, and you should see the letter 'A' printed
five times on the screen. 09
Quiz
1) Write an assembly language program that displays number ‘6’ Ten times using procedure.

2) Write an assembly language Program that display a String 6 times using procedure and loop.

End
10

You might also like