Title: Trap Routines
1Chapter 9
- Trap Routines
- TRAP number (go to service routine)
- RET (return from service routine)
- Subroutines (or Functions)
- JSR offset or JSRR rn (go to subr)
- RET (return from subroutine)
2Traps
- Execute TRAP vector number - Service
Routine Address - Trap Vectors are at memory locations
000000FF -
- Trap Vectors contain addresses of Pre
written (?) Service Routines - 2) PC is stored in R7 (save the return
address) - 3) Address of Trap Service Routine loaded into
PC (addr of service routine) - 4) Service Routine Program executed
- Trap service routine program ends with an RET (to
return to calling prog) - R7 loaded into PC
3 Subroutines
JSR Instruction JSR offset (11 bit) 0100 1
xxxxxxxxxxx PC ? R7, JMP Offset
Jump to Subroutine at offset from PC JSRR
Instruction JSRR Rx 0100 0 00 xxx 000000
PC ? R7, JMP Reg Jump to
Subroutine at address in Rx RET
Instruction RET 1100 000 111 000000
C1C0 R7 ? PC (JMP R7)
Return to Instruction after Jump
to Subroutine (or TRAP)
4Subroutines
- 1) Execute JSR offset or JSRR rn - Call
Subroutine or Method - Location of Subroutine is specified in the
Instruction -
- 2) PC stored in R7 (store return address)
- 3) Address from JSR offset or JSRR rn is loaded
into PC (beginning of subr) - 4) Subroutine is executed
- R0 likely contains passed parameter (or address)
- R5 may be used to return error message
- R0 likely contains return parameter (or address)
- 5) Subroutine program ends with an RET ( R7
loaded into PC) - Can you pass (return) two parameters ?
- Can a subroutine call another subroutine ?
- How does this mechanism support recursion?
5Subroutines vs Traps
- How are Subroutines different from Traps ?
- Traps are called using the TRAP instruction
- (Indirect call through the Trap Vector Table)
- Subroutines are called using JSR or JSRR
instructions (JSR Direct call, JSRR Indirect
call) - Both end with a RET ( load the return address)
A Trap is a Subroutine call - (Indirect)
through a Vector Table (x0000-x00FF). -
rather than with a PC offset (or a label)
6Trap Service Routine for Character Input (P 234)
.ORIG x04A0 START ST R7,SaveR7
Save Registers JSR SaveReg
LD R2,Newline Write newline
JSR WriteChar LEA
R1,PROMPT Write prompt character string Loop
LDR R2,R1,0 Get next prompt
char BRz Input Go to
Input when done JSR WriteChar
Write character ADD
R1,R1,1 BRnzp Loop Input
JSR ReadChar Read Input character
ADD R2,R0,0 Echo to monitor
JSR WriteChar
LD R2,Newline Write newline
JSR WriteChar JSR
RestoreReg Restore Registers Return
LD R7,SaveR7 RET
SaveR7 .FILL x0000 Newline .FILL
x000A Prompt .STRINGZ "Input a charactergt
"
7Trap Service Routine for Character Input (2)
SaveR1 .FILL x0000 SaveR2 .FILL
x0000 SaveR3 .FILL x0000 SaveR4
.FILL x0000 SaveR5 .FILL x0000 SaveR6
.FILL x0000 .END
WriteChar LDI R3,DSR Monitor Done?
BRzp WriteChar STI
R2,DDR Send Char RET DSR
.FILL xFE04 DDR .FILL xFE06
ReadChar LDI R3,KBSR Keyboard
Ready? BRzp ReadChar
LDI R0,KBDR Get Char
RET KBSR .FILL xFE00 KBDR .FILL
xFE02 SaveReg ST R1,SaveR1 Store
R1,.., R6 ST R2,SaveR2
ST R3,SaveR3 ST
R4,SaveR4 ST R5,SaveR5
ST R6,SaveR6
RET RestoreReg LD R1,SaveR1 Load
R1,..., R6 LD R2,SaveR2
LD R3,SaveR3 LD
R4,SaveR4 LD R5,SaveR5
LD R6,SaveR6 RET
Why is SaveR7 not with SaveR1 thru SaveR6 ?
Why wasnt R7 stored on each JSR ? Why wasnt
R1R6 stored on each JSR ?
8Chapter 10
- The Stack
- The Stack
- is a dynamic data structure
- descending from high memory to lower memory,
- used to store data in a
- First In Last Out
- order
9Illustration of a Stack
10Stack Operation
Implemented in memory
11The STACK
- The Stack is a dynamic data structure
descending from high memory to lower memory - (The stack grows down )
- The Stack Pointer (R6) always points to the top
word on the stack. - (The Stk Ptr is the stack reference that
is available to the programmer) - A new stack entry (a word) is PUSHed onto the
stack - To take a word off of the Stack, the top word is
- POPed off of the stack.
12The Stack
Most Popular Organization
Alternate Organization Initially PC xFE00
Initially PC begin
13 Stack R6 is the Stack Ptr Push Push ADD
R6, R6, -1 Decrement Stack Ptr
STR R0, R6, 0 Push R0 onto
Stack Pop Pop LDR R0, R6, 0 Pop
Data off of Stack (into R0) ADD R6, R6, 1
Increment Stack Ptr Which way does the
Stack grow? Where does Stack Ptr (R6) point?
14 Stack Underflow Check (Stack Empty)
Stack POP subroutine. SP is R6. Returns data
in R0. Fails if the stack is empty
(SPx4000) and reports error (1 --gt R5) POP
LD R1, STK_Empty Compare Stk Ptr with Empty
ADD R2, R6, R1 BRz fail_exit EXIT IF
STACK IS EMPTY if ok, Pop
LDR R0, R6, 0 Pop top of
Stack into R0 and ADD R6, R6, 1 Increment
Stk Ptr AND R5, R5, 0 R5 lt-- 0 (POP
successful) RET fail_exit AND R5, R5, 0
R5 lt-- 1 (POP failed - underflow) ADD R5, R5,
1 RET STK_Empty .FILL xC000 STK_Empty ?
-x4000 (Stack begins at x3FFF)
15 Stack Overflow Check (Stack too Large)
Stack PUSH subroutine. SP is R6. Value in R0
is pushed onto the stack. Fails if Stack is
full (SPx3F00) and reports error (1 ?
R5) PUSH LD R1, STK_Full Compare Stack
Ptr with Full ADD R2, R6, R1
BRz fail_exit EXIT IF STACK IS FULL if
ok, PUSH ADD R6, R6, -1 Decrement Stk
Ptr and STR R0, R6, 0 Push R0 onto top
of Stack AND R5, R5, 0 Report no
Error 0 ? R5 RET fail_exit AND R5, R5,
0 Report Error 1 ? R5 ADD R5,
R5, 1 RET STK_Full .FILL xC100 STK_Full
lt-- -x3F00 (Stack max is x3F00)
16 Subroutine for Push Pop
PUSH and POP Subroutines. STK Ptr is R6. Data
in R0. Success R0 0. Stack x3FFF to x3F00
(256 words max). POP and PUSH are Entry
Points. POP ST R2,Save2
R1 R2 are used by POP. Save them.
ST R1,Save1 LD
R1,STK_Empty Compare Stk Ptr with Empty
ADD R2,R6,R1 BRz
fail_exit EXIT IF STACK IS EMPTY
LDR R0,R6,0 POP top of
Stack into R0 and ADD
R6,R6,1 Increment stack pointer
BRnzp success_exit PUSH ST
R2,Save2 R1 R2 are used by PUSH.
Save them. ST R1,Save1
LD R1,STK_Full Compare
Stk Ptr with Full ADD
R2,R6,R1 BRz fail_exit
EXIT IF STACK IS FULL ADD
R6,R6,-1 Decrement Stk Ptr and
STR R0,R6,0 PUSH R0 onto
top of Stack success_exit AND R5,R5,0
R5 lt-- 0 (success) LD R1,Save1
Restore registers and Return LD
R2,Save2 RET fail_exit AND
R5,R5,0 R5 lt-- 1 (failure) ADD
R5,R5,1 LD R1,Save1
Restore registers and Return LD R2,Save2
RET
STK_Empty .FILL xC000 BASE
x4000. STK_Full .FILL xC100 Stack 256
words Save1 .FILL
x0000 Save2 .FILL x0000 .END
What is a reasonable length for the Stack ?