Program Building. Goals for this Lecture. Help you learn about: Program structure. Detecting and reporting failure

Similar documents
C Fundamentals! C vs. Java: Design Goals. q Java design goals! q Implications for Java! language, machine language, hardware!

CSC 2400: Computing Systems

Towards the Hardware"

Goals of this Lecture

Assembly Language Programming - III

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Lecture 2: C Programming Basic

BSM540 Basics of C Language

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

CIT Week13 Lecture

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

CSC 1600 Memory Layout for Unix Processes"

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Midterm. Median: 56, Mean: "midterm.data" using 1:2 1 / 37

Profilers and Debuggers. Introductory Material. One-Slide Summary

Assembly Language: Function Calls

C Programming Review CSC 4320/6320

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Assembly Language: Function Calls" Goals of this Lecture"

CPS104 Recitation: Assembly Programming

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls" Goals of this Lecture"

You may work with a partner on this quiz; both of you must submit your answers.

Intel assembly language using gcc

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Introduction Presentation A

Fundamental of Programming (C)

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

CPEG421/621 Tutorial

printf( Please enter another number: ); scanf( %d, &num2);

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

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

Chapter 11 Introduction to Programming in C

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

x86 assembly CS449 Fall 2017

Machine Language, Assemblers and Linkers"

Short Notes of CS201

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

BİL200 TUTORIAL-EXERCISES Objective:

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

CS201 - Introduction to Programming Glossary By

CSE 333 Lecture 2 Memory

Chapter 11 Introduction to Programming in C

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

Programming and Program Style"

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

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

CMPSC 311- Introduction to Systems Programming Module: Introduction to C

Chapter 11 Introduction to Programming in C

CS240: Programming in C

CSE 351: Week 4. Tom Bergan, TA

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Programming and Data Structure

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

CS 2505 Computer Organization I

CSE 374 Programming Concepts & Tools

Programming and Program Style"

Chapter 11 Introduction to Programming in C

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS241 Computer Organization Spring Data Alignment

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Computer Science & Engineering 150A Problem Solving Using Computers

The C language. Introductory course #1

CSE 12 Spring 2016 Week One, Lecture Two

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

ET156 Introduction to C Programming

Chapter 11 Introduction to Programming in C

Instructor: Alvin R. Lebeck

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

AMCAT Automata Coding Sample Questions And Answers

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

Final Exam. Fall Semester 2015 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

W4118: PC Hardware and x86. Junfeng Yang

Lab 10: Introduction to x86 Assembly

CS429: Computer Organization and Architecture

Transcription:

Simple C Program Building 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

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); 3 Building and Running To build (preprocess, compile, assemble, and link): $ gcc 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

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 $ gcc E circle.c > circle.i $ gcc S circle.i $ gcc c circle.s $ gcc 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. */ 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); 6

Results of Preprocessing 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); Where are the DEFINITIONS of printf() and scanf()? 7 What is Wrong? #include <stdio.h> int main() int result; result = SomeFunction(5); int SomeFunction(int x) return x * x; 8

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); 9 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); & ( address of ) operator Explained later in course, with pointers 10

What is Wrong? #include <stdio.h> int main() int sum; for(int i = 1; i <= 10; i++) sum = sum + i; int fact; for(i = 1; i <= 10; i++) fact = fact * i; 11 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); 12

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); 13 Results of Compiling File circle.s:.lc0:.lc1: main:.section.rodata.string "Enter the circle's radius:\n".string "%d".section.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() 14

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

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 your machine) Merges those definitions with circle.o to create 17 Results of Linking File circle: Listing omitted Not human-readable Machine language Complete executable binary file 18

Run-Time View At run-time, memory devoted to program is divided into sections: TEXT RODATA DATA BSS HEAP TEXT (read-only) Stores RODATA(read-only) Stores STACK(read/write) Stores STACK Other sections described later in course 19 Run-Time View: Startup At program startup: TEXT contains machine language code defining main(), printf(), scanf(), etc. STACK is empty TEXT STACK RODATA main printf scanf RODATA contains every string ti constant tused in program; each is an array of characters, terminated with the null character ( \0 ) \0) 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

