Docsity

How Programs Run, Lecture Notes - Computer Science, Study notes of Introduction to Computers

Partial preview of the text

Download How Programs Run, Lecture Notes - Computer Science and more Study notes Introduction to Computers in PDF only on Docsity!

How Programs Run

Computer Processors

Your computer processor

is a machine

that takes in a program

and runs it.

(The program must be written in the

processor's machine language .)

A Simple Interpreter

 LMC.jar is a simple interpreter based on "The Little Man Computer."  The language that this interpreter runs is representative of the kind of machine language that your computer processor might run.  Demo ...

A Simple Machine Language

add : add value at given address to accumulator  goto : go to given line number  ifzero : if accumulator contains zero, go to given line number  input : prompt for number and store at given address  load : load value at given address into accumulator  store : store accumulator value at given address  subtract : subtract value at given address from accumulator

Adding 1

0: input 15 //input x

1: load 15 //load x

2: add 14 //add 1

14: data 1 //

15: data //x

Repeatedly Adding 1

0: input 15 //input x

1: load 15 //load x

2: add 14 //add 1

3: goto 2 //repeat

14: data 1 //

15: data //x

x

answer = 1;

while (x != 0)

x = x - 1;

answer = answer + answer;

x

0: input 15 1: load 14 2: store 13 3: load 15 4: ifzero 11 5: subtract 14 6: store 15 7: load 13 8: add 13 9: store 13 10: goto 3 ... 13: data //answer 14: data 1 15: data //x input x answer = 1; while (x != 0) { x = x - 1; answer = answer + answer;

Equivalent Programs

Java x Machine Language 0: load 14 ... 14: data //x

Equivalent Programs

Java x = 3; Machine Language 0: load 15 1: store 14 ... 14: data //x 15: data 3

Equivalent Programs

Java if (x == 3) { } else { } Machine Language 0: load 14 1: sub 15 2: ifzero 7 3-5: 6: goto 10 7-9: ... 14: data //x 15: data 3

Equivalent Programs

Java while (x != 3) { } Machine Language 0: load 14 1: sub 15 2: ifzero 7 3-5: 6: goto 0 ... 14: data //x 15: data 3

Summary So Far

int x; x = 3; S.o.p(x + x); 6 load 15 add 15 15: data 3 6 int x; x = 3; S.o.p(x + x); load 15 add 15 15: data 3 Interpreter (program) Compiler (program) Processor (machine)

Why Compile?

 Compared to interpreting, compiling

seems to require an extra step.

 What is the advantage of compiling?