slides for chapter 8 note to instructors license © 2012 john s. conery the slides in this keynote...

Post on 12-Jan-2016

214 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Slides for Chapter 8

Note to Instructors

License

© 2012 John S. Conery

The slides in this Keynote document are based on copyrighted material from Explorations in Computing: An Introduction to Computer Science, by John S. Conery.

These slides are provided free of charge to instructors who are using the textbook for their courses.

Instructors may alter the slides for use in their own courses, including but not limited to: adding new slides, altering the wording or images found on these slides, or deleting slides.

Instructors may distribute printed copies of the slides, either in hard copy or as electronic copies in PDF form, provided the copyright notice below is reproduced on the first slide.

This Keynote document contains the slides for “The War of the Words”, Chapter 8 of Explorations in Computing: An Introduction to Computer Science.

The book invites students to explore ideas in computer science through interactive tutorials where they type expressions in Ruby and immediately see the results, either in a terminal window or a 2D graphics window.

Instructors are strongly encouraged to have a Ruby session running concurrently with Keynote in order to give live demonstrations of the Ruby code shown on the slides.

Explorations in Computing

© 2012 John S. Conery

An introduction to computer architecture

The War of the Words

✦ Hello, MARS

✦ The Temperature on MARS

✦ Corewar

✦ Self-Referential Code

✦ Clones

✦ People have been using mechanical devices to help with computation for thousands of years❖ Sumerians

❖ Egyptians

❖ Persians

❖ Greeks and Romans

❖ Chinese, Japanese and Koreans

❖ Mayans and Incans

A Brief History of Computing Machines

See “abacus” at Wikipedia

✦ In the middle ages European scientists dreamed about building machines to carry out calculations automatically

Automatic Calculators

Gottfried Leibniz (1646--1726)

Astronomers surely will not have to continue to exercise the patience which is required for computation.... It is unworthy of excellent men to lose hours like slaves in the labor of calculation which could safely be relegated to anyone else if machines were used.

I wish to God these calculations had been executed by steam!

Charles Babbage (1821)

✦ The first fully automatic calculatingmachine was the Harvard Mark I (1944)❖ storage for 72 numbers

❖ addition: 1/3 second

❖ multiplication: 6 seconds

❖ sequence of operations read from a paper tape

✦ The Mark I was a mechanicalcomputer❖ used electro-mechanical

switches called relays

The First Computers

✦ The first electronic machine was ENIAC (1946)❖ no moving parts

❖ used vacuum tubes, which wereelectronic switches

✦ Approximately 1000 times faster than the Mark I❖ addition: .0002 sec

❖ multiplication: .0026 sec

❖ controlled by a “plug board”

❖ programmers connected adders,multipliers, so output of one unitfed into another

The First Computers

Size: 8’ x 3’ x 80’Weight: 27 tons

Power: 150 kW (~1000 light bulbs)Modern laptop: 150 W

✦ Modern processors usesemiconductor technology❖ transistors instead of vacuum

tubes

✦ Approximately 109 times faster than the Mark I

✦ Reasons for increased performanceare not just improvements inhardware technology

❖ a landmark result in mathematicallogic paved the way for moderncomputer design

Modern Computers

SGI Altix cluster at NASA (10K CPUs)

✦ The idea was first presented in a paper by a Hungarian-born American mathematician named John von Neumann

✦ von Nuemann’s proposal:

❖ store the instructions that control a processor in the same memory that holds the data

✦ This idea can be traced to important mathematical results from the 1930s

❖ mathematical functions can be encoded as numbers

❖ therefore binary representations of functions (i.e. programs) can be stored in a memory along with binary encoding of numbers (i.e. data)

The Stored Program Computer

✦ The first computer based on the stored-program idea was named EDVAC❖ proposed in 1944

❖ von Neumann’s paper describing storedprogram organization presented in 1945

❖ machine operational in 1951

✦ The is some controversy over who shouldget credit for the stored-program design❖ von Neumann was a consultant on EDVAC

project

❖ engineers who built the machine also deserve credit

❖ Alan Turing developed a similar design in 1945 but was forced to keep it secret

EDVAC

✦ A practical consequence of storingprograms and data in the same memory:

