120 likes | 294 Views
Overview. Homework Problem – 14.Y (Interrupt Program) Homework Problem – 14.X C Compling Final Exam – Wednesday Dec 12. HW 14.Y. ; main program ; ; Interrupt Driven Keyboard program ; Read and echo an endless string ; KEYBOARD INTERRUPT VECTOR: x180
E N D
Overview • Homework Problem – 14.Y (Interrupt Program) • Homework Problem – 14.X C Compling • Final Exam – Wednesday Dec 12
HW 14.Y ; main program ; ; Interrupt Driven Keyboard program ; Read and echo an endless string ; KEYBOARD INTERRUPT VECTOR: x180 ; KBSR INTERRUPT ENABLE BIT: 15 ; .ORIG x4000 BEGIN LEA R6, BEGIN ; initialize stack pointer to x4000 ; builds downward LEA R0, PROMPT ; print prompt PUTS LD R0, NL OUT ; LEA R0, ISR ; load keyboard interrupt vector: x80 STI R0, KBIV ; (location 180) LD R0, KBSRIE ; enable keyboard interrupts STI R0, KBSR ; LOOP BRNZP LOOP ; loop forever ! HALT ; ; keyboard interrupt service routine: ; ISR ADD R6, R6, #-1 ; increment stack pointer STR R0, R6, #0 ; push R0, because we'll use it LDI R0, KBDR ; reset interrupt (by reading KBDR) OUT LDR R0, R6, #0 ; restore R0 ADD R6, R6, #1 RTI ; ; data ; PROMPT .STRINGZ "Input a character string: " NL .FILL x0A ; new line ; KBSR .FILL xFE00 ; keyboard status register KBSRIE .FILL x4000 ; bit 14 is interrupt enable bit KBDR .FILL xFE02 ; keyboard data register KBIV .FILL x180 ; keyboard interrupt vector ; .END
HW 14.X #include <studio.h> int Multiply(int b, int c); int d = 3; int main() { int a,b,c; int d = 4; a = 1; b = 2; c = d + Multiply (a, b); printf(“%d %d %d %d/n”, a, b, c, d); } int Multiply(int b, int c) { int a; a = b * c; return a; }
Problem 14.X (1) ; #include <studio.h> ; int Multiply (int b, int c); .orig x3000 LD R6, stk ; set run-time stk ptr (R6) LEA R4, global ; set global variable stk Ptr (R4) ADD R6, R6, #-1 ; allocate spot for return value ADD R6, R6, #-1 ; push R7 STR R7, R6, #0 ADD R6, R6, #-1 ; push R5 (callers frame ptr) STR R5, R6, #0 ; int d = 3; AND R0, R0, #0 ; define global value d = 3 ADD R0, R0, #3 STR R0, R4, #0 ; (R4) -> d = 3
Problem 14.X (2) ; int main() ; { ; int a,b,c; ADD R6, R6, #-1 ; set main's frame ptr R5 = (R6) ADD R5, R6, #0 ADD R6, R6, #0 ; (R5) -> a ADD R6, R6, #-1 ; (R5) -1 -> b ADD R6, R6, #-1 ; (R5) -2 -> c ; int d = 4; ADD R6, R6, #-1 ; (R5) -3 -> d = 4 AND R0, R0, #0 ADD R0, R0, #4 STR R0, R5, #-3 ; a = 1; AND R0, R0, #0 ; a = 1 ADD R0, R0, #1 STR R0, R5, #0 ; b = 2; AND R0, R0, #0 ; b = 2 ADD R0, R0, #2 STR R0, R5, #-1 ; c = d + Multiply (a, b); ADD R6, R6, #-1 ; push b onto stk LDR R0, R5, #-1 STR R0, R6, #0 ADD R6, R6, #-1 ; push a onto stk LDR R0, R5, #0 STR R0, R6, #0 JSR mult LDR R0, R6, #0 ; pop Multiply (a, b) value from stk ADD R6, R6, #1 ADD R6, R6, #2 ; pop 2 Multiply arguments LDR R1, R5, #-3 ; add d to Multiply (a, b) ADD R0, R1, R0 STR R0, R5, #-2 ; store in c
Problem 14.X (3) ; printf("%d %d %d %d/n", a, b, c, d); AND R1, R1, #0 ; ASCII Conversion values ADD R1, R1, #15 ADD R1, R1, #15 Add R2, R1, #2 ; load a #32 (blank) in R2 ADD R1, R1, #15 ADD R1, R1, #3 ; load a #48 (ascii 0) in R1 LDR R0, R5, #0 ; display a ADD R0, R0, R1 TRAP x21 ADD R0, R2, #0 TRAP x21 LDR R0, R5, #-1 ; display b ADD R0, R0, R1 TRAP x21 ADD R0, R2, #0 TRAP x21 LDR R0, R5, #-2 ; display c ADD R0, R0, R1 TRAP x21 ADD R0, R2, #0 TRAP x21 LDR R0, R5, #-3 ; display d ADD R0, R0, R1 TRAP x21 AND R0, R0, #0 ; display new line ADD R0, R0, x0A TRAP x21 ; return 0 AND R0, R0, #0 ; return value = zero STR R0, R5, #3 ; (write in return value slot) ADD R6, R5, #4 ; pop main local varables (a,b,c,d) LDR R5, R6, #0 ; pop frame ptr ADD R6, R6, #1 LDR R7, R6, #0 ; pop the return address ADD R6, R6, #1 RET ; }
Problem 14.X (4) ; int Multiply (int b, int c) ; { mult ADD R6, R6, #-1 ; allocate spot for return value ADD R6, R6, #-1 ; push R7 STR R7, R6, #0 ADD R6, R6, #-1 ; push R5 (callers frame ptr) STR R5, R6, #0 ; int a; ADD R6, R6, #-1 ; set Multiply frame ptr R5 = (R6) ADD R5, R6, #0 ADD R6, R6, #0 ; (R5) -> a ; a = b * c; LDR R0, R5, #5 ; load c LDR R1, R5, #4 ; load b loop ADD R1, R1, #-1 ; dec b BRz done ADD R0, R0, #0 BR loop done STR R0, R5, #0 ; store a ; return a; LDR R0, R5, #0 ; load a STR R0, R5, #3 ; (write in return value slot) ADD R6, R5, #1 ; pop multiply local varable LDR R5, R6, #0 ; pop frame ptr ADD R6, R6, #1 LDR R7, R6, #0 ; pop the return address ADD R6, R6, #1 RET ; } stk .FILL xF000 ; top of empty stack global .BLKW x0100 ; beginning of global variables .END
Problem 14.X (5)Run-time Stacks Run-time-Global-Variable-Stack: x3061 R4 -> d x3062 . . Run-time-User Stack: xEFF0 xEFF1 xEFF2 xEFF3 R6 & {mult}R5 -> a [addr:(R5)-0] mult local variable xEFF4 [R5] preserved for return to main xEFF5 [R7] preserved for return to main xEFF6 mult return value begin mult context page xEFF7 a {becomes mult b} pass parameter to mult xEFF8 b {becomes mult c} “ “ xEFF9 d [addr:(R5)-3] main local variable xEFFA c [addr:(R5)-2] “ “ xEFFB b [addr:(R5)-1] “ “ xEFFC {main}R5 -> a [addr:(R5)-0] “ “ xEFFD [R5] preserved for return to system xEFFE [R7] preserved for return to system xEFFF main return value begin main context page xF000 init R6 ->
Problem 14.X (6) Memory Map & Assignments Program: x3000 + Functions: main() and mult(int A, int B) Run-time-stack: xF000 – Global-Variable-Stack: After program + Function context (top to bottom): Pass variables to called function Local Variables (Context-Ptr points to first local variable) Preserved Context-Ptr and PC Return value Register Assignments: R0: Trap pass values R4: Gobal-Stack Ptr R5: Context Page Ptr R6: Run-time-stack Ptr R7: Preserved PC for calling function
Problem 14.X (7) Compilation Main() Global Variable Table: Name Type Location from (R4) Initial Value d int 0 3 Main() Local Variable Table: Name Type Location from (R5) Initial Value a int 0 - b int -1 - c int -2 - d int -3 4 Mult() Local Variable Table: Name Type Location Initial Value a int 0 -
Problem 14.X (8) Execution Run-time-Global-Variable-Stack: x3061 R4 -> d X3062 Run-time-stack: xEFF0 xEFF1 xEFF2 xEFF3 {mult}R5 -> a [addr:(R5)-0] mult local variable xEFF4 (R5) preserved for return to main xEFF5 (R7) preserved for return to main xEFF6 mult return value begin mult context page xEFF7 a {becomes mult b} pass parameter to mult xEFF8 b {becomes mult c} “ “ xEFF9 d [addr:(R5)-3] main local variable xEFFA c [addr:(R5)-2] “ “ xEFFB b [addr:(R5)-1] “ “ xEFFC {main}R5 -> a [addr:(R5)-0] “ “ xEFFD (R5) preserved for return to system xEFFE (R7) preserved for return to system xEFFF main return value begin main context page xF000 init R6 -> Output: 1 2 6 4
Final ExamWednesday, Dec 12, 10:30 – 12:45 Final with be Comprehensive: Logic and Computer Architecture (Approx 30%-) Machine/Assembly Language & Stacks (Approx 30%+) Interrupts, C, Compilation, Pointers (Approx 40%)