Topics Introduction to Microprocessors

Similar documents
CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

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

Intro to C: Pointers and Arrays

CS61C : Machine Structures

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

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

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

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

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture Introduction to C

CS 61c: Great Ideas in Computer Architecture

More C Pointer Dangers

CS61C : Machine Structures

First of all, it is a variable, just like other variables you studied

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CS61C : Machine Structures

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Lecture 2: C Programm

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to C (Part I) We are here!

Arrays and Pointers in C. Alan L. Cox

Final CSE 131B Spring 2004

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

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

PRINCIPLES OF OPERATING SYSTEMS

CSE 374 Programming Concepts & Tools

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 61c: Great Ideas in Computer Architecture

A brief introduction to C programming for Java programmers

Lecture 04 Introduction to pointers

CS61C : Machine Structures

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Computer Systems Principles. C Pointers

CS3157: Advanced Programming. Outline

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Kurt Schmidt. October 30, 2018

Programming refresher and intro to C programming

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

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

Introduction to C. Systems Programming Concepts

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Lecture 03 Bits, Bytes and Data Types

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

ELEC 377 C Programming Tutorial. ELEC Operating Systems

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Language comparison. C has pointers. Java has references. C++ has pointers and references

COMP 2355 Introduction to Systems Programming

CSE 303: Concepts and Tools for Software Development

C++ for Java Programmers

Lecture 3: C Programm

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS61C : Machine Structures

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Chapter 11 Introduction to Programming in C

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Programming in C. Pointers and Arrays

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Variation of Pointers

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Chapter 11 Introduction to Programming in C

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS C Primer. Tyler Szepesi. January 16, 2013

[0569] p 0318 garbage

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

Lectures 5-6: Introduction to C

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

DECLARAING AND INITIALIZING POINTERS

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CS16 Exam #1 7/17/ Minutes 100 Points total

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Transcription:

Topics 22440 Introduction to Microprocessors C-Language Review (I) Important: : You will not learn how to code in C in this one lecture! You ll still need some sort of C reference. C Syntax Important Tidits in C Pointers Passing Arguments to Functions Saeid Nooshaadi saeid@unsw.edu.au 1 22440 Introduction to Microprocessors 2 Compilation C compilers take C and convert it into an architecture specific machine code (string of 1s and 0s). Unlike Java which converts to architecture independent code. Unlike Mathematica and Matla environments which interpret the code. Advantages of C-style C compilation: Great run-time performance: generally much faster than Java for comparale code (ecause it optimizes for a given architecture) OK compilation time: enhancements in compilation procedure (Makefiles( Makefiles) ) allow only modified files to e recompiled 22440 Introduction to Microprocessors 3 22440 Introduction to Microprocessors 4

Disadvantages of C-style C compilation: All compiled files (including the executale) are architecture specific, depending on oth the CPU type and the operating system. Executale must e reuilt on each new system. Called porting your code to a new architecture. The change compile run [repeat] iteration cycle is slow 22440 Introduction to Microprocessors 5 main is called y OS C Syntax #include <stdio.h> int main (void) { unsigned int exp = 1; int k; /* Compute 2 to the 31st.*/ for (k=0; k<31; k++) { exp = exp * 2; return 0; Replaces current line y contents of specified file; < > means look in system area, " " user area. Declare efore use; each sequence of variale declarations must follow a left race. /* */ indicate commnets. gcc accepts // comments ut they aren t legal C. 22440 Introduction to Microprocessors 6 C Syntax: General C Syntax: Declarations Very similar to Java, ut with a few minor ut important differences Header files (.h(.h)) contain function declarations, just like in C++..c files contain the actual code. main () is called y OS main can have arguments (more on this later): int main (int( argc,, char *argv* argv[]) In no argument correct form is: int main (void) Comment your code: only /* */ works // doesn t t work in C gcc accepts // 22440 Introduction to Microprocessors 7 All declarations must go at the eginning of a C lock, efore assigning any values. Examples of incorrect declarations: int c = 0; c = c + 1; d = 23; /* error */ char d; for (int( i = 0; i < ; i++) 22440 Introduction to Microprocessors 8

