370 likes | 499 Views
Program Translation. Module 6, Analytical Engine. Review. We have learned… about local applications, global applications, and the logical problem solving that programmers use to build them. Today: How the Computer Works. Binary Representation of Data Abstracting Information
E N D
Program Translation Module 6, Analytical Engine
Review • We have learned… • about local applications, • global applications, • and the logical problem solving that programmers use to build them
Today: How the Computer Works • Binary Representation of Data • Abstracting Information • Language Abstraction Levels • Language Design Issues and Solutions • Converting between levels • Phases and Techniques used • Language Generations
Begin Input: NumDays double mass = 2.5;int numPennies = 1;while (mass < 1000){ numPennies *= 2; mass = numPennies * 2.5;}System.out.println(numPennies);System.exit(0); Mass = 2.5 NumPennies = 1 End Mass <1000? Display NumPennies NO YES Double NumPennies Mass = NumPennies * 2.5
Machine Code • Computers only understand machine code. • Ex: 1000110100100010101 • Humans write programs in programming languages as in the previous example. • How do we get from one to the other?
Binary: As low as we go • Boole (and others) noted that all discrete data can be represented with two complementary symbols (0/1, T/F, off/on, up/down) This is called Binary Notation • AE text uses Rosetta Stone as a metaphor for the link between various languages • We need a way to translate!
Base Ten Number Systems • We usually use 10 digits (0 – 9) to express all numbers. • How do we express numbers bigger than 9? • 124 = (1x102) + (2x 101) + (4x100) = 100 + 20 + 4
Binary Numbers • What if we only had 2 digits? (0 and 1) • I’d have to count like this:0 01 12 103 114 100: :
Base Two (Binary) To Base 10 • What does 10111 equal in base 10? • Write the powers of two
Base Two (Binary) To Base 10 • What does 10111 equal in base 10? • Write the powers of two 16 8 4 2 11 0 1 1 1 16 + 4 + 2 + 1 = 23!
Base 10 to Binary • How do you write 28 in binary? • Write the powers of 2 again 16 8 4 2 1
Base 10 to Binary • Write 28 in binary • How many times does 16 go into 28? 16 8 4 2 1 1 What’s left? 12 That’s an 8 and a 4. The number in binary is 11100. Be sure to go from largest possible power of two down to 1.
Letter Data • Map each letter to a numeric value • ASCII standard (AE, p. 246) • Each letter takes 8 1’s and 0’s to encode. • Each 1/0 slot is called a bit. • 8 bits = 1 byte 01000001 = A
Inside the Computer • 256 memory “slots”, numbered 0 – 255 • One special slot, called accumulator • Each slot is enough bits to hold a number (in binary) • Accumulator is used as a place to do arithmetic
Binary Instructions • We need to encode whole commands • Could use ASCII, but not efficient • Each instructions maps to a unique opcode • Operation Code • Tells which operation we want to do next • Last bits give the address where the data is stored.
Instruction layout opcode operand Each box contains a byte
Example Instructions 00000100 X Load the accumulator with whatever is in slot X 00000101 X Store accumulator value in slot X 00001111 X Halt execution 00000000 X Add contents of slot X to the accumulator
Example Program Accumulator 00000100 10000000(load) (128) 00000000 10000001(add) (129) 00000101 10000000(store) (128) 00011111 00000000(halt) Memory 128 00000110 Memory 129 00001001
Example Program Accumulator 00000100 10000000(load) (128) 00000000 10000001(add) (129) 00000101 10000000(store) (128) 00011111 00000000(halt) Memory 128 00000110 00000110 Memory 129 00001001
Example Program Accumulator 00000100 10000000(load) (128) 00000000 10000001(add) (129) 00000101 10000000(store) (128) 00011111 00000000(halt) Memory 128 00001111 00000110 + Memory 129 00001001
Example Program Accumulator 00000100 10000000(load) (128) 00000000 10000001(add) (129) 00000101 10000000(store) (128) 00011111 00000000(halt) Memory 128 00001111 00001111 Memory 129 00001001
Example Program Accumulator 00000100 10000000(load) (128) 00000000 10000001(add) (129) 00000101 10000000(store) (128) 00011111 00000000(halt) Memory 128 00001111 00001111 Memory 129 00001001
Practice • Write a program to compute whatever is in slot 00000001 to the third power and store the answer in slot 00000010. 00000100 X Load the accumulator with whatever is in slot X 00000101 X Store accumulator value in slot X 00001111 X Halt execution 00000011 X Multiply contents of slot X with the accumulator
Machine Language • Programming in machine language is HARD. • Find the arithmetic error (not really!)00000001 0001010000010001 0001001000000011 0001000000000110 0001000100001110 10001001
Assembler • Want to type: LOD x ADD y STO x HLT • Write a program (in machine language) that translates words into machine language • Words are called Assembly Language, and the program that translates it is called the assembler.
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
Example PIPPIN program ; PIPPIN code for Z = 2*X [1] LOD X ; acc <= X [2] MUL #2 ; acc <= 2*acc [3] STO Z ; acc => Z [4] HLT ; halt ; other examples AE pp. 252-4 Note: #2 is different from 2!
Other Languages • Wouldn’t it be easier to just write Z = X + Y? • Assembly language is called low-level: It has one line of code for one line of machine language • High-level languages make the code more compact (Java, C++, JavaScript, SmallTalk)
High and Low level Languages • Low Level:LOD X ; acc <= XADD Y ; acc <= acc + YSTO Z ; acc => ZHLT ; halt • High Level: Z = X + Y • What are we giving up by using a high level language?
More Translation • Now we need to translate the high-level language to Assembly language • Two types of programs accomplish this: • Interpreter: Translate one line, then execute it, then translate the next line… • Compiler: Translate the whole program at once, then execute. • An interpreter: “Beat the eggs, beat the eggs, beat the eggs,…” • A compiler: “Beat the eggs until stiff peaks form.”
High level Benefits • Easier and faster to program • Have special ways of grouping data together • The way the language looks is tailored to the people who will use it.
How? • How does this translation process work? • Three steps: • Scanning: Look at each word • Parsing: Figure out what each word refers to • Code Generation: Write out the assembly code
Programming Language Generations • First generation: Machine language, different for each computer you work on • Second generation: Assembly language (still machine-specific) • Third generation: “High level” – now same code works on all machines. (Example on flowchart slide)
Programming Language Generations • Fourth Generation: Programming languages designed to look more like English:get customerlist name, address • Fifth Generation: Used mostly in artificial intelligence: Languages designed to explain the problem, not necessarily obvious how it’s solved – that’s left to the computer.
Example Fifth Generation Program(Prolog) mom(shannon, freda). dad(shannon, charles). mom(sheila, freda). dad(jeff, charles). sibling(X,Y) :- mom(X,Z), mom(Y,Z) sibling(X,Y) :- dad(X,Z), dad(Y,Z) ?- sibling(shannon, X). X = sheila, jeff.
Practice Problems • What is an assembler? Why use one? • Write a PIPPIN program to triple the value that is presently in the accumulator. • Convert to decimal: 111111011011100011 • Convert to binary:473581023