F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

Similar documents
Lecture 3. More About C

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

PRINCIPLES OF OPERATING SYSTEMS

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

F28HS2 Hardware-Software Interface. Lecture 2: Programming in C - 2

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

1 Lexical Considerations

Fundamental of Programming (C)

EL2310 Scientific Programming

Operators and Expressions:

EL2310 Scientific Programming

Review of the C Programming Language

The C language. Introductory course #1

CSE 303 Lecture 8. Intro to C programming

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Lexical Considerations

Basics of Programming

Lexical Considerations

Introduction to C An overview of the programming language C, syntax, data types and input/output

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

just a ((somewhat) safer) dialect.

Chapter 11 Introduction to Programming in C

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

Review of the C Programming Language for Principles of Operating Systems

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

CSE 374 Programming Concepts & Tools

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

A Fast Review of C Essentials Part I

Introduction to C Language

CS313D: ADVANCED PROGRAMMING LANGUAGE

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Programming and Data Structures

Programming. Data Structure

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Chapter 11 Introduction to Programming in C

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Lecture 03 Bits, Bytes and Data Types

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 11 Introduction to Programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

EC 413 Computer Organization

Full file at C How to Program, 6/e Multiple Choice Test Bank

Course Outline Introduction to C-Programming

F28HS2 Hardware-Software Interface. Lecture 3 - Programming in C 3

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

Language Reference Manual

Work relative to other classes

CSCI 2132 Software Development. Lecture 8: Introduction to C

Creating a C++ Program

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Variables and literals

Programming Language A

Basic Assignment and Arithmetic Operators

Preview from Notesale.co.uk Page 6 of 52

Computer System and programming in C

Chapter 11 Introduction to Programming in C

Your first C++ program

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Computers Programming Course 5. Iulian Năstac

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Chapter 11 Introduction to Programming in C

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

The SPL Programming Language Reference Manual

Fundamentals of Programming

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

The PCAT Programming Language Reference Manual

BLM2031 Structured Programming. Zeyneb KURT

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

CSE 124 Discussion (10/3) C/C++ Basics

Lecture 3: C Programm

Full file at

6.096 Introduction to C++ January (IAP) 2009

Chapter 11 Introduction to Programming in C

2. Numbers In, Numbers Out

UIC. C Programming Primer. Bharathidasan University

Introduction to Supercomputing

Basics of Java Programming

Algorithms, Data Structures, and Problem Solving

EECS2031 Software Tools

OBJECT ORIENTED PROGRAMMING

Practical Malware Analysis

3. Java - Language Constructs I

Model Viva Questions for Programming in C lab

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

EL2310 Scientific Programming

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Chapter 2 - Introduction to C Programming

An overview of Java, Data types and variables

Transcription:

F28HS2 Hardware-Software Interface Lecture 1: Programming in C 1

Introduction in this half of the course we will study: system level programming in C assembly language programming for the ARM processor the relationship between high level programming language constructs and low level realisations high and low level approaches to manipulating information

C Overview strict, strongly typed, imperative system programming language combines high-level constructs with low level access to type representations and memory Reference: B. Kernighan & D. Ritchie, The C Programming Language (2 nd Ed), Prentice- Hall, 1988

Overview C looks like Java BUT IS VERY DIFFERENT! Java has high-level objects C exposes low-level memory formats & addresses must manage memory explicitly very easy to make programming errors almost invariably address errors

Running C programs gcc - GNU c compiler open source generates code for just about every conceivable platform $ gcc -o name 2 name 1.c generate code for name 1.c put executable in name 2 $ name 2 run program in name 2

Running C programs $ gcc -O generate optimised code $ gcc c name 1.c name N.c generate object files name 1.o name N.o only $ gcc o name name 1.o name N.o link object files name 1.o name N.o and put executable in name

Running C programs $ gcc name.c forgot o name? puts executable in a.out! $ man gcc Linux manual entry for GNU C compiler can often use cc instead of gcc proprietary C compiler for host OS/platform

Debugging C programs $ gcc -g o name 2 name 1.c $ gdb name 2 runs GNU debugger with name 2 can now: step through program display values of variables set break points $ man gdb Linux manual entry for GNU debugger

