C and Programming Basics

Similar documents
The Compilation Process

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

Computer Systems Lecture 9

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

Brief Assembly Refresher

Lecture 3 CIS 341: COMPILERS

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

Machine/Assembler Language Putting It All Together

Areas for growth: I love feedback

Assembly Language: Function Calls

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

CS429: Computer Organization and Architecture

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

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

C++ Tutorial AM 225. Dan Fortunato

EC 413 Computer Organization

Lecture 3. More About C

CpSc 1111 Lab 4 Formatting and Flow Control

Machine-Level Programming III: Procedures

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

Do not turn the page until 5:10.

The Stack & Procedures

The Stack & Procedures

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

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

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

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

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

Credits and Disclaimers

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

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

Introduction Presentation A

Machine Program: Procedure. Zhaoguo Wang

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

Procedures and the Call Stack

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

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

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

Assembly Programming IV

C - Basics, Bitwise Operator. Zhaoguo Wang

CSE 303 Lecture 8. Intro to C programming

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

Informatica e Sistemi in Tempo Reale

Corrections made in this version not seen in first lecture:

University of Washington

Fundamentals of Programming

Unit 8: Programming Languages CS 101, Fall 2018

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

Machine-level Programs Procedure

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

Concurrency. Johan Montelius KTH

Chapter 11 Introduction to Programming in C

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

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

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Chapter 11 Introduction to Programming in C

Course Outline Introduction to C-Programming

Machine Programming 3: Procedures

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

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CS 107 Lecture 10: Assembly Part I

Beginning C Programming for Engineers

CSE 351 Midterm Exam Spring 2016 May 2, 2015

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

Arrays. CSE 351 Autumn Instructor: Justin Hsia

CSCI 2132 Software Development. Lecture 8: Introduction to C

Chapter 11 Introduction to Programming in C

COMP s1 Lecture 1

EL2310 Scientific Programming

Variables and literals

Machine-Level Programming V: Unions and Memory layout

Handling of Interrupts & Exceptions

1 Number Representation(10 points)

CSE 351 Midterm - Winter 2017

Building an Executable

Programming refresher and intro to C programming

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Assignment 1 Clarifications

CIS 341 Midterm March 2, Name (printed): Pennkey (login id): Do not begin the exam until you are told to do so.

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

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

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Senthil Kumaran S

High Performance Computing

CS 3330 Exam 1 Fall 2017 Computing ID:

EL2310 Scientific Programming

How Software Executes

x86-64 Programming III & The Stack

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS153: Compilers Lecture 2: Assembly

Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

Machine-Level Programming (2)

C and C++ I. Spring 2014 Carola Wenk

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

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

Introduction to Compilers and Language Design Copyright (C) 2017 Douglas Thain. All rights reserved.

CS , Fall 2007 Exam 1

Transcription:

Announcements Assignment 1 Will be posted on Wednesday, Jan. 9 Due Wednesday, Jan. 16 Piazza Please sign up if you haven t already https://piazza.com/sfu.ca/spring2019/cmpt125 Lecture notes Posted just before class on the course website https://coursys.sfu.ca/2019sp-cmpt-125-d1/pages/ Final Exam Apr. 10 at 12:00-15:00 Location TBA

C and Programming Basics

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

The Compilation Process In Python: 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 Interpreter simulates one instruction at a time Compiler translates entire program in advance

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 number is represented in binary Machine languages are very hard for humans to write and understand Part of itunes (trust me)

Machine Language Instructions: Operation codes, operands Expressed as binary numbers Memory Operation code Operand?? 1001 0010 Write to memory 0000 0010 2

Machine Language Instructions: Operation codes, operands Expressed as binary numbers Memory Operation code Operand 2 1001 0010 Write to memory 0000 0010 2

Machine Language Instructions: Operation codes, operands Expressed as binary numbers Memory Operation code Operand 2 1001 0010 Write to memory 5 1001 0111 Add to memory 0000 0010 2 0000 0011 3

