ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly

Slides:



Advertisements
Similar presentations
Passing by-value vs. by-reference in ARM by value C code equivalent assembly code int a;.section since a is not assigned an a:.skip initial.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
The University of Adelaide, School of Computer Science
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
COMP3221 lec18-pointers.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
Kernighan/Ritchie: Kelley/Pohl:
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
Cs 61C L8 Proc II., Arrays and Pointers in C 1 Patterson Fall 00 ©UCB CS61C - Machine Structures Lecture 8 - Procedure Conventions part II, plus, Arrays.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Cs 61C L6 Instruction, Proc II.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Functions, Part II Lecture 6 February 5, 1999 Dave Patterson.
Intro to Computer Architecture
Run time vs. Compile time
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 15 : Functions in C/ Assembly - I
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CWRU EECS 3221 Language of the Machine EECS 322 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.
Runtime Environments Compiler Construction Chapter 7.
CWRU EECS 3141 Language of the Machine EECS 314 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Arrays and Strings in Assembly
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Lecture 3 Translation.
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Organization and Design Pointers, Arrays and Strings in C
Lecture 5: Procedure Calls
April 2006 Saeid Nooshabadi
Procedures (Functions)
Procedures (Functions)
April 2006 Saeid Nooshabadi
CDA 3101 Spring 2016 Introduction to Computer Organization
ARM Assembly Programming
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Pointers and Arrays Beyond Chapter 16
April 2006 Saeid Nooshabadi
April 2006 Saeid Nooshabadi
Topic 2b ISA Support for High-Level Languages
SPL – PS2 C++ Memory Handling.
Presentation transcript:

ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly April 2004 Modified from Notes by Saeid Nooshabadi

ELEC2041 lec18-pointers.2 Saeid Nooshabadi Overview °Arrays, Pointers, Functions in C °Example °Pointers, Arithmetic, and Dereference °Conclusion

ELEC2041 lec18-pointers.3 Saeid Nooshabadi Review: Register Convention Caller Saved Registers: Return address lr Arguments a1, a2, a3, a4 Return values a1, a2, a3, a4 Callee Saved Registers: v Registers v1 – v8

ELEC2041 lec18-pointers.4 Saeid Nooshabadi Review: Function Call Bookkeeping °Big Ideas: Follow the procedure conventions and nobody gets hurt. Data is just 1’s and 0’s, what it represents depends on what you do with it Function Call Bookkeeping: Caller Saved Registers are saved by the caller, that is, the function that includes the bl instruction Callee Saved Registers are saved by the callee, that is, the function that includes the mov pc, lr instruction Some functions are both a caller and a callee

ELEC2041 lec18-pointers.5 Saeid Nooshabadi Argument Passing Options °2 choices “Call by Value”: pass a copy of the item to the function/procedure -x … f(x) … x. Call to f does not change x “Call by Reference”: pass a pointer to the item to the function/procedure °Single word variables passed by value °What about passing an array? e.g., a[100] Pascal--call by value--copies 100 words of a[] onto the stack C--call by reference--passes a pointer (1 word) to the array a[] in a register

ELEC2041 lec18-pointers.6 Saeid Nooshabadi Pointers Implementation in ARM °c is int, has value 100, in memory at address 0x , p in v1, x in v2 p = &c; /* p gets 0x */ x = *p; /* x gets 100 */ *p = 200; /* c gets 200 */ ; p = &c; /* p gets 0x */ mov v1,0x ; p = 0x ; x = *p; /* x gets 100 */ ldr v2, [v1] ; dereferencing p ; *p = 200; /* c gets 200 */ mov a1, #200 str a1, [v1] ; dereferencing p

ELEC2041 lec18-pointers.7 Saeid Nooshabadi Simple Array: C vs. ARM Assembly int strlen(char *s) { char *p = s; /* p points to chars */ while (*p != ’\0’) p++; /* points to next char */ return p - s; /* end - start */ } sub a2, a1, #1; p = s - 1 Loop:ldrb a3,[a2,#1]! ; /*derefence p, p++*/ cmp a3, #0 bne Loop Exit:sub a1,a2,a1 ; p - s mov pc, lr

ELEC2041 lec18-pointers.8 Saeid Nooshabadi Arrays, Pointers, Functions in C °4 versions of array function that adds two arrays and puts sum in a third array ( sumarray ) Third array is passed to function Using a local array (on stack) for result and passing a pointer to it Third array is allocated on heap Third array is declared static °Purpose of example is to show interaction of C statements, pointers, and memory allocation

ELEC2041 lec18-pointers.9 Saeid Nooshabadi Calling sumarray, Version 1 int x[100], y[100], z[100]; sumarray(x, y, z); °C calling convention means above the same as sumarray(&x[0], &y[0], &z[0]); °Really passing pointers to arrays mov a1,sb ; x[0] starts at sb add a2,sb,#400 ; y[0] above x[100] add a3,sb,#800 ; z[0] above y[100] bl sumarray

