Microprocessmim: Stack Memory
Microprocessmim: Stack Memory
of Technology
Computer Engineering Department
Stack memory
Microprocessmim
اسم الطالب:
االختصاص:
المرحلة:
الدراسة:
تاريخ التسليم:
2019-2020
Stacks
intro
The stack is a block of memory that may be used for temporarily storing the
contents of the registers inside the CPU. It is a top-down data structure whose
elements are accessed using the stack pointer (SP) which gets decremented by two
as we store a data word into the stack and gets incremented by two as we retrieve a
data word from the stack back to the CPU register.
theory
The process of storing the data in the stack is called ‘pushing into’ the stack
and the
reverse process of transferring the data back from the stack to the CPU register is
known as
‘popping off’ the stack. The stack is essentially Last-In-First-Out (LIFO) data
segment. This
means that the data which is pushed into the stack last will be on top of stack and
will be popped off the stack first.
The stack pointer is a 16-bit register that contains the offset address of the
memory location in the stack segment. The stack segment, like any other segment,
may have a memory block of a maximum of 64 Kbytes locations, and thus may
overlap with any other segments. Stack Segment register (SS) contains the base
address of the stack segment in the memory.
The Stack Segment register (SS) and Stack pointer register (SP) together address
the stack-top as explained below:
If the stack top points to a memory location 52050H, it means that the location
52050H is already occupied with the previously pushed data. The next 16 bit push
operation will decrement the stack pointer by two, so that it will point to the new
stack-top 5204EH and the decremented contents of SP will be 204EH. This
location will now be occupied by the recently pushed data.
Thus for a selected value of SS, the maximum value of SP=FFFFH and the
segment can have maximum of 64K locations. If the SP starts with an initial value
of FFFFH, it will be decremented by two whenever a 16-bit data is pushed onto the
stack. After successive push operations, when the stack pointer contains 0000H,
any attempt to further push the data to the stack will result in stack overflow.
After a procedure is called using the CALL instruction, the IP is incremented to the
next instruction. Then the contents of IP, CS and flag register are pushed
automatically to the stack. The control is then transferred to the specified address
in the CALL instruction i.e. starting address of the procedure. Then the procedure
is executed.
Ex1:
org 0x7c00
bits 16
jmp main
print:
pop dx
pop bx
int 10h
jmp dx
main:
push bx
call print
cli
hlt
dw 0xAA55
Ex2
push — Push stack (Opcodes: FF, 89, 8A, 8B, 8C, 8E, ...)
The push instruction places its operand onto the top of the hardware supported stack in memory.
Specifically, push first decrements ESP by 4, then places its operand into the contents of the 32-bit
location at address [ESP]. ESP (the stack pointer) is decremented by push since the x86 stack
grows down - i.e. the stack grows from high addresses to lower addresses.
Syntax
push <reg32>
push <mem>
push <con32>
Examples
push [var] — push the 4 bytes at address var onto the stack
The pop instruction removes the 4-byte data element from the top of the hardware-supported stack
into the specified operand (i.e. register or memory location). It first moves the 4 bytes located at
memory location [SP] into the specified register or memory location, and then increments SP by 4.
Syntax
pop <reg32>
pop <mem>
Examples
pop edi — pop the top element of the stack into EDI.
pop [ebx] — pop the top element of the stack into memory at the four bytes starting at location EBX.
EX3;
Org 100h
MOV AX,1234H
MOV BX,5678H
PUSH AX
MOV AX,BX
POP AX