Raspberry Pi usually runs in user mode restricts what user can do superuser has full access precede every command with: sudo or run in superuser shell: sudo su

Program layout 1. #include... 2. #define... 3. extern... 4. declarations 5. function declarations 6. main(int argc,char ** argv) 7. {... }

Program layout 1. include files #include... == look in current directory #include <...> == look in system directories import libraries via header files: name.h e.g. <stdio.h> for I/O 1. #include... 2. #define... 3. extern... 4. declarations 5. function declarations 6. main(int argc, 7. char ** argv) 8. {... }

Program layout 2. macro and constant definitions 3. names/types of variables/functions used in this file but declared in linked files 1. #include... 2. #define... 3. extern... 4. declarations 5. function declarations 6. main(int argc, 7. char ** argv) 8. {... }

Program layout 4. & 5. declare all variables before all functions 6. & 7. main function with optional command line argument count and array declarations and statements terminated with a ; 1. #include... 2. #define... 3. extern... 4. declarations 5. function declarations 6. main(int argc, 7. char ** argv) 8. {... }

Display output 1 printf( text ) system function sends text to the display \n == newline \t == tab

Display output 1 e.g. hello.c #include <stdio.h> main(int argc,char ** argv) { printf("hello\n"); }... $ gcc -o hello hello.c $ hello hello $

Memory organisation memory stack allocated automatically global declarations local declarations function parameters heap allocated by program c.f. Java new no garbage collection top of stack top of heap stack heap

Declarations basic types char character int integer short short integer long long integer float floating point number double double size floating point number

Declarations type name; allocates space for new variable of type called name on stack name letters + digits + _ starting with a letter C convention lower case = variable name UPPER CASE = symbolic constant

Declarations can group declarations for same type type name 1 ; type name 2 ;... type name N ; type name 1,name 2...name N ;

Expressions constant value of constant name value from memory NB value may differ depending on what type context name is used in unary/binary expression function call NB C function == Java method

Constants signed integer e.g. 4231-2579 signed decimal e.g. 886.754 e.g. -3.9E11 == -3.9 * 10 11 character: letter e.g. a \n

Operator expressions unary op exp evaluate exp apply op to value binary infix exp 1 op exp 2 evaluate exp 1 evaluate exp 2 apply op to values

Arithmetic unary minus: - binary infix + == add - == subtract * == multiply / == division % == integer modulo/remainder

Arithmetic (...) brackets precedence (...) before... unary before... * or / or % before... + or before... function call expressions evaluated from left to right

Arithmetic mixed mode arithmetic permitted always works at maximum precision needed for a binary operator: char & short converted to int float always converted to double either operand is double then the other is converted to double either operand is long then the other is converetd to long

Function call function called as: name(exp 1...exp N ) evaluate actual parameters exp 1 to exp N pushing values onto stack function will access formal parameters via stack result of function execution is returned

Display output 2 printf( format,exp 1...exp N ) displays the values of expressions exp 1...exp N depending on the format characters %d == decimal integer %f == floating point %x == hexadecimal %s == string

Display output 2 NB variable number of arguments must have one format for each argument any non-format information in string is displayed as text

Address operator: & declaration: type name associates name with address of enough memory for type &name address of 1 st byte allocated to variable name lvalue on PC Linux/Raspbian: address == 4 bytes

Keyboard input int scanf( format,addr 1...addr N ) inputs from keyboard to memory at specified addresses depending on format characters typically, addr i is &name i i.e. address associated with variable name i int return value for success or end of input or failure

Example: polynomial evaluation evaluate AX 2 +BX+C #include <stdio.h> main(int argc,char ** argv) { int a,b,c,x; printf("a: "); scanf("%d",&a); printf("b: "); scanf("%d",&b); printf("c: "); scanf("%d",&c); printf("x: "); scanf("%d",&x); printf("%d\n",a*x*x+b*x+c); } $ poly a: 3 b: 8 c: 4 x: 6 160

Example: polynomial evaluation evaluate AX 2 +BX+C #include <stdio.h> main(int argc,char ** argv) { int a,b,c,x; } printf("a: "); scanf("%d",&a); printf("b: "); scanf("%d",&b); printf("c: "); scanf("%d",&c); printf("x: "); scanf("%d",&x); printf("%d\n",a*x*x+b*x+c); put value at address in memory for variable $ poly a: 3 b: 8 c: 4 x: 6 160