ELEC2041 lec18-pointers.10 Saeid Nooshabadi Pointers, Arithmetic, and Dereference int x = 1, y = 2;/* x and y are integer variables */ int z[10];/* an array of 10 ints, z points to start */ int *p;/* p is a pointer to an int */ x = 21;/* assigns x the new value 21 */ z[0] = 2; z[1] = 3/* assigns 2 to the first, 3 to the next */ p = &z[0];/* p refers to the first element of z */ p = z; /* same thing; p[ i ] == z[ i ]*/ p = p+1;/* now it points to the next element, z[1] */ p++;/* now it points to the one after that, z[2] */ *p = 4;/* assigns 4 to there, z[2] == 4*/ p = 3;/* bad idea! Absolute address!!! */ p = &x;/* p points to x, *p == 21 */ z = &yillegal!!!!! array name is not a variable y: x: p: z[0] z[1] z[2]

ELEC2041 lec18-pointers.11 Saeid Nooshabadi Version 1: Optimized Compiled Code void sumarray(int a[],int b[],int c[]) { int i; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; } sumarray: stmfd sp!,{v1- v2};save v1–v2 on stack add a4, a1,#400 ; beyond end of a[] Loop: cmp a1, a4 beq Exit ldr v1, [a1], #4 ;a1=a[i], a1=a1+4 ldr v2, [a2], #4 ;a2=b[i], a2=a2+4 add v2, v2, v1 ;v2=a[i] + b[i] str v2, [a3], #4 ;c[i]=a[i] + b[i] ; a3 = a3+4 bLoop Exit: ldmfd sp!,{v1-v2}; restore v1-v2 mov pc, lr

ELEC2041 lec18-pointers.12 Saeid Nooshabadi Version 2 to Fix Weakness of Version 1 °Would like recursion to work int * sumarray(int a[],int b[]); /* adds 2 arrays and returns sum */ sumarray(x, sumarray(y,z)); °Cannot do this with Version 1 style solution: what about this int * sumarray(int a[],int b[]) { int i, c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; }

ELEC2041 lec18-pointers.13 Saeid Nooshabadi Version 2: Revised Compiled Code for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c;} sumarray: stmfd sp!,{v1- v2};save v1–v2 on stack add a4, a1,#400 ; beyond end of a[] sub sp, sp,#400 ; space for c mov a3, sp ; ptr for c Loop: cmp a1, a4 beq Exit ldr v1, [a1], #4 ;a1=a[i], a1=a1+4 ldr v2, [a2], #4 ;a2=b[i], a2=a2+4 add v2, v2, v1 ;v2=a[i] + b[i] str v2, [a3], #4 ;c[i]=a[i] + b[i] ; a3 = a3+4 bLoop Exit: mov a1, sp ; &c[0] add sp,sp, 400 ; pop stack ldmfd sp!,{v1-v2}; restore v1-v2 mov pc, lr

ELEC2041 lec18-pointers.14 Saeid Nooshabadi Weakness of Version 2 °Legal Syntax; What’s Wrong? °Will work until call another function that uses stack °c[100] Won’t be reused instantly(e.g, add a printf ) °Stack allocated + unrestricted pointer is problem c[100] a1 low high Address stack grows sp

ELEC2041 lec18-pointers.15 Saeid Nooshabadi Version 3 to Fix Weakness of Version 2 °Solution: allocate c[] on heap int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } CodeStatic Heap Stack c[100] °Not reused unless freed Can lead to memory leaks Java, has garbage collectors to reclaim free space

ELEC2041 lec18-pointers.16 Saeid Nooshabadi Version 3: Revised Compiled Code sumarray: stmfd sp!,{a1-a2,v1- v2,lr} ;save a1-a2, v1–v2 & lr on stack add a4, a1,#400 ; beyond end of a[] mov a1,#400 ; bl malloc; get space for c mov a3, a1 ; get &c ldmfd sp!,{a1-a2}; restor a1-a2 Loop: cmp a1, a4 beq Exit ldr v1, [a1], #4 ;a1=a[i], a1=a1+4 ldr v2, [a2], #4 ;a2=b[i], a2=a2+4 add v2, v2, v1 ;v2=a[i] + b[i] str v2, [a3], #4 ;c[i]=a[i] + b[i] ; a3 = a3+4 bLoop Exit: sub a1, a3, #400 ; &c[0] ldmfd sp!,{v1-v2,pc}; restore v1-v2 ; and return

ELEC2041 lec18-pointers.17 Saeid Nooshabadi Lifetime of storage & scope °automatic (stack allocated) typical local variables of a function created upon call, released upon return scope is the function °heap allocated created upon malloc, released upon free referenced via pointers °external / static exist for entire program

ELEC2041 lec18-pointers.18 Saeid Nooshabadi Version 4 : Alternative to Version 3 °Static declaration int * sumarray(int a[],int b[]) { int i; static int c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c; } Code Static Heap Stack c[100] °Compiler allocates once for function, space is reused Will be changed next time sumarray invoked Used in C libraries

ELEC2041 lec18-pointers.19 Saeid Nooshabadi What about Structures? °Scalars passed by value °Arrays passed by reference (pointers) °Structures by value too °Can think of C passing everything by value, just that arrays are simply a notation for pointers and the pointer is passed by value

ELEC2041 lec18-pointers.20 Saeid Nooshabadi “And in Conclusion..” °In C : Scalars passed by value Arrays passed by reference °In C functions we can return a pointer to Arrays defined in Static, Heap or stack area. °Returning a pointer to an array in stack gives rise to unrestricted pointers