Upload
buinhan
View
218
Download
4
Embed Size (px)
Citation preview
LC-3 Data StructuresTextbook chapter 10
CMPE12 – Summer 2009 13-2
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-3
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-4
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 .BLKW 5
CMPE12 – Summer 2009 13-5
What is a pointer? A pointer is an address of a variable in memory
It’s just an address Lets the programmer use indirect addressing
Indirectly address variables Base of an array is the pointer to the first
element Wait, what?
myArray .BLKW 5 What happens when LEA R2, myARRAY?
CMPE12 – Summer 2009 13-6
Array exampleLEA R2, myArrayLDR R0, R2, #0STR R0, R2, #2
myArray .BLKW 5
CMPE12 – Summer 2009 13-7
Multi-dimensional arrays? How do you represent a
checkerboard? The problem: memory is
linear One-dimensional
CMPE12 – Summer 2009 13-8
Multi-dimensional arrays Consider two rows Array elements are sub-
labeled 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?
A 0 1 2 3
0 A00 A01 A02 A03
1 A10 A11 A12 A13
CMPE12 – Summer 2009 13-9
Array memory mappings Row major
Read down the row first Column major
Read down the column first
CMPE12 – Summer 2009 13-10
Array representation: Row major
A 0 1 2 3
0 1 2 3 4
1 5 6 7 8
x3100
x3101
x3102
x3103
x3104
x3105
x3106
x3107
CMPE12 – Summer 2009 13-11
Array representation: Column major
A 0 1 2 3
0 1 2 3 4
1 5 6 7 8
x3100
x3101
x3102
x3103
x3104
x3105
x3106
x3107
CMPE12 – Summer 2009 13-13
FIFO (Standard queue) FIFO is “first in, first out”
Standard queue (waiting in line)
4 3 2 1 SHOESFR
ON
T
CMPE12 – Summer 2009 13-14
LIFO (Standard stack) LIFO is “last in, first out”
Sometimes called FILO, “first in, last out” Standard stack
1234
TOP
CMPE12 – Summer 2009 13-15
Example: LIFO and FIFO(LIFO) Stack: (FIFO) Queue:
A, B, C D, E, F
A
B
C
D
E
F
C, B, A D, E, F
CMPE12 – Summer 2009 13-16
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-18
Stack overflow and underflow Trying to pop from empty stack
Underflow Trying to push onto full stack
Overflow
CMPE12 – Summer 2009 13-19
Stack overflow
TOPR0R1 .
.
push R0
push R1
.
.
CMPE12 – Summer 2009 13-20
Stack underflow
TOP
.
.
pop R0
pop R1
.
.
CMPE12 – Summer 2009 13-21
A coin holder as a stack
CMPE12 – Summer 2009 13-22
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-23
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-24
Stack in LC-3 In LC-3
The stack pointer moves as new data is enteredR6 acts as the stack pointer (the TOS register)
CMPE12 – Summer 2009 13-25
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
R6[R0]
CMPE12 – Summer 2009 13-26
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-27
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-28
When is the stack empty or full? If SP always points to
next empty element (next available location to push)
Stack overflow when… SP =
Stack underflow when… SP =
x3FFA
x3FFB
x3FFC
x3FFD
x3FFE
x3FFF
x4000
BASE
MAX
CMPE12 – Summer 2009 13-30
Stack protocol on LC-3: An example Conventions
PUSH pushes R0, returns success in R5POP pops into R0, returns success in R5Stack pointer is R6 and points to the top
empty elementAll other used registers need to be callee-
savedPUSH and POP should not overwrite any
other registers The stack goes from x3FFF to x3FFB
CMPE12 – Summer 2009 13-31
Stack protocol in LC-3: POP
POP ST R2, Save2 ; save, needed by POPST R1, Save1 ; save, needed by POPLD R1, nBASE ; nBASE contains -x3FFFADD R1, R1, #-1 ; R1 now has -x4000ADD R2, R6, R1 ; compare SP to BASEBRz fail_exit ; branch if stack is emptyADD R6, R6, #1 ; adjust stack pointerLDR R0, R6, #0 ; the actual ‘pop’BRnzp success_exit
CMPE12 – Summer 2009 13-32
Stack protocol in LC-3: PUSH
PUSH ST R2, Save2 ; needed by PUSHST R1, Save1 ; needed by PUSHLD R1, nMAX ; nMAX has -x3FFBADD R2, R6, R1 ; compare SP to x3FFBBRz fail_exit ; branch is stack is fullSTR R0, R6, #0 ; the actual ‘push’ADD R6, R6, #-1 ; adjust stack pointer
CMPE12 – Summer 2009 13-33
Stack protocol in LC-3: Return valuessuccess_exit
LD R1, Save1 ; restore registers LD R2, Save2AND R5, R5, #0 ; R5 <-- successRET
fail_exitLD R1, Save1 ; restore registers LD R2, Save2AND R5, R5, #0ADD R5, R5, #1 ; R5 <-- failRET
nBASE .FILL xC001 ; nBASE has -x3FFFnMAX .FILL xC005 ; nMAX has –x3FFBSave1 .FILL x0000Save2 .FILL x0000
CMPE12 – Summer 2009 13-34
Stack as an alternative to registersThree-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 zero-address 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-35
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