Indirection operator: * *expression evaluate expression to integer use integer as address to get value from memory so name in expression *(&name) 1. get address associated with name 2. get value from memory at that address

Assignment expression 1 = expression 2 ; evaluate expression 1 to give an address lvalue on left of assignment evaluate expression 2 to give a value rvalue on right of assignment put the value in memory at the address

Assignment assignments are expressions returns the value of expression 2 value ignored when assignment used as a statement

Logic and logical operators no boolean values 0 false any other value true unary! - not binary && - logical AND - logical OR

Comparison operators binary == - equality!= - inequality < - less than <= - less than or equal > - greater than >= - greater than or equal

Precedence (...) before && before before! before comparison before arithmetic before function call

Block { declarations statements } declarations are optional space allocated to declarations on stack for life of block

Iteration: while while(expression) statement 1. evaluate expression 2. if non-zero then i. execute statement ii. repeat from 1. 3. if zero then end iteration break in statement ends enclosing iteration

Example: sum and average sumav.c include <stdio.h> main(int argc,char ** argv) { int count; int sum; int n; count = 0; sum = 0;

Example: sum and average } printf("next> "); scanf("%d",&n); while(n!=0) { count = count+1; } sum = sum+n; printf("next> "); scanf("%d",&n); printf("count: %d, sum: %d, average: %d\n",count,sum,sum/count);

Example: sum and average $ sumav next> 1 next> 2 next> 3 next> 4 next> 5 next> 0 count: 5, sum: 15, average: 3

Iteration: for for(exp 1 ;exp 2 ;exp 3 ) statement exp 1 ; while(exp 2 ) { statement exp 3 ; } 1. execute statement exp 1 2. repeatedly test exp 2 3. each time exp 2 is true 1. execute statement 2. execute statement exp3 all exps and statement are optional

Iteration: for for(exp 1 ;exp 2 ;exp 3 ) statement usually: exp 1 initialises loop control variable exp 2 checks if termination condition met for control variable exp 3 changes control variable NB must declare control variable before for

Condition: if if(expression) statement 1 else statement 2 1. evaluate expression 2. if non-zero then execute statement 1 3. if zero then execute statement 2 else statement 2 is optional if expression is zero then go on to next statement

Condition: switch switch(expression) { case constant 1 : statements 1 case constant 2 : statements 2... } default: statements N 1. evaluate expression to a value 2. for first constant i with same value, execute statements i 3. if no constants match expression, evaluate default statements N

Condition: switch switch(expression) { case constant 1 : statements 1 case constant 2 : statements 2... } default: statements N only matches char & int/short/long constants break; end switch NB no break at end of statements i execute statements i+1!

Example: guessing number player thinks of a number between 1 and 100 computer has to guess number each time, player tells computer if guess is: correct high low computer uses divide and conquer to halve search space each time

Example: guessing number keep track of high and low boundaries initially high is 100 and low is 1 guess number between boundaries if high then set high to guess if low then set low to guess at end, output count of guesses

Example: guessing number #include <stdio.h> main(int argc, char ** argv) { int low, high, guess, response, count; low = 1; high = 100; count = 0;

Example: guessing number while(1) { guess = (high+low)/2; count = count+1; printf("i guess %d.\n",guess); printf("am I correct (0), high (1) or low (2)? "); scanf("%d", &response); if(response==0) break;

Example: guessing number } switch(response) { case 1: high = guess; break; case 2: low = guess; break; default: printf("i don't understand %d.\n",response); count = count-1; } } printf("i took %d guesses.\n",count);

Example: guessing number [greg@mull l01]$ guess I guess 50. Am I correct (0), high (1) or low (2)? 1 I guess 25. Am I correct (0), high (1) or low (2)? 2 I guess 37. Am I correct (0), high (1) or low (2)? 9 I don't understand 9. I guess 37. Am I correct (0), high (1) or low (2)? 1 I guess 31. Am I correct (0), high (1) or low (2)? 0 I took 4 guesses.