Chapter 9 Trap Routines RET Subroutines or Functions

Chapter 9 • Trap Routines & RET • Subroutines (or Functions) & JSRR & RET

LC-3 has Memory Mapped I/O LC-3 Memory Layout: x 0000 – x 00 FF Trap vectors (Supports Software Interrupts) x 0020 [x 0400] GETC (Read Char from Keyboard) x 0021 [x 0430] OUT x 0022 [x 0450] PUTS (Write string to Console) x 0023 [x 04 A 0] IN x 0024 [x 04 E 0] PUTSP (Write “packed” string to Console) x 0025 [x. FD 70] HALT (Turn off run latch in MCR) x 0100 – x 01 FF (Write Character to Console) (Prompt, input character from Keyboard, echo character to Console) Interrupt Vectors (Supports Hardware Interrupts) x 0200 – x 2 FFF System Programs & Data (“Operating System”) x 3000 – x. FDFF User Programs Area (with Traps & Subroutine Calls) x. FE 00 – x. FFFF I/O Programming “Registers” (Mapped I/O Registers) x. FE 00 KBSR [15 {Ready}, 14 {Intr enable}] (Keyboard Status Register) x. FE 02 KBDR [7: 0{ascii data}] (Keyboard Data Register) x. FE 04 DSR [15{Done}, 14{Intr enable}] (Display Status Register) x. FE 06 DDR [7: 0{ascii data}] (Display Data Register x. FFFE MCR [15{Run latch}] (Machine Control Register)

Trap Routines Trap Instruction: TRAP x 1111 0000 trap vector F 0 xx [PC ] R 7 Jump to routine at trap vector address Return: RET 1100 000 111 000000 [R 7] PC C 1 C 0 (JMP R 7) Some Conventions (For the LC-3): Data passed (read & write) in R 0, Error messages returned in R 5

Traps 1) Execute TRAP “vector” - Service Routine Addresses 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 3) Address of Trap Service Routine loaded into PC 4) Service Routine Program executed 7) Trap service routine program ends with an RET ([R 7] loaded into PC)

TRAP x 21 ; out. asm ; . ORIG x 0430 ST OUT Trap Vector Routine (Output Character) R 1, Save. R 1 ; ; Write the character ; Try. Write LDI R 1, DSR BRzp Try. Write. It STI R 0, DDR ; ; return from trap ; Return LD R 1, Save. R 1 RET ; DSR. FILL x. FE 04 DDR. FILL x. FE 06 Save. R 1. BLKW 1. END ; System call starting address ; R 1 will be used to poll the DSR ; hardware ; Get status ; Bit 15 on says display is ready ; Write character ; Restore registers ; Return from trap (JMP R 7, actually) ; Address of display status register ; Address of display data register

TRAP x 23 IN Trap Service Routine (Input Character) ; in. asm ; . ORIG x 04 A 0 START ST ST ST ; LD L 1 LDI BRzp STI ; LEA Service Routine for Keyboard Input Loop LDR BRz LDI BRzp STI R 0, R 1, #0 Input R 3, DSR L 2 R 0, DDR ADD BRnzp R 1, #1 Loop LDI BRzp STI R 3, KBSR Input R 0, KBDR R 3, DSR L 3 R 0, DDR LDI BRzp STI LD LD LD RET R 3, DSR L 4 R 2, DDR R 1, Save. R 1 R 2, Save. R 2 R 3, Save. R 3 L 2 ; Input L 3 ; L 4 R 1, Save. R 1 R 2, Save. R 2 R 3, Save. R 3 R 2, Newline R 3, DSR L 1 R 2, DDR R 1, Prompt ; Save the register values ; that are used so that they ; can be restored before RET ; Check DDR -- is it free? ; Move cursor to new clean line ; ; Prompt is starting address of prompt string Get next prompt character Check for end of prompt string ; Write next character of ; prompt string ; Increment Prompt pointer ; Has a character been typed? ; Load it into R 0 ; Echo input character ; to the monitor ; Move cursor to new clean line ; Service routine done, restore ; original values in registers. ; Return from trap (i. e. , JMP R 7) ; Save. R 1 Save. R 2 Save. R 3 DSR DDR KBSR KBDR Newline Prompt. END . BLKW 1. FILL x. FE 04. FILL x. FE 06. FILL x. FE 00. FILL x. FE 02. FILL x 000 A ; newline (ASCII). STRINGZ "Input a character>"