Run-Time View: Declarations int radius; int diam; double circum; TEXT STACK RODATA main printf scanf Enter the circle s radius\n\0 %d\0 A circle with radius %d has diameter %d\n\0 and circumference %f.\n\0 21 Run-Time View: Writing a String printf("enter the circle's radius:\n"); 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 22

Run-Time View: Reading an int scanf("%d", &radius); TEXT STACK RODATA main printf scanf circum diam radius 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: Computing Results diam = 2 * radius; circum = 3.14159 * (double)diam; TEXT STACK RODATA main printf scanf circum diam radius 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

Run-Time View: Writing an int printf("a circle with radius %d has diameter %d\n", radius, diam); 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 25 Run-Time View: Writing a double printf("and circumference %f.\n", circum); 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 26

Run-Time View: Exiting 27 What Goes Where? int fact(n) int i, result=1; for(i = 1; i<=n; i++) result = result * i; return result; int main() int n; printf( Enter a value\n ); scanf( %d, &n); printf( The factorial of %d is %d, n, fact(n)); 28

What Goes Where? int fact(n) int i, ret=1; for(i = 1; i<=n; i++) ret = ret * i; return ret; TEXT STACK RODATA int main() int n; printf( Enter a value\n ); scanf( %d, &n); printf( The factorial of %d is %d, n,fact(n)); 29 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 30

Symbolic Constants: #define Approach 1: #define void f(void) #define START_ STATE 0 #define POSSIBLE_COMMENT_STATE 1 #define COMMENT_STATE 2 int state; state = START_STATE; state = COMMENT_STATE; 31 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 32

Version 2 using #define #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 = * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); 33 Symbolic Constants: #define Approach 1 weaknesses Preprocessor does not respect context void f(void) #define MAX 1000 int MAX = 2000; 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 34

Symbolic Constants: const Approach 2: constant variables (oxymoron!!!) void f(void) const int START_STATE = 0; const int POSSIBLE_COMMENT_STATE STATE = 1; const int COMMENT_STATE = 2; int state; t state = START_STATE; state = COMMENT_STATE; 35 Version 2 using const #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 = * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); 36

Symbolic Constants: const Approach 2 strengths Works for any type of data const double PI = 3.14159; Handled by compiler, not preprocessor; p 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 */ 37 Symbolic Constants: enum Approach 3: Enumerations void f(void) enum State START_STATE, POSSIBLE_COMMENT_STATE, COMMENT_STATE, ; enum State state; state = START_STATE; state = COMMENT_STATE; 38

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. */ 39 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]; 40

Version 2 using enum #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 = * (double)diam; printf("a circle with radius %d has diameter %d\n", radius, diam); printf("and circumference %f.\n", circum); 41 Symbolic Constants: enum Approach 3 weakness Does not work for non-integral data types enum PI = 3.14159; /* Compiletime error */ 42

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. Use #define with all uppercase at beginning of file 43 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? 44

Detecting Bad User Input Solution Part 1: Detecting bad user input scanf("%d", &radius) scanf() returns number of values successfully read Solution: 45 printf and scanf 46

Reporting Failure to User Solution Part 2: Reporting failure to the user Stream Default Binding Purpose C Functions stdin Normal input stdout Normal output stderr Abnormal output To report failure to user, should write a message to stderr 47 Reporting Failure to OS Solution Part 3: Reporting failure to the operating system Nature of Program Status Code that Program Completion Should Return to OS Successful 0 EXIT_SUCCESS (#defined in stdlib.h as 0) Unsuccessful EXIT_FAILURE (#defined in stdlib.h as???) To generate status code x, program should: Execute return x statement to return from main() function, or Call exit(x) to abort program System-dependent; Shell can examine status code on tanner, 1 Note: In main() function, return statement and exit() function have same effect In other functions, they have different effects 48

What is Wrong? #include <stdio.h> int main() int n; printf( Enter a value\n ); scanf( %d, &n); printf( The factorial of %d is %d, n,fact(n)); return EXIT_SUCCESS; Compilation error: EXIT_ SUCCESS' undeclared (first use in this function) 49 Circle Program (Version 3) #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 if successful. */ const double PI = 3.14159; int radius, diam; double circum; printf("enter the circle's radius:\n"); /* Read radius here */ 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); 50

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) 51 Reading Assignment Required: Chapter 2 from the textbook 52