LC3 Stack

advertisement
LC-3 Data Structures
Textbook chapter 10
LC-3 data structures

Abstract data structures are…


Defined by the rules for inserting and extracting
data
In this section, we will talk about…



The array
The stack
Arithmetic using a stack
CMPE12 – Summer 2009
13-2
The array data structure




Array: A list of values arranged sequentially in
memory and grouped under a single name
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)
E.g., a list of integers starting at 5
number[0] = 5;
 number[1] = 6;
 number[2] = 7;
 number[3] = 8;

CMPE12 – Summer 2009
13-3
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
E.g.,

myArray
CMPE12 – Summer 2009
.BLKW
5
13-4
What is a pointer?

A pointer is an address of a variable in memory


Lets the programmer use indirect addressing


Indirectly address variables
Base of an array is the pointer to the first
element


It’s just an address
Wait, what?
myArray

.BLKW
5
What happens when LEA R2, myARRAY?
CMPE12 – Summer 2009
13-5
Array example
LEA
LDR
STR
myArray
CMPE12 – Summer 2009
R2, myArray
R0, R2, #0
R0, R2, #2
.BLKW 5
13-6
Multi-dimensional arrays?


How do you represent a
checkerboard?
The problem: memory is
linear
 One-dimensional
CMPE12 – Summer 2009
13-7
Multi-dimensional arrays




Consider two rows
Array elements are sublabeled with their row and
column
 A: 2 rows (numbered 0
and 1)
 A: 4 columns
(numbered 0 to 3)
In array A:
 A02 is the 0th row and
2nd column
How do you map this
onto a linear structure?
CMPE12 – Summer 2009
A 0 1 2 3
0 A00 A01 A02 A03
1 A10 A11 A12 A13
13-8
Array memory mappings

Row major


Read down the row first
Column major

Read down the column first
CMPE12 – Summer 2009
13-9
Array representation: Row major
x3100
A 0 1 2 3
x3101
x3102
x3103
0 1 2 3 4
x3104
x3105
1 5 6 7 8
x3106
x3107
CMPE12 – Summer 2009
13-10
Array representation: Column major
x3100
A 0 1 2 3
x3101
x3102
x3103
0 1 2 3 4
x3104
x3105
1 5 6 7 8
x3106
x3107
CMPE12 – Summer 2009
13-11
FIFO (Standard queue)

FIFO is “first in, first out”
Standard queue (waiting in line)
FRONT

4
CMPE12 – Summer 2009
3
2
1
SHOES
13-13
LIFO (Standard stack)

LIFO is “last in, first out”
Sometimes called FILO, “first in, last out”
 Standard stack

4
3
2
1
CMPE12 – Summer 2009
TOP
13-14
Example: LIFO and FIFO
(LIFO) Stack:
A, B, C
CMPE12 – Summer 2009
C, B, A
(FIFO) Queue:
D, E, F
D, E, F
C
F
B
E
A
D
13-15
The stack data structure


Stack structure is LIFO (last in, first out)
Basic pieces of stack
Stack itself
 Top of stack


Two basic stack operations
Push: Put data on top of stack
 Pop: Remove top element from stack

CMPE12 – Summer 2009
13-16
Stack overflow and underflow

Trying to pop from empty stack


Underflow
Trying to push onto full stack

Overflow
CMPE12 – Summer 2009
13-18
Stack overflow
R1
R0
.
TOP
.
push R0
push R1
.
.
CMPE12 – Summer 2009
13-19
Stack underflow
.
.
pop R0
pop R1
.
TOP
CMPE12 – Summer 2009
.
13-20
A coin holder as a stack
CMPE12 – Summer 2009
13-21
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
CMPE12 – Summer 2009
13-22
Stacks in hardware and software

Implemented in hardware with registers


Previous data entries move up to accommodate
each new data entry
Implemented in software with code, in memory
Stack pointer points to the top (empty) element
 Stack grows from high (0xffff) to low (0x0000)
memory

CMPE12 – Summer 2009
13-23
Stack in LC-3

