Passing Parameter Procedure in Microprocessor – Whenever we need to use a group of instructions several times throughout a program there are two ways we can avoid having to write the group of instructions each time we want to use them. One way is to write the group of instructions as a separate procedure. We can then just CALL the procedure whenever we need to execute that group of instructions. For calling the procedure we have to store the return address onto the stack. This process takes some time. If the group of instructions is big, enough then this overhead time is negligible with respect to execution time. But if the group of instructions is too short, the overhead time and execution time are comparable.
In such cases, it is not desirable to write procedures. For these cases, we can use macros. Macro is also a group of instructions. Each time we “CALL” a macro in our program, the assembler will insert the defined group of instructions in place of the “CALL”. An important point here is that the assembler generates machine codes for the group of instructions each time macro is called. So there is not overhead time involved in calling and returning from a procedure. The disadvantage of macro is that it generates inline code each time when the macro is called which takes more memory. In this section we discuss the procedures.
From the above discussions, we know that the procedure is a group of instructions stored as a separate program in the memory and it is called from the main program whenever required. The type of procedure depends on where the procedure is stored in the memory. If it is in the same code segment where the main program is stored then it is called near procedure otherwise it is referred to as far procedure. For near procedure CALL instruction pushes only the IP register contents on the stack, since CS register contents remains unchanged for main program and procedure. But for far procedures CALL instruction pushes both IP and CS on the stack.
We often want a procedure to process some data or address variable from the main program. For processing, it is necessary to pass these address variables or data, usually referred as Passing Parameter Procedure. There are four ways to pass parameters to and from the procedure
Using general memory
Passing Parameters Using Registers:
The data, to be passed is stored in the registers the procedure to process the data.
Passing Parameters Using Memory:
For the cases where we have to pass few parameters to and from a procedure, registers are a convenient way to do it. However, in cases where we need to pass a large number of Passing Parameter Procedure we use memory. This memory may be a dedicated section of general memory or a part of stack.
Passing Parameters Using Pointers:
Passing Parameters Using Stack:
To pass parameters to the procedure using stack we push them, on the stack before the call for the procedure in the main program. The instructions in the procedure read these parameters from the stack. Whenever the stack is used to Passing Parameter Procedure it is very important to keep track of what is pushed on the stack and where the stack pointer points all the time in the program.
In some situations it may happen that procedure1 is called from main program, procedure2 is called from procedure1 and procedure1 is again called from procedure2. In this situation program execution flow reenters in the procedure1. This type of procedures are called Reentrant Procedures. The flow of program execution for reentrant procedure is shown in Fig. 8.3.
A recursive procedure is a procedure which calls itself. Recursive procedures are used to work with complex data structures called trees. If the procedures is called with N (recursion depth) = 3. Then the n is decremented by one after each procedure CALL and the procedure is called until n = 0: Fig. 8.4 shows the flow diagram and pseudo-code for recursive procedure.