Simple C Programs. Goals for this Lecture. Help you learn about:



Similar documents
Lecture 27 C and Assembly

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Assembly Language: Function Calls" Jennifer Rexford!

Stack Allocation. Run-Time Data Structures. Static Structures

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

The C Programming Language course syllabus associate level

For a 64-bit system. I - Presentation Of The Shellcode

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Compiler Construction

Informatica e Sistemi in Tempo Reale

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Fundamentals of Programming

System Calls and Standard I/O

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

5 Arrays and Pointers

The programming language C. sws1 1

CP Lab 2: Writing programs for simple arithmetic problems

Compiler and Language Processing Tools

COS 217: Introduction to Programming Systems

C Programming Language

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Sources: On the Web: Slides will be available on:

X86-64 Architecture Guide

Instruction Set Architecture

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

64-Bit NASM Notes. Invoking 64-Bit NASM

Chapter 13 - The Preprocessor

Lecture 25 Systems Programming Process Control

arrays C Programming Language - Arrays

File Handling. What is a file?

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Chapter One Introduction to Programming

Programming the PIC18/ XC8 Using C- Coding Dr. Farahmand

CS3600 SYSTEMS AND NETWORKS

C++ INTERVIEW QUESTIONS

Operating Systems and Networks

7.1 Our Current Model

CS61: Systems Programing and Machine Organization

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

OPERATING SYSTEM SERVICES

C PROGRAMMING FOR MATHEMATICAL COMPUTING

C Interview Questions

CSC230 Getting Starting in C. Tyler Bletsch

CS420: Operating Systems OS Services & System Calls

Pemrograman Dasar. Basic Elements Of Java

Programming Languages CIS 443

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

C++ Programming Language

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

umps software development

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Some Scanner Class Methods

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Programming Languages

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.

Introduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code

Programming Embedded Systems

Semester Review. CSC 301, Fall 2015

Illustration 1: Diagram of program function and data flow

General Software Development Standards and Guidelines Version 3.5

10CS35: Data Structures Using C

Lecture 22: C Programming 4 Embedded Systems

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Chapter 2: Elements of Java

Lecture 5: Java Fundamentals III

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

SQLITE C/C++ TUTORIAL

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Example of Standard API

Comp151. Definitions & Declarations

Compilers I - Chapter 4: Generating Better Code

The Function Pointer

Professional. SlickEdif. John Hurst IC..T...L. i 1 8 О 7» \ WILEY \ Wiley Publishing, Inc.

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Software Vulnerabilities

C Compiler Targeting the Java Virtual Machine

CSCI 3136 Principles of Programming Languages

Introduction to Programming II Winter, 2014 Assignment 2

CPSC 226 Lab Nine Fall 2015

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

1 Abstract Data Types Information Hiding

Semantic Analysis: Types and Type Checking

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Binary storage of graphs and related data

csce4313 Programming Languages Scanner (pass/fail)

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Villanova University CSC 2400: Computer Systems I

Chapter 2 Elementary Programming

1 The Java Virtual Machine

Transcription:

Simple C Programs 1 Goals for this Lecture Help you learn about: Simple C programs Program structure Defining symbolic constants Detecting and reporting failure Functionality of the gcc command Preprocessor, compiler, assembler, linker Memory layout of a Linux process Text section, rodata section, stack section 2 1

Circle Program File circle.c: #include <stdio.h> int main(void) /* Read a circle's radius from stdin, and compute and write its diameter and circumference to stdout. Return 0. */ int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = 3.14159 * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; 3 Building and Running To build (preprocess, compile, assemble, and link): $ gcc217 circle.c o circle To run: $ circle Enter the circle's radius: 5 A circle with radius 5 has diameter 10 and circumference 31.415900. Typed by user 4 2

