The Compilation Process

Similar documents
C and Programming Basics

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

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Computer Systems Lecture 9

C++ Tutorial AM 225. Dan Fortunato

Assembly Language: Function Calls

Brief Assembly Refresher

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

EC 413 Computer Organization

Areas for growth: I love feedback

Corrections made in this version not seen in first lecture:

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

CS429: Computer Organization and Architecture

Machine/Assembler Language Putting It All Together

Changelog. Brief Assembly Refresher. a logistics note. last time

Machine-Level Programming III: Procedures

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Lecture 3 CIS 341: COMPILERS

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

CSE 303 Lecture 8. Intro to C programming

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

University of Washington

Credits and Disclaimers

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

Procedures and the Call Stack

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

Machine Program: Procedure. Zhaoguo Wang

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

The Stack & Procedures

GCC and Assembly language. GCC and Assembly language. Consider an example (dangeous) foo.s

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Machine-level Programs Procedure

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Concurrency. Johan Montelius KTH

The Stack & Procedures

Handling of Interrupts & Exceptions

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

Assembly Programming IV

CpSc 1111 Lab 4 Formatting and Flow Control

C - Basics, Bitwise Operator. Zhaoguo Wang

CS 107 Lecture 10: Assembly Part I

Lecture 3. More About C

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Introduction Presentation A

PROGRAMMAZIONE I A.A. 2017/2018

Function Calls and Stack

Do not turn the page until 5:10.

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

Machine-Level Programming (2)

COMP s1 Lecture 1

War Industries Presents: An Introduction to Programming for Hackers Part III - Advanced Variables & Flow Control.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 6

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

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

C and C++ I. Spring 2014 Carola Wenk

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Introduction: The Unix shell and C programming

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

CSE 351 Midterm Exam Spring 2016 May 2, 2015

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

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

Machine Language CS 3330 Samira Khan

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

x86-64 Programming III & The Stack

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

Machine-Level Programming V: Unions and Memory layout

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

Functions. Arash Rafiey. September 26, 2017

CpSc 1111 Lab 5 Formatting and Flow Control

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Arrays. CSE 351 Autumn Instructor: Justin Hsia

The C language. Introductory course #1

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

Important From Last Time

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

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

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

BSM540 Basics of C Language

Key Differences Between Python and Java

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

CS153: Compilers Lecture 2: Assembly

Machine Programming 3: Procedures

Assembly II: Control Flow

Important From Last Time

EL2310 Scientific Programming

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

last time Assembly part 2 / C part 1 condition codes reminder: quiz

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

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

How Software Executes

Building an Executable

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Transcription:

Crash Course in C

Lecture 2 Moving from Python to C: The compilation process Differences between Python and C Variable declaration and strong typing The memory model: data vs. address

The Compilation Process In Python (using the interpreter): algorithm IDLE editor program.py interpreter running program machine But in C, there is an extra step: algorithm gedit program.c gcc compiler machine code (a.out)./a.out running program machine An interpreter processes one instruction at a time, a compiler translates an entire program

Machine Language A machine language can be processed directly by a computer A program is a sequence of instructions Each instruction code (opcode) is represented by a number No variable names or subroutine names in machine language! Each instruction is represented in binary Machine languages are very hard for humans to write and understand Part of itunes (trust me)

Assembly Language A higher-level language compared to machine languages, but lower-level compared to C Abstract operation codes as mnemonics Abstract memory addresses as labels (variable names) An assembler translates mnemonics and labels into machine language.section TEXT, text,regular, pure_instructions.globl_main.align4, 0x90 _main: ## @main.cfi_startproc ## BB#0: pushq %rbp Ltmp2:.cfi_def_cfa_offset 16 Ltmp3:.cfi_offset %rbp, -16 movq %rsp, %rbp Ltmp4:.cfi_def_cfa_register %rbp subq $16, %rsp leaq L_.str(%rip), %rdi movl $0, -4(%rbp) movb $0, %al callq _printf movl $0, %ecx movl %eax, -8(%rbp) ## 4-byte Spill movl %ecx, %eax addq $16, %rsp popq %rbp ret.cfi_endproc.section L_.str:.asciz.subsections_via_symbols TEXT, cstring,cstring_literals ## @.str "Hello World!\n" Assembly for Hello World! program

Language Translators Python s interpreter speaks to the machine translates and run instructions one at a time C code is compiled using a compiler (like gcc) translates all instructions before running faster performance at run time no interactive interpreter in C Programming languages are formal and lack the richness of human languages. If a program is nearly, but not quite, syntactically correct, then it will not compile The compiler will not figure it out

Python and C - Some Differences Python print arg1,... arg1 = raw_input() int, float, str, bool,... variables declared during execution and, or, not if-elif-else for i in range(n) indented blocks lists may grow/shrink C printf(format, arg1,...) scanf(format, &arg1,...) int, float, char,... variables declared at compile time &&,,! if { } else if { } else { } for (i = 0; i < n; i++) { } { blocks in curly braces } arrays are fixed size

Variable Declaration In C programs, you must declare your variables before using them int main ( ) { } int a = 5; int b = 17; printf("the sum of %d + %d is %d\n", a, b, a+b); The line int a = 5 declares that you will use an integer variable named a, with an initial value of 5

Strong Typing In C, you can t change the type of a variable Once an int, always an int It is possible to change types in Python gcc reserves space for your variables in memory. Usually 4 bytes for an int or a float Usually 8 bytes for a long long or a double Usually 1 byte for a char

Memory Model Each variable is stored in a unique location in main memory its address represented by a number, usually an integer Thus a variable can be described in three ways its type (e.g. int) its value (e.g. 17) its address (its integer memory location) Sometimes a program needs to use the memory address explicitly

Pointers The address of operator (&) allows the address of a variable to be retrieved int x = 17; // x is an integer variable printf( %d, &x); // prints the address of x Sometimes it is necessary to store one variable s address in a second variable A variable that contains the address of another variable is called a pointer Pointer variables store addresses They are declared with the type of the variable they point to followed by an *

Your First Pointer #include <stdio.h> int main ( ) { int a = 0, b = 0; } scanf("%d", &a); scanf("%d", &b); printf("the sum of %d + %d is %d\n", a, b, a+b); The input function scanf() needs to know where to put its input &a represents the address of a which is passed to scanf s pointer parameter

Acknowledgement These slides are the work of Brad Bart (with minor modifications)