Download Computer Organization and Assembly Language Programming - Notes | CS 350 and more Study notes Programming Languages in PDF only on Docsity!
CS 350
Computer Organization & Assembly
Language Programming
Spring 2009
Notes 15, 2009–03–
1 To read in a string terminated by a carriage return: pt first char in buffer read character into R while R0! carriage return store R0 into buffer increment buffer pointer read character into R Store null into buffer [Note: this doesn't handle possibility of running past end of buffer]
Question: How do we read in
a string?
Chapter 10: The Stack
3
Stacks
• "Stack" is an important abstract data type
- Interrupt-Driven I/O
+ For tracking nested interrupts
- Handling nested structures
+ Examples involving arithmetic expressions
- Reversing data
+ Example: 2’s comp binary to ASCII strings
LC-3 Stack
• By convention, R6 points to top of stack
TOS), and stack grows downward
• Push
- ADD R6, R6, #-1!; pt next empty slot
STR R0, R6, #0! ; store data in R
• Pop
- LDR R0, R6, #0!^ ; load data from^ TOS
ADD R6, R6, #1! ; pt new empty slot
7
LC-3 Stack
• How to initialize stack pointer?
- Using pre-decrement PUSH / post-increment
POP
+ Stack pointer points to last location pushed
into
- So stack pointer should be initialized to
largest legal address + 1
Overflow & Underflow
• Overflow occurs when you push onto a
full stack
- If stack pointer = max top then overflow
• Underflow occurs when you pop an
empty stack.
- If stack pointer = bottom+1 then underflow
• Return R5 = 1 if push/pop fail, R5 = 0 on
success; also set P or N condition code
9 ; Subroutines for carrying out the PUSH and POP functions. ; R6 is the stack pointer. ; INIT ST R1,Save1 ; Save registers LD R6,BOTTOM : init stack pointer ADD R1,R6,#1 ; Set underflow indicator NOT R1,R1 ; ... as negative of bottom+ ADD R1, #1 ; ... ST R1,UFLOW ; ... LD R1,MINTOP ; Set overflow indicator NOT R1,R1 ; ... as neg(MINTOP) ADD R1,#1 ; ... ST R1,OFLOW ; ... LD R1,Save1 ; Restore registers RET ; and return BOTTOM .FILL x3FFF ; Pt bottom of stack MINTOP .FILL x3FFB ; Pt min legal top value
Initialize Stack
PUSH ST R2,Save2 ; Save registers that ST R1,Save1 ; are needed by PUSH. LD R1,OFLOW ; MINTOP contains -x3FFB ADD R2,R6,R1 ; Compare stack pointer to -x3FFB BRz FAIL ; Branch if stack is full. ADD R6,R6,#-1 ; Adjust stack pointer STR R0,R6,#0 ; The actual "push" BR SUCCESS OFLOW .BLKW 1 ; neg(min legal top value)
PUSH with Overflow Check
13
Example of Stack Usage
• Checking for balanced parentheses
- Is string of parentheses (of various shapes)
correctly parenthesized?
- Examples
+ ( [ ] { } ) ( { } ) is legal
+ ( [ ) ] is illegal — Mismatched pair [ )
+ [ ( ) is illegal — Too many left shapes
+ { { } } } is illegal — Too many right shapes
Stack! empty [stack of unmatched left shapes] pt first character in string while not at end of string if current character is a left shape Push it onto the stack else (it's a right shape) if stack is empty Error! Unmatched right shape else pop stack to get a left shape if the left and right shapes don't match Error! Left and right shapes don't match end if pt next character in string end while if stack not empty Error! Too many left shapes
Use Stack To Track
Unmatched Left Shapes
15
Example of Stack Usage
• Stack-based calculators
• Old HP^ calculators
- Instead of (e.g.) 3+5 = 8
- 3, enter, 5, +
• Stack holds values of pending operations
- When you do an operation, you pop values off
the stack, do the operation and push the
result onto the stack
19
Stack Input Operation
7 enter push 7
7 2 enter push 2
7 2 1 push 1
7 2 1 plus
pop 1, pop 2,
push
7 3 minus
pop 3, pop 7, push
Interrupt-Driven I/O (Part 2)
• Interrupts were introduced in Chapter 8.
1. External device signals need to be serviced.
2. Processor saves state and starts service
routine.
3. When finished, processor restores state and
resumes program.
Interrupt is an unscripted subroutine call,
triggered by an external event.
Interrupt-Driven I/O (Part 2)
• Chapter 8 didn’t explain how (2) and (3)
occur,
because it involves a stack.
• Now, we’re ready…
Interrupt is an unscripted subroutine call,
triggered by an external event.
21
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]
Where to Save Processor
State?
• Memory allocated by service routine?
- Must save state^ before^ invoking routine,
so we wouldn’t know where.
- Also, interrupts may be nested –
that is, an interrupt service routine might also
get interrupted!
• Use a stack!
- Location of stack “hard-wired”.
- Push state to save, pop to restore.
25
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.
Supervisor Stack
• Use R6 as stack pointer.
- So that our PUSH/POP^ routines still work.
• When switching from User mode to
Supervisor mode (as result of interrupt)
- Save R6 to Saved.USP.
27
Invoking the Service Routine
• Note:^ The following all happens between
the STORE RESULT of the last user
instruction and the FETCH of the first ISR
instruction.
Returning from Interrupt
RTI is a privileged instruction.
• Can only be executed in Supervisor Mode.
• If executed in User Mode, causes an
exception.
• (More about that later.)
31
Example (1)
PC^ x
Program A
x3006^ ADD
Executing ADD at location x3006 when Device B interrupts.
Saved.SSP
Example (2)
x
PSR for A
PC^ x
R
Program A
x3006^ ADD
Saved.USP = R6. R6 = Saved.SSP.
Push PSR and PC onto stack, then transfer to
Device B service routine (at x6200).
x ISR for Device B
x6210 RTI
33
Example (3)
x
PSR for A
PC^ x
R
Program A
x3006^ ADD
Executing AND at x6202 when Device C interrupts.
x ISR for Device B
x6202 AND
x6210 RTI
Example (6)
x
PSR for A
x
PSR for B
PC^ x
Program A
x3006^ ADD
x ISR for Device B
x6202 AND
ISR for Device C
Execute RTI at x6210; pop PSR and PC from stack.
Restore R6. Continue Program A as if nothing happened.
x
x6315 RTI
x6210 RTI
Saved.SSP
37
Exception: Internal Interrupt
• When something unexpected happens
inside the processor, it may cause an
exception. Examples include:
- Privileged operation (e.g., RTI in user mode)
- Executing an illegal opcode
- Divide by zero
- Accessing an illegal address (e.g., protected
system memory)
Exception: Internal Interrupt
• Internal interrupts are handled just like
external interrupts
- Vector is determined internally by type of
exception
- Priority is the same as running program