written 8.3 years ago by |
- Procedures are written to process data or address variables from the main program.
- To achieve this, it is necessary to pass the information about address, variables or data. This technique is called as parameter passing.
- The four major ways of passing parameters to and from a procedure are:
- Passing parameters using registers
- Passing parameters using memory
- Passing parameters using pointers
Passing parameters using stack
Passing parameters using registers-
The data to be passed is stored in the registers and these registers are accessed in the procedure to process the data.
Example:
.model small .data MULTIPLICAND DW 1234H MULTIPLIER DW 4232H .code MOV AX, MULTIPLICAND MOV BX, MULTIPLIER CALL MULTI : : MULTI PROC NEAR MUL BX ; Procedure to access data from BX register RET MULTI ENDP : : END
The disadvantage of using registers to pass parameters is that the number of registers limits the number of parameters you can pass.
E.g. An array of 100 elements can’t be passed to a procedure using registers.
Passing parameters using memory-
In the cases where few parameters have to be passed to and from a procedure, registers are convenient. But, in cases when we need to pass a large number of parameters to procedure, we use memory. This memory may be a dedicated section of general memory or a part of it.
Example:
.model small .data MULTIPLICAND DW 1234H ; Storage for multiplicand value MULTIPLIER DW 4232H ; Storage for multiplier value MULTIPLICATION DW ? ; Storage for multiplication result .code MOV AX, @Data MOV DS, AX : : CALL MULTI : : MULTI PROC NEAR MOV AX, MULTIPLICAND MOV BX, MULTIPLIER : : MOV MULTIPLICATION, AX ; Store the multiplication value in named memory location RET MULTI ENDP END
Passing parameter using pointers-
A parameter passing method which overcomes the disadvantage of using data item names (i.e. variable names) directly in a procedure is to use registers to pass the procedure pointers to the desired data.
Example:
.model small .data MULTIPLICAND DB 12H ; Storage for multiplicand value MULTIPLIER DB 42H ; Storage for multiplier value MULTIPLICATION DW ? ; Storage for multiplication result .code MOV AX, @Data MOV DS, AX MOV SI, OFFSET MULTIPLICAND MOV DI, OFFSET MULTIPLIER MOV BX, OFFSET MULTIPLICATION CALL MULTI : : MULTI PROC NEAR : : MOV AL, [SI] ; Get multiplicand value pointed by SI in accumulator MOV BL, [DI] ; Get multiplier value pointed by DI in BL : : MOV [BX], AX ; Store result in location pointed out by BX RET MULTI ENDP END
Passing parameters using stack-
In order to pass the parameters using stack we push them on the stack before the call for the procedure in the main program. The instructions used in the procedure read these parameters from the stack. Whenever stack is used to pass parameters it is important to keep a track of what is pushed on the stack and what is popped off the stack in the main program.
Example:
.model small .data MULTIPLICAND DW 1234H MULTIPLIER DW 4232H .code MOV AX, @data MOV DS, AX : : PUSH MULTIPLICAND PUSH MULTIPLIER CALL MULTI : : MULTI PROC NEAR PUSH BP MOV BP, SP ; Copies offset of SP into BP MOV AX, [BP + 6] ; MULTIPLICAND value is available at ; [BP + 6] and is passed to AX MUL WORD PTR [BP + 4] ; MULTIPLIER value is passed POP BP RET ; Increments SP by 4 to return address MULTI ENDP ; End procedure END