Chapter-8
Chapter-8
Overview
The stack segment of a program is used for temporary storage of data
and addresses. Stack is used to implement procedures.
Offset Offset
00F0 00F0
00F2 00F2
00F4 00F4
00F6 0100 SP 00F6 00FE SP
00F8 00F8
1234 AX 1234 AX
00FA 00FA
00FC 5678 BX 00FC 5678 BX
00FE 00FE 1234 SP
0100 SP 0100
Offset
00F0
00F2
00FC SP
00F4
00F6 1234 AX
00F8
5678 BX
00FA
00FC 5678 SP
00FE 1234
0100
STACK
Note that PUSH and POP are word operations, so a byte instruction
such as
Illegal: PUSH DL
Is illegal. So is a push of immediate data, such as
Illegal: PUSH 2
Note: an immediate data push is legal for the 80186/80486 processors.
Offset Offset
00F0 00F0
00F2 00F2
00FC SP
00F4 00F4
00F6 FFFF CX 00F6 00FE SP
00F8 00F8
0001 DX 5678 CX
00FA 00FA
00FC 5678 SP 00FC 5678 0001 DX
00FE 1324 00FE 1234 SP
0100 0100
STACK STACK
Figure 8.2C After POP DX
Offset
00F0
00F2
00F4
0100 SP
00F6
00F8 5678 CX
00FA
00FC 5678 1234 DX
00FE 1234
0100 SP
STACK (empty)
8.2 A Stack Application
TITLE PGM8_1: REVERSE INPUT PUSH AX LOOP TOP
.MODEL SMALL INC CX EXIT:
.STACK 100H INT 21H MOV AH, 4CH
.CODE JMP WHILE_ INT 21H
MAIN PROC END_WHILE: MAIN ENDP
MOV AH, 2 MOV AH,2 END MAIN
MOV DL, ‘?’ MOV DL, 0DH
INT 21H INT 21H
XOR CX, CX MOV DL, 0AH
MOV AH, 1 INT 21H
INT 21H JCXZ EXIT
WHILE_: TOP:
CMP AL, 0DH POP DX
JE END_WHILE INT 21H
8.3 Terminology of Procedures
An assembly language program can be structured as a collection of
procedures. One of the procedures is the main procedure, and it
contains the entry point to the program.
When one procedure calls another, control transfers to the called
procedure.
Procedure Declaration
The syntax of procedure declaration is the following:
PROC1 PROC
first instruction
RET
RET
The RET (return) instruction causes control transfer back to the
calling procedure. Every procedure (except the main procedure)
should have a RET someplace; usually it’s the last statement in the
procedure.
00FC
PROC1 PROC
00FE
0200 first instruction
0100 SP
RET
00FC
PROC1 PROC
00FE 0012 SP
IP 0200 first instruction
0100
RET
00FC
PROC1 PROC
00FE 0012 SP
0200 first instruction
0100
IP 0300 RET
00FC
PROC1 PROC
00FE
0200 first instruction
0100 SP
0300 RET