Procedures in Assembly
The Idea of procedure is to divide the main problem into sub problems and acquire the solution by solving the individual sub problems and combining through calling them from a mother problem.
The Idea is similar to functions() as used in High level languages. When a procedure call one another the control is transferred from the caller to the called, when the called procedure is done it usually returns the control to the caler function immediately after the calling statement.
The basic form of the procedure is
name PROC type
; body of the procedure
RET
name ENDP
There are 2 types of procedures. NEAR and FAR. A procedure is called near if the procedure and the main program is in the same segment. If the procedure is in other segment then the proc is called FAR.
To invoke a procedure we write the following command
CALL name_of_proc
How is control returned?
The control is returned from the procedure to caller program when the RET instruction is executed. Every procedure should have a RET instruction in some place.
MAIN PROC
..............
..............
CALL PROC_1
statement_x
..............
..............
PROC_1 PROC
..............
..............
RET
In the program mentioned above in a stage of execution of MAIN PROC the statement CALL PROC_1 will occur. This will transfer the control of the program to PROC_1 and the instructions in PROC_1 shall continue to execute. When the instruction RET is reached the control is returned to MAIN PROC and the program shall resume to execute from statement_x
Example ( factorial ! )
Let us describe the application of proc by finding the factorial
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB “This is from procedure_1$”
MSG2 DB 0DH, 0AH, “This is from main procedure$”
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
.STACK 100H
.DATA
MSG1 DB “This is from procedure_1$”
MSG2 DB 0DH, 0AH, “This is from main procedure$”
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
CALL PROCEDURE_1
LEA DX,MSG2
MOV AH,9
INT 21H
MOV AH, 4CH
INT 21H
MAIN ENDP
PROCEDURE_1 PROC
LEA DX, MSG1
MOV AH, 9
INT 21H
RET
PROCEDURE_1 ENDP
END MAIN
Macro in Assembly
The Idea of Macro is somewhat similar of procedure, divide the main problem into sub problems and acquire the solution by solving the individual sub problems. But macro at all is not like a procedure. It is just a block of program that has a name. The syntax for defining macro is -
macro_name MACRO operand1,operand2, . . . .
statements
ENDM
Inside the Program we call the macro just by -
macro_name (operands)
When a macro is invoked, the program control does not jump as it does in the case of procedure. Instead the block of program defined by the macro name is copied to the place where it occurs before executing.
Example: Factorial using MACRO
fact macro
mov cx,var1
xor ax,ax
mov ax,1
top:
mul cx
loop top
call outdec
endm
.stack 100h
.data
msg1 db 0ah,0dh,"Factorial is=$"
msg2 db "Enter a number:$"
;msg3 db 0ah,0dh,"your entered value is out of range!!$"
var1 dw 0
;limit dw 0
.code
main proc
mov ax,@data
mov ds,ax
lea dx,msg2
mov ah,9
int 21h
call indec
mov var1,ax
lea dx,msg1
mov ah,9
int 21h
fact
mov cx,ax
mov ah,4ch
int 21h
main endp
include outdec.asm
include indec.asm
end main
No comments:
Post a Comment