9-Stack and Procedure
9-Stack and Procedure
ASSISTANT PROFESSOR
STACK
• ‘1-D Data Structure’
• Memory block in the RAM
• Items are added and removed from one end
• Last In First Out
• Used for temporary storage
PUSH SOURCE
Saves a ‘WORD’ in stack
PUSH source
Lower Byte -> Lower Address of Stack
Higher Byte -> Higher Address of Stack
PUSH CX will save the current value of CX in the address pointed by SP
POP DESTINATION
Retrieves a ‘WORD’ from the stack
POP destination
POP CX gets the word from the address pointed by SP
Neither PUSH , nor POP works on byte type registers.
USE OF BP
SS:BP allows us to save SP value in BP
Thus stack operation can be done without perturbing stack
PROCEDURES
Syntax:
name PROC type
;body of the procedure
RET
name ENDP
Type (near or far) is optional
Near: the statement that calls the procedure is in the same segment as the procedure
itself. NEAR is assumed if type is omitted.
Far: the statement that calls the procedure is in a different segment.
PROCEDURES
Main PROC
CALL PROC1
next instruction
PROC1 PROC
first instruction
RET
RET
The RET instruction causes control to transfer back to the calling procedure.
Every procedure should have a RET someplace (except the main procedure)
Usually it is the last statement in the procedure
CALL