Overview IO memory mapped programmed interrupt driven Traps



![Traps 1) Execute 2) Trap Vectors are at memory locations [0000: 00 FF] 3) Traps 1) Execute 2) Trap Vectors are at memory locations [0000: 00 FF] 3)](https://slidetodoc.com/presentation_image_h2/0aa4fc1d99e1e4caf556ef43b027314b/image-4.jpg)




![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](https://slidetodoc.com/presentation_image_h2/0aa4fc1d99e1e4caf556ef43b027314b/image-9.jpg)







- Slides: 16

Overview • I/O – memory mapped programmed / interrupt driven • Traps mechanism & RET • Subroutines & JSRR & RET • Interrupt mechanism & RTI

LC-3 has Memory Mapped I/O LC-3 Memory Layout: x 0000 – x 00 FF Trap vectors x 0100 – x 2 FFF System Programs & Data x 3000 – x. FDFF User Programs Area x. FE 00 – x. FFFF I/O Programming “Registers” x. FE 00 KBSR (Keyboard Status Register [15 {Ready}, 14 {Intr enable] ) x. FE 02 KBDR (Keyboard Data Register [7: 0{ascii data}] ) x. FE 04 DSR (Display Status Register) [15{Done}, 14{Intr enable}] ) x. FE 06 DDR (Display Data Register [7: 0{ascii data}]) x. FFFE MCR (Machine Control Register) [15{Run latch}] )

Trap Routine 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 (JMP R 7) C 1 C 0
![Traps 1 Execute 2 Trap Vectors are at memory locations 0000 00 FF 3 Traps 1) Execute 2) Trap Vectors are at memory locations [0000: 00 FF] 3)](https://slidetodoc.com/presentation_image_h2/0aa4fc1d99e1e4caf556ef43b027314b/image-4.jpg)
Traps 1) Execute 2) Trap Vectors are at memory locations [0000: 00 FF] 3) Trap Vectors contain addresses of Trap Service Routines 4) (PC) is loaded into R 7 5) Address of Trap Service Routine loaded into PC 6) Service Routine Program executed 7) Trap service routine program ends with an RET ( (R 7) loaded into PC) TRAP “vector” - Operating System Service Routines

OUT Service Routine - TRAP x 21 . ORIG ST x 0430 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

IN Trap service routine - TRAP x 23 ; ; Service Routine for Keyboard Input START . ORIG ST ST ST x 04 A 0 R 1, Save. R 1 R 2, Save. R 2 R 3, Save. R 3 LD LDI BRzp STI R 2, Newline R 3, DSR L 1 R 2, DDR LEA R 1, Prompt 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 ; Save the values in the registers ; that are used so that they ; can be restored before RET ; L 1 ; Check DDR -- is it free? ; Move cursor to new clean line ; Loop L 2 ; Input L 3 ; L 4 ; Save. R 1 Save. R 2 Save. R 3 DSR DDR KBSR KBDR Newline Prompt ; ; 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) . BLKW 1. FILL x. FE 04. FILL x. FE 06. FILL x. FE 00. FILL x. FE 02. FILL x 000 A ; ASCII code for newline. STRINGZ "Input a character>". END

HALT service routine – TRAP x 25. ORIG ST ST ST x. FD 70 R 7, Save. R 7 R 1, Save. R 1 R 0, Save. R 0 ; Where this routine resides ; R 1: a temp for MC register ; 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

; ; ; ; puts. asm 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 ST ST x 0450 R 7, Save. R 7 R 0, Save. R 0 R 1, Save. R 1 R 3, Save. R 3 ; ; ; Where this ISR resides Save R 7 for later return Save other registers that Are needed by this routine ; ; 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 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. FILL x 0000 Save. R 1. FILL x 0000 Save. R 3. FILL x 0000 Save. R 7. FILL x 0000. END
![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](https://slidetodoc.com/presentation_image_h2/0aa4fc1d99e1e4caf556ef43b027314b/image-9.jpg)
Subroutines JSR Instruction: JSR offset (11 bit) 0100 1 xxxxxx [PC ] R 7 JMP Jump to subroutine at offset from PC JSRR Instruction JSRR Rb 0100 0 00 xxx 000000 [PC ] R 7 JMP Jump to subroutine at address in Rb Return RET 1100 000 111 000000 [R 7] PC (JMP R 7) C 1 C 0

Subroutines 1) Execute 2) Location of Subroutine is specified “in” the Instruction 3) JSR or JSRR - Call Subroutine or Method (PC) stored in R 7 4) Address from JSR or JSRR is loaded into PC • Subroutine is executed R 0 possibly contains passed parameter (or address) R 5 may be used to return error message Ro possibly contains return parameter (or address) 6) Subroutine program ends with an RET ( (R 7) loaded into PC) How does this mechanism support recursion?

