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

Similar documents
NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation

Workspace for '3-ctips' Page 1 (row 1, column 1)

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

Introduction to the C Programming Language

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture 2: C Programming Basic

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

BSM540 Basics of C Language

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

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?

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

CC112 Structured Programming

Unit 7. Functions. Need of User Defined Functions

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Introduction to the C Programming Language

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

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

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

BSM540 Basics of C Language

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

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Lecture 9 - C Functions

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

CSE 230 Intermediate Programming in C and C++ Functions

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

JTSK Programming in C II C-Lab II. Lecture 3 & 4

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Object Oriented Design

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

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CS113: Lecture 4. Topics: Functions. Function Activation Records

Lectures 5-6: Introduction to C

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Programming and Data Structure

Functions. Systems Programming Concepts

Have examined process Creating program Have developed program Written in C Source code

Page 1. Agenda. Programming Languages. C Compilation Process

Topic 6: A Quick Intro To C

CSE 333 Lecture 7 - final C details

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Introduction to C. Systems Programming Concepts

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

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Computer Systems Lecture 9

Short Notes of CS201

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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

There are three steps involved in converting your idea of what is to be done to a working program

Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu

CSCI 171 Chapter Outlines

CS201 - Introduction to Programming Glossary By

There are three steps involved in converting your idea of what is to be done to a working program

C Programming for Engineers Functions

CSE 374 Programming Concepts & Tools

Functions in C C Programming and Software Tools

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

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

7 Functions. MAE 9 85 Maurício de Oliveira. return t ype function name ( argument d e c l a r a t i o n s ) {

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

Chapter 10 Introduction to Classes

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Lecture 5. Functions II. Functions with Arguments. CptS 121 Summer 2016 Armen Abnousi

Functions. Arash Rafiey. September 26, 2017

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Functions BCA-105. Few Facts About Functions:

This exam is to be taken by yourself with closed books, closed notes, no calculators.

C introduction: part 1

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Lecture 7. Log into Linux New documents posted to course webpage

Object Oriented Design

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Exercise Session 2 Systems Programming and Computer Architecture

Errors During Compilation and Execution Background Information

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

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Fundamentals of Programming Session 12

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Lecture 04 FUNCTIONS AND ARRAYS

Programming. Projects with Multiple Files

independent compilation and Make

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

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

Exercise Session 2 Simon Gerber

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

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

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

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Chapter 11 Introduction to Programming in C

COMP322 - Introduction to C++

Basic Types and Formatted I/O

Transcription:

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

TOPICS TODAY Assembling & Linking Assembly Language Separate Compilation in C Scope and Lifetime

LINKING IN ASSEMBLY

FUNCTIONS & SEPARATE COMPILATION IN C

C Parameter Passing Notes We'll say formal parameter vs actual parameter. Formal parameters are place holders in function definition. Actual parameters (aka arguments) actually have a value. In C, all parameters are passed by value. Parameter passing by reference is simulated by passing the address of the variable. scanf("%d", &n) ;! Array names represent the address of the array. In effect, arrays are passed by reference. int UpdateArray (int A[], int n) {! A[0] += 5 ;!...!

#include <stdio.h> typedef double Radius; #define PI 3.1415 A Simple C Program double circlearea( Radius radius ) { return PI * radius * radius ; } double calccircumference( Radius radius ) { return 2 * PI * radius ; } int main() { Radius radius = 4.5; double area = circlearea( radius ); double circumference = calccircumference( radius ); printf ( Area = %10.2f, Circumference = %10.2f\n, area, circumference); } return 0;

Separate Compilation: Why? Keeps files small. Different people can work on different parts of the program. Easier to find functions. Keeps large program logically organized. Do not have to re-compile entire program when changes are made to a small portion. Parts of the program (e.g., code for a data structure) may be reusable in other programs. Problem: need a mechanism for external references.

circleutils.h /* circleutils.h*/ /* #includes required by the prototypes, if any */ /* typedefs and #defines */ typedef double Radius; /* function prototypes */ double circlearea( Radius radius ); double calccircumference( Radius radius );

circleutils.c /* circleutils.c */ #include "circleutils.h" #define PI 3.1415 /* Function implementations */ double circlearea( Radius radius ) { } return ( PI * radius * radius ); double calccircumference( Radius radius ) { } return (2 * PI * radius );

main program /* sample.c */ #include <stdio.h> #include "circleutils.h" int main( ) { Radius radius = 4.5; double area, circumference ; area = circlearea( radius ); circumference = calccircumference( radius ); printf ( Area = %lf, Circumference = %lf\n, area, circumference); } return 0;

Header files should contain function prototypes type definitions #define constants Header Files extern declarations for global variables other #includes Header files should end with.h System header files #included with < > #include <stdio.h>! Your own header files #included with " " #include "circleutils.h"! Header files are expected to include all other header files needed to work with implemented functions.

Guarding Header Files Header files should not be included multiple times. multiple declaration of function prototypes: OK multiple type definition: BAD multiple #include can lead to loops where a.h file includes itself. Solution: #ifndef _UNIQUE_VAR_NAME_ #define _UNIQUE_VAR_NAME_... #endif

#ifndef CIRCLEUTIL_H #define CIRCLEUTIL_H /* circleutils.h*/ Guarded circleutils.h /* #includes required by the prototypes, if any */ /* typedefs and #defines */ typedef double Radius; /* function prototypes */ double circlearea( Radius radius ); double calccircumference( Radius radius ); #endif

Compiling and linking How to compile: gcc -c -Wall circleutils.c! gcc -c -Wall sample.c! gcc -Wall -o sample sample.o circleutils.o! Or!gcc -Wall -o sample sample.c circleutils.c!

Compiler vs linker Compiler: translates one.c file into a.o file Verifies that all functions are being called correctly Verifies that all variables exist Verifies language syntax Linker: combines.o files and C libraries into executable file Finds functions called by one.c/.o file, but defined in another E.g. printf( ), scanf( ). Finds global variables used by one.c/.o file, but defined in another (more on this soon) gcc uses ld to link & load! Easier to invoke ld through gcc!

Linking with C libraries By default, the standard C library which includes printf, scanf and char and string functions is always linked with your program. Other libraries must be explicitly linked with your code. Typical C libraries have the form libxxx.a. Standard C library: libc.a. Math library:libm.a. Use the -l flag and the xxx part of the library name to link.!gcc -Wall -o sample sample.c circleutils.c -lm!

Project Organization main( ) is generally defined in its own.c file and generally just calls helper functions E.g. project1.c Project-specific helper functions may be in the same.c file as main( ) main( ) comes first Helper function order that makes sense to you Reusable functions in their own.c file Group related functions in the same file E.g. circleutils.c Prototypes, typedefs, #defines, etc. for reusable function in a.h file Same file root name as the.c file. E.g. circleutils.h

SCOPE & LIFETIME

Variable Scope and Lifetime The scope of a variable refers to that part of a program that may refer to the variable. The lifetime of a variable refers to the time in which a variable occupies a place in memory. The scope and lifetime of a variable are determined by how and where the variable is defined.

static and extern In C/C++, the keyword static is overloaded. A static local variable has lifetime = duration of program. A static global variable has file scope A static function has file scope extern is means that the variable is defined in another file. extern int other_variable ;!! an extern declaration is an example of a declaration that is not a definition. (Another example is a function prototype.)

NEXT TIME Pointers