Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

Similar documents
Decoding example Binary=>Decimal=>Assembly=>C? Start at program at address 4,194,304 (2 22 )

Language of the Machine Recursive functions

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins

Support for high-level languages

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

From last time. What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3

In Java we have the keyword null, which is the value of an uninitialized reference type

CSE 303: Concepts and Tools for Software Development

ECE 15B COMPUTER ORGANIZATION

September, Saeid Nooshabadi. Review: Floating Point Representation COMP Single Precision and Double Precision

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

ARM Assembly Programming

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

ARM Assembly Programming

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CSC 1600 Memory Layout for Unix Processes"

Lecture 7: Procedures and Program Execution Preview

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS A331 Programming Language Concepts

Programming Languages

C-Style Strings. CS2253 Owen Kaser, UNBSJ

CSC 2400: Computer Systems. Using the Stack for Function Calls

G Programming Languages - Fall 2012

Run-time Environment

Run Time Environment

ARM Assembly Programming II

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

System Software Assignment 1 Runtime Support for Procedures

HW1 due Monday by 9:30am Assignment online, submission details to come

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

CSE /003, Fall 2014, Homework 4 Due October 7, 2014 in Class (at 2:00pm for 002, 3:30pm for 003)

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Programs in memory. The layout of memory is roughly:

Run-time Environments - 2

Anne Bracy CS 3410 Computer Science Cornell University

ECE232: Hardware Organization and Design

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS61C : Machine Structures

CS61C Floating Point Operations & Multiply/Divide. Lecture 9. February 17, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Review 1/2 Big Idea: Instructions determine meaning of data; nothing inherent inside the data Characters: ASCII takes one byte

Class Information ANNOUCEMENTS

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Concepts Introduced in Chapter 7

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CS61C : Machine Structures

Run-Time Environments

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

CSC 2400: Computer Systems. Using the Stack for Function Calls

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture Notes on Memory Management

CS61C Pointers, Arrays, and Wrapup of Assembly Language. Lecture 11. February 24, 1999 Dave Patterson (http.cs.berkeley.

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Scope, Functions, and Storage Management

Anne Bracy CS 3410 Computer Science Cornell University

CSC 8400: Computer Systems. Using the Stack for Function Calls

ECE 471 Embedded Systems Lecture 8

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Pointers, Dynamic Data, and Reference Types

Data Transfer Instructions

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Chapter 3:: Names, Scopes, and Bindings

MIPS Functions and Instruction Formats

CS61C : Machine Structures

NET3001. Advanced Assembly

Lecture 5: Procedure Calls

o Code, executable, and process o Main memory vs. virtual memory

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Run-time Environments

Compiler Construction

Run-time Environments

Contents. 8-1 Copyright (c) N. Afshartous

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

CS240: Programming in C

Lecture Notes on Memory Management

18-600: Recitation #3

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Systems Architecture The Stack and Subroutines

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Lecture 5: Procedure Calls

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Chapter 9 :: Subroutines and Control Abstraction

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

Transcription:

COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly http://www.cse.unsw.edu.au/~cs3221 Overview Arrays, Pointers, Functions in C Example Pointers, Arithmetic, and Dereference Conclusion September, 2003 Saeid@unsw.edu.au COMP3221 lec18-pointers.1 COMP3221 lec18-pointers.2 Review: Register Convention Caller Saved Registers: Return address Arguments Return values lr a1, a2, a3, a4 a1, a2, a3, a4 Callee Saved Registers: v Registers v1 v8 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 COMP3221 lec18-pointers.3 COMP3221 lec18-pointers.4

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 Pointers Implementation in ARM c is int, has value 100, in memory at address 0x10000000, p in v1, x in v2 p = &c; /* p gets 0x10000000 */ x = *p; /* x gets 100 */ *p = 200; /* c gets 200 */ ; p = &c; /* p gets 0x10000000 */ mov v1,0x1000000 ; p = 0x10000000 ; x = *p; /* x gets 100 */ ldr v2, [v1] ; dereferencing p ; *p = 200; /* c gets 200 */ mov a1, #200 str a1, [v1] ; dereferencing p COMP3221 lec18-pointers.5 COMP3221 lec18-pointers.6 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 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 COMP3221 lec18-pointers.7 COMP3221 lec18-pointers.8

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 Version 1: Optimized Compiled Code void sumarray(int a[],int b[],int c[]) { int 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 b Loop Exit: ldmfd sp!,{v1-v2; restore v1-v2 mov pc, lr COMP3221 lec18-pointers.9 COMP3221 lec18-pointers.10 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]; return c; COMP3221 lec18-pointers.11 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 */ z[2] p = &z[0]; /* p refers to the first element of z */ z[1] p = z; /* same thing; p[ i ] == z[ i ]*/ z[0] p = p+1; /* now it points to the next element, z[1] */ y: p++; /* now it points to the one after that, z[2] */ x: *p = 4; /* assigns 4 to there, z[2] == 4*/ p = 3; /* bad idea! Absolute address!!! */ p = &x; /* p points to x, *p == 21 */ z = &y illegal!!!!! array name is not a variable COMP3221 lec18-pointers.12 p: 4 3 2 2 21

Version 2: Revised Compiled Code 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 Exit: COMP3221 lec18-pointers.13 b Loop mov a1, sp ; &c[0] add sp,sp, 400 ; pop stack ldmfd sp!,{v1-v2; restore v1-v2 mov pc, lr COMP3221 lec18-pointers.14 Weakness of Version 2 Legal Syntax; What s Wrong? Will work until call another function sp that uses stack c[100] Won t be reused a1 instantly(e.g, add a printf) Stack allocated + unrestricted pointer is problem c[100] Address high stack grows low Version 3 to Fix Weakness of Version 2 Solution: allocate c[] on heap Stack int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); return c; Not reused unless freed Can lead to memory leaks Java, has garbage collectors to reclaim free space Heap c[100] Static Code 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 b Loop Exit: sub a1, a3, #400 ; &c[0] ldmfd sp!,{v1-v2,pc; restore v1-v2 ; and return COMP3221 lec18-pointers.15 COMP3221 lec18-pointers.16

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 Version 4 : Alternative to Version 3 Static declaration int * sumarray(int a[],int b[]) { int i; static int c[100]; return c; Compiler allocates once for function, space is reused Will be changed next time sumarray invoked Used in C libraries Stack Heap Static c[100] Code COMP3221 lec18-pointers.17 COMP3221 lec18-pointers.18 What s This Stuff Good For? 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 In 1974 Vint Cerf co-wrote TCP/IP, the language that allows computers to communicate with one another. His wife of 35 years (Sigrid), hearing-impaired since childhood, began using the Internet in the early 1990s to research cochlear implants, electronic devices that work with the ear's own physiology to enable hearing. Unlike hearing aids, which amplify all sounds equally, cochlear implants allow users to clearly distinguish voices--even to converse on the phone. Thanks in part to information she gleaned from a chat room called "Beyond Hearing," Sigrid decided to go ahead with the implants in 1996. The moment she came out of the operation, she immediately called home from the doctor's office--a phone conversation that Vint still relates with tears in his eyes. One Digital Day, 1998 (www.intel.com/onedigitalday) COMP3221 lec18-pointers.19 COMP3221 lec18-pointers.20

In C : And in Conclusion.. 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 COMP3221 lec18-pointers.21