❖ improved memory and processor technologywill also speed up program execution

✦ Mark I read instructions from paper tape

✦ It won’t matter if a CPU can add apair of numbers in under 10-9 secondsif it takes 10-1 seconds to read eachinstruction from cards or paper tape

✦ If instructions are encoded in binaryand stored in memory along with datathe CPU can access instructionsjust as quickly

Technological Implications

✦ More far-reaching implications:❖ one program can treat another program

as a piece of data

✦ ENIAC was programmed by re-wiringthe front panel

✦ The fact that programs are just anotherform of data means we can ❖ use text editors to create programs

❖ use compilers to translate them intointernal binary representations

❖ use an operating system to load theprogram into memory and run it

Programming Implications

The ability to execute a general-purpose, stored program is the The ability to execute a general-purpose, stored program is the difference between a difference between a computercomputer and a and a calculatorcalculator

✦ Turing’s most important contribution tocomputer science was a paper writtenin 1936❖ a paper on mathematical logic

❖ contained the specification of what wenow call a universal Turing machine

❖ used to prove the halting problem

✦ The proof is based on the ideathat a description of one machine can be written on a “tape” and the tapeis then read as input by another machine

Theoretical Implications

✦ Some recommended reading to learn more about the idea that functions can be encoded as numbers:

To Learn More

History of computing machines, by one of the co-authors of the EDVAC report

A readable history of math, and how Gödel’s ideas had a profound effect on math and CS

✦ A block in Ruby is a set of expressions enclosed in braces

>> a.each { |x| puts x if x.length > 5 }

>> time { a.sort }

✦ Ruby treats blocks as pieces of data that can be passed as parameters to methods like each and time

✦ One of the implications of the halting problem there are limits to what we can do with blocks

Programs as Data in Ruby

def time(&f) tstart = Time.now f.call return Time.now - tstart end

def foo(&f, &g) if f == g ... end end

✦ Another example of passing a block as a parameter:

>> a = RandomArray.new(:cars, 10)

=> ["porsche", "kia", "ferrari", "tesla", "lancia", "jaguar", "mg", "lotus", "oldsmobile", "lexus"]

>> a.sort

=> ["ferrari", "jaguar", "kia", ... "porsche", "tesla"]

>> a.sort { |x,y| greater(x,y) }

=> ["tesla", "porsche", "oldsmobile", ... "jaguar", "ferrari"]

>> a.sort { |x,y| shorter(x,y) }

=> ["mg", "kia", "tesla", "lotus", ... "porsche", "oldsmobile"]

Programs as Data in Ruby

Project

✦ The goal for this chapter: learn about the von Neumann (stored program) architecture

✦ The project is based on a computer game called Core War❖ basically a computer-based version of Battleship

✦ Emphasizes the notion that programs and data both reside in the same memory❖ load two programs into a single memory

❖ take turns executing instructions from each program

❖ each program tries to write a “halt” instruction over the code of the other program

MARSLab

✦ Outline:❖ overview of MARS (the computer used to play Core War)

❖ MARSLab, the RubyLabs implementation of MARS

❖ example MARS programs

❖ playing Core War with MARSLab

Live Demo

Architecture

✦ The two main components in machines organized according to thevon Neumann architecture:❖ the central processing unit (CPU)

❖ the memory unit

✦ The processor is in control❖ the CPU can read information from memory (aka fetch)

❖ it can also write information into memory (aka store)

Memory

✦ Think of memory as a large array

✦ Items are accessed by their address❖ address : memory :: index : array

✦ When a processor wants to fetch an instructionor a piece of data it sends an address to memory

✦ The memory responds by sending the contentsof that cell

Words

✦ In MARS each memory cell holds a single instruction or a single piece of data❖ the generic term: word

✦ In real computers, each memory location refers a single byte❖ 1 byte = 8 bits

❖ 1 word = 4 bytes (32-bit processor) or 8 bytes (64-bit processor)

Title for Ch. 8:“The War of the Words”

Fetch-Decode-Execute

✦ The central processing unit (CPU) in a stored-program computer continually executes a very simple algorithm:

✦ Before we look at these steps in more detail let’s look at a simple “hello, world” program ❖ this program just adds two numbers and halts

