cs1q computer systems lecture 3 simon gay. lecture 3cs1q computer systems - simon gay2 where we are...

22
CS1Q Computer Systems Lecture 3 Simon Gay

Upload: emma-owens

Post on 16-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

CS1Q Computer SystemsLecture 3

Simon Gay

Page 2: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 2

Where we are

Global computing: the Internet

Networks and distributed computing

Application on a single computer

Operating System

Architecture

Digital Logic

Electronics

Physics

How do we design amachine that can execute programs?

Page 3: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 3

Structure of a Computer

CPUMEM-ORY

DIS-PLAY

DISK more...

buses

CPU - Central Processing Unit; microprocessor; e.g. Pentium 4Memory - stores both programs and dataPeripherals - display, disk, keyboard, modem, printer, …Disk - larger, slower and more permanent than memoryBuses - pathways for information

Page 4: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 4

CPU Architecture

• The CPU is in control. It executes individual instructions.

• The CPU does not execute Ada statements directly.

• The CPU has its own machine language which is simpler, but general enough that programs in Ada (or other languages) can be translated into it.

• Why?

– The CPU does not force the use of any one high-level language.

– It’s more efficient to design and manufacture a general-purpose machine, rather than one for each language.

Page 5: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 5

Which CPU?• A wide variety of CPUs are in use today:

– The Intel family (486, Pentium, Pentium 2, P3, P4,…)• popular in desktop computers

– The 64-bit Intel family (Itanium)• popular in high-performance workstations and servers

– The PowerPC range• used in Apple computers: iMac, PowerBook, etc

– The ARM range• used in handheld computers, embedded systems

– DSP (digital signal processors), integrated microcontrollers, ...

• Most of the world’s CPUs are not in PCs!

Page 6: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 6

The IT Machine

• A simplified CPU, whose design shares many features of modern real CPUs.

• We can understand its operation in detail, without getting bogged down in complexity.

• We have a software emulator, so we can run programs in the lab.

• We’ll compare the IT machine with some real CPU designs later.

Page 7: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 7

Registers: The ITM’s VariablesThe ITM has 16 registers, which are like Ada variables. Each register can store a 16 bit value. Their names are R0 - Rf.

LDVAL and ADD instructions allow basic calculations.

R1 := 1;R2 := 2;R3 := R1 + R2;

LDVAL R1,$0001LDVAL R2,$0002ADD R3,R1,R2

combination of addition and assignment

an immediate value

(Register R0 always stores the value 0 and cannot be changed.)

Page 8: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 8

Working with Registers

Rd 0Rc 0Rb -600Ra 1254R9 1R8 0R7 0R6 2R5 -56R4 134

Rf -5Re 3

R0 0R1 13R2 -2R3 0

Rd 0Rc 0Rb -600Ra 1254R9 1R8 0R7 0R6 2R5 -56R4 134

Rf -5Re 3

R0 0R1 13R2 -2R3 11

ADD R3,R1,R2

ALU

Page 9: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 9

The ALU

• The Arithmetic and Logic Unit is a subsystem of the CPU.

• The ALU carries out operations such as addition, subtraction, comparisons, … when required by instructions such as ADD.

Page 10: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 10

Memory• The registers are not sufficient for storing large

amounts of data. So the ITM has memory.• The memory is like an array of 16 bit words. Each

location (element) has an address (index).• The ITM is a 16 bit machine, so a memory address

is a 16 bit word. Therefore the maximum memory size is 65536 words.

• As well as storing data, the memory stores the instructions which make up a program.

• In practice, (most of) the memory is outside the CPU.

Page 11: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 11

Assembly Language andMachine Language

Instructions such as ADD R3,R1,R2 are in assembly language.Assembly language is a human-readable form of machine language.

Machine language is a binary representation of instructions.

ADD R3,R1,R2

0011 0011 0001 0010

3 3 1 2

It is the machine language form which is stored in memory.

assembly language

machine language (binary)

machine language (hex)

Page 12: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 12

The Stored Program Computer

• Storing the program in memory, in the same way as data, is one of the most important ideas in computing.

