Uploaded by engrismailkhan1

my ppt

advertisement
PRESENTATION
BY : SUGHRA(49263)
ALI HASSAN(51727)
Contents
•
•
•
•
•
•
•
Defining procedures
MAIN procedure
PROC and ENDP directives
CALL and RET instructions
NESTED procedures
Example
Procedure Parameters
Defining Procedures
 Procedure is a block of statements
which can be called from any place in
the program and as many times as
required.
 Every procedure is named uniquely as
the identifiers.
 PROC and ENDP directives declare
and indicate the start and end of
procedure.
 Procedures other the main or startup
procedure must be ended with a RET
statement before ENDP directive.
Purpose of Procedures
• To shorten the length of code
• Make code easier to
understand by grouping similar
instructions
• To avoid repeating the block of
statements used many time in
a program
• Can also be used as loop by
using their recursive
functionality
Procedures:
• A block of code that can be called any where in the program.
• A procedure is the ASM equivalent of a JAVA or a C++
function.
Syntax:
How to call a procedure?
Name proc
.
.
ret
Name endp
.
.
call Name
MAIN Procedure
• A procedure with a procedure
name “MAIN”
• It is same as the main function in
C++
• It is executed without being
called
• All other procedures all called
from here
• Every program needs to have
this procedure but there are also
alternatives.
• It must has to be ended with the
two instructions:
MOV ah,4ch
INT 21h
PROC and ENDP Directives
• Are used to declare the procedure
• Indicate the start and end of procedures
• Procedure name comes before PROC and ENDP every
time a procedure is declared
• Every procedure has an address which the assembler
remembers for its execution
CALL Instruction
• Used to call a procedure
• Stops the execution of its own procedure
• Transfers control to procedure being called
• Pushes the offset address of next instruction to be
executed in the stack
• Copies the offset of called procedure to the IP register
• The execution of called procedure starts
RET Instruction
• Used to return from a procedure to the procedure from
where it was called
• When used in a program without main procedure
returns the control to operating system
• Comes at the end of a procedure
• Pops the offset of the instruction after CALL instruction
from stack and copies it to IP register
• The execution of main procedure starts from where it
was stopped
Syntax
.model small
.stack 100h
.data
.code
Main proc
.
.
call Name
mov ah,4ch
int 21h
Main endp
Name proc
.
.
ret
Name endp
End main
The mov ah, 4ch and
int 21h instruction
transfer the control to
the operating system
in the main procedure
Stack and IP Register Condition
During CALL and RET
CALL
main PROC
00000020
call MySub
00000025
mov ax,bx
.
.
RET
main ENDP
MySub PROC
00000040
mov ax,dx
.
.
ret
MySub ENDP
NESTED PROCEDURE CALLS
A nested procedure call occurs
when a called procedure calls
another procedure.
.code
main proc
call sub1
mov ah,4ch
int 21h
main endp
sub1 proc
call sub2
ret
sub1 endp
sub2 proc
ret
sub2 endp
end main
Example
.model small
.stack 100h
.data
.code
main proc
call myProc
call myProc
mov ah,4ch
int 21h
main endp
myProc proc
mov dl,'a'
mov ah,2
int 21h
ret
myProc endp
end main
OUTPUT
aa
main PROC
Procedure Parameters
• There are several ways to pass
parameters to procedure, the
easiest way to pass parameters is
MOV
MOV
AL, 1
BL, 2
CALL
CALL
CALL
m2
m2
m2
mov dl,al
add dl,48
by using registers
• As shown in the example
mov ah,2
int 21h
mov ah,4ch
int 21h
OUTPUT
8
main ENDP
m2
PROC
MUL
RET
m2
END main
BL
ENDP
; AL = AL * BL.
Download