1 / 13

CHAPTER 3

CHAPTER 3. LAB STACK. overview. The stack segment of a program is used for temporary storage of data and address.

uriah-cruz
Download Presentation

CHAPTER 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 3 LAB STACK

  2. overview • The stack segment of a program is used for temporary storage of data and address. • In section 4, introduce PUSH and POP instructions that add and remove word from stack. The last word to be added to the stack is the first to be removed, a stack can be used to reverse a list of data.

  3. 1. STACK • A stack is one-dimensional data structure. • Items are added and removed from one end of the structure, that is, it is processed in a “last-in, first-out” manner. • The most recent addition to the stack is called the top of the stack. • Analogy: is a stack of dishes; the last dish to go on the stack is the top one, and it’s the only one that can be removed easily.

  4. A program must set aside a block of memory to hold the stack. We have been doing this by declaring a stack segment; for example .STACK 100h • When the program is assembled and loaded in memory, SS will contain the segment number of the stack segment. • For the preceding stack declaration, SP, the stack pointer, is initialized to 100h. • This represent the empty stack position. • When the stack is not empty, SP contains the offset address of the top of the stack.

  5. PUSH and PUSHF • To add a new word to the stack we PUSH it on. Syntax: PUSH source Where source is a 16-bit register or memory word. Eg: PUSH AX • Execution of PUSH causes the following happen: • SP is decreased by 2 • A copy of the source content is moved to the address specified by SS:SSP. The source is unchanged. • The instruction PUSHF, which has no operands, pushes the content of the FLAGS register onto the stack.

  6. PUSH and PUSHF • Initially, SP contains the offset address of the memory location immediately following the stack segment; • the first PUSH decreases SP by 2, making it point to the last word in the stack segment. • Because each PUSH decreases SP, the stack grow toward beginning of memory.

  7. Figure: empty STACK(declaring at STACK 100h) Offset 00F2 00F4 00F6 00F8 0100 SP 00FA 1234 00FC AX 00FE 0100 SP 5678 BX STACK (empty)

  8. Figure: After PUSH AX Offset 00F2 00F4 00F6 00F8 00FE SP 00FA 1234 00FC AX 00FE SP 0100 5678 BX STACK

  9. Figure: After PUSH BX Offset 00F2 00F4 00F6 00F8 00FC SP 00FA 1234 00FC SP AX 00FE 0100 5678 BX STACK

  10. POP and POPF • To remove the top item from the stack, we POP it. Syntax: POP destination Where destination is a 16-bit register (except IP) or memory word. Eg: POP BX • Executing POP causes the following happen: • SP is increased by 2 • The content of SS:SP (the top of the stack) is moved to the destination. • The instruction POPF pops the top of the stack into the FLAGS register. • There is no effect of PUSH, PUSHF, POP, POPF on the flags. • Note that PUSH and POP are word operations, so a byte instruction and immediate data is illegal, such as • PUSH DL • PUSH 2

  11. Before POP Offset 00F2 00F4 00F6 00F8 00FC SP 00FA FFFF 00FC SP CX 00FE 0100 0001 DX STACK

  12. After POP CX Offset 00F2 00F4 00F6 00F8 00FE SP 00FA 5678 00FC CX 00FE SP 0100 0001 DX STACK

  13. After POP DX Offset 00F2 00F4 00F6 00F8 0100 SP 00FA 5678 00FC CX 00FE 0100 SP 1234 DX STACK

More Related