1 / 18

Overview

Overview. Projects – Project 1’s Project 2’s no later than Dec 14th Homework Problem – 14.X Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays Evaluations Final Exam Dec 14 th 10:30 – 12:45. Example 14.X. #include <studio.h>

jill
Download Presentation

Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overview • Projects – Project 1’s Project 2’s no later than Dec 14th • Homework Problem – 14.X • Pointer Variables • Pass by Value • Pass by Reference (or by Pointer) • Arrays • Evaluations • Final Exam Dec 14th 10:30 – 12:45

  2. Example 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; }

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

  4. 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, #1 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, #2 ; pop 2 Multiply arguments ADD R6, R6, #1 LDR R1, R5, #-3 ; add d to Multiply (a, b) ADD R0, R1, R0

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

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

  7. Problem 14.X (5)Stack EFF0 EFF1 EFF2 EFF3 mult R5 a local variable EFF4 (R5) EFF5 (R7) EFF6 multiply return value EFF7 a {b} EFF8 b {c} pass values EFF9 d EFFA c EFFB b EFFC main R5 a local variables EFFD (R5) EFFE (R7) EFFF main return value F000 init R6

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

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

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

  11. Pointers in C • Declaration int *p; /* p is a pointer to an int */ • A pointer in C is always a pointer to a particular data type:int*, double*, char*, etc. • Operators *p-- returns the value pointed to by p &z-- returns the address of variable z

  12. Declaring Pointer variables int *ptr; ptr is a pointer variable that points to an int variable char *cp; cp is a pointer variable that points to a character variable double *dp; dp is a pointer variable that points to a double variable * is referred to as the indirection operator, or the dereference operator

  13. Address Operator & int object; int *ptr; object = 4; ptr = &objectpointer variable ptr points to variable object

  14. Example Define local variables: int object; int *ptr; Now, let’s assign values to them: object = 4; ptr = &object *ptr = *ptr + 1;

  15. Example: Parameter Passing by Value #include <stdio.h> void Swap(int firstVal, int secondVal); int main() { int valueA = 3; int valueB = 4; Swap(valueA, valueB); } void Swap(int firstVal, int secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = firstVal; firstVal = secondVal; secondVal = tempVal; } Snapshot before return from subroutine

  16. Example: Parameter Passing by Reference #include <stdio.h> void NewSwap(int *firstVal, int *secondVal); int main() { int valueA = 3; int valueB = 4; NewSwap(&valueA, &valueB); } void NewSwap(int *firstVal, int *secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = *firstVal; *firstVal = *secondVal; *secondVal = tempVal; } Snapshots during the exchange

  17. Scanf( ) function • Recall reading from the keyboard in C: scanf(“%d”, &input); • Why do we use &input ?

  18. Pointer Example #include <stdio.h> int IntDivide(int x, int y, int *quoPtr, int *remPtr); int main() { int dividend; /* The number to be divided */ int divisor; /* The number to divide by */ int quotient; /* Integer result of division */ int remainder; /* Integer remainder of division */ int error; /* Did something go wrong? */ printf("Input dividend: "); scanf("%d", &dividend); printf("Input divisor: "); scanf("%d", &divisor); error = IntDivide(dividend,divisor,&quotient,&remainder); if (!error) /* !error indicates no error */ printf("Answer: %d remainder %d\n", quotient, remainder); else printf("IntDivide failed.\n"); } int IntDivide(int x, int y, int *quoPtr, int *remPtr) { if (y != 0) { *quoPtr = x / y; /* Modify *quoPtr */ *remPtr = x % y; /* Modify *remPtr */ return 0; } else return -1; }

More Related