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

Similar documents
PRINCIPLES OF OPERATING SYSTEMS

Laboratory 1 Semester 1 11/12

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Problem Set 1: Unix Commands 1

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Class Information ANNOUCEMENTS

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

Introduction to Supercomputing

Introduction: The Unix shell and C programming

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

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


Kurt Schmidt. October 30, 2018

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Topic 6: A Quick Intro To C

Programming in C S c o t t S c h r e m m e r

Exercise Session 6 Computer Architecture and Systems Programming

just a ((somewhat) safer) dialect.

Lectures 5-6: Introduction to C

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

CS240: Programming in C

Programs in memory. The layout of memory is roughly:

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

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

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

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

Lecture 4: Build Systems, Tar, Character Strings

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CSC111 Computer Science II

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

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

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Software Development With Emacs: The Edit-Compile-Debug Cycle

o Code, executable, and process o Main memory vs. virtual memory

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Using the Unix system. UNIX Introduction

Lecture 03 Bits, Bytes and Data Types

CSE 351. GDB Introduction

Warm-up sheet: Programming in C

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Lecture 3: C Programm

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

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

Memory Allocation in C

Intermediate Programming, Spring Misha Kazhdan

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

CS61, Fall 2012 Section 2 Notes

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Department of Computer Science and Engineering Yonghong Yan

Reliable C++ development - session 1: From C to C++ (and some C++ features)

CSE 351. Introduction & Course Tools

COMP s1 Lecture 1

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Getting started with UNIX/Linux for G51PRG and G51CSA

Lectures 5-6: Introduction to C

CSC 1600 Memory Layout for Unix Processes"

We first learn one useful option of gcc. Copy the following C source file to your

Unix and C Program Development SEEM

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

EL2310 Scientific Programming

Lecture 2: C Programm

CS3210: Tutorial Session 2. Kyuhong Park-- edited by Kyle Harrigan

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

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

Reviewing gcc, make, gdb, and Linux Editors 1

23. Check that hello.txt now contains (substitute your info for mine): hello rcwhaley: Dr. Whaley

Linux/Cygwin Practice Computer Architecture

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Lecture 07 Debugging Programs with GDB

211: Computer Architecture Summer 2016

Programming. Data Structure

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

Lab 1 Introduction to UNIX and C

Exercise 1: Basic Tools

CMPE-013/L. Introduction to C Programming

RVDS 3.0 Introductory Tutorial

RVDS 4.0 Introductory Tutorial

Programming Studio #1 ECE 190

Executables and Linking. CS449 Spring 2016

Programming refresher and intro to C programming

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

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

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Errors During Compilation and Execution Background Information

377 Student Guide to C++

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

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Lab 1 Introduction to UNIX and C

Run-time Environments - 3

Transcription:

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

Session Objectives To understand the process of compilation To be aware of provisions for data structuring provided by a C programming language To understand scope, memory organization and data representation of C data types To understand the tools used to implement the algorithms and data structures: Editor VIM Compiler GCC Debugger GDB 2

Session Objectives To understand how to create a library To understand the how to use a library 3

Compilation Process Step 1: Pre-processing All statements starting with '#' are evaluated #define #include #ifdef #undef #else Pre-processor also adds notes to the file Step 2: Lexical and Semantic analysis Here is where we get all the errors and wanrning Step 3: Intermidiate language conversion C code is converted to equivalent assembly code 4

Compilation Process Step 4: Assembly to an object file that has the equivalent achitecture specific operational codes Step 5: Static linking with libraries and bootstrap code Here is where executable is generated Demo of this process using gcc (GNU C Compiler) 5

Process Segments and Variable Scope What we already know: Type Scope Lifetime Default Value Global/Extern Full Program As long as process executes 0 Local Within Function Function Execution Time Garbage Static Within Function As long as process executes 0/initial Why Register and how exactly Within are these Function restrictions Execution imposed? Garbage Function Time 6

Segments The program, when it is loaded in to memory for execution along with data that the operating system stores in the memory is known as process. The process is divided in to segments each with a purpose:.text or Code segment Stores all the operational codes for all functions.global or Data Segment Stores global variables.bss (Bit Set Start) part of Data Segment Stores uninitialized global variables In a program(the.out file), only its size is given Initialized to zero when the memory is allocated 7