In LC-3

The stack pointer moves as new data is entered
 R6
acts as the stack pointer (the TOS register)
CMPE12 – Summer 2009
13-24
PUSH in LC-3

Write data to top empty location of stack

Top of stack is pointed to by stack pointer (SP)

Decrement stack pointer (stack is moving down
in memory)

E.g., Assume data to push is in R0
PUSH
STR R0, R6, #0
ADD R6, R6, #-1

[R0]
CMPE12 – Summer 2009
R6
13-25
POP in LC-3

Increment stack pointer (stack is moving down
in memory)

Now points to full location

Read data from top of stack

E.g., Assume data to pop goes into R0
POP
ADD R6, R6, #1
LDR R0, R6, #0

R6
CMPE12 – Summer 2009
13-26
Checking for overflow and 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
Flow chart is similar for push and overflow
POP
CMPE12 – Summer 2009
13-27
When is the stack empty or full?


If SP always points to
next empty element (next
available location to
push)
x3FFA
Stack overflow when…
 SP =
x3FFD
x3FFB
MAX
x3FFC
x3FFE

Stack underflow when…
 SP =
x3FFF
BASE
x4000
CMPE12 – Summer 2009
13-28
Stack protocol on LC-3: An example

Conventions
pushes R0, returns success in R5
 POP pops into R0, returns success in R5
 Stack pointer is R6 and points to the top
empty element
 PUSH
 All
other used registers need to be calleesaved
and POP should not overwrite any
other registers
 PUSH

The stack goes from x3FFF to x3FFB
CMPE12 – Summer 2009
13-30
Stack protocol in LC-3: POP
POP
ST
ST
LD
ADD
ADD
BRz
ADD
LDR
BRnzp
CMPE12 – Summer 2009
R2, Save2
R1, Save1
R1, nBASE
R1, R1, #-1
R2, R6, R1
fail_exit
R6, R6, #1
R0, R6, #0
success_exit
;
;
;
;
;
;
;
;
save, needed by POP
save, needed by POP
nBASE contains -x3FFF
R1 now has -x4000
compare SP to BASE
branch if stack is empty
adjust stack pointer
the actual ‘pop’
13-31
Stack protocol in LC-3: PUSH
PUSH
ST
ST
LD
ADD
BRz
STR
ADD
CMPE12 – Summer 2009
R2, Save2
R1, Save1
R1, nMAX
R2, R6, R1
fail_exit
R0, R6, #0
R6, R6, #-1
;
;
;
;
;
;
;
needed by PUSH
needed by PUSH
nMAX has -x3FFB
compare SP to x3FFB
branch is stack is full
the actual ‘push’
adjust stack pointer
13-32
Stack protocol in LC-3: Return values
success_exit
LD
R1, Save1
LD
R2, Save2
AND
R5, R5, #0
RET
fail_exit
LD
R1, Save1
LD
R2, Save2
AND R5, R5, #0
ADD R5, R5, #1
RET
nBASE
.FILL xC001
nMAX
.FILL xC005
Save1
.FILL x0000
Save2
.FILL x0000
CMPE12 – Summer 2009
; restore registers
; R5 <-- success
; restore registers
; R5 <-- fail
; nBASE has -x3FFF
; nMAX has –x3FFB
13-33
Stack as an alternative to registers
Three-address vs zero-address
 The LC-3 explicitly specifies the location of each
operand: it is a three-address machine


e.g.
ADD
R0, R1, R2
Some machines use a stack data structure for
all temporary data storage: these are zeroaddress machines
The instruction ADD would simply pop the top two
values from the stack, add them, and push the
result back on the stack
 Some calculators use a stack to do arithmetic,
most general purpose microprocessors use a
register bank

CMPE12 – Summer 2009
13-34
Recommended exercises



Read and implement the examples in textbook
sections 10.3, 10.4, and 10.5
Ex 10.3, 10.4, 10.5 ok but tedious
Ex 10.6 (and 10.7), 10.8, 10.9
CMPE12 – Summer 2009
13-35
Download