Presentation is loading. Please wait.

Presentation is loading. Please wait.

TRAP Routines Subroutines Privileged Instructions

Similar presentations


Presentation on theme: "TRAP Routines Subroutines Privileged Instructions"— Presentation transcript:

1 TRAP Routines Subroutines Privileged Instructions
Chapter 9 TRAP Routines Subroutines Privileged Instructions

2 Subroutines Used for Can be either
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

3 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  IR[8:0] JSR L x 0 0 pageoffset9

4 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]) JSR L x 0 0 x x x index6 BaseReg

5 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.

6 TRAP Instructions TRAP: A special instruction RET – return 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) trapvector8 TRAP

7 TRAP Example System Control Block TRAP & Interrupts
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 x300B R7 Service routine Address x300A

8 LC-2 TRAP Routines GETC (TRAP x20) OUT (TRAP x21) PUTS (TRAP x22)
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. : x0430 x0450 x04A0 x04E0 xFD70 x0000 x0020 x0021 x0022 x0023 x0024 x0025 x00FF The LC-2 System Control Block

9 Character Input Service Routine (IN)
Figure 9.4 p.177

10 IN (continued)

11 OUT Figure 9.2 p. 178

12 HALT Figure 9.6 p. 181

13 PUTS ;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

14 Subroutine call examples
;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

15 Library Routines Library Library routine invocation
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.


Download ppt "TRAP Routines Subroutines Privileged Instructions"

Similar presentations


Ads by Google