1 / 9

90 likes | 223 Views

CHAPTER 10 THE STACK. Stack - A LIFO (last-in first-out) storage structure. The first item we put into the stack is taken out last . The last item we put into the stack is taken out first . Two main operations on the stack: PUSH : Add an item to the top of the stack

Download Presentation
## CHAPTER 10 THE STACK

**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

**CHAPTER 10THE STACK**Stack - A LIFO (last-in first-out) storage structure. The first item we put into the stack is taken out last. The last item we put into the stack is taken out first. Two main operations on the stack:PUSH: Add an item to the top of the stack POP: Remove an item from the top of the stack S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Stack Implementation in LC-3**R6 Stack pointer (TOS – Top of Stack) R0 Source register for Push & Destination register for Pop TOS (R6) x4000 when stack is empty TOS (R6) x3FFB when stack is full R5 0 when Push/Pop is success R5 1 when stack overflow/underflow S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Stack - A Software Implementation**In the example below, the stack consists of 5 locations, x3FFF through x3FFB. Register R6 is the stack pointer (TOS – Top of Stack). / / / / / / / / / / / / / / / / / / / / / / / / TOP / / / / / / / / / / / / #12 #12 / / / / / / / / / / / / #5 #5 TOP / / / / / / / / / / / / #31 #31 / / / / / / #18 TOP #18 #18 x4000 R6 x3FFF R6 x3FFC R6 x3FFE R6 Initial State After One Push After Three More Pushes After Two Pops S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Basic Push and Pop Code**Stack grows downward - when items are added (pushed into stack), TOS moves closer to address 0. Push ADD R6, R6, #-1 ; decrement stack ptr R6 STR R0, R6, #0 ; store data (contained in R0) into the mem. location pointed to by stack ptr Pop LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; increment stack ptr S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Pop with Underflow Detection**• If we try to pop too many items off the stack,an underflow condition occurs. • Check for underflow by checking TOS before removing data. • If TOS = x4000, then underflow • Set or reset register R5 to indicate success or underflow • R5 = 0 if Pop is success; R5 = 1 for underflow S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Pop with Underflow Detection**POP LD R1, EMPTY ; EMPTY = -x4000 ADD R2, R6, R1 ; Compare stack pointer with x4000 BRz FAIL ; Branch if stack is empty LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; Increment stack ptr AND R5, R5, #0 ; Pop is SUCCESS: R5 = 0 RETFAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RETEMPTY .FILL xC00 ; xC000 = -x4000 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Push with Overflow Detection**• If we try to push too many items onto the stack, an overflow condition occurs. • Check for overflow by checking TOS before adding data. • If TOS = x3FFB, then overflow • Set or reset register R5 to indicate success or overflow • R5 = 0 if Push is success; R5 = 1 for overflow S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Push with Overflow Detection**PUSH LD R1, MAX ; MAX = -x3FFB ADD R2, R6, R1 ; Compare stack pointer with x3FFB BRz FAIL ; Branch if stack is full ADD R6, R6, #-1 ; Decrement stack ptr STR R0, R6, #0 ; Push data onto TOS AND R5, R5, #0 ; Push is SUCCESS: R5 = 0 RETFAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RETMAX .FILL xC005 ; xC005 = -x3FFB S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu**Example**Consider an application program that calls subroutine Sub1. The routine Sub1 then calls subroutine Sub2 and Sub2 then calls Sub3. Give the LC-3 assembly language program segment to implement the application program. S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu

More Related