1
LC-3 Data StructuresTextbook chapter 10
CMPE12 – Summer 2008
CMPE12 – Summer 2008 – Slides by ADB 2
LC-3 data structuresAbstract data structures are…
Defined by the rules for inserting and extracting data
In this section, we will talk about…The arrayThe stackArithmetic using a stack
2
CMPE12 – Summer 2008 – Slides by ADB 3
The array data structureArray: A list of values arranged sequentially in memory and grouped under a single nameIn C, the expression a[4] refers to the 5th element of the array aMost 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 2008 – Slides by ADB 4
Properties of arraysEach element is the same size (i.e. same type)Elements are stored contiguouslyFirst element is located at the lowest memory address
In assembly language we mustAllocate correct amount of space for an arrayMap array addresses to memory addresses
E.g.,myArray .BLKW 5
3
CMPE12 – Summer 2008 – Slides by ADB 5
What is a pointer?A pointer is an address of a variable in memory
It’s just an addressLets the programmer use indirect addressing
Indirectly address variablesBase of an array is the pointer to the first element
Wait, what?myArray .BLKW 5
What happens when LEA R2, myARRAY?
CMPE12 – Summer 2008 – Slides by ADB 6
Array exampleLEA R2, myArrayLDR R0, R2, #0STR R0, R2, #2
myArray .BLKW 5
4
CMPE12 – Summer 2008 – Slides by ADB 7
Multi-dimensional arrays?How do you represent a checkerboard?The problem: memory is linear
One-dimensional
CMPE12 – Summer 2008 – Slides by ADB 8
Multi-dimensional arraysConsider two rowsArray 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?
A13A12A11A101
A03A02A01A000
3210A
5
CMPE12 – Summer 2008 – Slides by ADB 9
Array memory mappingsRow major
Read down the row firstColumn major
Read down the column first
CMPE12 – Summer 2008 – Slides by ADB 10
Array representation: Row major
87651
43210
3210A
x3107
x3106
x3105
x3104
x3103
x3102
x3101
x3100
6
CMPE12 – Summer 2008 – Slides by ADB 11
Array representation: Column major
87651
43210
3210A
x3107
x3106
x3105
x3104
x3103
x3102
x3101
x3100
CMPE12 – Summer 2008 – Slides by ADB 13
FIFO (Standard queue)FIFO is “first in, first out”
Standard queue (waiting in line)
4 3 2 1 SHOES
FRO
NT
7
CMPE12 – Summer 2008 – Slides by ADB 14
LIFO (Standard stack)LIFO is “last in, first out”
Sometimes called FILO, “first in, last out”Standard stack
1234
TOP
CMPE12 – Summer 2008 – Slides by ADB 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
8
CMPE12 – Summer 2008 – Slides by ADB 16
The stack data structureStack structure is LIFO (last in, first out)Basic pieces of stack
Stack itselfTop of stack
Two basic stack operationsPush: Put data on top of stackPop: Remove top element from stack
CMPE12 – Summer 2008 – Slides by ADB 18
Stack overflow and underflowTrying to pop from empty stack
UnderflowTrying to push onto full stack
Overflow
9
CMPE12 – Summer 2008 – Slides by ADB 19
Stack overflow
TOPR0R1 .
.
push R0
push R1
.
.
CMPE12 – Summer 2008 – Slides by ADB 20
Stack underflow
TOP
.
.
pop R0
pop R1
.
.
10
CMPE12 – Summer 2008 – Slides by ADB 21
A coin holder as a stack
CMPE12 – Summer 2008 – Slides by ADB 22
A hardware stackImplemented in hardware (i.e., with registers)Previous data entries move up to accommodate each new data entryNote that the Top Of Stack is always in the same place
11
CMPE12 – Summer 2008 – Slides by ADB 23
Stacks in hardware and softwareImplemented in hardware with registers
Previous data entries move up to accommodate each new data entry
Implemented in software with code, in memoryStack pointer points to the top (empty) elementStack grows from high (0xffff) to low (0x0000) memory
CMPE12 – Summer 2008 – Slides by ADB 24
Stack in LC-3In LC-3
The stack pointer moves as new data is enteredR6 acts as the stack pointer (the TOS register)
12
CMPE12 – Summer 2008 – Slides by ADB 25
PUSH in LC-3Write 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 R0PUSH ADD R6, R6, #-1
STR R0, R6, #1
R6[R0]
CMPE12 – Summer 2008 – Slides by ADB 26
POP in LC-3Increment stack pointer (stack is moving down in memory)
Now points to full locationRead data from top of stack
E.g., Assume data to pop goes into R0POP LDR R0, R6, #1
ADD R6, R6, #1
R6
13
CMPE12 – Summer 2008 – Slides by ADB 27
Checking for overflow and underflowBefore pushing, we have to test for overflowBefore popping, we have to test for underflowIn both cases, use R5 to report success or failureFlow chart is similar for push and overflow
POP
CMPE12 – Summer 2008 – Slides by ADB 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 =
x4000
x3FFF
x3FFE
x3FFD
x3FFC
x3FFB
x3FFA
BASE
MAX
14
CMPE12 – Summer 2008 – Slides by ADB 30
Stack protocol on LC-3: An exampleConventionsPUSH 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 2008 – Slides by ADB 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 emptyLDR R0, R6, #1 ; the actual popADD R6, R6, #1 ; adjust stack pointerBRnzp success_exit
15
CMPE12 – Summer 2008 – Slides by ADB 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 fullADD R6, R6, #-1 ; adjust stack pointerSTR R0, R6, #1 ; the actual push
CMPE12 – Summer 2008 – Slides by ADB 33
Stack protocol in LC-3: Return valuessuccess_exit:
LD R1, Save1 ; restore registers LD R2, Save2AND R5, R5, #0 ; R5 <-- successRET
fail_exit:LD 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
16
CMPE12 – Summer 2008 – Slides by ADB 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, R2Some 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 stackSome calculators use a stack to do arithmetic; most general purpose microprocessors use a register bank
CMPE12 – Summer 2008 – Slides by ADB 35
Recommended exercises
Read and implement the examples in textbook sections 10.3, 10.4, and 10.5Ex 10.3, 10.4, 10.5 ok but tediousEx 10.6 (and 10.7), 10.8, 10.9
Top Related