Chapter 9 Trap Routines TRAP number go to
Chapter 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)
Traps 1) Execute TRAP “vector number” - Service Routine Address Trap Vectors are at memory locations [0000: 00 FF] Trap Vectors contain addresses of “Pre written (? )” Service Routines 2) [PC] is stored in R 7 (save the return address) 3) Address of Trap Service Routine loaded into PC (addr of service routine) 4) Service Routine Program executed 7) Trap service routine program ends with an RET (to return to calling prog) [R 7] loaded into PC
Subroutines JSR Instruction: JSR offset (11 bit) 0100 1 xxxxxx [PC ] R 7, JMP Offset Jump to Subroutine at offset from PC JSRR Instruction: JSRR Rx 0100 0 00 xxx 000000 [PC ] R 7, JMP [Reg] Jump to Subroutine at address in Rx RET Instruction: RET 1100 000 111 000000 C 1 C 0 [R 7] PC (JMP [R 7]) Return to Instruction after Jump to Subroutine (or TRAP)
Subroutines 1) Execute JSR offset or JSRR rn - Call Subroutine or Method Location of Subroutine is specified in the Instruction 2) [PC] stored in R 7 (store return address) 3) Address from JSR offset or JSRR rn is loaded into PC (beginning of subr) 4) Subroutine is executed R 0 likely contains passed parameter (or address) R 5 may be used to return error message R 0 likely contains return parameter (or address) 5) Subroutine program ends with an RET ( [R 7] loaded into PC) Can you pass (return) two parameters ? Can a subroutine call another subroutine ? How does this mechanism support recursion?
Subroutines 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 ([x 0000 -x 00 FF]). - rather than with a PC offset (or a label)
Trap Service Routine for Character Input (P 234). ORIG START x 04 A 0 ST JSR LD JSR R 7, Save. R 7 Save. Reg R 2, Newline Write. Char ; Save Registers LEA LDR BRz JSR ADD BRnzp R 1, PROMPT R 2, R 1, #0 Input Write. Char R 1, #1 Loop ; Write prompt character string ; Get next prompt char ; Go to Input when done ; Write character JSR ADD JSR Read. Char R 2, R 0, #0 Write. Char ; Read Input character ; Echo to monitor LD JSR LD RET R 2, Newline Write. Char Restore. Reg R 7, Save. R 7 ; Write “newline” ; ; Loop ; ; Input ; ; Save. R 7 Newline Prompt ; Restore Registers & Return . FILL x 0000. FILL x 000 A. STRINGZ "Input a character> "
Trap Service Routine for Character Input (2) Write. Char DSR DDR ; ; Read. Char KBSR KBDR ; Save. Reg ; Restore. Reg LDI BRzp STI RET. FILL R 3, DSR Write. Char R 2, DDR ; Monitor Done? LDI BRzp LDI RET. FILL R 3, KBSR Read. Char R 0, KBDR ST ST ST RET R 1, Save. R 1 R 2, Save. R 2 R 3, Save. R 3 R 4, Save. R 4 R 5, Save. R 5 R 6, Save. R 6 ; Store R 1, . . , R 6 LD LD LD RET R 1, Save. R 1 R 2, Save. R 2 R 3, Save. R 3 R 4, Save. R 4 R 5, Save. R 5 R 6, Save. R 6 ; Load R 1, . . . , R 6 ; Send Char x. FE 04 x. FE 06 ; Keyboard Ready? ; Save. R 1 Save. R 2 Save. R 3 Save. R 4 Save. R 5 Save. R 6 ; . FILL x 0000 x 0000 . END ; Get Char x. FE 00 x. FE 02 Why is Save. R 7 not with Save. R 1 thru Save. R 6 ? Why wasn’t R 7 stored on each JSR ? Why wasn’t R 1–R 6 stored on each JSR ?
Chapter 10 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
Illustration of a Stack
Stack Operation Implemented in memory:
The STACK • The Stack is a dynamic “data structure” descending from high memory to lower memory (The stack grows down ) • The Stack Pointer (R 6) 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.
The Stack Most Popular Organization Initially [PC] = x. FE 00 Alternate Organization Initially [PC] = begin
Stack R 6 is the Stack Ptr Push: Push ADD STR R 6, #-1 ; Decrement Stack Ptr R 0, R 6, #0 ; “Push” [R 0] onto Stack Pop LDR R 0, R 6, #0 ADD R 6, #1 Pop: ; “Pop” Data off of Stack (into R 0) ; Increment Stack Ptr Which way does the Stack grow? Where does Stack Ptr (R 6) point?
Stack Underflow Check (Stack Empty) ; Stack POP subroutine. ; Returns “data” in R 0. Fails if the stack is empty (SP=x 4000) and reports error (1 --> R 5) POP ; SP is R 6. LD R 1, STK_Empty ; Compare Stk Ptr with “Empty” ADD R 2, R 6, R 1 BRz fail_exit ; LDR R 0, R 6, #0 ; Pop top of Stack into R 0 and ADD R 6, #1 ; Increment Stk Ptr AND R 5, #0 ; R 5 <-- 0 (POP successful) AND R 5, #0 ; R 5 <-- 1 (POP failed - underflow) ADD R 5, #1 EXIT IF STACK IS EMPTY if ok, Pop RET fail_exit RET STK_Empty. FILL x. C 000 ; STK_Empty -x 4000 (Stack begins at x 3 FFF)
Stack Overflow Check (Stack too Large) ; Stack PUSH subroutine. SP is R 6. Value in R 0 is pushed onto the stack. ; Fails if Stack is full (SP=x 3 F 00) and reports error (1 R 5) PUSH LD R 1, STK_Full ; Compare Stack Ptr with “Full” ADD R 2, R 6, R 1 BRz fail_exit ; ADD R 6, #-1 ; Decrement Stk Ptr and STR R 0, R 6, #0 ; “Push” R 0 onto top of Stack AND R 5, #0 ; “Report” no Error 0 R 5 AND R 5, #0 ; “Report Error” 1 R 5 ADD R 5, #1 EXIT IF STACK IS FULL ; if ok, PUSH RET fail_exit RET STK_Full. FILL x. C 100 ; STK_Full <-- -x 3 F 00 (Stack max is x 3 F 00)
Subroutine for Push & Pop ; PUSH and POP Subroutines. STK Ptr is R 6. Data in R 0. Success: R 0 = 0. ; Stack: x 3 FFF to x 3 F 00 (256 words max). POP PUSH success_exit ST R 2, Save 2 ST R 1, Save 1 POP and PUSH are Entry Points. ; R 1 & R 2 are used by “POP”. Save them. LD R 1, STK_Empty ; Compare Stk Ptr with “Empty” ADD R 2, R 6, R 1 BRz fail_exit ; LDR R 0, R 6, #0 ; “POP” top of Stack into R 0 and ADD R 6, #1 ; BRnzp success_exit ST R 2, Save 2 ST R 1, Save 1 LD R 1, STK_Full ADD R 2, R 6, R 1 BRz fail_exit ; ADD R 6, #-1 ; Decrement Stk Ptr and STR R 0, R 6, #0 ; AND R 5, #0 ; R 5 <-- 0 LD R 1, Save 1 ; Restore registers and Return LD R 2, Save 2 STK_Empty STK_Full . FILL x. C 000 ; BASE = –x 4000. x. C 100 ; Stack 256 words Save 1 Save 2 . FILL. END x 0000 EXIT IF STACK IS EMPTY Increment stack pointer ; R 1 & R 2 are used by “PUSH”. Save them. ; Compare Stk Ptr with “Full” EXIT IF STACK IS FULL “PUSH” R 0 onto top of Stack (success) RET fail_exit AND ADD LD LD RET R 5, #0 R 5, #1 R 1, Save 1 R 2, Save 2 ; R 5 <-- 1 (failure) ; Restore registers and Return What is a reasonable length for the Stack ?
- Slides: 16