0% found this document useful (0 votes)
20 views

The Stack

The stack segment is an area of memory used for temporary storage of data during subroutine calls and interrupts. The SS register points to the stack segment and the SP register points to the top of the stack. The PUSH instruction decrements SP and stores a value onto the stack. The POP instruction increments SP and retrieves a value from the top of the stack. The stack uses LIFO order, so the last value pushed will be the first value popped. Equal numbers of pushes and pops must be performed to avoid corrupting the stack. The stack is important for storing return addresses during subroutine calls and interrupt handling.

Uploaded by

Lokesh Lee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

The Stack

The stack segment is an area of memory used for temporary storage of data during subroutine calls and interrupts. The SS register points to the stack segment and the SP register points to the top of the stack. The PUSH instruction decrements SP and stores a value onto the stack. The POP instruction increments SP and retrieves a value from the top of the stack. The stack uses LIFO order, so the last value pushed will be the first value popped. Equal numbers of pushes and pops must be performed to avoid corrupting the stack. The stack is important for storing return addresses during subroutine calls and interrupt handling.

Uploaded by

Lokesh Lee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

The Stack

Stack Segment and Base pointer registers – for stack segment

 SS

 SP – point to subroutine / interrupt return address


 BP – hold an old stack pointer value or mark a place in the subroutine stack independent of
SP
o Avoids juggling single SP to reference subroutine parameters and addresses

Stack is an area of memory for keeping temporary data.

Stack is used by CALL instruction to keep return address for procedure, RET instruction gets this value from
the stack and returns to that offset.

Quite the same thing happens when INT instruction calls an interrupt, it stores in stack flag register, code
segment and offset. IRET instruction is used to return from interrupt call.

We can also use the stack to keep any other data, there are two instructions that work with the stack:

PUSH - stores 16 bit value in the stack.

POP - gets 16 bit value from the stack.

Syntax for PUSH instruction:

PUSH REG
PUSH SREG
PUSH memory
PUSH immediate
REG: AX, BX, CX, DX, DI, SI, BP, SP.

SREG: DS, ES, SS, CS.

memory: [BX], [BX+SI+7], 16 bit variable, etc...

immediate: 5, -24, 3Fh, 10001101b, etc...

Syntax for POP instruction:

POP REG
POP SREG
POP memory
REG: AX, BX, CX, DX, DI, SI, BP, SP.

SREG: DS, ES, SS, (except CS).

memory: [BX], [BX+SI+7], 16 bit variable, etc...


Notes:

 PUSH and POP work with 16 bit values only!

 Note: PUSH immediate works only on 80186 CPU and later!

The stack uses LIFO (Last In First Out) algorithm,


this means that if we push these values one by one into the stack:
1, 2, 3, 4, 5
the first value that we will get on pop will be 5, then 4, 3, 2, and only then 1.

It is very important to do equal number of PUSHs and POPs, otherwise the stack maybe corrupted and it
will be impossible to return to operating system.

As you already know we use RET instruction to return to operating system, so when program starts there is
a return address in stack (generally it's 0000h).

PUSH and POP instruction are especially useful because we don't have too much registers to operate with,
so here is a trick:

 Store original value of the register in stack (using PUSH).

 Use the register for any purpose.

 Restore the original value of the register from stack (using POP).

Here is an example:

ORG 100h

MOV AX, 1234h


PUSH AX ; store value of AX in stack.

MOV AX, 5678h ; modify the AX value.


POP AX ; restore the original value of AX.

RET
END
Another use of the stack is for exchanging the values,
here is an example:

ORG 100h

MOV AX, 1212h ; store 1212h in AX.


MOV BX, 3434h ; store 3434h in BX

PUSH AX ; store value of AX in stack.


PUSH BX ; store value of BX in stack.

POP AX ; set AX to original value of BX.


POP BX ; set BX to original value of AX.

RET

END

The exchange happens because stack uses LIFO (Last In First Out) algorithm, so when we push 1212h and
then 3434h, on pop we will first get 3434h and only after it 1212h.

The stack memory area is set by SS (Stack Segment) register, and SP (Stack Pointer) register.

Generally operating system sets values of these registers on program start.

"PUSH source" instruction does the following:

 Subtract 2 from SP register.

 Write the value of source to the address SS:SP.

"POP destination" instruction does the following:

 Write the value at the address SS:SP to destination.

 Add 2 to SP register.

The current address pointed by SS:SP is called the top of the stack.

For COM files stack segment is generally the code segment, and stack pointer is set to value of 0FFFEh. At the
address SS:0FFFEh stored a return address for RETinstruction that is executed in the end of the program.

You can visually see the stack operation by clicking on [Stack] button on emulator window. The top of the
stack is marked with "<" sign.
8086 - Stack Instructions

Get 16 bit value from the stack.

Algorithm:

 operand = SS:[SP] (top of the stack)


 SP = SP + 2

REG
POP SREG Example:
memory MOV AX, 1234h
PUSH AX
POP DX ; DX = 1234h
RET
C Z S O P A
unchanged

Pop all general purpose registers DI, SI, BP, SP, BX, DX, CX, AX from the stack.
SP value is ignored, it is Popped but not set to SP register).

