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)

Slides:



Advertisements
Similar presentations
Chapter 16 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Advertisements

Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
Programming and Data Structure
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
Chapter 5 The LC-3 LC-3 Computer Architecture Memory Map
Chapter 6 Programming in Machine Language The LC-3 Simulator
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
Chapters 9 & 10 Midterm next Wednesday (11/19) Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET The Stack SSP & USP Interrupts RTI.
Chapter 4 H1 Assembly Language: Part 2. Direct instruction Contains the absolute address of the memory location it accesses. ld instruction:
Overview Compiling a C Program with Subroutines. Final Exam Wednesday, Dec 10, 10:30 – 12:45 Final with be Comprehensive: Logic and Computer Architecture.
Chapter 9 Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
Chapter 9 Trap Routines TRAP number (go to service routine) & RET (return from service routine) Subroutines (or Functions) JSR offset or JSRR rn (go to.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
CMPE13Cyrus Bazeghi Chapter 14 Functions in C. CMPE13 2 Functions Smaller, simpler, subcomponents of programs Provide abstraction –hide low-level details.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
 Using functions  Writing functions  Basics  Prototypes  Parameters  Return types  Functions and memory  Pointer parameters.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Memory and Data Structures Patt and Patel Ch. 10 & 16.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Chapter 10 And, Finally... The Stack
Chapter 14 Functions.
CSE 220 – C Programming Pointers.
Chapter 12 Variables and Operators
Computer Science 210 Computer Organization
Chapter 12 Variables and Operators
Chapter 14 Functions.
Chapter 12 Variables and Operators
Chapter 16 Pointers and Arrays
Chapter 14 Functions.
More C Stack Frames / Pointer variables
Chapter 16 Pointers and Arrays
Computer Science 210 Computer Organization
Pointers and Arrays.
Chapter 14 Functions.
Chapter 16 Pointers and Arrays
Chapter 16 Pointers and Arrays
Chapter 14 Functions.
Chapter 16 Pointers and Arrays
Chapter 17 Recursion.
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

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 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 ; 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, #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

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 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) ; { multADD 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 loopADD R1, R1, #-1; dec b BRz done ADD R0, R0, #0 BR loop doneSTR 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) Stack EFF0 EFF1 EFF2 EFF3mult R5a local variable EFF4(R5) EFF5(R7) EFF6multiply return value EFF7a {b} EFF8b {c}pass values EFF9d EFFAc EFFBb EFFCmain R5a local variables EFFD(R5) EFFE(R7) EFFFmain return value F000init R6

Problem 14.X (6) Memory Map & Assignments Program: x 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: NameTypeLocation from (R4)Initial Value dint 0 3 Main() Local Variable Table: NameTypeLocation from (R5)Initial Value aint 0 - bint -1 - cint -2 - dint -3 4 Mult() Local Variable Table: NameTypeLocationInitial Value aint 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 xEFF6mult return value begin mult context page xEFF7a {becomes mult b} pass parameter to mult xEFF8b {becomes mult c} “ “ xEFF9d [addr:(R5)-3]main local variable xEFFAc [addr:(R5)-2] “ “ xEFFBb [addr:(R5)-1] “ “ xEFFC {main}R5 ->a [addr:(R5)-0] “ “ xEFFD(R5)preserved for return to system xEFFE(R7)preserved for return to system xEFFFmain return value begin main context page xF000 init R6 -> Output:

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

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

Address Operator & int object; int *ptr; object = 4; ptr = &object pointer variable ptr points to variable object

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

Example: Parameter Passing by Value #include 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

Example: Parameter Passing by Reference #include 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

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

Pointer Example #include 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; }