Segments.rodata (Read only Data) part of Data Segment Stores constants suct as strings etc For example: Char a[]= Hello, world! A stores address and is located in its corrosponding segment Hello, world!\0 is stored in.rodata Stack Function local variables are stored here A Frame corrosponding to memory requirements of a function's local variables (including parameters) and its return address is created on function call This Frame is pushed in to stack It is removed when the function returns after execution No initialization happens here 8

Segments Heap Dynamically allocated memory is here When you call malloc, calloc or realloc, memory is created here and the address of this memory is returned When you call free, this memory is 'freed' i.e. It is released to the operating system Stack Free Memory Stack/Heap Whatever value was there(garbage) Heap Data Code Data Initialized to Zero Code Only opcodes here 9

Primitives and Data Representation C primitives Numeric Integers char 1 byte signed int 4 byte (in gcc 32), signed (always 1 word) short 2 bytes (in gcc 32), signed long 4 bytes (in gcc 32) long long 8 bytes (in gcc 32) 10

C Primitives C primitives Numeric floating point float 4 byte signed double 8 byte (in gcc 32), signed (always 1 word) long double 12 bytes (in gcc 32), signed Pointer Size of address bus 8 bytes in 64 bit computer» Even if compiler is 32 bit 4 bytes in 32 bit computer 11

Data Representation Integer types Signed MSB is sign bit 0 means +'ve and 1 means -'ve +'ve integers are in direct binary forms -'ve integers are in 2's complement notation Unsigned All bits are used for representation of data Directly represented in binary forms Pointer types Unisgned direct binary representation Floating point numeric types Split as exponent and Mantissa Are signed Layout is based on compiler used 12

Derived Data Types Arrays The variable of an array points to starting address in memory of the array Print %p of variable to get what it represents sizeof(array variable) returns total size of array Structures The variable points to starting address of structure instance in memory sizeof(structure variable) returns word aligned size of the total structure variable in memory Unions The variable points to starting address of union instance in memory sizeof(union variable) is maximum variable size in it 13

GNU Tool Chain and Linux Bash Bourne Again Shell Linux Kernel Man Help(Manual Pages) Debian Operating System VIM Visual Editor Improved (Wheezy) GCC GNU C Compiler GLibC GNU C Library BinUtils Assembler, Linker,Archiver,etc. GDB GNU Debugger Make Automation of building 14

Shell - bash To get to bash press Windows button or click Activities Type terminal Select Terminal 15

binutils ar Similar to DLL files in Windows Creates a lib archive ar cr lib<name>.a <file1>.o <file2>.o... objdump Shows contents of object/executable files Display segments objdump -h <file> Notice.text,.data,.rodata and.bss Try nm -a <file> 16

bash Commands Create your directory mkdir <directory name> Change directory cd <directory>.. Delete directory with contents rm -rf <directory name> See contents of directory ls -l Find a file locate <filename> Update available files (as root) updatedb Open/create a file for editing vim <filename> Remember Unix/Linux uses '/' not '\' Case sensitive Root directory Not c:\ / If you want to install software Call us 17

Getting Help Man command To get help on commands man <command> To get help on library functions man 3 <function> Press q to quit Use arrow kets to navigate Press / and type a string to search first occurance Press n to go to next occurance 18

Visual Editor - Improved Checking if vim is installed vim --version Opening or Creating a new file vim <filename> Three modes Normal Mode Vi opens in this mode Press Esc key in any mode to get here Insert mode Press i (note the lower case) to get here from normal mode Command line mode Press : from normal mode to get here Visual Mode Press v in normal mode to get here In Insert mode, vi works just as any other text editor 19

gcc Checking if gcc is installed gcc --version Compiling a C file gcc <file>.c -o <file> Options -I <Folder> Specify folder having header files -L <Folder> Specify folder having library files -l<library> Compile and link with library Define a flag during compilation gcc -D <flag> <file>.c -o<file> Stop compilation after: Pre processing gcc -E <file>.c -o<file>1.c Assembly gcc -S <file>.c -o<file>.s Object file (OP Codes) gcc -c <file>.c -o<file>.o Linking (executable) gcc <file>.c -o<file> 20

