Friday, December 25, 2009

Assembly Language Part 9

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
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.



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

        MSG1   DB    “This is from procedure_1$”
        MSG2  DB  0DH, 0AH, “This is from main procedure$”
        MOV AX, @DATA
        MOV DS, AX


        LEA DX,MSG2
        MOV AH,9

        INT 21H

     MOV AH, 4CH
     INT 21H

        LEA  DX, MSG1
        MOV AH, 9
        INT 21H


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, . . . .

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

.model small
fact macro 
    mov cx,var1
    xor ax,ax
    mov ax,1
    mul cx
    loop top   
    call outdec
.stack 100h
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
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
    mov cx,ax
    mov ah,4ch
    int 21h
   main endp
    include outdec.asm
    include indec.asm   
end main

No comments:

Post a Comment

- - Important Notice - -

Programming google group is up. If you want to join request personally to any of the managers!

Message from DIUCSP

This blog is open for all students of CS, CSE, CIS of Daffodil International University.

Join This blog to stay on touch with other students. If you have a gmail account then just sign in as the g-mail user and become a follower of the blog. You can then submit comments on our posts.

You will find information on different subjects of our course, discuss problems and have solution. Try to maintain the subjectwise discussion for early response. And remember this blog is not only for the genius or more than average CS, CSE, CIS students with great computer skills so dont feel shy in submitting your simple daily life problems (even the silliest ones) because I believe if you feel shy in learning now then you will feel shy all your life for not learning it at all.

Please be conscious about sharing your personal Data, DaffodilCSE blog moderator is not responsible for any risk you take, If you feel safe post comment as your own will, If you do not want to share your mail, comment anonymously. Thanks

They are with us

There was an error in this gadget


Earn from a cool looking gadget..! It works with Google adsense and any other advertising programs!
Grab now !