Phần cứng - Chapter 9: Trap routines
GETC (TRAP x20)
Read a single character from KBD.
ASCII copied in R0, R0[15:8] cleared.
OUT (TRAP x21)
Write R0[7:0] to CRT.
PUTS (TRAP x22)
Write a string to CRT. String address in R0.
IN (TRAP x23)
Print a prompt on the screen and read a single character from KBD.
Character is echoed to CRT. ASCII copied to R0[7:0] and R0[15:8] cleared.
HALT (TRAP x25)
Prints message on CRT & halts execution.
15 trang |
Chia sẻ: huyhoang44 | Lượt xem: 694 | Lượt tải: 0
Bạn đang xem nội dung tài liệu Phần cứng - Chapter 9: Trap routines, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Chapter 9TRAP Routines Subroutines Privileged Instructions TRAP RoutinesSubroutinesUsed forFrequently executed code segments: written only once.Hides the implementation details.Passing of parameters and return values, in registers or memory.Is called from within a program; control is returned to the same point it was called from.Can be eitherProcedures, orFunctions2Jump to Sub - 1: JSR/JMPJSR/JMP: jump subroutine (direct)JSR: IR[11] = 1, and R7 (PC) i.e. PC is saved in R7JMP: IR[11] = 0, PC is not saved.PC PC[15:9] @ IR[8:0] 0 0 0 0 15 14 13 1211 10 9 8 7 6 5 4 3 2 1 0JSRLx0 0 pageoffset93Jump to Sub - 2: JSRR/JMPRJSRR/JMPR: jump subroutine (base + offset)JSRR: IR[11] = 1, and R7 (PC) i.e. PC is saved in R7JMPR: IR[11] = 0, PC is not saved.PC (BaseReg) + Zext(IR[5:0]) 0 0 0 0 15 14 13 1211 10 9 8 7 6 5 4 3 2 1 0JSRLx0 0x x x index6BaseReg4Privileged InstructionsThere are several instructions that are best executed by a supervisor program (OS) rather than a user program:IO instructionsLoading of memory mapping registersResetting the clockHalt i.e. instructions where one program can affect the behavior of another. Hardware enforces two modes of operationUser ModePrivileged Mode (aka. supervisor, kernel, monitor mode)Only the supervisor program can execute privileged instructions.5TRAP InstructionsTRAP: A special instructionA subroutine call used to invoke a specific supervisor routine.It switches the execution to privileged mode, and reverts back to user mode when the routine completes.PC Mem[Zext(IR[7:0])]RET – return instructionReturn the execution to the last calling point.PC (R7)1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 115 14 13 1211 10 9 8 7 6 5 4 3 2 1 00 0 0 0 trapvector8TRAP6TRAP Example= x04A0x300BR7Service routineAddress x300ASystem Control BlockOr Interrupt Vector TableIn LC-28 bits specify one of 256 locations (x0000 to x00FF)The location contains the address of the TRAP service routine.TRAP & InterruptsExact same mechanismsA TRAP is an instruction (event internal to a program).An interrupt is external to a program (from an I/O device)Both invoke a supervisor service routine.7LC-2 TRAP RoutinesGETC (TRAP x20)Read a single character from KBD. ASCII copied in R0, R0[15:8] cleared.OUT (TRAP x21)Write R0[7:0] to CRT.PUTS (TRAP x22)Write a string to CRT. String address in R0.IN (TRAP x23)Print a prompt on the screen and read a single character from KBD.Character is echoed to CRT. ASCII copied to R0[7:0] and R0[15:8] cleared.HALT (TRAP x25)Prints message on CRT & halts execution.::x0430x0450x04A0x04E0xFD70x0000:x0020x0021x0022x0023x0024x0025::x00FFThe LC-2 System Control Block8Character Input Service Routine (IN)Figure 9.4 p.1779IN (continued)10OUTFigure 9.2 p. 17811HALTFigure 9.6 p. 18112PUTS ;puts.asm (TRAP x22);R0 points to null-terminated string;R0, R1 & R3 saved; R7 is lost. .ORIG x0450 ST R7,SaveR7 ST R0,SaveR0 ST R1,SaveR1 ST R3,SaveR3;LOOP LDR R1,R0,#0 BRz RETURNL2 LDI R3,CRTSR BRzp L2 STI R1,CRTDR ADD R0,R0,#1 BR LOOP RETURN LD R3,SaveR3 LD R1,SaveR1 LD R0,SaveR0 LD R7,SaveR7 RET;CRTSR .FILL xF3FCCRTDR .FILL xF3FFSaveR0 .FILL x0000SaveR1 .FILL x0000SaveR3 .FILL x0000SaveR7 .FILL x0000 .END13Subroutine call examples;Calling program .ORIG $3000 ld r1,num1 ld r2,num2 jsr multi st r3,prod HALT;;Input Data & Resultnum1 .fill x0006num2 .fill x0003prod .blkw 1;Subroutine multi;Multiply 2 positive numbers;Parameters:; In: R1,R2; Out: R3;multi and r3,r3,#0 add r4,r1,#0 brz zeroloop add r3,r2,r3 add r1,r1,#-1 brp loopzero ret .end14Library RoutinesLibraryA set of routines for a specific domain application.Example: math, graphics, GUI, etc.Defined outside a program.Library routine invocationLabels for the routines are defined as external. In LC-2:.External LabelEach library routine contains its own symbol table.A linker resolves the external addresses before creating the executable image.15
Các file đính kèm theo tài liệu này:
- najjar_chap9_4063.ppt