Professional Documents
Culture Documents
How Does Processor Handle It? Supervisor Mode and the Stack
Examines INT signal just before starting FETCH phase Problem
If INT=1, dont fetch next instruction PC and PSR shouldnt be saved on user stack
Instead
What if R6 is uninitialized?
Save state (PC, PSR (privilege and CCs)) on stack
What if user has set R6 to refer to OS memory?
Update PSR (set privilege bit)
User could see OS data (when trap returns)
Index INTV into IVT to get start address of ISR (put in PC)
1
More Control Example (1)
At the device Program A
Control register has Interrupt Enable bit
Supervisor Stack User Stack
Must be set for interrupt to be generated
////// R6 //////
interrupt enable ////// //////
x3006 ADD
ready KBSR ////// //////
interrupt signal ////// //////
to processor ////// //////
At the processor
Sometimes have Interrupt Mask register (LC-3 does not) PC x3007
When set, processor ignores INT signal PSR x0001
Why?
Example: may not want to be interrupted while in ISR Executing ADD at location x3006 when Device B interrupts
(INTV = x1d)
CSE 240 10-20 CSE 240 10-21
PC x6200 PC x6203
Push PC and PSR onto stack, set privilege bit, find Executing AND at x6202 when Device C interrupts
Device B (INTV=x2d) service routine address in IVT, and transfer control
CSE 240 10-22 CSE 240 10-23
2
Example (4) Example (5) Still privileged
PC x6300 PC x6203
Push PC and PSR onto stack, set privilege bit, then transfer to Execute RTI at x6315; pop PSR and PC from stack
Device C service routine (at x6300)
CSE 240 10-24 CSE 240 10-25
3
Data Type Conversion ASCII to Binary
I/O Single digit numbers are trivial (subtract x30)
Keyboard input routines read ASCII characters (not binary values)
E.g., 7 is ASCII x37, x37 - x30 = x7
Console output routines write ASCII (s not x73)
x32 '2'
Consider this program Input x35 '5'
TRAP x23 ; input from keyboard x39 '9'
ADD R1, R0, #0 ; move to R1 Assume we've read three ASCII digits (e.g., "259")
TRAP x23 ; input from keyboard into a memory buffer x0
ADD R0, R1, R0 ; add two inputs
TRAP x21 ; display result
TRAP x25 ; HALT How do we convert this to a number we can use?
Convert first character to digit (subtract x30) and multiply by 100
Input: 2 and 3
Convert second character to digit and multiply by 10
Output: e
Convert third character to digit
Why? Add the three digits together
ASCII '2' (x32) + ASCII '3' (x33) = ASCII 'e' (x65)
CSE 240 10-28 CSE 240 10-29
4
R0 pointer in string buffer
Code for Lookup Table ASCII to Binary Conversion R2 character from memory
R5 current value
; mult. R0 by 10, using lookup table;(product in R0) ASCIItoBin
LEA R0, BUF ; R0 = string pointer
LEA R1, Lookup10 ; R1 = table base AND R5, R5, #0 ; R5 = 0 (current value)
ADD R1, R1, R0 ; add index (R0) L2
LDR R0, R1, #0 ; load from M[R1] LDR R2, R0, #0 ; get character from buffer
... BRz DONE ; done, if end-of-string
LD R4, NegASCIIOffset ; R4 = -x30
Lookup10 .FILL #0 ; entry 0
ADD R2, R2, R4 ; convert char to number
.FILL #10 ; entry 1 MUL R5, R5, #10 ; mult current value by 10
.FILL #20 ; entry 2 ADD R5, R5, R2 ; add number to cur. val.
.FILL #30 ; entry 3 ADD R0, R0, #1 ; decrement pointer
.FILL #40 ; entry 4 BR L2 ; loop
DONE
.FILL #50 ; entry 5 HALT
.FILL #60 ; entry 6
.FILL #70 ; entry 7
.FILL #80 ; entry 8 BUF .STRINGZ "32767"
NegASCIIOffset .FILL xFFD0
.FILL #90 ; entry 9
5
Conversion (2 of 3) Conversion Code (3 of 3)
Begin100 LD R2, ASCIIoffset End10 STR R2, R1, #2 ; store ASCII 10's digit
LD R3, Neg100 ADD R0, R0, #10 ; restore last subtract
Loop100 ADD R0, R0, R3 ;
BRn End100 LD R2, ASCIIoffset
ADD R2, R2, #1 ; add one to digit
ADD R2, R2, R0 ; convert one's digit
BR Loop100
End100 STR R2, R1, #1 ; store ASCII 100's digit STR R2, R1, #3 ; store one's digit
LD R3, Pos100 RET
ADD R0, R0, R3 ; restore last subtract
; ;
LD R2, ASCIIoffset ASCIIplus .FILL x2B ; plus sign
LD R3, Neg10 ASCIIneg .FILL x2D ; neg sign
Loop10 ADD R0, R0, R3 ASCIIoffset .FILL x30 ; zero
BRn End10 Neg100 .FILL xFF9C ; -100
ADD R2, R2, #1 ; add one to digit
Pos100 .FILL 100
BR Loop10
Neg10 .FILL xFFF6 ; -10