0% found this document useful (0 votes)
24 views9 pages

9-Stack and Procedure

The document provides an overview of stack and procedure concepts in programming, highlighting that a stack is a one-dimensional data structure that operates on a Last In First Out (LIFO) basis for temporary storage. It explains the PUSH and POP operations for saving and retrieving data, as well as the use of BP for stack management. Additionally, it outlines the syntax and types of procedures, including the CALL and RET instructions for invoking and returning from procedures.

Uploaded by

shahriarabid54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views9 pages

9-Stack and Procedure

The document provides an overview of stack and procedure concepts in programming, highlighting that a stack is a one-dimensional data structure that operates on a Last In First Out (LIFO) basis for temporary storage. It explains the PUSH and POP operations for saving and retrieving data, as well as the use of BP for stack management. Additionally, it outlines the syntax and types of procedures, including the CALL and RET instructions for invoking and returning from procedures.

Uploaded by

shahriarabid54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

STACK AND PROCEDURE HASAN MONIR

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

To invoke a procedure, the CALL instruction is used.


There are two kinds of procedure calls, direct and indirect.
Direct: CALL name
Indirect: CALL address_expression
address_expression specifies a register or memory location containing the address
of a procedure.
The return address of the calling program is saved on the stack. This is the offset of
the next instruction after the CALL statement. CS:IP has segment: offset of this
instruction at the time when the call is executed
 IP gets the offset address of the first instruction of the procedure. This transfers
control to the procedure.

You might also like