TRAP x 25 HALT Service Routine ; halt. asm Halts the program ; . ORIG x. FD 70 ; Where this routine resides ST R 7, Save. R 7 ST R 1, Save. R 1 ; R 1: a temp for MC register ST R 0, Save. R 0 ; R 0 is used as working space ; print message that machine is halting LD TRAP LEA TRAP LD TRAP R 0, ASCIINew. Line x 21 R 0, Message x 22 R 0, ASCIINew. Line x 21 ; ; clear bit 15 at x. FFFE to stop the machine ; LDI R 1, MCR ; Load MC register into R 1 LD R 0, MASK ; R 0 = x 7 FFF AND R 0, R 1, R 0 ; Mask to clear the top bit STI R 0, MCR ; Store R 0 into MC register ; ; return from HALT routine. ; (how can this routine return if the machine is halted above? ) ; LD R 1, Save. R 1 ; Restore registers LD R 0, Save. R 0 LD R 7, Save. R 7 RET ; JMP R 7, actually ; ; Some constants ; ASCIINew. Line. FILL x 000 A Save. R 0. BLKW 1 Save. R 1. BLKW 1 Save. R 7. BLKW 1 Message. STRINGZ "Halting the machine. " MCR. FILL x. FFFE ; Address of MCR MASK. FILL x 7 FFF ; Mask to clear the top bit. END

TRAP 22 PUTS Trap Service Routine (Output a Character String) ; puts. asm Output a Character String ; This service routine writes a NULL-terminated string to the console. ; It services the PUTS service call (TRAP x 22). ; Inputs: R 0 is a pointer to the string to print. ; Context Information: R 0, R 1, and R 3 are saved, and R 7 is lost ; in the jump to this routine ; . ORIG x 0450 ; Where this Service Routine resides ST R 7, Save. R 7 ; Save R 7 for later return ST R 0, Save. R 0 ; Save other registers that are used by this routine ST R 1, Save. R 1 ST R 3, Save. R 3 ; ; Loop through each character in the array ; ; Loop LDR R 1, R 0, #0 ; Retrieve the character(s) BRz Return ; If it is 0, done L 2 LDI R 3, DSR BRzp L 2 STI R 1, DDR ; Write the character ADD R 0, #1 ; Increment pointer BRnzp Loop ; Do it all over again ; ; Return from the request for service call Return LD R 3, Save. R 3 ; Restore Registers LD R 1, Save. R 1 LD R 0, Save. R 0 LD R 7, Save. R 7 RET ; ; Register locations DSR. FILL x. FE 04 DDR. FILL x. FE 06 Save. R 0 Save. R 1 Save. R 3 Save. R 7 . FILL. END x 0000
![Subroutines JSR Instruction: JSR offset (11 bit) 0100 1 xxxxxx [PC ] R 7, Subroutines JSR Instruction: JSR offset (11 bit) 0100 1 xxxxxx [PC ] R 7,](http://slidetodoc.com/presentation_image_h2/566bd2ba3c7bf3348ffc2e2e0e652a68/image-9.jpg)
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 Return: 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 or JSRR - Call Subroutine or Method Location of Subroutine is specified in the Instruction 2) [PC] stored in R 7 3) Address from JSR or JSRR is loaded into PC 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) 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 rather than through a Register (the Trap Vector Table [x 0000 -x 00 FF]).

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 ?

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) points to the top word on the stack. (The Stk Ptr is the “stack reference” that is available to the programmer) • A new entry (word) is “PUSHed” onto the stack • To take a word off of the Stack, the top word is “POPed” off of the stack.

Stack R 6 is the Stack Ptr Push: Push ADD STR R 6, #-1 ; Decrement Stack Ptr R 0, R 6, #0 ; “Push” Data onto Stack Pop LDR R 0, R 6, #0 ADD R 6, #1 Pop: ; “Pop” Data off of Stack ; 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: 18