CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Similar documents
CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

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

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

Number Representation & I See The C

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

CS 61C: Great Ideas in Computer Architecture Introduc=on to C, Part I. We are here!

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduc)on to C, Part I

CS 61c: Great Ideas in Computer Architecture

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

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

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

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

CS61C : Machine Structures

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

CS61C : Machine Structures

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

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

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

CS Programming In C

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

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

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

CS 61c: Great Ideas in Computer Architecture

CS 110 Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro

CS 61C: Great Ideas in Computer Architecture RISC-V Instruction Formats

9/14/17. Levels of Representation/Interpretation. Big Idea: Stored-Program Computer

CS 110 Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS 110 Computer Architecture MIPS Instruction Formats

EL2310 Scientific Programming

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

Lecture #6 Intro MIPS; Load & Store

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

CS240: Programming in C. Lecture 2: Overview

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Inf2C - Computer Systems Lecture 2 Data Representation

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Presented By : Gaurav Juneja

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

Operations, Operands, and Instructions

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables

Data III & Integers I

Lectures 5-6: Introduction to C

CS 113: Introduction to

Lectures 5-6: Introduction to C

A Fast Review of C Essentials Part I

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

Chris Riesbeck, Fall Introduction to Computer Systems

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

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

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

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

Programming. Data Structure

Programming refresher and intro to C programming

CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and RISC-V Instruction Set Architecture

CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

Lecture 3: Instruction Set Architecture

C - Basics, Bitwise Operator. Zhaoguo Wang

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Programming in C and C++

Data III & Integers I

CS61C Machine Structures. Lecture 8 - Introduction to the MIPS Processor and Assembly Language. 9/14/2007 John Wawrzynek

Topic 6: A Quick Intro To C

Data III & Integers I

Instructors: Randy H. Katz David A. PaHerson hhp://inst.eecs.berkeley.edu/~cs61c/fa10. Fall Lecture #1. Agenda

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

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

CS 110 Computer Architecture

Computer Organization & Systems Exam I Example Questions

CS 110 Computer Architecture MIPS Instruction Formats

BLM2031 Structured Programming. Zeyneb KURT

ECE 154A Introduction to. Fall 2012

PRINCIPLES OF OPERATING SYSTEMS

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CMPE-013/L. Introduction to C Programming

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

CS61C C/Assembler Operators and Operands Lecture 2 January 22, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Agenda. Old School CS61c. Agenda. New School CS61c 8/26/10. CS 61C: Great Ideas in Computer Architecture (Machine Structures)

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Motivation was to facilitate development of systems software, especially OS development.

EL2310 Scientific Programming

CS61C : Machine Structures

CS 107 Lecture 0: Welcome

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CSC231 C Tutorial Fall 2018 Introduction to C

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

CS61C : Machine Structures

Transcription:

CS 110 Computer Architecture Lecture 2: Introduction to C, Part I Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University Slides based on UC Berkley's CS61C 1

Agenda Compile vs. Interpret Administrivia Quick Start Introduction to C News/Technology Break Pointers And in Conclusion, 2

Agenda Compile vs. Interpret C vs. Java vs. Python Administrivia Quick Start Introduction to C News/Technology Break Pointers And in Conclusion, 3

Two s-complement Examples Assume for simplicity 4 bit width, -8 to +7 represented 3 0011 +2 0010 5 0101 Overflow when magnitude of result too big to fit into result representation 7 0111 +1 0001-8 1000 Overflow! 3 0011 + (-2) 1110 1 1 0001-8 1000 + (-1) 1111 +7 1 0111 Overflow! Carry in = carry from less significant bits Carry out = carry to more significant bits -3 1101 + (-2) 1110-5 1 1011 Carry into MSB = Carry Out MSB Carry into MSB = Carry Out MSB 4

Suppose we had a 5-bit word. What integers can be represented in two s complement? -32 to +31 0 to +31-16 to +15 5

Suppose we had a 5 bit word. What integers can be represented in two s complement? -32 to +31 0 to +31-16 to +15 6

ENIAC (U.Penn., 1946) First Electronic General-Purpose Computer Blazingly fast (multiply in 2.8ms!) 10 decimal digits x 10 decimal digits But needed 2-3 days to setup new program, as programmed with patch cords and switches 7

EDSAC (Cambridge, 1949) First General Stored-Program Computer Programs held as numbers in memory 35-bit binary 2 s complement words 8

But actually: first working programmable, fully automatic digital computer: Zuse Z3 (Germany 1941) 9

Components of a Computer Processor Control Enable? Read/Write Memory Input Datapath PC Address Program Bytes Registers Write Data Arithmetic & Logic Unit (ALU) Read Data Data Output Processor-Memory Interface I/O-Memory Interfaces 10

Machine Interpretation Great Idea: Levels of Representation/Interpretation High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Assembler Machine Language Program (MIPS) Hardware Architecture Description (e.g., block diagrams) Architecture Implementation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) We are here! Anything can be represented as a number, i.e., data or instructions 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 Logic Circuit Description (Circuit Schematic Diagrams) 11

Introduction to C The Universal Assembly Language 12

Intro to C C is not a very high-level language, nor a big one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages. Kernighan and Ritchie Enabled first operating system not written in assembly language: UNIX - A portable OS! 13

