Download presentation
Presentation is loading. Please wait.
1
S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu 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 POP: Remove an item from the top of the stack
2
S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Stack Implementation in LC-3 R6Stack pointer (TOS – Top of Stack) R0Source 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 R51 when stack overflow/underflow
3
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). / / / #18 TOP / / / #12 #5 #31 #18 TOP / / / #12 #5 #31 #18 TOP Initial StateAfter One Push After Three More Pushes After Two Pops x4000x3FFFx3FFCx3FFE R6
4
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
5
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
6
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 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET EMPTY.FILL xC00 ; xC000 = -x4000
7
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
8
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 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET MAX.FILL xC005 ; xC005 = -x3FFB
9
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.