❖ but it illustrates the sorts of things that can be done by a single machine instruction, and shows how data is stored with the program

fetch get the next instruction from memory

decode figure out what the instruction is supposed to do

execute carry out the operation defined by the instruction

Hello, MARS

✦ Here is the text of the “hello, world” program

✦ The programming language for Core War is called Redcode

✦ Redcode programs (like Ruby programs) are created with a text editor application and stored in plain text files

✦ The most important thing to remember:❖ each statement in the program is translated into one word in memory

There are 4 statementsin this program

Everything following ; is a comment

Hello, MARS

✦ A statement can have a label at the front of the line

✦ The opcode specifies what the CPU should do❖ typically a three-letter name shown in upper case

✦ Following the opcode are one or two operands❖ these tell the processor where to find information required by the opcode

The first 3 instructions have labelsThis program has 3 DAT instructions

and 1 ADD instruction

operands

Hello, MARS

✦ The last line in a file has the word end instead of an opcode❖ this tells MARS where to start executing the program

❖ this small program will be loaded into four words in memory

❖ when the program is run, MARS will start with the instruction labeled hello

Begin execution with the linethat has this label

DAT

✦ A line that has the word DAT is a piece of data❖ usually the operand is the # symbol followed by a number x

❖ the line means “this word contains the number x”

✦ Important: DAT also serves as a halt instruction❖ if the CPU ever tries to execute an instruction that has DAT as an opcode the MARS

program terminates

ADD

✦ An instruction that has the opcode ADD tells the CPU to add two numbers❖ ADD has two operands

❖ in the example below, both operands are labels

❖ this instruction means “find the value in the word labeled x, and add it to the word labeled y”

Tracing the Program

✦ To run this program the CPU does two complete fetch/decode/execute cycles❖ the first instruction adds the contents of memory cell x to the memory cell y

❖ memory cell y now contains the number 11

❖ the second instruction is the DAT that follows the ADD

❖ executing the DAT causes the program to halt

To write this program in Ruby:

y = y + x

✦ The CPU does four memory operations when it executes an ADD instruction ❖ the first access occurs when the CPU fetches the instruction

❖ the add instruction tells the CPU to do two more fetches, to get the contents of cells x and y

❖ after the CPU adds the two values, the result is written back out to memory and stored in cell y

Memory Traffic

Testing the Program

✦ The RubyLabs module we will use this week is MARSLab❖ the module has methods that simulate the actions of a MARS CPU

❖ a program that simulates the actions of a processor and memory is known as a virtual machine

>> include MARSLab

=> Object

✦ To make a small “test machine” to try out a program:

>> m = make_test_machine(:test_hello)

=> #<MiniMARS mem = [DAT #0 #7, ... ] pc = [ *2 ]>

MARS = Memory Array

Redcode Simulator

Testing the Program

✦ Call a method named dump to look at the contents of the test machine memory

>> m.dump

0000: DAT #0 #7

0001: DAT #0 #4

0002: ADD -2 -1

0003: DAT #0 #0

Live Demo

The Redcode program has been translated into MARS machine language instructions

Testing the Program

✦ A method named step will execute the next instruction

>> m

=> #<MiniMARS mem = [DAT #0 #7...] pc = [ *2 ]>

>> m.step

=> ADD -2 -1

>> m.dump

0000: DAT #0 #7

0001: DAT #0 #11

0002: ADD -2 -1

0003: DAT #0 #0

Add the word 2 locations before this instruction to the word 1 location back

The program counter holds the address of the

next instruction

Note how “DAT 4” has changed to “DAT 11”

Aside: Binary Code

✦ In a real CPU the machine language programs are binary numbers❖ memory chips hold an electric charge (e.g. 0v or 3.2v)

✦ To store an instruction or a piece of data in memory ❖ convert the item into binary

❖ store 0s using one voltage, 1s using the other

✦ For this lab we’ll just show instructions by using their opcodes (ADD, JMN, etc)

✦ We’ll also refer to data as “instructions” because DAT is an opcode❖ it’s the instruction that halts the machine

A Simple Machine

✦ MARS is a very simple computer❖ there are only 11 different

instructions

❖ the only kind of data is aninteger

❖ one instruction (MOV) just copiesdata

