150 likes | 347 Views
Chapter 9 Overview. 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 (Supports Software Interrupts) x0020 [x0400] GETC (Read Char from Keyboard)
E N D
Chapter 9Overview • 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 (Supports Software Interrupts) x0020 [x0400] GETC (Read Char from Keyboard) x0021 [x0430] OUT (Write Character to Console) x0022 [x0450] PUTS (Write string to Console) x0023 [x04A0] IN (Prompt, input character from Keyboard, echo character to Console) x0024 [x04E0] PUTSP (Write “packed” string to Console) x0025 [xFD70] HALT (Turn off run latch in MCR) x0100 – x01FF Interrupt Vectors (Supports Hardware Interrupts) x0200 – x2FFF System Programs & Data (“Operating System”) x3000 – xFDFF User Programs Area xFE00 – xFFFF I/O Programming “Registers” (Mapped I/O Registers) xFE00 KBSR [15 {Ready}, 14 {Intr enable}] (Keyboard Status Register) xFE02 KBDR [7:0{ascii data}] (Keyboard Data Register) xFE04 DSR [15{Done}, 14{Intr enable}] (Display Status Register) xFE06 DDR [7:0{ascii data}] (Display Data Register xFFFE MCR [15{Run latch}] (Machine Control Register)
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)
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 stored in 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)
TRAP x21 OUT Trap Vector Routine (Output Character) ; out.asm ; .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
TRAP x23IN Trap Service Routine (Input Character) ; 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
TRAP x25HALT Service Routine .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
TRAP 22 PUTS Trap Service Routine (Output a Character String) ; 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
Programming Exercise #1 Write a program to add the contents of R0 and R1, and indicate in R2 if there was an overflow
Programming Exercise #1 ; Add R3=R0+R1, R2=0 indicates no overflow ; .ORIG x3000 AND R2, R2, #0 ;Initially R2=0 (no Overflow assumed) ADD R3, R0, R1 ;R3=R0+R1 ; test for overflow ADD R0, R0, #0 ;test R0 BRN NEG ;Branch if RO negative ADD R1, R1, #0 ;test R1 BRN DONE ;No overflow if operand signs differ (R1 NEG) ADD R3, R3, #0 ;maybe, test R3 BRZP DONE ;No overflow if result sign matches (All POS) ADD R2, R2, #1 ;R2=1 indicating overflow NEG ADD R1, R1, #0 ;test R1 BRZP DONE ;No overflow if operand signs differ (R1 POS) ADD R3, R3, #0 ;maybe, test R3 BRN DONE ;No overflow if result sign matches (All NEG) ADD R2, R2, #1 ;R2=1 indicating overflow DONE HALT .END
Programming Exercise #2 Write a program to count the 1’s in register R0
Programming Exercise #2 ; Program to count 1's in Register R0 ; R3 is a working copy of R0 ; R1 contains the count ; R2 is a loop counter .orig x3100 ADD R3, R0, #0 ;copy R0 into R3 AND R1, R1, #0 ;clear count ADD R3, R3, #0 ;test for Neg BRZP NEXT ;count if Neg ADD R1, R1, #1 NEXT AND R2, R2, #0 ;check remaining 15 bits ADD R2, R2, #-15 LOOP ADD R3, R3, R3 ;shift R3 left BRZP AGAIN ;count if Neg ADD R1, R1, #1 AGAIN ADD R2, R2, #1 ;loop until done BRN LOOP HALT .END
Programming Exercise #3 Write a program to add two, two digit numbers read from the console
; Program to add two 2 digit decimal numbers read from the console ; R1 & R2 are working registers to load 2 digit number ; R3 is first number ; R4 is second number ; R5 is the sum ; R6 is conversion offset .orig x3600 LEA R0, MESSAGE ;print message PUTS ; Get first number LD R0, NEWLINE ;print PROMPT1 OUT OUT LEA R0, PROMPT1 PUTS GETC ;get first character OUT LD R6, M30 ;convert char to hex ADD R0, R0, R6 ADD R1, R0, R0 ;R1 = 2xR0 ADD R2, R1, #0 ;copy R1 into R2 ADD R2, R2, R2 ;R2 = 4xR0 ADD R2, R2, R2 ;R2 = 8xR0 ADD R2, R2, R1 ;R2 = 10xR0 GETC ;get second character OUT ADD R0, R0, R6 ;convert to hex ADD R3, R2, R0 ;R3 = first decimal number ; Get second number LEA R0, PROMPT2 ;get first character PUTS GETC OUT ADD R0, R0, R6 ;convert char to hex ADD R1, R0, R0 ;R1 = 2xR0 ADD R2, R1, #0 ;copy R1 into R2 ADD R2, R2, R2 ;R2 = 4xR0 ADD R2, R2, R2 ;R2 = 8xR0 ADD R2, R2, R1 ;R2 = 10xR0 GETC ;get second character OUT ADD R0, R0, R6 ;convert to hex ADD R4, R2, R0 ;R4 = first decimal number Programming Exercise #3
; Add the numbers and print results ADD R5, R4, R3 ;R5 = R3 + R4 LEA R0, SUM ;prepare to print results PUTS LD R4, P100 ;find 1st digit LD R3, M100 AND R0, R0, #0 LOOP1 ADD R0, R0, #1 ADD R5, R5, R3 ;subtract 100 until negative BRZP LOOP1 ADD R5, R5, R4 ADD R0, R0, #-1 LD R6, P30 ;convert to ascii & print ADD R0, R0, R6 OUT AND R0, R0, #0 ;find 2nd digit LOOP2 ADD R0, R0, #1 ADD R5, R5, #-10 ;subtract 10 until negative BRZP LOOP2 ADD R5, R5, #10 ADD R0, R0, #-1 LD R6, P30 ;convert to ascii & print ADD R0, R0, R6 OUT ADD R0, R5, R6 ;convert and print 3rd digit OUT LD R0, NEWLINE OUT HALT MESSAGE .STRINGZ "Enter two 2-digit decimal numbers:" NEWLINE .FILL x000A PROMPT1 .STRINGZ " The sum of " PROMPT2 .STRINGZ " and " SUM .STRINGZ " is " M30 .FILL xFFD0 ;-x30 P30 .FILL X0030 ; x30 M100 .FILL xFF9C ;-100 P100 .FILL x0064 ; 100 .END Programming Exercise #3 (2)