CSCI206 - Computer Organization & Programming

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 9: MIPS Instruction Set
Programming and Data Structure
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
The University of Adelaide, School of Computer Science
Computer Organization CS224
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
Lecture 8: MIPS Instruction Set
Kernighan/Ritchie: Kelley/Pohl:
L5 – Addressing Modes 1 Comp 411 – Spring /29/08 Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Pointers Applications
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
Lecture 4. Miscellaneous Addressing Mode, Memory Map, Pointer, and ASCII Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Organization and Design Addressing Modes Montek Singh Wed, Sep 19, 2012 Lecture 6.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Pointers and Arrays in C and Assembly Language. Arrays Provide useful abstraction Match up to machine memory organization Array index computation –time.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 14, 15 Addressing Mode.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
CSCI206 - Computer Organization & Programming
Functions and Pointers
CS2100 Computer Organisation
Pointers and Pass By Reference
COSC 220 Computer Science II
Procedures (Functions)
Pointers and Arrays in C and Assembly Language
Pointers.
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Pointers.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Instructions - Type and Format
CSCI206 - Computer Organization & Programming
Computer Organization and Design Addressing Modes
Pointers  Week 10.
Lecture 18 Arrays and Pointer Arithmetic
Operands and Addressing Modes
October 1 Programming Questions?
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
The University of Adelaide, School of Computer Science
Logical and Decision Operations
Computer Organization and Design Assembly & Compilation
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
Pointers.
Initializing variables
3.
Computer Organization and Design Addressing Modes
CSCE 206 Lab Structured Programming in C
Operands and Addressing Modes
9/27: Lecture Topics Memory Data transfer instructions
Pointer Syntax Review *ptrName Declaring Pointers
August 31 addresses Drop box Questions? 31 August 2004
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

CSCI206 - Computer Organization & Programming Introducing Pointers in C zyBook: 9.1, 9.3 prelab for lab 7

Pointers Powerful feature of C. Key: pointers are simply memory addresses of the variables! Java/C++ “references” are pointers with extra safety checks. In C the programmer is responsible. * is the dereference operator, used to declare a pointer and access memory pointed to by a pointer. & is the address of operator, it returns a pointer.

Pointers int i = 7; int j = -1; int* pi = &i; int *pj = &j; printf(" i = %d\n", i); printf(" j = %d\n", j); printf(" pi= %p\n", pi); printf(" pj= %p\n", pj); printf(" data pointed to by pi= %d\n", *pi); printf(" data pointed to by pj= %d\n\n", *pj); *pi = 6; declare and initialize pointers

Pointers int i = 7; int j = -1; int *pi = &i; int *pj = &j; printf(" i = %d\n", i); printf(" j = %d\n", j); printf(" pi= %p\n", pi); printf(" pj= %p\n", pj); printf(" data pointed to by pi= %d\n", *pi); printf(" data pointed to by pj= %d\n\n", *pj); *pi = 6; dereference the pointer to access the value it points to

Result of executing the above C program on polaris [bash xmeng@polaris 18-CPointers]$ gcc -o pointers pointers.c [bash xmeng@polaris 18-CPointers]$ ./pointers i = 7 j = -1 pi= 0x7ffc3383e58c pj= 0x7ffc3383e588 data pointed to by pi= 7 data pointed to by pj= -1 [bash xmeng@polaris 18-CPointers]$ cat /proc/cpuinfo … address sizes : 39 bits physical, 48 bits virtual

Result of executing the above C program on mips [bash xmeng@mips 18-CPointers]$ gcc -o pointers pointers.c [bash xmeng@mips 18-CPointers]$ ./pointers i = 7 j = -1 pi= 0x7f977318 pj= 0x7f97731c data pointed to by pi= 7 data pointed to by pj= -1 This is a more familiar 32-bit address space.

Pointers compared to assembly dereference (*pointer) lw $t0, pointer or sw $t0, pointer e.g., lw $t0, 4($s0) # value of $s0 plus 4, an address! address of operator (&variable) la $t0, variable