❖ the only arithmetic is additionor subtraction

❖ six instructions are “branchinstructions” used to implementloops

‣ aka “jump” instructions

DAT x data / halt

MOV x,y copy x to y

ADD x,y add x to y

SUB x,y subtract x from y

JMP x continue execution at x

JMZ x,y continue at x if y is zero

JMN x,y continue at x if y nonzero

DJN x,y decrement y, jump to x if nonzero

CMP x,y skip the next instruction if x = y

SLT x,y skip the next instruction if x < y

SPL x start a new thread at x

Assembly Language

✦ Redcode is a type of languagecalled assembly language❖ an assembler is a program that

translates AL programs intobinary machine language

✦ AL programs are specific to oneparticular type of processor❖ Intel, IBM, and other CPU

chips all have their ownassembly language

DAT x data / halt

MOV x,y copy x to y

ADD x,y add x to y

SUB x,y subtract x from y

JMP x continue execution at x

JMZ x,y continue at x if y is zero

JMN x,y continue at x if y nonzero

DJN x,y decrement y, jump to x if nonzero

CMP x,y skip the next instruction if x = y

SLT x,y skip the next instruction if x < y

SPL x start a new thread at x

Real processors have similartypes of instructions

Another Example Program

✦ Here is the Fahrenheit to Celsiusalgorithm from Chapter 2

fahr DAT #80

cels DAT #0

ftmp DAT #0

start MOV fahr, ftmp

SUB #32, ftmp

DAT x data / halt

MOV x,y copy x to y

ADD x,y add x to y

SUB x,y subtract x from y

JMP x continue execution at x

JMZ x,y continue at x if y is zero

JMN x,y continue at x if y nonzero

DJN x,y decrement y, jump to x if nonzero

CMP x,y skip the next instruction if x = y

SLT x,y skip the next instruction if x < y

SPL x start a new thread at x

Input temperature

Result saved here

Note: in Ruby the expression is cels = (fahr - 32) * 5 / 9

First step: subtract 32 from fahr, save result in ftmp

(continued on the next slide)

Another Example (cont’d)

✦ Fahrenheit to Celsius (cont’d):

mult ADD ftmp, acc

SUB #1, count

JMN mult, count

div SUB #9, acc

SLT #0, acc

DAT #0

ADD #1, cels

JMP div

acc DAT #0

count DAT #5

end start

DAT x data / halt

MOV x,y copy x to y

ADD x,y add x to y

SUB x,y subtract x from y

JMP x continue execution at x

JMZ x,y continue at x if y is zero

JMN x,y continue at x if y nonzero

DJN x,y decrement y, jump to x if nonzero

CMP x,y skip the next instruction if x = y

SLT x,y skip the next instruction if x < y

SPL x start a new thread at x

This loop adds ftmp to acc 5 times, i.e. acc = 5 * ftmp

This loop counts the number of times 9 can be subtracted, i.e.

cels = acc / 9

Live Demo

✦ The MARS system we will use has 4K words❖ 1K = 210 = 1024

❖ 4K = 4096

✦ When a game is started, the system loads two programs into random locations

Playing a Game of Core War

To make it easier to see 4096 cells they are displayed as a matrix

✦ The CPU will alternate between programs❖ fetch/decode/execute one instruction from A (red)

❖ fetch/decode/execute one instruction from B (blue)

✦ Each program tries to cause the other to halt❖ store a DAT instruction over the code of the other program

Playing a Game of Core War (cont’d)

✦ The code for a very simple “core warrior” is shown at right

✦ The first word in the program isa “bomb”❖ to this program, it’s a piece of data

❖ to the other program, it’s a halt instruction

✦ The Dwarf program stores the bombin every 4th memory location❖ each time through the loop the address

where the bomb will be stored is incremented by 4

Dwarf

Live Demo

(details on the next slide)

✦ The MOV instruction is the one that“throws the bomb”

✦ The instruction MOV X, Y means“copy the contents of X to cell Y”

✦ The instruction MOV X, @Y is thesame, except Y is not the destination,it is the address of the destination

✦ In this program, vache has the value4, 8, 12, ...❖ the bomb will be stored 4, 8, 12, ...

words past the end of the code

