CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 9: Low-Level Programming

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
CS ExCo Advanced in Topics Object-Oriented Programming.
C Programming for engineers Teaching assistant: Ben Sandbank Home page:
Lab#1 (14/3/1431h) Introduction To java programming cs425
Cs2220: Engineering Software Class 2: Introduction Java Fall 2010 University of Virginia David Evans.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
6/27/2015G. Levine1 PROGRAMMING LANGUAGES Text: Programming Languages, Design and Implementation Pratt and Zelkowitz 4 th ed. Prentice-Hall.
Lecture 1CS 380C 1 380C Last Time –Course organization –Read Backus et al. Announcements –Hadi lab Q&A Wed 1-2 in Painter 5.38N –UT Texas Learning Center:
Language Issues Misunderstimated? Sublimable? Hopefuller? "I know how hard it is for you to put food on your family.” "I know the human being and fish.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 22: Low-Level Programming in.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 10: *&!%[]++
Class 24: Programming with Objects University of Virginia cs1120 David Evans.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
CSC 110 A 1 CSC 110 Introduction to Python [Reading: chapter 1]
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANCI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Introduction COMP104: Fundamentals and Methodology.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
High-Level Programming Languages: C++
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages –14 David Watt (Glasgow) Steven Wong (Singapore) Moodle : Computing Science → Level 3 → Programming Languages 3 © 2012 David.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Introduction to Computer Programming in c
Chapter 1 Object Orientation: Objects and Classes.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 23: Review.
CS 671 Compilers Prof. Kim Hazelwood Spring 2008.
Computer Programming 2 Why do we study Java….. Java is Simple It has none of the following: operator overloading, header files, pre- processor, pointer.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
CS 655: Programming Languages David Evans Office 236A, University of Virginia Computer.
Programming Languages
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
David Evans Class 16: NP-Completeness / The Story so Far CS150: Computer Science University of Virginia Computer Science.
CPS120: Introduction to Computer Science Variables and Constants.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Cs205: engineering software university of virginia fall 2006 David Evans Object-Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
小型系統 心情 vs. 古典樂 心情 vs. 古典樂 浪漫求籤系統 美食導航系統 季潔亭雅鈺熒岱芸 美食導航系統 楊氏音樂模擬大會考人瑋 若維 芷萱 伽倩 楊氏音樂模擬大會考 麥當勞熱量計算系統 火星文困擾你嗎 ? 火星文困擾你嗎 ? 歌詞知多少 - 挑戰你的腦容量英琪 日馨 青雪 鈺娟.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Software Engineering Algorithms, Compilers, & Lifecycle.
Lecture 1b- Introduction
Interpreted languages Jakub Yaghob
Learning to Program D is for Digital.
CS216: Program and Data Representation
Welcome to CSE1002.
Lecture 15: Using Low-Level Languages CS201j: Engineering Software
Objective of This Course
Programming Languages
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
History and Background
Lecture 18: Deep C Garbage Collection CS201j: Engineering Software
Comp Org & Assembly Lang
Presentation transcript:

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 9: Low-Level Programming

2 UVa CS216 Spring Lecture 9: Low-Level Programming Menu Complexity Question Low-Level Programming Exam 1 Out Wednesday, Due Monday, 11:01AM Covers everything through Lecture 8 Expect questions on: order notation, algorithm analysis, lists, trees, recursive programming, dynamic programming, greedy algorithms Not on complexity classes (Lecture 8)

3 UVa CS216 Spring Lecture 9: Low-Level Programming Problem Classes if P  NP: P Interval Scheduling:  (n log n) Sequence Alignment: O(n 2 ) Subset Sum O(n) How many problems are in the  (n) class? How many problems are in P but not in the  (n) class? How many problems are in NP but not in P? infinite NP 3SAT NP-Complete Note the NP- Complete class is a ring – others are circles

4 UVa CS216 Spring Lecture 9: Low-Level Programming Is it ever useful to be confident that a problem is hard?

5 UVa CS216 Spring Lecture 9: Low-Level Programming Knapsack Cipher [Merkle & Hellman, 1978] Public Key: A = {a 1, a 2,…,a n } –Set of integers Plain Text: x 1,…x n x i = 0 or 1 Cipher Text:

6 UVa CS216 Spring Lecture 9: Low-Level Programming Subset Sum is Hard Given s and A it is NP-Complete to find a subset of A that sums to s Need to make decrypting each (for recipient with the “private key”)

7 UVa CS216 Spring Lecture 9: Low-Level Programming Superincreasing Set Pick {a 1, a 2,…,a n } is a superincreasing sequence How hard is subset sum if A is superincreasing?

8 UVa CS216 Spring Lecture 9: Low-Level Programming Knapsack Ciphers Private Key = {p 1, p 2,…,p n } –A superincreasing sequence –Values M and W : Public Key = {a 1, a 2,…, a n }

9 UVa CS216 Spring Lecture 9: Low-Level Programming Flawed Security Argument Subset Sum is NP-Complete Breaking knapsack cipher involves solving a subset sum problem Therefore, knapsack cipher is secure Flaw: NP-Complete means there is no fast general solution. Some instances may be solved quickly. (Note: Adi Shamir found a way of breaking knapsack cipher [1982])

10 UVa CS216 Spring Lecture 9: Low-Level Programming Levels of Abstraction: Program Real World Problem High-Level Program Machine Instructions Physical Processor Physical World Virtual World From Lecture 3

11 UVa CS216 Spring Lecture 9: Low-Level Programming Crossing-Levels Python Program x86 Instructions Python Interprete r C Program x86 Instructions C compiler

12 UVa CS216 Spring Lecture 9: Low-Level Programming Programming Languages

