The stack. Summary. (Textbook chapter 10) 1. The array data structure 2. The stack data structure 3. Arithmetic using a stack

The stack (Textbook chapter 10) Summary 1. The array data structure 2. The stack data structure 3. Arithmetic using a stack CMPE12 – Fall 2006 – A. ...
Author: Clifford Logan
4 downloads 0 Views 2MB Size
The stack (Textbook chapter 10)

Summary 1. The array data structure 2. The stack data structure 3. Arithmetic using a stack

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

2

1

Arrays Definition: A list of values arranged sequentially in memory and grouped under a single name Example: a list of telephone numbers In C, the expression a[4] refers to the 5th element of the array a • Most assembly languages have only basic concept of arrays (.BLKW in LC-3)

3

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Properties of Arrays Properties of arrays:

– Each element is the same size (i.e. same type) – Elements are stored contiguously – First element is located at the lowest memory address

In assembly language we must – Allocate correct amount of space for an array – Map array addresses to memory addresses – Ex:

myArray .BLKW

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

5

4

2

Pointer Address of a variable in memory Allows us to indirectly access variables Base of an array = pointer to the first element

LEA LDR STR myArray

R2, myArray R0, R2, #0 R0, R2, #2

.BLKW 5

5

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Multi-dimensional arrays 4 columns

2 rows

A 0 1 2 3 0 A00 A01 A02 A03 1 A10 A11 A12 A13

x3100 x3101 x3102 x3103 x3104 x3105

2 “major” memory mappings:

x3106 x3107

Row-major CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Column-major 6

3

LIFO and FIFO • Abstract Data Structures – defined by the rules for inserting and extracting data

• LIFO (Last In-First Out) A, B, C

FIFO (First In-First Out) A, B, C

7

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Stack Data Structure • A stack is a LIFO structure • Operations: • PUSH (enter item at top of stack) • POP (remove item from top of stack)

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

TOS = Top Of Stack

8

4

Stack overflow and underflow Error conditions: • Underflow (trying to pop from empty stack) • Overflow (trying to push onto full stack)

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

9

A “physical” stack • A coin holder as a stack

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

10

5

A hardware stack • Implemented in hardware (i.e. with registers) – Previous data entries move up to accommodate each new data entry – Note that the Top Of Stack is always in the same place.

11

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

A software stack • Implemented in memory

NOTE that the stack grows “downward”!

– The Top Of Stack moves as new data is entered – Here R6 is the TOS register, a pointer to the Top Of Stack

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

12

6

Stack in LC-3 1. Declare an array to be your stack 2. Use a register to point to the TOS (R6 is the convention) 3. Implement PUSH and POP

13

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Push in LC-3 • Assume data to push is in a specific register (R0) • Decrement TOS pointer (our stack is moving down) • Then write data in R0 to new TOS PUSH

ADD STR

R6, R6, #-1 R0, R6, #0

NOTE: the TOS points to the last element pushed CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

14

7

Pop in LC-3 • Assume data to pop will go to a specific register (R0) • Read data at current TOS into R0 • Then increment TOS pointer

POP

LDR ADD

R0, R6, #0 R6, R6, #1

15

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Checking for over/underflow • Before pushing, we have to test for overflow • Before popping, we have to test for underflow • In both cases, use R5 to report success or failure

POP

Same for PUSH and overflow CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

16

8

When is the stack empty or full? First way: TOS always points to top valid element

x3FFA x3FFB

Stack Overflow:

x3FFC

TOS =

x3FFD

Stack Underflow:

x3FFF

TOS =

x4000

MAX

x3FFE

BASE

17

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

When is the stack empty or full? Second way: TOS always points to next available location to push

x3FFA

Stack Overflow:

x3FFD

TOS =

x3FFE

Stack Underflow:

x4000

x3FFB

MAX

x3FFC

x3FFF

BASE

TOS = CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

18

9

Stack protocol on LC-3 - Example • Use standard conventions: – – – – –

PUSH pushes R0, returns success in R5 POP pops into R0, returns success in R5 Stack pointer is R6 All other used registers need to be callee-saved The TOS points to the top valid element

• The stack goes from x3FFF to x3FFB

19

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Stack protocol in LC-3: POP POP

ST ST LD ADD ADD BRz LDR ADD BRnzp

R2, Save2 R1, Save1 R1, BASE R1, R1, #-1 R2, R6, R1 fail_exit R0, R6, #0 R6, R6, #1 success_exit

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

; ; ; ; ; ; ; ;

(1/3)

save, needed by POP save, needed by POP BASE contains x-3FFF R1 now has x-4000 compare SP to x-4000 branch if stack is empty the actual ‘pop’ adjust stack pointer

20

10

Stack protocol in LC-3: PUSH PUSH

ST ST LD ADD BRz ADD STR

R2, Save2 R1, Save1 R1, MAX R2, R6, R1 fail_exit R6, R6, #-1 R0, R6, #0

; ; ; ; ; ; ;

(2/3)

needed by PUSH needed by PUSH MAX has x-3FFB compare SP to x-3FFB branch is stack is full adjust Stack Pointer the actual ‘push’

21

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

Stack prot. in LC-3: Return values (3/3)

success_exit

fail_exit

BASE MAX Save1 Save2

LD R1, Save1 LD R2, Save2 AND R5, R5, #0 RET LD R1, Save1 LD R2, Save2 AND R5, R5, #0 ADD R5, R5, #1 RET .FILL xC001 .FILL xC005 .FILL x0000 .FILL x0000

CMPE12 – Fall 2006 – A. Di Blas (Orig. by C. Barzeghi)

; restore registers ; R5

Suggest Documents