Steps in the Build Process To build one step at a time: Preprocess: circle.c circle.i Compile: circle.i circle.s Assemble: circle.s circle.o $ gcc217 E circle.c > circle.i $ gcc217 S circle.i $ gcc217 c circle.s $ gcc217 circle.o o circle Link: circle.o circle Why build one step at a time? Helpful for learning how to interpret error messages Permits partial builds (described later in course) 5 The Preprocessor s View File circle.c: #include <stdio.h> int main(void) /* Read a circle's radius from stdin, and compute and write its diameter and circumference to stdout. Return 0. */ Preprocessor directive Preprocessor replaces with contents of file /usr/include/stdio.h int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = 3.14159 * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; Comment Preprocessor removes 6 3

Results of Preprocessing File circle.i: int printf(char*, ); int scanf(char*, ); Declarations of printf(), scanf(), and other functions; compiler will have enough information to check subsequent function calls int main(void) int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = 3.14159 * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; Note: Definitions of printf() and scanf() are not present 7 The Compiler s View File circle.i: int printf(char*, ); int scanf(char*, ); int main(void) int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = 3.14159 * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; Function declarations Compiler notes return types and parameter types so it can check your function calls Function definition Compound statement alias block Return type of main() should be int 8 4

The Compiler s View (cont.) File circle.i: int printf(char*, ); int scanf(char*, ); int main(void) int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = 3.14159 * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; String constants Declaration statements Must appear before any other kind of statement in block; variables must be declared before use Function call statements & ( address of ) operator Explained later in course, with pointers 9 The Compiler s View (cont.) File circle.i: int printf(char*, ); int scanf(char*, ); Constant of type int int main(void) int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = 3.14159 * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; Constant of type double Expression statements Cast operator Unnecessary here, but good style to avoid mixed-type expressions 10 5

The Compiler s View (cont.) File circle.i: int printf(char*, ); int scanf(char*, ); Function call statements printf() can be called with 1 or more actual parameters int main(void) int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = 3.14159 * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; Return statement Convention: 0 returned from main() means success; non-0 means failure 11 Results of Compiling File circle.s:.lc0:.lc1: main:.section.rodata.string "Enter the circle's radius:\n".string "%d".text.globl main.type main, @function pushl movl pushl call addl subl leal pushl pushl call %ebp %esp, %ebp $.LC0 printf $16, %esp $8, %esp -4(%ebp), %eax %eax $.LC1 scanf Assembly language Still missing definitions of printf() and scanf() 12 6

The Assembler s View File circle.s:.lc0:.lc1: main:.section.rodata.string "Enter the circle's radius:\n".string "%d".text.globl main.type main, @function pushl movl pushl call addl subl leal pushl pushl call %ebp %esp, %ebp $.LC0 printf $16, %esp $8, %esp -4(%ebp), %eax %eax $.LC1 scanf Assembly language Assembler translates assembly language into machine language Details provided in 2nd half of course 13 Results of Assembling File circle.o: Listing omitted Not human-readable Machine language Object file Still missing definitions of printf() and scanf() 14 7

The Linker s View File circle.o: Listing omitted Not human-readable Machine language The linker: Observes that Code in circle.o calls printf() and scanf() Code in circle.o does not define printf() or scanf() Fetches machine language definitions of printf() and scanf() from standard C library (/usr/lib/libc.a on hats) Merges those definitions with circle.o to create 15 Results of Linking File circle: Listing omitted Not human-readable Machine language Complete executable binary file 16 8

Run-Time View At run-time, memory devoted to program is divided into sections: TEXT RODATA DATA BSS HEAP STACK TEXT (read-only) Stores executable machine language instructions RODATA(read-only) Stores read-only data, esp. string constants STACK(read/write) Stores values of local variables Other sections described later in course 17 Run-Time View: Startup At program startup: TEXT contains machine language code defining main(), printf(), scanf(), etc. TEXT STACK RODATA main printf scanf STACK is empty Enter the circle s radius\n\0 %d\0 RODATA contains every string constant used in program; each is an array of characters, terminated with the null character ( \0 ) A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 18 9

