1 / 16

Overview

Overview. I/O – memory mapped programmed / interrupt driven Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI. LC-3 has Memory Mapped I/O. LC-3 Memory Layout: x0000 – x00FF Trap vectors x0100 – x2FFF System Programs & Data

porter
Download Presentation

Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. LC-3 has Memory Mapped I/O LC-3 Memory Layout: x0000 – x00FF Trap vectors x0100 – x2FFF System Programs & Data x3000 – xFDFF User Programs Area xFE00 – xFFFF I/O Programming “Registers” xFE00 KBSR (Keyboard Status Register [15 {Ready}, 14 {Intr enable] ) xFE02 KBDR (Keyboard Data Register [7:0{ascii data}] ) xFE04 DSR (Display Status Register) [15{Done}, 14{Intr enable}] ) xFE06 DDR (Display Data Register [7:0{ascii data}]) xFFFE MCR (Machine Control Register) [15{Run latch}] )

  3. Trap Routine Trap Instruction: TRAP x 1111 0000 trap vector F0xx [PC ] R7 Jump to routine at trap vector address Return RET 1100 000 111 000000 C1C0 [R7]  PC (JMP R7)

  4. Traps • Execute TRAP “vector” - Operating System Service Routines 2) Trap Vectors are at memory locations [0000:00FF] • Trap Vectors contain addresses of Trap Service Routines • (PC) is loaded into R7 • Address of Trap Service Routine loaded into PC • Service Routine Program executed • Trap service routine program ends with an RET ( (R7) loaded into PC)

  5. OUT Service Routine - TRAP x21 .ORIG x0430 ; System call starting address ST R1, SaveR1 ; R1 will be used to poll the DSR ; hardware ; Write the character TryWrite LDI R1, DSR ; Get status BRzp TryWrite ; Bit 15 on says display is ready WriteIt STI R0, DDR ; Write character ; return from trap Return LD R1, SaveR1 ; Restore registers RET ; Return from trap (JMP R7, actually) DSR .FILL xFE04 ; Address of display status register DDR .FILL xFE06 ; Address of display data register SaveR1 .BLKW 1 .END

  6. IN Trap service routine - TRAP x23 ; Service Routine for Keyboard Input ; .ORIG x04A0 START ST R1,SaveR1 ; Save the values in the registers ST R2,SaveR2 ; that are used so that they ST R3,SaveR3 ; can be restored before RET ; LD R2,Newline L1 LDI R3,DSR ; Check DDR -- is it free? BRzp L1 STI R2,DDR ; Move cursor to new clean line ; LEA R1,Prompt ; Prompt is starting address ; of prompt string Loop LDR R0,R1,#0 ; Get next prompt character BRz Input ; Check for end of prompt string L2 LDI R3,DSR BRzp L2 STI R0,DDR ; Write next character of ; prompt string ADD R1,R1,#1 ; Increment Prompt pointer BRnzp Loop ; Input LDI R3,KBSR ; Has a character been typed? BRzp Input LDI R0,KBDR ; Load it into R0 L3 LDI R3,DSR BRzp L3 STI R0,DDR ; Echo input character ; to the monitor ; L4 LDI R3,DSR BRzp L4 STI R2,DDR ; Move cursor to new clean line LD R1,SaveR1 ; Service routine done, restore LD R2,SaveR2 ; original values in registers. LD R3,SaveR3 RET ; Return from trap (i.e., JMP R7) ; SaveR1 .BLKW 1 SaveR2 .BLKW 1 SaveR3 .BLKW 1 DSR .FILL xFE04 DDR .FILL xFE06 KBSR .FILL xFE00 KBDR .FILL xFE02 Newline .FILL x000A ; ASCII code for newline Prompt .STRINGZ "Input a character>" .END

  7. HALT service routine – TRAP x25 .ORIG xFD70 ; Where this routine resides ST R7, SaveR7 ST R1, SaveR1 ; R1: a temp for MC register ST R0, SaveR0 ; R0 is used as working space ; print message that machine is halting LD R0, ASCIINewLine TRAP x21 LEA R0, Message TRAP x22 LD R0, ASCIINewLine TRAP x21 ; ; clear bit 15 at xFFFE to stop the machine ; LDI R1, MCR ; Load MC register into R1 LD R0, MASK ; R0 = x7FFF AND R0, R1, R0 ; Mask to clear the top bit STI R0, MCR ; Store R0 into MC register ; ; return from HALT routine. ; (how can this routine return if the machine is halted above?) ; LD R1, SaveR1 ; Restore registers LD R0, SaveR0 LD R7, SaveR7 RET ; JMP R7, actually ; ; Some constants ; ASCIINewLine .FILL x000A SaveR0 .BLKW 1 SaveR1 .BLKW 1 SaveR7 .BLKW 1 Message .STRINGZ "Halting the machine." MCR .FILL xFFFE ; Address of MCR MASK .FILL x7FFF ; Mask to clear the top bit .END

  8. ; puts.asm ; This service routine writes a NULL-terminated string to the console. ; It services the PUTS service call (TRAP x22). ; Inputs: R0 is a pointer to the string to print. ; Context Information: R0, R1, and R3 are saved, and R7 is lost ; in the jump to this routine ; .ORIG x0450 ; Where this ISR resides ST R7, SaveR7 ; Save R7 for later return ST R0, SaveR0 ; Save other registers that ST R1, SaveR1 ; Are needed by this routine ST R3, SaveR3 ; ; ; Loop through each character in the array ; ; Loop LDR R1, R0, #0 ; Retrieve the character(s) BRz Return ; If it is 0, done L2 LDI R3,DSR BRzp L2 STI R1, DDR ; Write the character ADD R0, R0, #1 ; Increment pointer BRnzp Loop ; Do it all over again ; ; Return from the request for service call Return LD R3, SaveR3 LD R1, SaveR1 LD R0, SaveR0 LD R7, SaveR7 RET ; ; Register locations DSR .FILL xFE04 DDR .FILL xFE06 SaveR0 .FILL x0000 SaveR1 .FILL x0000 SaveR3 .FILL x0000 SaveR7 .FILL x0000 .END

  9. Subroutines JSR Instruction: JSR offset (11 bit) 0100 1 xxxxxxxxxxx [PC ] R7 JMP Jump to subroutine at offset from PC JSRR Instruction JSRR Rb 0100 0 00 xxx 000000 [PC ] R7 JMP Jump to subroutine at address in Rb Return RET 1100 000 111 000000 C1C0 [R7]  PC (JMP R7)

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

  11. Trap service routine for character input(subroutines) (1) .ORIG x04A0 START ST R7,SaveR7 JSR SaveReg LD R2,Newline JSR WriteChar LEA R1,PROMPT ; ; Loop LDR R2,R1,#0 ; Get next prompt char BRz Input JSR WriteChar ADD R1,R1,#1 BRnzp Loop ; Input JSR ReadChar ADD R2,R0,#0 ; Move char to R2 for writing JSR WriteChar ; Echo to monitor ; LD R2, Newline JSR WriteChar JSR RestoreReg LD R7,SaveR7 RET ; JMP R7 terminates ; the TRAP routine SaveR7 .FILL x0000 Newline .FILL x000A Prompt .STRINGZ "Input a character>" ; WriteChar LDI R3,DSR BRzp WriteChar STI R2,DDR RET ; JMP R7 terminates subroutine DSR .FILL xFE04 DDR .FILL xFE06 ; ReadChar LDI R3,KBSR BRzp ReadChar LDI R0,KBDR RET KBSR .FILL xFE00 KBDR .FILL xFE02 ; SaveReg ST R1,SaveR1 ST R2,SaveR2 ST R3,SaveR3 ST R4,SaveR4 ST R5,SaveR5 ST R6,SaveR6 RET

  12. Trap service routine for character input(subroutines) (2) ; RestoreReg LD R1,SaveR1 LD R2,SaveR2 LD R3,SaveR3 LD R4,SaveR4 LD R5,SaveR5 LD R6,SaveR6 RET ; SaveR1 .FILL x0000 SaveR2 .FILL x0000 SaveR3 .FILL x0000 SaveR4 .FILL x0000 SaveR5 .FILL x0000 SaveR6 .FILL x0000 ; .END

  13. Stack Push: Push ADD R6, R6, #-1 STR R0, R6, #0 Pop: Pop LDR R0, R6, #0 ADD R6, R6, #1 Which way does the stack grow?

  14. Underflow Pop LD R1, Empty ADD R2, R6, R1 ;Compare stack BRz Underflow ; pointer with x4000 LDR R0, R6, #0 ADD R6, R6, #1 AND R5, R5, #0 RET Underflow AND R5, R5, #0 ADD R5, R5, #1 RET Empty .FILL xC000 ; Empty  -x4000

  15. Overflow Push LD R1, Max ADD R2, R6, R1 BRz Overflow ADD R6, R6, #-1 STR R0, R6, #0 AND R5, R5, #0 RET Overflow AND R5, R5, #0 ADD R5, R5, #1 RET max .FILL xC005 ; Max  x3FFB

  16. Subroutine for Push & Pop ; Subroutines for carrying out the PUSH and POP functions. This ; program works with a stack consisting of memory locations x3FFF ; (BASE) through x3FFB (MAX). R6 is the stack pointer. ; POP ST R2,Save2 ; are needed by POP. ST R1,Save1 LD R1,BASE ; BASE contains -x3FFF. ADD R1,R1,#-1 ; R1 contains -x4000. ADD R2,R6,R1 ; Compare stack pointer to x4000 BRz fail_exit ; Branch if stack is empty. LDR R0,R6,#0 ; The actual "pop." ADD R6,R6,#1 ; Adjust stack pointer BRnzp success_exit PUSH ST R2,Save2 ; Save registers that ST R1,Save1 ; are needed by PUSH. LD R1,MAX ; MAX contains -x3FFB ADD R2,R6,R1 ; Compare stack pointer to -x3FFB BRz fail_exit ; Branch if stack is full. ADD R6,R6,#-1 ; Adjust stack pointer STR R0,R6,#0 ; The actual "push" success_exit LD R1,Save1 ; Restore original LD R2,Save2 ; register values. AND R5,R5,#0 ; R5 <-- success. RET fail_exit LD R1,Save1 ; Restore original LD R2,Save2 ; register values. AND R5,R5,#0 ADD R5,R5,#1 ; R5 <-- failure. RET BASE .FILL xC001 ; BASE contains -x3FFF. MAX .FILL xC005 Save1 .FILL x0000 Save2 .FILL x0000 .END

More Related