150 likes | 292 Views
Chapter 9. TRAP Routines Subroutines Privileged Instructions TRAP Routines. Subroutines. Used for Frequently executed code segments: written only once. Hides the implementation details. Passing of parameters and return values, in registers or memory.
E N D
Chapter 9 TRAP Routines Subroutines Privileged Instructions TRAP Routines
Subroutines • Used for • Frequently 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 either • Procedures, or • Functions
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 x 0 0 pageoffset9 JSR L Jump to Sub - 1: JSR/JMP • JSR/JMP: jump subroutine (direct) • JSR: IR[11] = 1, and R7 (PC) i.e. PC is saved in R7 • JMP: IR[11] = 0, PC is not saved. • PC PC[15:9] @ IR[8:0]
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 x 0 0 x x x index6 JSR L Jump to Sub - 2: JSRR/JMPR • JSRR/JMPR: jump subroutine (base + offset) • JSRR: IR[11] = 1, and R7 (PC) i.e. PC is saved in R7 • JMPR: IR[11] = 0, PC is not saved. • PC (BaseReg) + Zext(IR[5:0]) BaseReg
Privileged Instructions • There are several instructions that are best executed by a supervisor program (OS) rather than a user program: • IO instructions • Loading of memory mapping registers • Resetting the clock • Halt i.e. instructions where one program can affect the behavior of another. • Hardware enforces two modes of operation • User Mode • Privileged Mode (aka. supervisor, kernel, monitor mode) • Only the supervisor program can execute privileged instructions.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 trapvector8 1 1 1 1 0 0 0 0 TRAP Instructions • TRAP: A special instruction • A 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 instruction • Return the execution to the last calling point. • PC (R7) TRAP 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
System Control Block Or Interrupt Vector Table In LC-2 8 bits specify one of 256 locations (x0000 to x00FF) The location contains the address of the TRAP service routine. TRAP & Interrupts Exact same mechanisms A 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. = x04A0 R7 x300B Address x300A Service routine TRAP Example
x0000 : : x0020 : x0021 x0430 x0022 x0450 x0023 x04A0 x0024 x04E0 x0025 xFD70 : : x00FF The LC-2 System Control Block LC-2 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.
Character Input Service Routine (IN) Figure 9.4 p.177
OUT Figure 9.2 p. 178
HALT Figure 9.6 p. 181
;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 RETURN L2 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 xF3FC CRTDR .FILL xF3FF SaveR0 .FILL x0000 SaveR1 .FILL x0000 SaveR3 .FILL x0000 SaveR7 .FILL x0000 .END PUTS
;Calling program .ORIG $3000 ld r1,num1 ld r2,num2 jsr multi st r3,prod HALT ; ;Input Data & Result num1 .fill x0006 num2 .fill x0003 prod .blkw 1 ;Subroutine multi ;Multiply 2 positive numbers ;Parameters: ; In: R1,R2; Out: R3 ; multi and r3,r3,#0 add r4,r1,#0 brz zero loop add r3,r2,r3 add r1,r1,#-1 brp loop zero ret .end Subroutine call examples
Library Routines • Library • A set of routines for a specific domain application. • Example: math, graphics, GUI, etc. • Defined outside a program. • Library routine invocation • Labels for the routines are defined as external. In LC-2: .External Label • Each library routine contains its own symbol table. • A linker resolves the external addresses before creating the executable image.