stacking up - binghamton

26
Binghamton University CS-120 Summer 2015 Stacking Up Text: Introduction to Computer Systems : Chapter 10

Upload: others

Post on 11-Dec-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Stacking UpText: Introduction to Computer Systems : Chapter 10

Page 2: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Data Structures

• Conceptual Models

• Organize how we think about data

• Imply extra rules on how we can interact with data• Reduce freedom of interaction with data

• Enables clearer (implied) understanding, more power

• Challenge – apply the right data structure to the right problem

Page 3: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Vector Data Structure

• List of data n items

• Each data item contains a single word

• We can read or write any data item using an index

• Index starts at zero, and ends at n-1

• Hard to insert or delete individual data elements at arbitrary indexes

Page 4: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Vectors in LC3

lea r1,VEC ; r1->VEC

ld r2,I ; r2=I

add r3,r1,r2 ; r3->VEC[I]

ldr r4,r3,#0 ; r4=VEC[I] – Read VEC[I]

str r5,r3,#0 ; VEC[I]=r5 – Write VEC[I]

VEC .blkw 23 ; VEC – vector with n=23

Page 5: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Data Structure Type

• Data structures: Organized collection of lower level data• Vector: List of numbers

• Vector: List of LC3 instructions

• Vector: List of LC3 words

• Vector: List of LC3 pairs of words

Page 6: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Example: Vector of (x,y) coordinates; Subroutines

getx ; put r6[r0].X in r1

add r2,r0,r0

add r2,r2,r6

ldr r1,r2,#0

ret

gety ; put r6[r0].Y in r1

add r2,r0,r0

add r2,r2,r6

ldr r1,r2,#1

ret

negr3 ; negate the value in r3

not r3,r3

add r3,r3,#1

ret

; find lowest x coord

ld r3,NMAX

ld r0,COUNT

lea r6,VEC

minl add r0,r0,#-1

brn done

jsr getx

add r4,r3,r1

brzp minl

add r3,r1,#0

jsr negr3

br minl

done jsr negr3

st r3,MINX

HALT

; data

NMAX .fill x8001

COUNT .fill #23

MINX .fill #0

VEC .blkw 46

Page 7: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

The Stack

Page 8: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Stack Terminology (Stack of Books)

• Empty Stack : space on the floor for a stack of books

• Push : put a new book on the top of the stack of books

• Pop : take a book from the top of the stack of books

• Full Stack : the top of the stack of books hits the ceiling

• Stack Size : the number of books that can fit between the floor and the ceiling

• Stack Underflow : Trying to take a book from an empty stack

• Stack Overflow : Trying to add a book to a full stack

Page 9: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Other Pipe-Like Data Structures

• Stack – Last-in / First-out List (LIFO)• Supports PUSH and POP

• Que - First-in / First out List (FIFO)• Supports QUE and POP

• DeQue (pronounced “deck”) – Combo Stack/Que• Supports PUSH/QUE to add

• Supports POP/DEQUE to remove

Page 10: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Pipe Data Structures

PUSH

POP

QUE

DEQUE

Page 11: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Stack Direction

• Push/Pop from the “top” of the stack• Stack grows up

• Push on top of stack, Pop from top of stack

• Obvious and Intuitive (like a stack of books)

• Push/Pop from the “bottom” of the stack• Stack grows down

• Push on bottom of stack, Pop from bottom of stack

• Counter intuitive!!!!

• Useful when two stacks grow towards the middle

• Implemented this way in text

Page 12: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Implementing a Stack in LC3

• Each element in the stack is a 16 bit word

• Need a pointer to the base of the stack (BASE)• Negate for quick compare (STKMIN)

• Need a pointer to the current top of the stack (R6)

• Need a pointer to the maximum stack entry (STKMAX)• Negated for quick compare

Page 13: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Creating an Empty StackINITSTK ; Initialize a stack

st r5,INR5ld r6,BASE ;not r5,r6add r5,r5,#1st r5,STKMINld r5,STKSIZEadd r5,r5,r6not r5,r5add r5,r5,#1st r5,STKMAXld r5,INR5ret

BASE .fill 0x6000 STKSIZE .fill 0x1000 STKMIN .fill #0STKMAX .fill #0INR5 .fill #0OVERFLOW .fill #0UNDERFLOW .fill #0

Page 14: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Pushing on the stackPUSH ; push the value in r0 on the stack

st r5,PSR5ld r5,STKMAXadd r5,r5,r6brnz PUSHOKld r7,OVERFLOWbr PUSHEX

PUSHOK str r0,r6,#0add r6,r6,#1

PUSHEX ld r5,PSR5ret

PSR5 .fill #0

Page 15: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Popping from this stackPOP ; Pop value from the stack into r0

st r5,POR5

add r6,r6,#-1

ld r5,STKMIN

add r5,r5,r6

brzp POPOK

add r6,r6,#1

ld r7,UNDERFLOW

br POPEX

POPOK ldr r0,r6,#0