Indirect Addressing

Live Demo

✦ The MARS computer has a 4096-wordmemory

✦ What happens when the destinationfor the bomb reaches 4096?

✦ Answer: the memory addresses“wrap around” from 4095 to 0❖ the simulator calculates the memory

location for address x with the formula x % 4096

✦ It doesn’t matter where a program isloaded into memory, it will always beable to access all 4096 words

Running Out of Memory

Live Demo

✦ The code for another “core warrior”is shown at right

✦ This program (called Imp) has justone instruction

✦ What does it do?❖ recall addresses are relative to the

instruction

❖ e.g. ADD -2, -1 means “add thecontents of memory 2 words beforethis instruction to the cell 1 wordbefore this instruction”

❖ so MOV 0, 1 means:

copy this instruction to the memorylocation right after this instruction

Imp

Live Demo

✦ What happens after the CPUexecutes MOV 0, 1?❖ according to the fetch/decode/

execute algorithm, the CPUshould continue with the nextinstruction in memory

❖ what is now in memory at thislocation?

❖ MOV 0, 1 .....

this tiny “program” just copiesitself over and over, and movesrelentlessly through memory

Imp

Live Demo

copy this instruction to the memory location right after this instruction

✦ What will happen when these twoprograms compete against eachother?❖ can Dwarf ever win?

❖ can Imp ever win?

❖ what are the possible outcomeswhen the Imp reaches thecode for the Dwarf?

Imp vs. Dwarf

Live Demo

✦ A program can copy itself from one location to another❖ not fair in Battleship, but OK in Core War

❖ if a program has n instructions, write a loop that iterates n times

❖ each time through, copy an instruction to a new location

✦ The program shown at right will copyitself to a new location in memory❖ 833 words past the place where it was

loaded

✦ It then jumps to the copy, and thecycle repeats...

Jumping Around

Live Demo

✦ MARS has an instruction that starts a new thread

✦ The idea is to “split” a programinto two pieces❖ analogy: the old video game named

Centipede

❖ a worm is crawling down from thetop of the screen

❖ whenever it is hit its splits in two,with each piece continuing onindependently

✦ In MARS:

SPL x

means “start a second program beginning at location x”

Threads

✦ An example of how to use SPL is a program shown here

✦ The “imp cannon” splits itself into two pieces❖ it starts with a loop that just counts

down to zero

❖ one piece jumps back to the start of the program

❖ the second piece is an Imp that starts crawling through memory

✦ End result: a series of imps, following each other through memory...

Cannon

Live Demo

✦ A more complicated program that usesthis strategy is named Mice❖ it combines the Flea and Cannon

strategies

❖ it makes a copy of the entireprogram

❖ it then executes a SPL

❖ the original copy and the new copycontinue to execute in parallel

Clones

Live Demo

✦ One of the most important ideas in the development of computer science was the von Neumann architecture ❖ programs and data in the same memory

Summary

faster execution of programs

compilers, operating systems, ...

✦ Memory is basically a large array of words❖ a processor can fetch a word by sending an address to memory

❖ the processor can also store data

✦ The CPU operates according to a fetch/decode/execute cycle

Summary (cont’d)

fetch get the next instruction from memory

decode figure out what the instruction is supposed to do

execute carry out the operation defined by the instruction

✦ To explore this idea we experimented with a game called Core War❖ two programs are loaded into random locations

❖ the MARS CPU alternates executing one instruction from each program

❖ a program tries to halt the other program

❖ halt = DAT

Summary (cont’d)

Summary (cont’d)

✦ MARS programs are written in a language named Redcode❖ an “assembly language”

✦ Each line in the program corresponds to one word in memory

✦ The parts of an instruction are the label, opcode, and operands

The first 3 instructions have labels This program has 3 DAT instructionsand 1 ADD instruction

operands

DAT x data / halt

MOV x,y copy x to y

ADD x,y add x to y

SUB x,y subtract x from y

JMP x continue execution at x

JMZ x,y continue at x if y is zero

JMN x,y continue at x if y nonzero

DJN x,y decrement y, jump to x if nonzero

CMP x,y skip the next instruction if x = y

SLT x,y skip the next instruction if x < y

SPL x start a new thread at x

top related