Note: this instruction works only on 80186 CPU and later!

Algorithm:

 POP DI
 POP SI
 POP BP
 POP xx (SP value ignored)
 POP BX
 POP DX
 POP CX
 POP AX

POPA No operands C Z S O P A
unchanged
Get flags register from the stack.

Algorithm:

 flags = SS:[SP] (top of the stack)


POPF No operands  SP = SP + 2

C Z S O P A
popped

Store 16 bit value in the stack.

Note: PUSH immediate works only on 80186 CPU and later!

Algorithm:

 SP = SP - 2
 SS:[SP] (top of the stack) = operand
REG
SREG
PUSH
memory
immediate Example:
MOV AX, 1234h
PUSH AX
POP DX ; DX = 1234h
RET
C Z S O P A
unchanged

Push all general purpose registers AX, CX, DX, BX, SP, BP, SI, DI in the stack.
Original value of SP register (before PUSHA) is used.

Note: this instruction works only on 80186 CPU and later!

Algorithm:

 PUSH AX
 PUSH CX
 PUSH DX
 PUSH BX
PUSHA No operands
 PUSH SP
 PUSH BP
 PUSH SI
 PUSH DI

C Z S O P A
unchanged
Store flags register in the stack.

Algorithm:

 SP = SP - 2
PUSHF No operands  SS:[SP] (top of the stack) = flags

C Z S O P A
unchanged

Output File type

#make_exe# -
more advanced format of an executable file.

not limited by size and number of segments.

stack segment should be defined in the program.

you may select exe template from the new menu in to create a simple exe program with pre-defined data, stack,
and code segments.

the entry point (where execution starts) is defined by a programmer.

this file type is selected automatically if stack segment is found.

supported by dos and windows command prompt.

8086 Instructions uses Stack

Transfers control to procedure, return address is (IP) is pushed to stack. 4-byte


address may be entered in this form:1234h:5678h, first value is a segment second
value is an offset (this is a far call, so CS is also pushed to stack).

Example:

procedure ORG 100h ; for COM file.


name
label
CALL
4-byte
address
CALL p1

ADD AX, 1

RET ; return to OS.


p1 PROC ; procedure declaration.

MOV AX, 1234h

RET ; return to caller.

p1 ENDP

C Z S O P A
unchanged

Return from near procedure.

Algorithm:

 Pop from stack:


o IP
 if immediate operand is present: SP = SP + operand

Example:

ORG 100h ; for COM file.

CALL p1

No
operands
RET ADD AX, 1
or even
immediate

RET ; return to OS.

p1 PROC ; procedure declaration.

MOV AX, 1234h

RET ; return to caller.

p1 ENDP

C Z S O P A
unchanged
Interrupt numbered by immediate byte (0..255).

Algorithm:

Push to stack:

o flags register
o CS
o IP
 IF = 0
 Transfer control to interrupt procedure

immediate Example:
INT
byte
MOV AH, 0Eh ; teletype.

MOV AL, 'A'

INT 10h ; BIOS interrupt.

RET

C Z S O P A I
unchanged 0

Interrupt Return.

Algorithm:

Pop from stack:

o IP
No
IRET o CS
operands
o flags register

C Z S O P A
popped

Return from Far procedure.

Algorithm:
No
operands  Pop from stack:
RETF
or even o IP and CS
immediate  if immediate operand is present: SP = SP + operand

C Z S O P A
unchanged

1. Swap_Using_Stack.asm

ORG 100h

MOV AX, 1212h ; store 1212h in AX.


MOV BX, 3434h ; store 3434h in BX

PUSH AX ; store value of AX in stack.


PUSH BX ; store value of BX in stack.

POP AX ; set AX to original value of BX.


POP BX ; set BX to original value of AX.

RET

END

You might also like