Explain the following with diagram or example:- Parameter passing using stack in 8086 assembly language 5m Dec2005

By | October 28, 2014

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

code:

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

http://cssimplified.com/assignments/write-a-simple-near-procedure-in-8086-assembly-language-that-receives-one-16-bit-number-as-parameter-value-on-the-stack-from-the-main-module-it-returns-0-if-the-upper-byte-of-the-number-is-0-else-re

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.

Example:

DATA SEGMENT
MSG DB “BIT RETURNED IS : $”
DATA ENDS

CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:
MOV AX,DATA
MOV DS,AX

MOV BX,0FFFH

LEA DX,MSG
MOV AH,9
INT 21H

PUSH BX

CALL CHECK

ADD DL,30H
MOV AH,2
INT 21H

MOV AH,4CH
INT 21H
CODE ENDS

CHECK PROC NEAR
POP AX
POP BX

AND BH,11110000B

MOV CL,4
ROL BH,CL

CMP BH,0
JE SKIP

MOV DL,1
JMP DONE
SKIP:
MOV DL,0
DONE:
PUSH AX
RET
CHECK ENDP

END START