13 UVa CS216 Spring Lecture 9: Low-Level Programming Programming Languages Phylogeny (Simplified) Fortran (1954), IBM (Backus) Algol (1958) Scheme (1975) CPL (1963), U Cambridge Combined Programming Language BCPL (1967), MIT Basic Combined Programming Language B (1969), Bell Labs C (1970), Bell Labs C++ (1983), Bell Labs Java (1995) Objective C Simula (1967) Python (1990), Guido van Rossum ABC (~1980) BASIC (1963) Smalltalk (1971), PARC LISP (1957)

14 UVa CS216 Spring Lecture 9: Low-Level Programming Why so many Programming Languages?

15 UVa CS216 Spring Lecture 9: Low-Level Programming “Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. Printed by Ottaviano Dei Petrucci in 1501 (first music with movable type)

16 UVa CS216 Spring Lecture 9: Low-Level Programming J S Bach, “Coffee Cantata”, BWV 211 (1732) “Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. (1501)

17 UVa CS216 Spring Lecture 9: Low-Level Programming Modern Music Notation Roman Haubenstock- Ramati, Concerto a Tre John Cage, Fontana Mix

18 UVa CS216 Spring Lecture 9: Low-Level Programming

19 UVa CS216 Spring Lecture 9: Low-Level Programming Thought and Action Languages change the way we think –Scheme: think about procedures –BASIC: think about GOTO –Algol, Pascal: think about assignments, control blocks –Java: think about types, squiggles, exceptions –Python? Languages provide abstractions of machine resources –Hide dangerous/confusing details: memory locations, instruction opcodes, number representations, calling conventions, etc.

20 UVa CS216 Spring Lecture 9: Low-Level Programming Abstractions Higher level abstractions –Python, Java, BASIC, … –Easier to describe abstract algorithms –But, cannot manipulate low-level machine state How are things stored in memory? Opportunities for optimization lost Lower level abstractions –C, C++, JVML, MSIL, Assembly, … –Harder to describe abstraction algorithms –Provides programmer with control over low- level machine state

21 UVa CS216 Spring Lecture 9: Low-Level Programming Biggest Single Difference: Memory Management High-level languages (Python, Java) provide automatic memory management –Programmer has no control over how memory is allocated and reclaimed –Garbage collector reclaims storage Low-level languages (C, Assembly) leave it up to the programmer to manage memory

22 UVa CS216 Spring Lecture 9: Low-Level Programming Programming Languages Phylogeny (Simplified) Fortran (1954), IBM (Backus) Algol (1958) Scheme (1975) CPL (1963), U Cambridge Combined Programming Language BCPL (1967), MIT Basic Combined Programming Language B (1969), Bell Labs C (1970), Bell Labs C++ (1983), Bell Labs Java (1995) Objective C Simula (1967) Python (1990), Guido van Rossum ABC (~1980) BASIC (1963) Smalltalk (1971), PARC LISP (1957)

23 UVa CS216 Spring Lecture 9: Low-Level Programming C Programming Language Developed to build Unix OS Main design considerations: –Compiler size: needed to run on PDP-11 with 24KB of memory (Algol60 was too big to fit) –Code size: needed to implement the whole OS and applications with little memory –Performance, Portability Little (if any consideration): –Security, robustness, maintainability

24 UVa CS216 Spring Lecture 9: Low-Level Programming C Language No support for: –Array bounds checking –Null dereferences checking –Data abstraction, subtyping, inheritance –Exceptions –Automatic memory management Program crashes (or worse) when something bad happens Lots of syntactically legal programs have undefined behavior

25 UVa CS216 Spring Lecture 9: Low-Level Programming Example C Program void test (int x) { while (x = 1) { printf (“I’m an imbecile!”); x = x + 1; } In Java: void test (int x) { while (x = 1) { printf (“I’m an imbecile!”); x = x + 1; } > javac Test.java Test.java:21: incompatible types found : int required: boolean while (x = 1) { ^ 1 error Weak type checking: In C, there is no boolean type. Any value can be the test expression. x = 1 assigns 1 to x, and has the value 1. I’m an imbecile!

26 UVa CS216 Spring Lecture 9: Low-Level Programming Fortran (1954) Algol (1958) C (1970), Bell Labs C++ (1983), Bell Labs Java (1995), Sun CPL (1963), U Cambridge Combined Programming Language BCPL (1967), MIT Basic Combined Programming Language B (1969), Bell Labs LET := = = = =

27 UVa CS216 Spring Lecture 9: Low-Level Programming = vs. := Why does Python use = for assignment? Algol (designed for elegance for presenting algorithms) used := CPL and BCPL based on Algol, used := Thompson and Ritchie had a small computer to implement B, saved space by using = instead C was successor to B (also on small computer) C++’s main design goal was backwards compatibility with C Python was designed to be easy for C and C++ programmers to learn

28 UVa CS216 Spring Lecture 9: Low-Level Programming C Bounds Non-Checking int main (void) { int x = 9; char s[4]; gets(s); printf ("s is: %s\n“, s); printf ("x is: %d\n“, x); } > gcc -o bounds bounds.c > bounds abcdefghijkl s is: abcdefghijkl x is: 9 > bounds abcdefghijklm s is: abcdefghijklmn x is: > bounds abcdefghijkln s is: abcdefghijkln x is: > bounds aaa... [a few thousand characters] crashes shell (User input) = 0x6d = 0x6e Note: your results may vary (depending on machine, compiler, what else is running, time of day, etc.). This is what makes C fun!

29 UVa CS216 Spring Lecture 9: Low-Level Programming Charge Wednesday: Exam 1 is out, due Monday No regularly scheduled Small Hall and office hours while Exam 1 is out