POPEX ld r5,POR5

ret

POR5 .fill #0

Page 16: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Using the Stack – Example Specification

Given VEC, a numeric vector with N entries, sort the vector from lowest to highest

Page 17: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Stack Sortinit stack; ordered=0

do {

push vec[0] on stack

j=0; ordered=1

for (i=1, i<n; i++) {

if (vec[i]>=stack) {

while(x=pop()) { vec[j]=x, j=j+1 }

} else ordered=0

push vec[i] on stack;

}

while(x=pop()) { vec[j]=x, j=j+1 }

} until (ordered=1)

Page 18: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

New Routine: SCOMP

SCOMP ; Compare value in R0 with head of stack…. set CC

; CC=N R0<Stk ; CC=Z R0=Stk ; CC=P R0>Stk

; WARNING: Assumes stack is not empty!

; WARNING: Modifies R5

ldr r5,r6,#-1

not r5,r5

add r5,r5,#1

add r5,r0,r5

ret

Page 19: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

StackSort Register Usage

REG Usage

R0 Current Value (usually VEC[i] or VEC[j])

R1 ORDERED flag (0=not ordered, 1=ordered)

R2 →VEC[i]

R3 →VEC[j]

R4 Negative N

R5 Temp Values (Used in SCOMP)

R6 Current Stack Pointer

R7 Subroutine Return Address

Page 20: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

StackSort LC3jsr INITSTK

and r1,r1,#0

ld r4,N

lea r5,VEC

add r4,r4,r5

not r4,r4

add r4,r4,#1

oloop: lea r2,VEC

add r3,r2,#0

ldr r0,r2,#0

jsr PUSH

add r1,r1,#1

lea r7,iempty

st r7,UNDERFLOW

iloop add r2,r2,#1

add r5,r4,r2

brp eiloop

ldr r0,r2,#0

jsr SCOMP

brnz norder

ipl jsr POP

str r0,r3,#0

add r3,r3,#1

br ipl

norder and r1,r1,#0

iempty ldr r0,r2,#0

jsr PUSH

br iloop

eiloop lea r7,oempty

st r7,UNDERFLOW

opl jsr POP

str r0,r3,#0

add r3,r3,#1

br opl

oempty add r1,r1,#0

brz oloop

HALT

Page 21: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Stack Application 2 - Calculator

“Reverse Polish Notation”

• If you get a number, push it

• If you get an operator:• pop arguments • Calculate• push the result

• For example, instead of “3 + 4”, we write “3 4 +”

• For example, instead of “(3 + 4) x 5” we write “3 4 + 5 x”

• For example, instead of “3 + 4 x 5” we write “3 4 5 x +”

Page 22: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Example Calculator ADD function

ADD ; pop 2 values, add, and push the result

jsr pop

add r1,r0,#0

jsr pop

add r0,r1,r0

jsr push

ret

Page 23: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Subroutine Calling Itself (Recursion)

• For example: Factorial

fact(n)=fact(n-1) x n for n>1

fact(1)=1

• Q: How where do I save/restore R7?

• Q: Can I assume FACT does not change R1?????

FACT ; arg in r1 result in r2 (uses r3)

; Save R7

add r2,r1,#0 ;

add r1r1,#-1

brnz fend

jsr FACT

add r3,r2,#0

fml add r2,r2,r3

add r1,r1,#-1

brp fml

fend ; Restore R7

ret

Page 24: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Subroutine Calling Itself (Recursion)

• For example: Factorial

fact(n)=fact(n-1) x n for n>1

fact(1)=1

• Need to save R7 before calling save

• Need to restore R7 after calling restore

FACT ; arg in r1 result in r2 (uses r3)

st r7,FACTR7

jsr FACTSAVE

add r2,r1,#0

add r1,r1,#-1

brnz fend

jsr FACT

add r3,r2,#0

fml add r2,r2,r3

add r1,r1,#-1

brp fml

fend jsr FACTREST

ld r7,FACTR7

ret

FACTR7 .fill #0

Page 25: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Stack Usage – Manage Recursion

FACTSAVE ; Save FACTR7 and R1 in stack

st r7,FSR7

st r0,FSR0

ld r0,FACTR7

jsr PUSH

add r0,r1,#0

jsr PUSH

ld r0,FSR0

ld r7,FSR7

ret

FSR0 .fill #0

FSR7 .fill #0

FACTREST ; Restore FACTR7 and R1 from stack

st r7,FRR7

st r0,FRR0

jsr POP

add r1,r0,#0

jsr POP

st r0,FACTR7

ld r0,FRR0

ld r7,FRR7

ret

FRR0 .fill #0

FRR7 .fill #0

Page 26: Stacking Up - Binghamton

Binghamton

University

CS-120

Summer 2015

Binghamton

University

CS-120

Summer 2015

Invoking FACT

jsr INITSTK

ld r1,QFACT

jsr FACT

st r2,ANS

HALT

QFACT .fill #5

ANS .fill #0