Pointers allow pass by reference void addone(int i){ i = i + 1; } void addone(int *i){ *i = *i + 1; addone: addi $a0, $a0, 1 jr $ra lw $t0, 0($a0) addi $t0, $t0, 1 sw $t0, 0($a0) pass by copy pass by ref

Pass by pointer [bash xmeng@polaris 18-CPointers]$ make conversion #include <stdio.h> #include <stdlib.h> void ConvFeetInches(int totDist, int inFeet, int inInches) { inFeet = totDist / 12; inInches = totDist % 12; return; } int main(void) { int initMeasure = 45; int resFeet = 0; int resIn = 0; ConvFeetInches(initMeasure, resFeet, resIn); printf("%d feet %d inches\n", resFeet, resIn); return 0; } [bash xmeng@polaris 18-CPointers]$ make conversion gcc -Wall -g -c conversion.c gcc -o conversion conversion.o [bash xmeng@polaris 18-CPointers]$ ./conversion 0 feet 0 inches [bash xmeng@polaris 18-CPointers]$

Pass by pointer [bash xmeng@polaris 18-CPointers]$ make conversionp #include <stdio.h> #include <stdlib.h> void ConvFeetInches(int totDist, int* inFeet, int* inInches) { *inFeet = totDist / 12; *inInches = totDist % 12; return; } int main(void) { int initMeasure = 45; int resFeet = 0; int resIn = 0; ConvFeetInches(initMeasure, &resFeet, &resIn); printf("%d feet %d inches\n", resFeet, resIn); return 0; } [bash xmeng@polaris 18-CPointers]$ make conversionp gcc -Wall -g -c conversionp.c gcc -o conversionp conversionp.o [bash xmeng@polaris 18-CPointers]$ ./conversionp 3 feet 9 inches [bash xmeng@polaris 18-CPointers]$

Pointers vs arrays At the machine level, arrays are treated as pointers in C. int arr[10]; assert(arr == &arr[0]); // TRUE printf("%p\n", &arr[0]); printf("%p\n", arr); 0x7fff85d95490

Pointer Arithmetic Addr of array : 0x7fff85d95490 int array[10] = {30,31,32,33,34,35,36,37,38,39}; int *ptr; printf("Addr of array : %p\n", array); printf("ptr holds addr: %p\n", ptr); ptr = array; printf("ptr holds addr: %p\n\n", ptr); ptr = ptr + 1; printf("Addr of arr[1]: %p\n", &(array[1])); printf("ptr hold addr : %p\n", ptr); printf("ptr points to : %d\n\n", *ptr); Addr of array : 0x7fff85d95490 ptr holds addr: (nil) ptr holds addr: 0x7fff85d95490 Addr of arr[1]: 0x7fff85d95494 ptr hold addr : 0x7fff85d95494 ptr points to : 31

Pointer Arithmetic ptr = ptr + 4; printf("ptr hold addr: %p\n", ptr); printf("ptr points to: %d\n\n", *ptr); ptr--; // or ptr -= 1; double arr2[5] = {0.0, 0.5, 3.1415, 2.71, 1.41}; double *ptr2; ptr2 = arr2; printf("ptr2 holds addr: %p\n", ptr2); ptr2++; printf("ptr2 holds addr: %p\n\n", ptr2);

C array with unknown size This is often used to pass arrays into functions. Does not allocate space for the array! We don’t know input size at compile time. Must pass size as another parameter or use a sentinel value. Beginning of array is passed as a pointer. int arr[]; // valid, same as int *arr int *arr; // valid, same as above

Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; }

clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } move $t0,$zero # i = 0 loop1: sll $t1, $t0, 2 # $t1 = i * 4 add $t2, $a0, $t1 # $t2 = address of array[i] sw $zero, 0($t2) # array[i] = 0 addi $t0, $t0, 1 # i = i + 1 slt $t3, $t0, $a1 # $t3 = (i < size) bne $t3, $zero, loop1 # if (i < size) go to loop1 array access using indices requires multiply by 4 to convert index to array offset

clear2(int. array, int size) { int clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } Clear2 move $t0, $a0 # p = address of array[0] sll $t1, $a1, 2 # $t1 = size * 4 add $t2, $a0, $t1 # $t2 = address of array[size] loop2: sw $zero, 0($t0) # Memory[p] = 0 addi $t0, $t0, 4 # p = p + 4 slt $t3, $t0, $t2 # $t3 = (p < &array[size]) bne $t3, $zero, loop2 # if (p < &array[size]) go to loop2 compute end address. increment pointer by 4 to get next array element.

Clear3 clear3(int *array, int size) { while (size--){ *array++ = 0; clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear3(int *array, int size) { while (size--){ *array++ = 0; } } book real world Few C programmers would actually use a for loop like this, instead consider a while loop.

Pointers vs arrays In assembly it is often easier to operate on pointers rather than arrays. Avoids converting indexes to memory addresses. However, this doesn’t mean operating on pointers in C is more efficient than arrays. The compiler is very good at optimizing array access! Array code can be more readable and less error prone!

Summary Pointers are just addresses of variables. Pointers have types such that ptr ++ means different amount of increment, depending on the type of the pointer.