Run-Time View: Declarations int radius; int diam; double circum; Computer pushes memory onto STACK for each local variable: 4 bytes for int, 8 bytes for double TEXT STACK RODATA main printf scanf circum diam radius Enter the circle s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 19 Run-Time View: Writing a String printf("enter the circle's radius:\n"); Computer passes address containing E to printf(); printf() prints characters until it encounters \0 TEXT STACK RODATA main printf scanf circum diam radius Enter the circle s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 20 10

Run-Time View: Reading an int scanf("%d", &radius); Computer passes address containing % to scanf(); scanf() waits for user input; user types 5; scanf() reads character(s), converts to decimal (d) constant, assigns to radius TEXT STACK RODATA Enter the circle s radius\n\0 %d\0 main printf scanf circum diam radius 5 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 21 Run-Time View: Computing Results diam = 2 * radius; circum = 3.14159 * (double)diam; Computer uses radius to compute diam and circum TEXT STACK RODATA Enter the circle s radius\n\0 %d\0 main printf scanf circum diam radius 31.4159 10 5 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 22 11

Run-Time View: Writing an int printf("a circle with radius %d has diameter %d\n", radius, diam); Computer passes address of A, value of radius, and value of diam to printf(). printf() prints until \0, replacing 1 st %d with character comprising 5, 2 nd %d with characters comprising 10 TEXT STACK RODATA main printf scanf circum diam radius 31.4159 10 5 Enter the circle s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 23 Run-Time View: Writing a double printf("and circumference %f.\n", circum); TEXT STACK RODATA Computer passes address of a and value of circum to printf(). printf() prints until \0, replacing %f with characters comprising 31.415900 main printf scanf circum diam radius 31.4159 10 5 Enter the circle s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 24 12

Run-Time View: Exiting return 0; Computer reclaims memory used by program; sections cease to exist 25 Toward Version 2 Problem (stylistic flaw): 3.14159 is a magic number Should give it a symbolic name to Increase code clarity Thereby increase code maintainability Solution: (In Java: final fields, final variables) In C: three approaches 26 13

Symbolic Constants: #define Approach 1: #define Preprocessor directive: replace START_STATE with 0 void f(void) #define START_STATE 0 #define POSSIBLE_COMMENT_STATE 1 #define COMMENT_STATE 2 int state; state = START_STATE; state = COMMENT_STATE; Preprocessor replaces with 0 Preprocessor replaces with 2 27 Symbolic Constants: #define Approach 1 strengths Preprocessor does substitutions only for tokens int mystart_state; /* No replacement */ Preprocessor does not do substitutions within string constants printf("what is the START_STATE?\n"); /* No replacement */ Simple textual replacement; works for any type of data #define PI 3.14159 28 14

Symbolic Constants: #define Approach 1 weaknesses Preprocessor does not respect context void f(void) #define MAX 1000 int MAX = 2000; Preprocessor replaces with 1000!!! Preprocessor does not respect scope void f(void) #define MAX 1000 void g(void) int MAX = 2000; Conventions: Use all uppercase for constants -- and only for constants Place #defines at beginning of file, not within function definitions 29 Symbolic Constants: const Approach 2: constant variables (oxymoron!!!) void f(void) const int START_STATE = 0; const int POSSIBLE_COMMENT_STATE = 1; const int COMMENT_STATE = 2; int state; state = START_STATE; state = COMMENT_STATE; Compiler does not allow value of START_STATE to change 30 15

Symbolic Constants: const Approach 2 strengths Works for any type of data const double PI = 3.14159; Handled by compiler, not preprocessor; compiler respects context and scope Approach 2 weaknesses Does not work for array lengths (unlike C++) const int ARRAY_LENGTH = 10; int a[array_length]; /* Compiletime error */ 31 Symbolic Constants: enum Approach 3: Enumerations void f(void) enum State START_STATE, POSSIBLE_COMMENT_STATE, COMMENT_STATE, ; Defines a new type named enum State enum State state; state = START_STATE; state = COMMENT_STATE; Defines a variable named state to be of type enum State The constants of type enum State are START_STATE, 32 16

