Explain the following with the help of a suitable diagram or an example:- Parameter passing using stack in 8086 assembly language 5m Dec2005
Most HLLs use the stack to pass parameters because this method is fairly efficient. To pass parameters on the stack, push them immediately before calling the subroutine. The subroutine then reads this data from the stack memory and operates on it appropriately.
You could gain access to the parameters passed on the stack by removing the data from the stack (Assuming a near procedure call):
CallProc proc near pop RtnAdrs pop kParm pop jParm pop iParm push RtnAdrs . . . ret CallProc endp
There is, however, a better way. The 80×86’s architecture allows you to use the bp (base pointer) register to access parameters passed on the stack. This is one of the reasons the disp[bp], [bp][di], [bp][si], disp[bp][si], and disp[bp][di] addressing modes use the stack segment rather than the data segment. The following code segment gives the standard procedure entry and exit
StdProc proc near push bp mov bp, sp . . . pop bp ret ParmSize StdProc endp
ParmSize is the number of bytes of parameters pushed onto the stack before calling the procedure. In the CallProc procedure there were six bytes of parameters pushed onto the stack so ParmSize would be six. Take a look at the stack immediately after the execution of mov bp, sp in StdProc.
The best technique for parameter passing is through stack. It is also a standard technique for passing parameters when the assembly language is interfaced with any high level language. Parameters are pushed on the stack and are referenced using BP register in the called procedure. One important issue for parameter passing through stack is to keep track of the stack overflow and underflow to keep a check on errors. Let us see example, by using stack for parameter passing.
Solved program can be found on this link
The instruction MOV BP, SP transfers the contents of the SP to the BP register. Now BP is used to access any location in the stack, by adding appropriate offset to it. For example, MOV AX, [BP + 12] instruction transfers the word beginning at the 12th byte from the top of the stack to AX register. It does not change the contents of the BP register or the top of the stack. It copies the pushed value of AH and AL at offset 008Eh into the AX register. This instruction is not equivalent to POP instruction.
Stacks are useful for writing procedures for multi-user system programs or recursive procedures. It is a good practice to make a stack diagram as above while using procedure call through stacks. This helps in reducing errors in programming.
MSG DB “BIT RETURNED IS : $”
CHECK PROC NEAR