Overview
description
Transcript of 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
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}] )
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)
Trap Routine
Traps
1) Execute TRAP “vector” - Operating System Service Routines
2) Trap Vectors are at memory locations [0000:00FF]
3) Trap Vectors contain addresses of Trap Service Routines
4) (PC) is loaded into R7
5) Address of Trap Service Routine loaded into PC
6) Service Routine Program executed
7) Trap service routine program ends with an RET ( (R7) loaded into PC)
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 characterTryWrite LDI R1, DSR ; Get status BRzp TryWrite ; Bit 15 on says display is readyWriteIt STI R0, DDR ; Write character
; return from trapReturn LD R1, SaveR1 ; Restore registers RET ; Return from trap (JMP R7, actually)DSR .FILL xFE04 ; Address of display status registerDDR .FILL xFE06 ; Address of display data registerSaveR1 .BLKW 1 .END
IN Trap service routine - TRAP x23; Service Routine for Keyboard Input; .ORIG x04A0START 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,NewlineL1 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 stringLoop LDR R0,R1,#0 ; Get next prompt character BRz Input ; Check for end of prompt stringL2 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 R0L3 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 1SaveR2 .BLKW 1SaveR3 .BLKW 1DSR .FILL xFE04DDR .FILL xFE06KBSR .FILL xFE00KBDR .FILL xFE02Newline .FILL x000A ; ASCII code for newline Prompt .STRINGZ "Input a character>" .END
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 x000ASaveR0 .BLKW 1SaveR1 .BLKW 1SaveR7 .BLKW 1Message .STRINGZ "Halting the machine."MCR .FILL xFFFE ; Address of MCRMASK .FILL x7FFF ; 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 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, doneL2 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 callReturn LD R3, SaveR3 LD R1, SaveR1 LD R0, SaveR0 LD R7, SaveR7 RET;; Register locationsDSR .FILL xFE04DDR .FILL xFE06SaveR0 .FILL x0000 SaveR1 .FILL x0000SaveR3 .FILL x0000SaveR7 .FILL x0000 .END
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)
Subroutines
Subroutines
1) 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 messageRo possibly contains return parameter (or address)
6) Subroutine program ends with an RET( (R7) loaded into PC)
How does this mechanism support recursion?
Trap service routine for character input(subroutines) (1)
.ORIG x04A0START 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 x0000Newline .FILL x000APrompt .STRINGZ "Input a character>";WriteChar LDI R3,DSR BRzp WriteChar STI R2,DDR RET ; JMP R7 terminates subroutineDSR .FILL xFE04DDR .FILL xFE06 ;ReadChar LDI R3,KBSR BRzp ReadChar LDI R0,KBDR RETKBSR .FILL xFE00KBDR .FILL xFE02;SaveReg ST R1,SaveR1 ST R2,SaveR2 ST R3,SaveR3 ST R4,SaveR4 ST R5,SaveR5 ST R6,SaveR6 RET
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 x0000SaveR2 .FILL x0000SaveR3 .FILL x0000SaveR4 .FILL x0000SaveR5 .FILL x0000SaveR6 .FILL x0000; .END
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?
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
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
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