• It allows great flexibility, and means that programs which manipulate programs (e.g. compilers) are conceptually no different from programs which manipulate data.

Page 13: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 13

Execution of a ProgramInstructions are executed in sequence, starting with the instructionin memory location 0.

A special register, the program counter (PC), stores the address ofthe instruction being executed.

R1 := 5;R2 := 3;R3 := 2*R1 + R2;

LDVAL R1,$0005LDVAL R2,$0003LDVAL R4,$0002MUL R5,R1,R4ADD R3,R5,R2

Example:

Page 14: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 14

Rd 0Rc 0Rb -600Ra 1254R9 1R8 0R7 0R6 2R5 -56R4 134

Rf -5Re 3

R0 0R1 13R2 -2R3 0

Registers

21000000

Memory

Address Contents

0005000122000002

33520007

00030003240000040002000565140006

00000008

0000000c0000000b0000000a00000009

0000000f0000000e0000000d

ALU

0000PC

LDVAL R1,$0005

Instruction

Page 15: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 15

Assembly Language Programming• It is rarely necessary to program in assembly

language.• Assembly language programs are produced by

systematic (and automatic) translation of programs in high level languages (e.g. Ada).

• We will look at how some common high level constructs are translated.

• Compiler writers must understand assembly language.

• CPUs are designed with compilers in mind.

Page 16: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 16

Using MemoryTo use the memory, we must refer to an address. In assemblylanguage we can use a label instead of a numerical address.A label is just a name, similar to a variable name in Ada.

LOAD R3, x[R0]

any registerlabel of memory location

explain later

If we think of the label x as the name of a variable (the value of thisvariable is stored in the memory location labelled by x) this means:

R3 := x;

Page 17: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 17

Writing to MemoryThe instruction STORE, with a similar format to LOAD, changesthe contents of a memory location.

STORE R3, x[R0]

any registerlabel of memory location

explain later

Again thinking of x as the name of a variable, this means:

x := R3;

Page 18: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 18

ExampleWe can translate a fragment of Ada code into assembly language:

x := 5;y := 3;z := 2*x + y;

Declare the labels x, y, z, initialising the variables to 0:

x DATA $0000y DATA $0000z DATA $0000

DATA is not a machine language instruction. It just tells theassembler (which translates assembly language to machine language)to allocate space in memory.

Page 19: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 19

ExampleNow translate the Ada statements. We need to use registers, becauseonly the LOAD and STORE instructions can access memory.

LDVAL R6, $0005STORE R6, x[R0]LDVAL R6, $0003STORE R6, y[R0]LOAD R1, x[R0]LOAD R2, y[R0]LDVAL R4, $0002MUL R5, R1, R4ADD R3, R5, R2STORE R3, z[R0]

x := 5;

y := 3;

R1 := x;

R2 := y;

R5 := x*2;

R3 := x*2+y;

z := x*2+y;

Page 20: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 20

A Complete ProgramLDVAL R6, $0005STORE R6, x[R0]LDVAL R6, $0003STORE R6, y[R0]LOAD R1, x[R0]LOAD R2, y[R0]LDVAL R4, $0002MUL R5, R1, R4ADD R3, R5, R2STORE R3, z[R0]CALL exit[R0]

x DATA $0000y DATA $0000z DATA $0000

stops execution

Page 21: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 21

Program and Data in Memory260000000005000176000002

00150007

00140003260000040003000576000006

11000008

2400000c0015000b1200000a00140009

3352000f6514000e0002000d

7300001000160011e0000012fffd0013

000000140000001500000016

x

y

zThese locations are data,not part of the program.

CALL exit[R0]

LOAD R2, y[R0]

1 2 0 0 0015

calculated by the assembler

some instructions areonly one word long

Page 22: CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed

Lecture 3 CS1Q Computer Systems - Simon Gay 22

Optimizations

• There are ways of improving this program by making it shorter.

• Compilers use a variety of techniques to produce optimized (as good as possible) code.

• We won’t worry about this issue - we’ll just concentrate on a straightforward and systematic translation of simple Ada statements into assembly language.