Trap service routine for character input(subroutines) (1). ORIG START x 04 A 0 ; ; Loop ; Input ST JSR LD JSR LEA R 7, Save. R 7 Save. Reg R 2, Newline Write. Char R 1, PROMPT LDR BRz JSR ADD BRnzp R 2, R 1, #0 Input Write. Char R 1, #1 Loop JSR ADD JSR Read. Char R 2, R 0, #0 Write. Char LD JSR LD RET R 2, Newline Write. Char Restore. Reg R 7, Save. R 7 ; Get next prompt char ; Move char to R 2 for writing ; Echo to monitor ; ; Save. R 7 Newline Prompt ; Write. Char DSR DDR ; Read. Char KBSR KBDR ; Save. Reg ; JMP R 7 terminates the TRAP routine . FILL x 0000. FILL x 000 A. STRINGZ "Input a character>" LDI BRzp STI RET. FILL R 3, DSR Write. Char R 2, DDR 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 ; JMP R 7 terminates subroutine x. FE 04 x. FE 06 x. FE 00 x. FE 02

Trap service routine for character input(subroutines) (2) ; Restore. Reg ; Save. R 1 Save. R 2 Save. R 3 Save. R 4 Save. R 5 Save. 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 . FILL x 0000 x 0000 . END

Stack Push: Push ADD STR R 6, #-1 R 0, R 6, #0 Pop: Pop LDR R 0, R 6, #0 ADD R 6, #1 Which way does the stack grow?

Underflow Pop LD R 1, Empty ADD R 2, R 6, R 1 ; Compare stack BRz Underflow ; pointer with x 4000 LDR R 0, R 6, #0 ADD R 6, #1 AND R 5, #0 RET Underflow AND R 5, #0 ADD R 5, #1 RET Empty . FILL x. C 000 ; Empty -x 4000

Overflow Push LD R 1, Max ADD R 2, R 6, R 1 BRz Overflow ADD R 6, #-1 STR R 0, R 6, #0 AND R 5, #0 RET Overflow AND R 5, #0 ADD R 5, #1 RET max . FILL x. C 005 ; Max x 3 FFB

Subroutine for Push & Pop ; Subroutines for carrying out the PUSH and POP functions. This ; program works with a stack consisting of memory locations x 3 FFF ; (BASE) through x 3 FFB (MAX). R 6 is the stack pointer. ; POP PUSH success_exit ST R 2, Save 2 ; are needed by POP. ST R 1, Save 1 LD R 1, BASE ; BASE contains -x 3 FFF. ADD R 1, #-1 ; R 1 contains -x 4000. ADD R 2, R 6, R 1 ; Compare stack pointer to x 4000 BRz fail_exit ; Branch if stack is empty. LDR R 0, R 6, #0 ; The actual "pop. " ADD R 6, #1 ; Adjust stack pointer BRnzp success_exit ST R 2, Save 2 ; Save registers that ST R 1, Save 1 ; are needed by PUSH. LD R 1, MAX ; MAX contains -x 3 FFB ADD R 2, R 6, R 1 ; Compare stack pointer to -x 3 FFB BRz fail_exit ; Branch if stack is full. ADD R 6, #-1 ; Adjust stack pointer STR R 0, R 6, #0 ; The actual "push" LD R 1, Save 1 ; Restore original LD R 2, Save 2 ; register values. AND R 5, #0 ; R 5 <-- success. LD R 1, Save 1 ; Restore original LD R 2, Save 2 ; register values. AND R 5, #0 ADD R 5, #1 ; R 5 <-- failure. ; BASE contains -x 3 FFF. RET fail_exit RET BASE . FILL x. C 001 MAX . FILL x. C 005 Save 1 . FILL x 0000 Save 2 . FILL x 0000 . END