Stack Operations

The stack is a LIFO structure in which words (not bytes) are inserted and deleted from the top of the stack. Insertion is referred to as "pushing" and deletion is referred to as "popping" the word to/from the stack. During program execution, the SS:SP (SS:ESP) register pair is used to address the top of the stack.

Q#1: Briefly explain how the SS:SP is used to address the top of the stack.

Some notes about the stack:
(1) The bottom of the stack is fixed at the address of the highest offset which is determined by how much stack space has been allocated.
(2) The top of stack is dynamic in that it changes as PUSH and POP operations are performed.
(3) The SS and SP are maintained by the microprocessor.

PUSH Instruction (general form):

(i14) [<label>] PUSH <source> [<comment>]

<source> is used to identify the location of a 16-bit value to be pushed onto the stack.

The stack operates as follows:
(1) Decrement the SP by 2
(2) Store the 16-bit value at the location identified by SS:SP

The source operand can be either: (a) general register (b) segment register (c) memory location. Also, the source operand must specify a word.

POP Instruction (general form):

(i15) [<label>] POP <destination> [<comment>]

P#1: Given the way we know PUSH to work, you explain to me how POP works.

PUSHF Instruction (general form):

(i16) [<label>] PUSHF [<comment>]

PUSHF causes the SP to be decremented by 2 and then pushes the flags register onto the stack.

Q#2: Why would we want to do this?

POPF Instruction (general form):

(i17) [<label>] POPF [<comment>]

POPF puts the 16-bit values pointed to by the SS:SP into the flags register and then increments the SP by 2.

Note: The flags register is not affected by: PUSH, POP, or PUSHF. Each of the flag bits is potentially modifiable by the POPF instruction.

P#2: Consider the following stack definition:

.stack 10h

Q#3: Assuming that the SS register is pointing to 0917, what is the value of the SP register?

P#3: Show pictorially what the stack looks like. In particular, draw a picture of MEM and the SS and SP registers.

P#4: Explain what it means for the stack to be empty using your diagram.

P#5: Assume the following data segment:

.data
val1     WORD  10
val2     WORD  23q
         WORD  111000b

Q#4: What would be the result of executing the instruction: PUSH val2? Also, show this using your stack picture.

PUSHA Instruction (general form):

(i18) [<label>] PUSHA [<comment>]

PUSHA is an 80286+ instruction that pushes the AX, CX, DX, BX, SP, BP, SI, DI registers onto the stack in that order.

POPA Instruction (general form):

(i19) [<label>] POPA [<comment>]

Reverses the process of PUSHA

PUSHAD Instruction (general form):

(i20) [<label>] PUSHAD [<comment>]

PUSHAD is an 80386+ instruction that pushes the EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI registers onto the stack in that order.

POPAD Instruction (general form):

(i21) [<label>] POPAD [<comment>]

Let's take a look at the reverse string example from Assembly Language for Intel-Based Computers

(i22) CALL <Label>

When the CALL statement is executed, the microprocessor pushes the address of the instruction following the CALL on the stack and then transfers execution to procedure Label.

(i23) <label> RET

When the RET instruction is executed, the microprocessor pops the return address from the top of the stack and places the value in EIP (Protected Mode) or IP (Real Mode).

Problem: Write an assembly language procedure called SUMIT that sums the integers from 1 to the value in ECX and returns the result in EDX.

sumit PROC


      ret
sumit ENDP

©Douglas J. Ryan / ryandj@pacificu.edu