Machine Language Instructions: Operation codes, operands Expressed as binary numbers Memory Operation code Operand 2 1001 0010 Write to memory 5 1001 0111 Add to memory 0000 0010 2 0000 0011 3 I made these up

BIN DEC Machine Language Binary numbers 0000 00 0001 01 0010 02 0011 03 0100 04 0101 05 0110 06 0111 07 1000 08 1001 09 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15

BIN DEC Machine Language Binary numbers Carry 0 0 0 1 1 + 0 0 1 0 1 0000 00 0001 01 0010 02 0011 03 0100 04 0101 05 0110 06 0111 07 1000 08 1001 09 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15

BIN DEC Machine Language Binary numbers Carry 1 0 0 0 1 1 + 0 0 1 0 0 1 0000 00 0001 01 0010 02 0011 03 0100 04 0101 05 0110 06 0111 07 1000 08 1001 09 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15

BIN DEC Machine Language Binary numbers Carry 0 1 0 0 0 1 1 + 0 0 1 0 0 1 0 1 0000 00 0001 01 0010 02 0011 03 0100 04 0101 05 0110 06 0111 07 1000 08 1001 09 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15

BIN DEC Machine Language Binary numbers Carry 0 1 0 0 0 1 1 + 0 0 1 0 0 1 0 1 XOR (add without carry) IN 0 0 1 1 0 1 0 1 OUT 0 1 1 0 0000 00 0001 01 0010 02 0011 03 0100 04 0101 05 0110 06 0111 07 1000 08 1001 09 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15

BIN DEC Machine Language Binary numbers Carry 0 1 0 0 0 1 1 + 0 0 1 0 0 1 0 1 0000 00 0001 01 0010 02 0011 03 0100 04 0101 05 0110 06 0111 07 1000 08 1001 09 1010 10 XOR (add without carry) IN 0 0 1 1 0 1 0 1 OUT 0 1 1 0 AND (carry) IN 0 0 1 1 0 1 0 1 OUT 0 0 0 1 1011 11 1100 12 1101 13 1110 14 1111 15

BIN DEC HEX Hexadecimals Binary: each digit has two possible values: 0 or 1 Decimals: ten possible values per digit: 0-9 Hexadecimal: each digit has sixteen possible values: 0-9, A-F Convenient: one hexadecimal digit represents 4 binary digits (bits) 0000 00 0 0001 01 1 0010 02 2 0011 03 3 0100 04 4 0101 05 5 0110 06 6 0111 07 7 1000 08 8 1001 09 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F

Hexadecimals Binary: each digit has two possible values: 0 or 1 Decimals: ten possible values per digit: 0-9 Hexadecimal: each digit has sixteen possible values: 0-9, A-F Convenient: one hexadecimal digit represents 4 binary digits (bits)

Assembly Language A high-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 translate and run instructions one at a time C code is compiled using gcc translate 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

Differences Between Python and C 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 (strong vars) &&,,! if { } else if { } else { } for (i = 0; i < n; i++) { } { blocks in curly braces } arrays are fixed in 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 code int a = 5 declares that you will use an integer variable named a, which has 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 The type of variable is important because all data is represented by 0 s and 1 s

Printing Characters #include <stdio.h> int main() { char c = 0 ; printf( %c\n, c); } Output: 0

Printing Characters #include <stdio.h> int main() { char c = 0 ; printf( %c\n, c); } Output: 0 Interpret the variable c as a character

Printing Characters #include <stdio.h> int main() { char c = 0 ; printf( %d\n, c); } Interpret the variable c as a decimal integer

Printing Characters #include <stdio.h> int main() { char c = 0 ; printf( %d\n, c); } Output: 48 Interpret the variable c as a decimal integer

Memory Model Each var stored in a unique memory location its address represented by a number (an integer, usually) Thus a variable is composed of 3 things: its type its value its address Some programs need the address explicitly Addresses can be stored in variables A variable that contains the address of another variable is called a pointer

Using an Address #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 store the input integer &a represents the address of a

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