March, 2006 Saeid Nooshabadi

Slides:



Advertisements
Similar presentations
COMP3221 lec23-decode.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 23: Instruction Representation; Assembly and Decoding.
Advertisements

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
CS 61C L03 Introduction to C (1)Harvey / Wawrzynek Fall 2003 © UCB 8/29/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
COMP3221 lec04--prog-model.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Programmer’s Model of Microprocessors
CS 61C L2 Introduction to C (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 2: Introduction To C
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
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
CS61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Guide To UNIX Using Linux Third Edition
CS 61C L04 C Pointers (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS61C L2 Number Representation & Introduction to C (1) Beamer, Summer 2007 © UCB Scott Beamer Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
C++ fundamentals.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2014 © UCB Senior Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Arrays and Pointers in C Alan L. Cox
Computer Architecture ECE 4801 Berk Sunar Erkay Savas.
Introduction to Python
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Programming With C.
COMP3221 lec04--prog-model.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Programmer’s Model of Microprocessors
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
IT 251 Computer Organization and Architecture Introduction to the C Programming Language Part 1 Chia-Chi Teng.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
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:
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 TM 1 Embedded Systems Lab./Honam University ARM Microprocessor Programming Model.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
1 The user’s view  A user is a person employing the computer to do useful work  Examples of useful work include spreadsheets word processing developing.
23/07/2016CSE1303 Part B lecture notes 1 Introduction to computer systems Lecture B01 Lecture notes section B01.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Computers’ Basic Organization
Information and Computer Sciences University of Hawaii, Manoa
LINKED LISTS.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
User-Written Functions
The Machine Model Memory
CSC201: Computer Programming
IT 252 Computer Organization and Architecture
A bit of C programming Lecture 3 Uli Raich.
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Additional Assembly Programming Concepts
CS 61C: Great Ideas in Computer Architecture Introduction to C
March 2006 Saeid Nooshabadi
C Basics.
Lecture 6 C++ Programming
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Computers: Hardware and Software
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Instructor Paul Pearce
Memory Allocation CS 217.
C second, objective-c, Go up!
Lecture Topics: 11/1 General Operating System Concepts Processes
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Building Java Programs
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Focus of the Course Object-Oriented Software Development
March, 2006 Saeid Nooshabadi
Lecture 3 More on Flow Control, More on Functions,
Presentation transcript:

March, 2006 Saeid Nooshabadi saeid@unsw.edu.au ELEC2041 Microprocessors and Interfacing Lecture 2: C-Language Review - 1 http://webct.edtec.unsw.edu.au/ March, 2006 Saeid Nooshabadi saeid@unsw.edu.au

Overview C Syntax Important Tidbits in C Pointers Passing Arguments to Functions

Review: What is Subject about? Application (Netscape) ELEC2041 Operating Compiler System (Windows XP) Software Assembler Instruction Set Architecture Hardware Processor Memory I/O system Datapath & Control Digital Design Circuit Design transistors Coordination of many levels of abstraction

Review: Programming Levels of Representation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; High Level Language Program (e.g., C) ldr r0 , [r2, #0] ldr r1 , [r2, #4] str r1 , [r2, #0] str r0 , [r2, #4] Compiler Assembly Language Program (e.g. ARM) ELEC2041 Assembler 1110 0101 1001 0010 0000 0000 0000 0000 1110 0101 1001 0010 0000 0000 0000 0100 1110 0101 1000 0010 0001 0000 0000 0000 1110 0101 1000 0010 0001 0000 0000 0100 Machine Language Program (ARM) Machine Interpretation Control Signal Specification ALUOP[0:3] <= InstReg[9:11] & MASK °

Review: 5 Classic Components of a Computer Network/Bus Control Datapath Memory Processor Input Output Control Datapath Memory Processor Input Output ALU Registers That is, any computer, no matter how primitive or advance, can be divided into five parts: 1. The input devices bring the data from the outside world into the computer. 2. These data are kept in the computer’s memory until ... 3. The datapath request and process them. 4. The operation of the datapath is controlled by the computer’s controller. All the work done by the computer will NOT do us any good unless we can get the data back to the outside world. 5. Getting the data back to the outside world is the job of the output devices. The most COMMON way to connect these 5 components together is to use a network of busses.

Review: ELEC2041 DSLMU Hardware DSLMU is A Board for 21st Century The State-of-the-Art Development Board DSLMU Features: Designed by the University of Manchester with lots of collaboration from UNSW An ARM Microcontroller With 2 MB of Flash and up to 4 MB of SRAM Memory 2 Xilinx FPGAs for extended interfacing and specialised co-processors Optional Ethernet chip LCD Module Lots of uncommitted Switches and LEDs Terminal connector to FPGAs Two PCBs

Review: DSLMU Hardware Block Diagram Two Printed circuit Boards All Details and Circuit Diagrams included on the Companion CD-ROM

Quick Survey How many of you have experience with: Java? C++? C? Important: You will not learn how to code in C in this one lecture! You’ll still need some sort of C reference for this course.

But how is it architecture specific? Compilation (#1/3) 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 Haskell/Scheme, Mathematica, Matlab environments which interpret the code. But how is it architecture specific? You’ll know the answer to this by the end of next week.

Advantages of C-style compilation: Great run-time performance: generally much faster than Haskell or Java for comparable code (because it optimizes for a given architecture) OK compilation time: enhancements in compilation procedure (Makefiles) allow only modified files to be recompiled

Disadvantages of C-style compilation: All compiled files (including the executable) are architecture specific, depending on both the CPU type and the operating system. Executable must be rebuilt on each new system. Called “porting your code” to a new architecture. The “changecompilerun [repeat]” iteration cycle is slow

Bored@UNSW Boring Lectures and Boring Lecturers@UNSW!? All Lecturers you have had so far have been boring! ... I am a lecturer too. So I must be boring as well! I know many boring things. Many very, very boring things. I must be boring. I must bore. I am a lecturer. But… in another sense, to bore is to dig, to probe under the surface, to uncover that which has been hidden, to view that which has not previously been seen... From Charles Yablon: "Forms"

C Syntax #include <stdio.h> int main (void) { Replaces current line by contents of specified file; “< >” means look in system area, “ " " ” user area. #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; main is called by OS Declare before use; each sequence of variable declarations must follow a left brace. /*… */ indicate commnets. gcc accepts “//” comments but they aren’t legal C.

C Syntax: General int main (int argc, char *argv[]) Very similar to Java, but with a few minor but important differences Header files (.h) contain function declarations, just like in C++. .c files contain the actual code. main () is called by OS main can have arguments (more on this later): int main (int argc, char *argv[]) In no argument correct form is: int main (void) Comment your code: only /* */ works // doesn’t work in C gcc accepts //

C Syntax: Declarations All declarations must go at the beginning of a C block, before assigning any values. Examples of incorrect declarations: int c = 0; c = c + 1; d = 23; /* error */ char d; for (int i = 0; i < 10; i++)

C uses struct instead of classes, but they’re very similar. C Syntax: Structs C uses struct instead of classes, but they’re very similar. Sample declaration: struct alpha { int a; char b; }; To create an instance of this struct: struct alpha inst1; Read up on more struct specifics in a C reference.

What evaluates to FALSE in C? 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.

Consider memory to be a single huge array: Address vs. Value (#1/3) Consider memory to be a single huge array: Each cell of the array has an address associated with it. Each cell also stores some value. Don’t confuse the address referring to a memory location with the value stored in that location. 23 42 ... 101 102 103 104 105 ...

Address vs. variable (#2/3) 1 word = 4 Bytes = 32 bits a b c d Variables 80 84 88 92 Addresses 24 65 32 90 Variable Value Variable Address

Address vs variable (#3/3) 1 word = 4 Bytes = 32 bits variables Addresses a 80 24 84 65 b 88 32 c 92 90 d Variable Address Variable Value 104 84 ptr

Pointers in C (#1/7) 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. 23 42 ... 101 102 103 104 105 ... x y Location (address) name 104 p More specifically, a C variable can contain a pointer to something else. It actually stores the memory address that something else is stored at.

Pointers in C (#2/7) Why use pointers? So what are the drawbacks? a If we want to pass a huge struct or array, it’s easier to pass a pointer than the whole thing. In general, pointers allow cleaner, more compact code. So what are the drawbacks? Pointers are probably the single largest source of bugs in software, so be careful anytime you deal with them. Struc #1 80 24 65 b 32 c d 90 96 Struc #2 45 a b 55 11 c 88 d Struc #n

How to create a pointer: int *p, int x; Pointers in C (#3/7) How to create a pointer: & operator: get address of a variable int *p, int x; p ? x Note the “*” gets used 2 different ways in this example. In the declaration to indicate that p is going to be a pointer, and in the printf to get the value pointed to by p. x = 3; p ? x 3 p = &x; p x 3 How get a value pointed to? * “dereference operator”: get value pointed to printf(“p points to %d\n”,*p);

How to change a variable pointed to? Pointers in C (#4/7) How to change a variable pointed to? Use dereference * operator on left of = p x 3 p x 5 *p = 5;

Pointers in C (#5/7) 1 word = 4 Bytes To declare a pointer, just precede the variable name with a “*” Examples: int *a; int b; int *ptr, var, var2; Warning: In the third example above, the variable ptr is a pointer to an integer, while var and var2 are actual integer variables. The asterisk only applies to one variable. a 80 ? Pointers 84 ? b 88 ? ptr 92 ? var var2 ? 104

Pointers in C (#6/7) Mixing int and char pointers Examples: 1 word = 4 Bytes Mixing int and char pointers Examples: int *a; char *b; int *ptr, var; char *chptr, ch; a Pointers 80 ? 84 ? b 88 ? ptr 92 ? var chptr ? 100,101,102, 103 ? ch 104 int Size: 1 word = 4 Bytes = 32 bits char Size: 1 Byte = 8 bits

An int* cannot point to a character, or vice versa. Why not? Pointers in C (#7/7) Notice that a pointer can only point to one type (int, char, a struct, etc.). An int* cannot point to a character, or vice versa. Why not? Safety: Pointers are known to cause problems 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 bugs!

Arguments to Functions Arguments can be: passed by value: Make a copy of the original argument (doesn’t really affect types such as integers). int p : addOne (p) passed by reference: Pass a pointer, so the called function makes modifications to the original struct. int *p Passing by reference can be dangerous, so be careful.

Pointers and Passing Arguments (#1/2) Java and C pass an “by 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

Pointers and Passing Arguments(#2/2) 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

Arguments to Functions: Example int a =1, b=1 *alpha = &a, *beta = &b; void point_less (int a, int b) { a = 0; b = 0; } void point_full (int *a, int *b) { *a = 0; *b = 0; } point_less(a,b); /* After calling point_less, and b are unchanged */ point_full(alpha,beta); /* After calling point_full,a and b are changed */ 80 1 a 1 b 84 88 80 alpha 92 84 beta 96 1 80 84

Peer Instruction Question #Errors 1 2 3 4 5 6 7 8 9 (1)0 void main(); { int *p, x=5, y; // init y = *(p = &x) + 10; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n",x,y,p); } flip-sign(int *n){*n = -(*n)} How many errors? See following answer slide

Peer Instruction Answer #Errors 1 2 3 4 5 6 7 8 9 (1)0 void main(); { int *p, x=5, y; // init y = *(p = &x) + 10; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n",x,y,*p); } flip-sign(int *n){*n = -(*n);} How many errors? I get 7. Syntactic #1) int not void [main type] #2) main(); #3) // init #4) int z declaration late #5) flip-sign name #6) *p #7) ; missing at end Style #1) indenting #2) never use z

References: Nick Parlante: Stanford CS Education Library (http://cslibrary.stanford.edu/); A Collection of very useful material including: Essential C: (http://cslibrary.stanford.edu/101/) A relatively quick, 45 page discussion of most of the practical aspects of programming in C. Binky Pointer Video: (http://cslibrary.stanford.edu//104) Silly but memorable 3 minute animated video demonstrating the basic structure, techniques, and pitfalls of using pointers. Pointer Basics: (http://cslibrary.stanford.edu/106) The companion text for the Binky video. Pointers and Memory: (http://cslibrary.stanford.edu/102) A 31 page explanation of everything you ever wanted to know about pointers and memory. Linked List Basics: (http://cslibrary.stanford.edu/103) A 26 page introduction to the techniques and code for building linked lists in C.

All declarations go at the beginning of each function. Things to Remember All declarations go at the beginning 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