glibc It is the library that provides implementation code (opcode) all standard functions like printf, scanf, malloc, etc. To check if it is installed locate libc.so.6 Type the full path of the file libc.so.6 to execute it and get the version Header files for stdio, stdlib that declare the functions are available in /usr/include/ 21

gdb When compiling, always compile with flag -ggdb gcc -ggdb <file>.c -o <file> To execute gdb./<file> You will see a Reading symbols from<path of file>...done. (gdb) This is GDB prompt where you can type commands To list contents of file (gdb) list <file>.c:<line number> For example:(gdb) list hello.c:11 22

gdb To set a break point (gdb) break <file>.c:<linenumber> For example: (gdb) break hello.c:13 To start execution (gdb) start To print a variable (gdb) print <variable name> For example: (gdb) print a To move up in stack (gdb) up To move down (gdb) down To continue execution (gdb) continue To quit (gdb) quit Press 'y' For help (gdb) help 23

Visual Editor - Improved Normal Mode Used to navigate between modes Can scroll text Can delete lines with delete Press y twice to copy a line (Yanking) Press p to paste at cursor Press d twice to delete a line Press i to edit text Press v for Visual mode Press : for commandline Press Esc to get here Command line mode Press : from normal mode to get here Press w<enter> to write to file Press wq<enter> to write and exit Press q!<enter> to exit without saving Type help to get help Visual Mode Move arrow keys to select Press <enter> to copy 24

Creating a Library Create a file add.c with: int add (int a, int b) { return a+b; } Create a file sub.c with: int subtract(int a, int b) { return a b; } Create a file mpy.c with: int multiply (int a, int b) { return a*b; } Create a file div.c with: int divide(int a, int b) { return a / b; } 25

Creating a Library Create a file calculator.h with: int add (int, int); int subtract(int, int); int multiply(int, int); Int divide (int, int); Compile as follows: gcc -c add.c -o add.o gcc -c sub.c -o sub.o gcc -c mpy.c -o mpy.o gcc -c div.c -o div.o Creating the library: ar cr libcalculator.a add.o sub.o mpy.o div.o 26

Using a Custom Library Create a file testcalculator.c with: #include <stdio.h> #include calculator.h int main (int argc, char *argv[]) { int a = 14, b= 22; printf ( %d %d\nsum: %d\n,a,b,add(a,b)); printf( Difference: %d\n,subtract(a,b)); printf( Product: %d\n,multiply(a,b)); printf( Ratio: %d\n,divide(a,b)); } 27

Using a Custom Library Move libcalculator.a and calculator.h to current directory Compile as : gcc -I./ -L./ testcalculator.c -o testcalculator -lcalculator -I tells compiler all custom header files are in current directory -L tells compiler all custom libraries are in current directory -lcalculator tells compiler to link with libcalculator.a file -o tells the compiler to create the output file as testcalculator.out and not a.out Now, we can run the executable as:./testcalculator<enter> Demo including GDB debugging of this library 28

Self Study For further understanding, answer the following questions What are memory holes? What are bit fields? What are memory leaks? How do you find and fix them? (Need to understand how malloc and free work for this) May need valgrind What is typedef? How is it used? What is #define What are #if, #ifdef, #else, #elif and #endif * Do not just gather information, experiment and understand 29

Summary Process of compiling code involves multiple steps Programming languages arrange memory as per definition of primitives and provide constructs to derive new data types Data in process is stored in different segments and the programmer must be aware of this while writing code GNU Tool chain provides all tools needed to develop a complete application. 30

Summary Process of compiling code involves multiple steps Programming languages arrange memory as per definition of primitives and provide constructs to derive new data types Data in process is stored in different segments and the programmer must be aware of this while writing code GNU Tool chain provides all tools needed to develop a complete application. 31

Summary C is written with functional programming in mind C programs are not always single file programs C is developed for modular programming such that different modules work as libraries for the main application user interface Libraries can be created and used in projects 32