Intro to C Why C?: we can write programs that allow us to exploit underlying features of the architecture memory management, special instructions, parallelism C and derivatives (C++/Obj-C/C#) still one of the most popular application programming languages after >40 years! 14

TIOBE Index of Language Popularity The ratings are based on the number of skilled engineers world-wide, courses and third party vendors. http://www.tiobe.com 15

16

TIOBE Programming Community Index 17

Disclaimer You will not learn how to fully code in C in these lectures! You ll still need your C reference for this course K&R is a must-have Check online for more sources Key C concepts: Pointers, Arrays, Implications for Memory management We will use ANSI C89 original old school C 18

Compilation: Overview C compilers map C programs into architecturespecific machine code (string of 1s and 0s) Unlike Java, which converts to architectureindependent bytecode Unlike Python environments, which interpret the code These differ mainly in exactly when your program is converted to low-level machine instructions ( levels of interpretation ) For C, generally a two part process of compiling.c files to.o files, then linking the.o files into executables; Assembling is also done (but is hidden, i.e., done automatically, by default); we ll talk about that later 19

C Compilation Simplified Overview foo.c (more later in course) bar.c C source files (text) Compiler foo.o Compiler bar.o Compiler/assembler combined here Machine code object files Linker lib.o Pre-built object file libraries a.out Machine code executable file 20

Compilation: Advantages Excellent run-time performance: generally much faster than Scheme or Java for comparable code (because it optimizes for a given architecture) Reasonable compilation time: enhancements in compilation procedure (Makefiles) allow only modified files to be recompiled 21

Compilation: Disadvantages Compiled files, including the executable, are architecture-specific, depending on processor type (e.g., MIPS vs. RISC-V) and the operating system (e.g., Windows vs. Linux) Executable must be rebuilt on each new system I.e., porting your code to a new architecture Change Compile Run [repeat] iteration cycle can be slow during development but Make tool only rebuilds changed pieces, and can do compiles in parallel (linker is sequential though -> Amdahl s Law) 22

C Pre-Processor (CPP) foo.c CPP foo.i Compiler C source files first pass through macro processor, CPP, before compiler sees code CPP replaces comments with a single space CPP commands begin with # #include file.h /* Inserts file.h into output */ #include <stdio.h> /* Looks for file in standard location */ #define M_PI (3.14159) /* Define constant */ #if/#endif /* Conditional inclusion of text */ Use save-temps option to gcc to see result of preprocessing Full documentation at: http://gcc.gnu.org/onlinedocs/cpp/ 23

Typed Variables in C int variable1 = 2; float variable2 = 1.618; char variable3 = 'A'; Must declare the type of data a variable will hold Types can't change Type Description Examples int integer numbers, including negatives 0, 78, -1400 unsigned int integer numbers (no negatives) 0, 46, 900 long larger signed integer -6,000,000,000 char single text character or symbol 'a', 'D', '? float floating point decimal numbers 0.0, 1.618, -1.4 double greater precision/big FP number 10E100 24

Integers: Python vs. Java vs. C Language Python Java sizeof(int) >=32 bits (plain ints), infinite (long ints) 32 bits C Depends on computer; 16 or 32 or 64 C: int should be integer type that target processor works with most efficiently Only guarantee: sizeof(long long) sizeof(long) sizeof(int) sizeof(short) Also, short >= 16 bits, long >= 32 bits All could be 64 bits 25

Consts and Enums in C Constant is assigned a typed value once in the declaration; value can't change during entire execution of program const float golden_ratio = 1.618; const int days_in_week = 7; You can have a constant version of any of the standard C variable types Enums: a group of related integer constants. Ex: enum cardsuit {CLUBS,DIAMONDS,HEARTS,SPADES}; enum color {RED, GREEN, BLUE}; 26

Compare #define PI 3.14 and const float pi=3.14 which is true? A: Constants PI and pi have same type B: Can assign to PI but not pi C: Code runs at same speed using PI or pi 27

Agenda Compile vs. Interpret C vs. Java vs. Python Administrivia Quick Start Introduction to C News/Technology Break Pointers And in Conclusion, 28

Administrivia Find a partner for the lab and send your selection to Xu Qingwen (xuqw) due Friday, 11am! (Submissions after that loose 25% of Lab 1 score! Write an email if you cannot find a partner!) Labs start next week! Check your schedule! You cannot get checked without a partner! The tasks for Lab 1 will be posted on the website today. Prepare for it over the weekend. HW1 will be posted today. Ask questions about it on piazza. Or get help during the lab or during OH. 29

Agenda Compile vs. Interpret C vs. Java vs. Python Administrivia Quick Start Introduction to C News/Technology Break Pointers And in Conclusion, 30

Typed Functions in C int number_of_people () { } return 3; float dollars_and_cents () { } return 10.33; int sum ( int x, int y) { } return x + y; You have to declare the type of data you plan to return from a function Return type can be any C variable type, and is placed to the left of the function name You can also specify the return type as void Just think of this as saying that no value will be returned Also necessary to declare types for values passed into a function Variables and functions MUST be declared before they are used 31

Structs in C Structs are structured groups of variables, e.g., typedef struct { int length_in_seconds; int year_recorded; } Song; Song song1; Dot notation: x.y = value song1.length_in_seconds = 213; song1.year_recorded = 1994; Song song2; song2.length_in_seconds = 248; song2.year_recorded = 1988; 32

A First C Program: Hello World Original C: ANSI Standard C: main() { printf("\nhello World\n"); } #include <stdio.h> int main(void) { } printf("\nhello World\n"); return 0; 33

C Syntax: main When C program starts C executable a.out is loaded into memory by operating system (OS) OS sets up stack, then calls into C runtime library, Runtime 1 st initializes memory and other libraries, then calls your procedure named main () We ll see how to retrieve command-line arguments in main() later 34