6. program translation cs100: the world of computing john dougherty haverford college

15
6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Upload: gyles-skinner

Post on 13-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

6. Program Translation

CS100: The World of Computing

John Dougherty

Haverford College

Page 2: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Overview

The Problem The “Source” – high-level code The Target – low-level (machine) code Types of translation The translation algorithm/process PIPPIN

Page 3: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

The Problem

People communication in ambiguous, high-level languages, using experience, context, and can ask for clarity interactively e.g., Thoreau threw through the tunnel.

Machines have no “sense” of context or experience, and need unambiguous instructions

Page 4: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

The Source

High-level programming languages close to natural language (but not quite) Alice, Javascript, C++, Java, C#

Known as Source code Each instructions implies many lower-level

instructions (as we’ll see …)

Page 5: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

The Target

Low-level instructions that are clear and simple – typically fixed in size, with a command and some reference to data Opcode Operand

Known as … Machine code || binary code || executable

Page 6: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Program Translation

From High- to Low-Level Recall “divide and conquer” in programming

Input Process Output … then details of Process, then details of …

Typically many low-level operations per high-level instruction

From source code to machine/binary code Two ways to translate …

Page 7: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Interpretation

Works with the source always Translates and executes “on the fly” Like a language translator at the UN Easier to debug Executes slower

Page 8: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Compilation

Works with executable Translates the entire program from source to

machine code once Executes the machine code as many times as

needed Recompile often during development Executes substantially faster Most software is distributed (except open source) Hides algorithm

Page 9: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Phases of Translation

Scanning – breaking text sequence into tokens (i.e., meaningful chunks) “while”, “=”, “For all together”

Parsing – organizing the tokens to discover the meaning of the program

Code Generation – writing the sequence of machine level operations Opcodes, operands

Page 10: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Language Levels

High-level: one-to-many relation to machine language (e.g., z = x + y is 4 PIPPIN ops)

Assembly language: one-to-one (roughly) relation to machine language (PIPPIN)

Low-level: machine, or binary, language of 0s and 1s

Page 11: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Arithmetic Instructions

To demonstrate this process, we’ll look at standard arithmetic expressions and statements in a high-level language

Expressions have a pattern, or (recursively-defined) formVar = expWhere exp = value | exp + exp | exp – exp | …

(demonstration of Rosetta)

Page 12: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

PIPPIN instruction layout

opcode operand

Each box contains a byte

Page 13: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Sample PIPPIN Opcodes

00000100 LOD (load from X)

00000101 STO (store to X)

00001111 HLT (halt execution)

00000000 ADD (acc = acc + X)

Page 14: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Example PIPPIN program

; PIPPIN code for Z = X + Y

[1] LOD X ; acc <= X[2] ADD Y ; acc <= acc + Y[3] STO Z ; acc => Z[4] HLT ; halt

; other examples AE pp. 252-4

Page 15: 6. Program Translation CS100: The World of Computing John Dougherty Haverford College

Programming Paradigms

Imperative: procedures as abstractions, details of how to do a task (e.g., FORTRAN, Pascal)

Functional: mathematical approach of input-process-return value – functions can be composed of other functions (including themselves), and can be evaluated (e.g., LISP)

Declarative: describe the information, but not the way it is processed (e.g., Prolog)

Object-Oriented: interacting objects (e.g., Java, C++, C#, Smalltalk, Javascript, Alice)