Software Lesson 2 Outline

Similar documents
What is Software? Software, for our purposes, is just a word meaning programs. CS1313: Software Lesson Fall

Software Lesson 1 Outline

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

C Introduction Lesson Outline

Compiler Construction D7011E

CS Computer Programming in Fortran 90 & C

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Computers and Computation. The Modern Computer. The Operating System. The Operating System

x86 assembly CS449 Fall 2017

Programming. Dr Ben Dudson University of York

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Syntax and Grammars 1 / 21

Compilers Crash Course

You may work with a partner on this quiz; both of you must submit your answers.

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

CS101 Introduction to Programming Languages and Compilers

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Compiler Construction LECTURE # 1

Intel assembly language using gcc

Introduction to Compilers

Reversing. Time to get with the program

Assembly Language: Function Calls

Compilers. Introduction. Yannis Smaragdakis, U. Athens (original slides by Sam

Chapter 2 A Quick Tour

CS241 Computer Organization Spring 2015 IA

Assembly Language: Function Calls" Goals of this Lecture"

Follow us on Twitter for important news and Compiling Programs

x86 assembly CS449 Spring 2016

Profilers and Debuggers. Introductory Material. One-Slide Summary

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Assembly Language: Function Calls" Goals of this Lecture"

Comp 524 Spring 2009 Exercise 1 Solutions Due in class (on paper) at 3:30 PM, January 29, 2009.

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Computing and compilers

CSE 351: Week 4. Tom Bergan, TA

Principles of Programming Languages. Lecture Outline

Introduction to Programming

Compiler Structure. Lexical. Scanning/ Screening. Analysis. Syntax. Parsing. Analysis. Semantic. Context Analysis. Analysis.

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

What are compilers? A Compiler

GCC as Optimising Compiler

CSCE150A. Administrivia. Overview. Hardware. Software. Example. Program. Pseudocode. Flowchart. Control Structures. Hello World Program CSCE150A

Computer Science & Engineering 150A Problem Solving Using Computers

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

C - Basics, Bitwise Operator. Zhaoguo Wang

Lecture 1 - Introduction (Class Notes)

CS241 Computer Organization Spring Introduction to Assembly

PLAGIARISM. Administrivia. Compilers. CS143 11:00-12:15TT B03 Gates. Text. Staff. Instructor. TAs. Office hours, contact info on 143 web site

Code Generation. Lecture 30

Introduction Presentation A

Chapter Twelve. Systems Design and Development

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Lab 10: Introduction to x86 Assembly

Pioneering Compiler Design

From High Level to Machine Code. Compilation Overview. Computer Programs

CMSC 430 Introduction to Compilers. Spring Code Generation

CS2112 Fall Assignment 4 Parsing and Fault Injection. Due: March 18, 2014 Overview draft due: March 14, 2014

Computer Science and Programming. CPS 06 Program Design and Methodology I. What is Computer Science? Computer Science

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

Software Development. Integrated Software Environment

Programming 1 - Honors

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

symbolic name data type (perhaps with qualifier) allocated in data area, stack, or heap duration (lifetime or extent)

Overview. Overview. Programming problems are easier to solve in high-level languages

W4118: PC Hardware and x86. Junfeng Yang

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Machine Programming 1: Introduction

CPS104 Recitation: Assembly Programming

Compiling Regular Expressions COMP360

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Programming 1. Lecture 1 COP 3014 Fall August 28, 2017

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

CS429: Computer Organization and Architecture

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

(0) introduction to the course. how to learn a programming language. (0) course structure

CS 314 Principles of Programming Languages

PLAGIARISM. Administrivia. Course home page: Introduction to Programming Languages and Compilers

Introduction to Programming Languages and Compilers. CS164 11:00-12:00 MWF 306 Soda

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Winter 2019 CISC101 1/17/2019

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

Second Part of the Course

CSE P 501 Exam 11/17/05 Sample Solution

Welcome to CS120 Fall 2012

Computer Organization & Assembly Language Programming

Transcription:

Software Lesson 2 Outline 1. Software Lesson 2 Outline 2. Languages 3. Ingredients of a Language 4. Kinds of Languages 5. Natural Languages #1 6. Natural Languages #2 7. Natural Languages #3 8. Natural Languages #4 9. Programming Languages 10. Natural Languages vs Programming Languages 11. Programming Language Hierarchy 12. High Level Languages 13. Assembly Languages 14. Machine Languages 15. Converting Between Languages 16. Compiler 17. Interpreter 18. Assembler 19. Our Old Friend hello_world.c 20. Compiler Details 21. Compiler Details (cont d) 22. Elements of a Compiler #1 23. Elements of a Compiler #2 24. Phases of Compiling 25. Compiling a C Statement 26. Assembly Code for hello_world.c #1 27. Assembly Code for hello_world.c #2 28. Machine Code for hello_world.c 29. How to Program in Machine Language Directly 30. Why Not Do Everything in Machine Language? 31. Why Not Do Everything in Assembly Language? 32. The Programming Process 33. What is an Algorithm? 34. Algorithms 35. Algorithm Example: Eating a Bowl of Corn Flakes 36. Top-Down Design 37. Eating Cornflakes: Top Level CS1313 Spring 2018 1

Languages What is a language? Kinds of languages Natural languages Programming languages (also known as Formal languages) Converting between programming languages Compilers Interpreters Assemblers CS1313 Spring 2018 2

Ingredients of a Language Symbols: a set of words and punctuation (in computing, words and punctuation are collectively known as tokens) Grammar (also known as syntax): a set of rules for putting tokens together to get valid statements Semantics: a set of rules for interpreting the meaning of a grammatically valid statement CS1313 Spring 2018 3

Kinds of Languages Natural languages: used in human communication Programming languages (also known as formal languages): used by computers (among others) CS1313 Spring 2018 4

Natural Languages #1 There are said to be over 7000 natural languages in the world: https://www.ethnologue.com/guides/how-many-languages Examples: English, Chinese, Swahili, Navajo, Quechua, Maori Typically can be described by formal rules (grammar), but often aren t rigidly governed by these rules in everyday use: Any noun can be verbed. I might could get me one o them there computers. CS1313 Spring 2018 5

Natural Languages #2 Can mix words from different languages and even syntax (elements of grammar) from different languages in a single sentence: Hey, amigo, is it all right by you if I kibbitz your pachisi game while we watch your anime? CS1313 Spring 2018 6

Natural Languages #3 Can be ambiguous: When did he say she was going? could be interpreted as: State the time at which he asked, She was going? According to him, at what time was she going? CS1313 Spring 2018 7

Natural Languages #4 Plenty of flexibility regarding correctness; for example, ain t, split infinitives, ending a sentence with a preposition. That is something up with which I will not put. CS1313 Spring 2018 8

Programming Languages Examples: C, Java, HTML, Haskell, Prolog, SAS Also known as formal languages Completely described and rigidly governed by formal rules Cannot mix the words of multiple languages, or the syntax of multiple languages, in the same program Cannot be ambiguous Words and syntax must be EXACTLY correct in every way CS1313 Spring 2018 9

Natural Languages vs Programming Languages PROPERTY NAT L PROG Completely described and rigidly governed by formal rules no YES CAN mix the words of multiple languages, or YES no the syntax of multiple languages, in the same program CAN be ambiguous YES no Words and syntax must be EXACTLY correct in every way no YES CS1313 Spring 2018 10

Programming Language Hierarchy High Level Languages Assembly Languages Machine Languages CS1313 Spring 2018 11

Human-readable High Level Languages Most are standardized, so they can be used on just about any kind of computer. Examples: C, Fortran 90, Java, HTML, Haskell, SAS Typically they are designed for a particular kind of application; for example: C for operating system design Fortran 90 for scientific & engineering applications Java for web applets and embedded systems HTML for hypertext (webpages) SAS for statistics But often, their uses in real life are broader their original purpose. CS1313 Spring 2018 12

Assembly Languages Human-readable Specific to a particular CPU family; for example: Intel/AMD x86 (PC) ARM (handhelds such as cell phones and tablets) IBM POWER (big IBM machines) So, for example, a program in x86 assembly language cannot be directly run on a POWER or ARM machine. Set of simple commands; for example: Load a value from a location in main memory Add two numbers Branch to an instruction out of sequence CS1313 Spring 2018 13

Machine Languages Not human-readable, except with immense effort Binary code that the CPU family understands directly Binary representation of the CPU family s assembly language CS1313 Spring 2018 14

Converting Between Languages Compilers, interpreters and assemblers are programs that convert human-readable source code into machine-readable executable code. CS1313 Spring 2018 15

Compiler Converts a human-readable high level language source code of a program into a machine language executable program Converts an entire source code all at once Must be completed before executing the program Examples: Fortran 90, C, C++, Pascal CS1313 Spring 2018 16

Interpreter Converts a human-readable high level language source code into actions that are immediately performed Converts and executes one statement at a time Conversion and execution alternate Examples: Perl, HTML, SAS, Mathematica, Unix shell (interactive system within Unix) CS1313 Spring 2018 17

Assembler Converts a human-readable CPU-specific assembly code into CPU-specific, non-human-readable machine language Like a compiler, but for a low level assembly language instead of for a high level language CS1313 Spring 2018 18

Our Old Friend hello_world.c % cat hello_world.c /* ************************************************* *** Program: hello_world *** *** Author: Henry Neeman (hneeman@ou.edu) *** *** Course: CS 1313 010 Spring 2018 *** *** Lab: Sec 014 Fridays 2:30pm *** *** Description: Prints the sentence *** *** "Hello, world!" to standard output. *** ************************************************* */ #include <stdio.h> int main () { /* main */ /* ******************************** *** Execution Section (body) *** ******************************** * * Print the sentence to standard output * (i.e., to the terminal screen). */ printf("hello, world!\n"); } /* main */ % gcc -o hello_world hello_world.c % hello_world Hello, world! CS1313 Spring 2018 19

Compiler Details CS1313 Spring 2018 20

Compiler Details (cont d) CS1313 Spring 2018 21

Elements of a Compiler #1 Lexical Analyzer: identifies program s word elements Comments (ignored by compiler) Keywords (for example, int, while) Constants (for example, 5, 0.725, "Hello, world!") User-defined Identifiers (for example, addend) Operators; for example: Arithmetic: + - * / % Relational: ==!= < <= > >= Logical: &&! CS1313 Spring 2018 22

Elements of a Compiler #2 Parser: determines the program s grammar Semantic Analyzer: determines what the program does Intermediate Code Generator: expresses, as an assembly-like program, what the program does Optimizer: makes code more efficient (faster) Assembly Code Generator: produces the final assembly code that represents what the program does CS1313 Spring 2018 23

Compiler Phases of Compiling Assembler: turns assembly code into machine code Linker/loader: turns machine code into an executable file Both the assembler and the linker/loader are invoked automatically by the compiler, so you don t have to worry about them. CS1313 Spring 2018 24

Compiling a C Statement CS1313 Spring 2018 25

Assembly Code for hello_world.c #1 On Pentium4 Using gcc On IBM POWER4 Using gcc pushl %ebp movl %esp, %ebp subl $8, %esp subl $12, %esp pushl $.LC0 call printf addl $16, %esp leave ret Different opcodes! mflr 0 stw 31,-4(1) stw 0,8(1) stwu 1,-64(1) mr 31,1 lwz 3,LC..1(2) bl.printf nop lwz 1,0(1) lwz 0,8(1) mtlr 0 lwz 31,-4(1) blr CS1313 Spring 2018 26

Assembly Code for hello_world.c #2 On Pentium4 Using gcc (GNU compiler) pushl %ebp movl %esp, %ebp subl $8, %esp subl $12, %esp pushl $.LC0 call printf addl $16, %esp leave ret Different sequences of instructions! On Pentium4 Using icc (Intel compiler) pushl %ebp movl %esp, %ebp subl $3, %esp andl $-8, %esp addl $4, %esp push $ STRING.0 call printf xorl %eax, %eax popl %ecx movl %ebp, %esp popl %ebp ret CS1313 Spring 2018 27

Machine Code for hello_world.c 10111101010100010101011110101001 10111010101000010101101011101000 01110101010000101011010111010001 01010100101010101101010101011010... CS1313 Spring 2018 28

How to Program in Machine Language Directly 1. Write the assembly code for the program directly by hand; that is, not in a high level language. 2. For each assembly language instruction, look up the bit pattern of the associated machine code. 3. On the computer console, flip switches to match the bit pattern of the machine code. 4. Press the Run button. Actually, on modern computers, programming directly in machine language is just about impossible. CS1313 Spring 2018 29

Why Not Do Everything in Machine Language? Incredibly tedious and ridiculously error-prone! Fun and easy! Not nearly as tedious or error-prone! CS1313 Spring 2018 30

Why Not Do Everything in Assembly Language? Can t be run on any other kind of computer. May be completely obsolete in a few years. Portable to many kinds of computers. Will still be useable in 20 years ( legacy codes). CS1313 Spring 2018 31

The Programming Process Formulate Problem Compile Construct Algorithm Bugs? Yes Debug No Choose Programming Language Run Write Program Bugs? Yes No Get an A/Impress Your Boss/Sell for Zillions! CS1313 Spring 2018 32

What is an Algorithm? An algorithm is: a step-by-step method that is written in a natural language (for example, English) or in pseudocode (something that sort of looks like a programming language but isn t as precise), instead of in a programming language, that solves a well-defined (though not necessarily useful) problem, on a well-defined set of inputs (which may be empty), using finite resources (for example, computing time and storage), and that produces a well-defined set of outputs (which may be empty). CS1313 Spring 2018 33

Algorithms An algorithm is a language-independent way of expressing the method of solving a problem; that is, an algorithm could be expressed in two different languages (for example, English and Japanese) and still be the same algorithm. A program, by contrast, is a language-dependent implementation of the method of solving a problem; that is, the same set of steps expressed in two different programming languages would be two different programs, even if the two programs accomplished exactly the same result. Many programs, but not all, implement algorithms. CS1313 Spring 2018 34

Algorithm Example: Eating a Bowl of Corn Flakes Get bowl from cupboard Get spoon from drawer Get box of corn flakes from pantry Get jug of milk from refrigerator Place bowl, spoon, corn flakes and milk on table Open box of corn flakes Pour corn flakes from box into bowl Open jug of milk Pour milk from jug into bowl Close jug of milk Go to table Pick up spoon Repeat until bowl is empty of corn flakes Using spoon, pick up corn flakes and milk from bowl Put spoon with corn flakes and milk into mouth Pull spoon from mouth, leaving corn flakes and milk Repeat... Chew... until mouthful is mush Swallow Leave mess for housemates to clean up CS1313 Spring 2018 35

Top-Down Design Algorithms for most non-trivial problems tend to be fairly complicated. As a result, it may be difficult to march from an algorithm s beginning to its end in a straight line, because there may be too many details to keep in your head all at one time. Instead, you can use a technique called top-down design: start with the whole problem, then break it into a few pieces, then break each of those pieces into a few pieces, then break each of those pieces into a few pieces, and so on, until each piece is pretty small. CS1313 Spring 2018 36

Get stuff Transport stuff Set up stuff Eat Finish Eating Cornflakes: Top Level CS1313 Spring 2018 37