C Syntax: Structs C uses struct instead of classes, ut they re very similar. Sample declaration: struct alpha { int a; char ; ; To create an instance of this struct: struct alpha inst1; Read up on more struct specifics in a C reference. 22440 Introduction to Microprocessors 9 True or False What evaluates to FALSE in C 0 (integer) NULL (pointer:( more on this later) What evaluates to TRUE in C everything else No such thing as a Boolean type in C. 22440 Introduction to Microprocessors Address vs. Value Consider memory to e a single huge array: Each cell of the array has an address associated with it. Each cell also stores some value. Don t t confuse the address referring to a memory location with the value stored in that location. Addresses 1 word = 4 Bytes = 32 its 88 24 65 32 90 a c d Variales 1 2 3 4 5 23 42 Variale Address Variale Value 22440 Introduction to Microprocessors 11 22440 Introduction to Microprocessors 12

Addresses Variale Address Variale Value 1 word = 4 Bytes = 32 its 65 88 32 4 24 a 90 c d ptr variales 22440 Introduction to Microprocessors 13 Pointers in C An address refers to a particular memory location. In other words, it points to a memory location. Pointer: : High Level Language (in this case C) way of representing a memory address. Location (address) name 1 2 3 4 5 23 42 4 y p x More specifically, a C variale can contain a pointer to something else. It actually stores the memory address that something else is stored at. 22440 Introduction to Microprocessors 14 Why use pointers If we want to pass a huge struct or array, it s s easier to pass a pointer than the whole thing. In general, pointers allow cleaner, more compact code. So what are the drawacks Pointers are proaly the single largest source of ugs in software, so e careful anytime you deal with them. 96 24 a 65 32 90 c d 45 a 55 11 c 88 d Struc #1 Struc #2 How How to create a pointer: & operator: get address of a variale int *p, int x; p x x = 3; p x 3 p = &x; p x 3 How How get a value pointed to * dereference operator : : get value pointed to Note the * gets used 2 different ways in this example. In the declaration to indicate that p is going to e a pointer, and in the printf to get the value pointed to y p. Struc #n printf( p points to %d\n,*p); 22440 Introduction to Microprocessors 15 22440 Introduction to Microprocessors 16

How How to change a variale pointed to Use dereference * operator on left of = p x 3 *p = 5; p x 5 To declare a pointer, just precede the variale name with a * Examples: int *a; Pointers int ; int *ptr, var,, var2; Warning: : In the third example aove, the variale ptr is a pointer to an integer, while var and var2 are actual integer variales. The asterisk only applies to one variale. 88 ptr 4 1 word = 4 Bytes a var var2 22440 Introduction to Microprocessors 17 22440 Introduction to Microprocessors 18 Mixing int and char pointers Examples: int *a; char *; int *ptr, var; char *chptr* chptr, ch; Pointers int Size: 1 word = 4 Bytes = 32 its char Size: 1 Byte = 8 its a 88 ptr 4 1 word = 4 Bytes 0,1,2, 3 var chptr ch Notice that a pointer can only point to one type (int( int, char,, a struct,, etc.). An int* cannot point to a character, or vice versa. Why not Safety: Pointers are known to cause prolems to careless programmers, so limit what a pointer can do. void * is a type that can point to anything (generic pointer) Use sparingly to help avoid program ugs! 22440 Introduction to Microprocessors 19 22440 Introduction to Microprocessors 20

Arguments to Functions Arguments can e: passed y value: Make a copy of the original argument (doesn t t really affect types such as integers). int p : addone (p) passed y reference: Pass a pointer, so the called function makes modifications to the original struct. int *p : addone (p) Passing y reference can e dangerous, so e careful. Pointers and Passing Arguments Java Java and C pass an y value procedure/function gets a copy of the parameter, so changing the copy cannot change the original void addone (int x) { x = x + 1; int y = 3; addone(y); y is still = 3 22440 Introduction to Microprocessors 21 22440 Introduction to Microprocessors 22 How How to get a function to change a value void addone (int *p) { *p = *p + 1; int y = 3; addone(&y); y is now = 4 22440 Introduction to Microprocessors 23 Arguments to Functions: Example int a =1, =1 *alpha = &a, *eta = &; void point_less (int( a, int ) { a = 0; = 0; void point_full (int( *a, int *) { *a = 0; * = 0; 88 96 point_less(a,); /* After calling point_less, and are unchanged */ point_full(alpha,eta); /* After calling point_full,a and are changed */ a alpha eta 22440 Introduction to Microprocessors 24

Peer Instruction Question Peer Instruction Answer void main(); { int *p, x=5, y; // init y = *(p = &x) + ; int z; flip-sign(p); printf("x=%d,y=%d,p=%d =%d,y=%d,p=%d\n",x,y,p); flip-sign(int *n){*n = -(*n) How many errors #Errors 1 2 3 4 5 6 7 8 9 (1)0 22440 Introduction to Microprocessors 25 void main(); { int *p, x=5, y; // init y = *(p = &x) + ; int z; flip-sign(p); printf("x=%d,y=%d,p=%d =%d,y=%d,p=%d\n",x,y, n",x,y,*p); flip-sign(int *n){*n = -(*n); How many errors I get 7. #Errors 1 2 3 4 5 6 7 8 9 (1)0 22440 Introduction to Microprocessors 26 References: Nick Parlante: Stanford CS Education Lirary (http://cslirary.stanford.edu cslirary.stanford.edu/); A Collection of very useful material including: Essential C: (http://cslirary.stanford.edu/1/( http://cslirary.stanford.edu/1/) ) A relatively quick, 45 page discussion of most of the practical aspects of programming in C. Binky Pointer Video: : (http://cslirary.stanford.edu//4( http://cslirary.stanford.edu//4) ) Silly ut memorale 3 minute animated video demonstrating the asic structure, ure, techniques, and pitfalls of using pointers. Pointer Basics: : (http://cslirary.stanford.edu/6( http://cslirary.stanford.edu/6) ) The companion text for the Binky video. Pointers and Memory: : (http://cslirary.stanford.edu/2( http://cslirary.stanford.edu/2) ) A 31 page explanation of everything you ever wanted to know aout pointers and memory. Linked List Basics: : (http://cslirary.stanford.edu/3( http://cslirary.stanford.edu/3) ) A 26 page introduction to the techniques and code for uilding linked lists s in C. Things to Rememer All declarations go at the eginning of each function. Only 0 and NULL evaluate to FALSE. All data is in memory. Each memory location has an address to use to refer to it and a value stored in it. A pointer is a High Level Language version of the address. * follows a pointer to its value & gets the address of a value 22440 Introduction to Microprocessors 27 22440 Introduction to Microprocessors 28