Symbolic Constants: enum Approach 3 note Enumerated constants are interchangeable with ints START_STATE is the same as 0 POSSIBLE_COMMENT_STATE is the same as 1 Etc. state = 0; /* Can assign int to enum. */ i = START_STATE; /* Can assign enum to int. */ 33 Symbolic Constants: enum Approach 3 strengths Can explicitly specify values for names enum State START_STATE = 5, POSSIBLE_COMMENT_STATE = 3, COMMENT_STATE = 4, ; Can omit type name, thus effectively giving names to int literals enum MAX_VALUE = 9999; int i = MAX_VALUE; Works when specifying array lengths enum ARRAY_LENGTH = 10; int a[array_length]; 34 17

Symbolic Constants: enum Approach 3 weakness Does not work for non-integral data types enum PI = 3.14159; /* Compiletime error */ 35 Symbolic Constant Style Rules In summary of symbolic constants Style rules: 1. Use enumerations to give symbolic names to integral constants 2. Use const variables to give symbolic names to non-integral constants 3. Avoid #define altogether 36 18

Circle Program (Version 2) File circle.c (version 2): #include <stdio.h> int main(void) /* Read a circle's radius from stdin, and compute and write its diameter and circumference to stdout. Return 0. */ const double PI = 3.14159; int radius; int diam; double circum; printf("enter the circle's radius:\n"); scanf("%d", &radius); diam = 2 * radius; circum = PI * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; 37 Toward Version 3 Problem: Program does not handle bad user input $ circle Enter the circle's radius: abc User enters a non-number. How can the program detect that? What should the program do? 38 19

Detecting Bad User Input Solution Part 1: Detecting bad user input scanf() returns number of values successfully read Example: int returnvalue; returnvalue = scanf("%d", &i); if (returnvalue!= 1) /* Error */ Or, more succinctly: if (scanf("%d", &i)!= 1) /* Error */ Or, for more than one variable: if (scanf("%d%d", &i, &j)!= 2) /* Error */ 39 Reporting Failure to User Solution Part 2: Reporting failure to the user Stream stdin Default Binding Keyboard Purpose Normal input C Functions scanf(); fscanf(stdin, ); stdout stderr Video screen Video screen Normal output Abnormal output printf(); fprintf(stdout, ); fprintf(stderr, ); To report failure to user, should write a message to stderr 40 20

Reporting Failure to OS Solution Part 3: Reporting failure to the operating system Nature of Program Completion Successful Unsuccessful To generate status code x, program should: Execute return x statement to return from main() function, or Call exit(x) to abort program Shell can examine status code Status Code that Program Should Return to OS 0 EXIT_SUCCESS (#defined in stdlib.h as 0) EXIT_FAILURE (#defined in stdlib.h as???) System-dependent; on hats, 1 Note: In main() function, return statement and exit() function have same effect In other functions, they have different effects 41 Circle Program (Version 3) File circle.c (version 3): #include <stdio.h> #include <stdlib.h> int main(void) /* Read a circle's radius from stdin, and compute and write its diameter and circumference to stdout. Return 0 if successful. */ const double PI = 3.14159; int radius; int diam; double circum; printf("enter the circle's radius:\n"); if (scanf("%d", &radius)!= 1) fprintf(stderr, "Error: Not a number\n"); exit(exit_failure); /* or: return EXIT_FAILURE; */ diam = 2 * radius; circum = PI * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); return 0; 42 21

Summary Simple C programs Program structure Defining symbolic constants #define, constant variables, enumerations Detecting and reporting failure The stderr stream The exit() function Functionality of the gcc command Preprocessor, compiler, assembler, linker Memory layout of a Linux process TEXT, RODATA, STACK sections (More sections DATA, BSS, HEAP later in the course) 43 22