And, Finally... The Stack
And, Finally... The Stack
And, Finally... The Stack
The Stack
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
10-2
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Stacks
A LIFO (last-in first-out) storage structure.
The first thing you put in is the last thing you take out. The last thing you put in is the first thing you take out.
This means of access is what defines a stack, not the specific implementation. Two main operations: PUSH: add an item to the stack POP: remove an item from the stack
10-3
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
A Physical Stack
Coin rest in the arm of an automobile
1995 1996 1998 1982 1995 1998 1982 1995
Initial State
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
A Hardware Implementation
Data items move between registers
Empty:
Yes
Empty: TOP
No
Empty: TOP
No
Empty: TOP
No
TOP
10-5
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
A Software Implementation
Data items don't move in memory, just our idea about there the TOP of the stack is.
TOP R6
TOP
TOP
TOP
R6
R6
R6
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Pop LDR ADD R0, R6, #0 R6, R6, #1 ; load data from TOS ; decrement stack ptr
10-7
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
; SUCCESS: R5 = 0 ; FAIL: R5 = 1
10-8
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
FAIL
MAX
10-9
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Processor State
What state is needed to completely capture the state of a running process? Processor Status Register
Privilege [15], Priority Level [10:8], Condition Codes [2:0]
Program Counter
Pointer to next instruction to be executed.
Registers
All temporary state of the process thats not stored in memory.
10-11
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Use a stack!
Location of stack hard-wired. Push state to save, pop to restore. 10-12
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Supervisor Stack
A special region of memory used as the stack for interrupt service routines.
Initial Supervisor Stack Pointer (SSP) stored in Saved.SSP. Another register for storing User Stack Pointer (USP): Saved.USP.
When switching from User mode to Supervisor mode (as result of interrupt), save R6 to Saved.USP.
10-13
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Note: This all happens between the STORE RESULT of the last user instruction and the FETCH of the first ISR instruction.
10-14
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1. Pop PC from supervisor stack. (PC = M[R6]; R6 = R6 + 1) 2. Pop PSR from supervisor stack. (PSR = M[R6]; R6 = R6 + 1) 3. If PSR[15] = 1, R6 = Saved.USP. (If going back to user mode, need to restore User Stack Pointer.)
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example (1)
Program A
x3006
ADD
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example (2)
Program A x6200 ISR for Device B
R6
x3006
ADD RTI
x6210
////// PC x6200
Saved.USP = R6. R6 = Saved.SSP. Push PSR and PC onto stack, then transfer to Device B service routine (at x6200).
10-17
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example (3)
Program A x6200 x6202 x3006 ISR for Device B
R6
AND
ADD RTI
x6210
////// PC x6203
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example (4)
Program A x6200 x6202 x3006 ISR for Device B
R6
x6203
PSR for B
AND
ISR for Device C
ADD RTI
x6300
x3007
PSR for A x6210
////// PC x6300
x6315
RTI
Push PSR and PC onto stack, then transfer to Device C service routine (at x6300).
10-19
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example (5)
Program A x6200 x6202 x3006 ISR for Device B
x6203
PSR for B
AND
ISR for Device C
ADD RTI
x6300
R6
x3007
PSR for A x6210
////// PC x6203
x6315
RTI
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example (6)
Program A
Saved.SSP x6203
PSR for B x3006 x6200 x6202
AND
ISR for Device C
ADD RTI
x6300
x3007
PSR for A x6210
////// PC x3007
x6315
RTI
Execute RTI at x6210; pop PSR and PC from stack. Restore R6. Continue Program A as if nothing happened.
10-21
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: OpAdd
POP two values, ADD, then PUSH result.
START
POP
POP
ADD
OK?
Yes No
OK?
No
Yes
Range OK?
No
Yes
PUSH
RETURN
10-24
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: OpAdd
OpAdd JSR ADD BRp ADD JSR ADD BRp ADD JSR BRp JSR RET ADD ADD RET POP R5,R5,#0 Exit R1,R0,#0 POP R5,R5,#0 Restore1 R0,R0,R1 RangeCheck Restore2 PUSH R6,R6,#-1 R6,R6,#-1 ; ; ; ; ; ; ; ; ; ; ; Get first operand. Check for POP success. If error, bail. Make room for second. Get second operand. Check for POP success. If err, restore & bail. Compute sum. Check size. If err, restore & bail. Push sum onto stack.
10-25
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
User inputs 2 and 3 -- what happens? Result displayed: e Why? ASCII '2' (x32) + ASCII '3' (x33) = ASCII 'e' (x65)
10-26
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ASCII to Binary
Useful to deal with mult-digit decimal numbers Assume we've read three ASCII digits (e.g., "259") into a memory buffer. How do we convert this to a number we can use?
Convert first character to digit (subtract x30) and multiply by 100. Convert second character to digit and multiply by 10. Convert third character to digit. Add the three digits together. x32 x35 x39 '2' '5' '9'
10-27
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Since we have a small range of numbers (0-9), use number as an index into a lookup table.
Entry 0: Entry 1: Entry 2: Entry 3: etc. 0 x 100 = 0 1 x 100 = 100 2 x 100 = 200 3 x 100 = 300
10-28
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
10-30
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Conversion Routine (2 of 3)
ADD BRz ADD ; LDR ADD LEA ADD LDR ADD ; ADD BRz ADD R1, R1, #-1 DoneAtoB R2, R2, #-1 ; ; ; ; one less digit done if zero points to hundreds digit R4, R4, R5, R5, R4, R0, R2, #0 R4, R3 Lookup10 R5, R4 R5, #0 R0, R4 ; load digit ; convert to number ; multiply by 10 R1, R1, #-1 DoneAtoB R2, R2, #-1 ; one less digit ; done if zero ; points to tens digit
10-31
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Conversion Routine (3 of 3)
LDR ADD LEA ADD LDR ADD ; DoneAtoB NegZero ASCIIBUF Lookup10 R4, R4, R5, R5, R4, R0, R2, #0 ; load digit R4, R3 ; convert to number Lookup100 ; multiply by 100 R5, R4 R5, #0 R0, R4 ; adds 100's contrib
xFFD0 4 0 10 20
; -x30
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
10-33
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
NegSign
10-34
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Conversion (2 of 3)
Begin100 Loop100 LD R2, ASCIIoffset LD R3, Neg100 ADD R0, R0, R3 BRn End100 ADD R2, R2, #1 ; add one to digit BRnzp Loop100 STR R2, R1, #1 ; store ASCII 100's digit LD R3, Pos100 ADD R0, R0, R3 ; restore last subtract LD R2, ASCIIoffset LD R3, Neg10 ADD R0, R0, R3 BRn End10 ADD R2, R2, #1 ; add one to digit BRnzp Loop10 10-35
End100
Loop100
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Conversion Code (3 of 3)
End10 ; LD R2, ASCIIoffset ADD R2, R2, R0 ; convert one's digit STR R2, R1, #3 ; store one's digit RET ; ASCIIplus ASCIIneg ASCIIoffset Neg100 Pos100 Neg10 STR R2, R1, #2 ; store ASCII 10's digit ADD R0, R0, #10 ; restore last subtract
; ; ; ;
; -10
10-36