Download presentation
Presentation is loading. Please wait.
1
(The Stack and Procedures)
Lecture 8 (The Stack and Procedures)
2
Lecture Outline Introduction The Stack The PUSH Instruction
The POP Instruction Terminology of Procedures INDEC / OUTDEC procedures 1
3
Introduction The stack segment of a program is used for temporary storage of data and addresses. PUSH and POP instructions are used to add and remove words from the stack. 2
4
The Stack A stack is a one-dimensional data structure.
Items are added and removed from one end of the structure; that is, it processes in a “last-in-first-out” manner. A program must set aside a block of memory to hold the stack. Ex: .STACK 100H When the program is assembled and loaded in memory: SS will contain the segment number of the stack segment. SP is initialized to 100H, which represents the empty stack position. When the stack is not empty, SP contains the offset address of the top of the stack. 3
5
The PUSH Instruction To add a new word to the stack we PUSH it on.
Syntax: PUSH source Execution of PUSH causes the following to happen: SP is decreased/decremented by 2. A copy of the source content is moved to the address specified by SS:SP. The source is unchanged. 16-bit register or memory location 4
6
The PUSH Instruction 1234 5678 AX BX Offset 00F8 00FA 00FC 00FE 0100
Empty Stack SP 1234 Offset 00F8 00FA 00FC 00FE 0100 After PUSH AX SP 1234 Offset 00F8 00FA 00FC 00FE 0100 AFTER PUSH BX SP 5678 5
7
The POP Instruction To remove the top item from the stack, we POP it.
Syntax: POP destination Execution of POP causes the following to happen: The content of SS:SP (the top of the stack) is moved to the destination. SP is increased by 2. 16-bit register (except IP) or memory location 6
8
The POP Instruction 5678 1234 Offset 00F8 00FA 00FC 00FE 0100
FFFF 0001 CX DX 5678 1234 Offset 00F8 00FA 00FC 00FE 0100 After POP CX SP 0001 CX DX 5678 1234 Offset 00F8 00FA 00FC 00FE 0100 After POP DX SP CX DX 1234 Offset 00F8 00FA 00FC 00FE 0100 Stack SP 5678 7
9
Exercise 1 Write assembly code that uses the stack operations to swap the content of AX and DX. PUSH AX PUSH DX POP AX POP DX 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.