cs 113 from languages to hardware - swanseacsetzer/lectures/langhardware/02/langdrafthref.pdf · cs...

703
(C) Anton Setzer 2002 (except for pictures) CS 113 From Languages to Hardware http://www-compsci.swan.ac.uk/csetzer/ lectures/02/langhardware/index.html Dr. Anton Setzer http://www-compsci.swan.ac.uk/csetzer/ index.html Michaelmas Term 2002 From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-1

Upload: doandat

Post on 21-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

(C) Anton Setzer 2002 (except for pictures)

CS 113

From Languages to Hardware

http://www-compsci.swan.ac.uk/∼csetzer/lectures/02/langhardware/index.html

Dr. Anton Setzer

http://www-compsci.swan.ac.uk/∼csetzer/index.html

Michaelmas Term 2002

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-1

(C) Anton Setzer 2002 (except for pictures)

Welcome

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-2

(C) Anton Setzer 2002 (except for pictures)

Topics of this Module

• Basic building blocks of the computer: logicgates, memory cells, wires.

• Units built from these (like register units,processors).

• Other Hardware components (monitors, hard disks,. . . )

• Representation and manipulation of data.

• Machine instructions and assembly languages.

• Operating systems (only the basics).

• Translation of high level languages into machineinstructions.

• High performance architectures like superscalar/pipelined architectures and instruction levelparallelism.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-3

(C) Anton Setzer 2002 (except for pictures)

Why this Module?

• Understanding of how hardware works, eg.

– when administrating networks,– when working as system administrator.

– What is a clock cycle?– When moving from 1 GHz to 2 GHz, what’s the

increase in speed?– What is new about the Itanium, and what are

the problems?

• Prerequisite for modules like

– Compilers– Operating systems.– Networking.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-4

(C) Anton Setzer 2002 (except for pictures)

Why this Module? (Cont.)

• High level languages often shield us from details ofthe machine.But not always:

– Especially when working with C, C++.– Pointers, reference parameters.– Strange phenomena when using numbers:∗ overflow⇒ numbers become suddenly negative.∗ 0.2 becomes 0.19999999.∗ signed vs. unsigned integers.∗ coding of bit sequences as numbers.

• Understanding of object oriented programming.

– How does initialization of objects work.

• Understanding of byte code

– Java virtual machine– Microsoft intermediate language (.NET).

• How to speed up programs.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-5

(C) Anton Setzer 2002 (except for pictures)

Who am I?

• Lecturer.

• Nationality: German.

• Worked in

– Swansea and Leeds.– Munich.– Sweden (Gothenburg, Stockholm, Uppsala).– Switzerland (Berne).– Hiroshima (Japan).

• Academic Education: Mathematical Logic.

• Worked in both Mathematics and ComputerScience Departments.

• Main Research topics:

– Proof theory (more mathematics).– Martin-Lof’s Type Theory (more computer

science).∗ Prototype of future programming languages.

– Recent interest: Foundations of object-orientedlanguages

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-6

(C) Anton Setzer 2002 (except for pictures)

Address:Dr. Anton SetzerDept. of Computer ScienceUniversity of Wales SwanseaSingleton ParkSA2 8PPUK

Room Room 211, Faraday Building

Tel. (01792) 513368

Fax. (01792) 295651

Email [email protected]

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-7

(C) Anton Setzer 2002 (except for pictures)

Administrative Information

• Assessment: 100% written examination in January.

• Two lectures per week.

– Monday 13:00, Faraday K.– Wednesday 13:00, Faraday L.

• Web page contains overhead slides from thelectures.Course material will be continually updated.

• Course material always available from student officein the department of computer science, room 206.

– From trays on the left side of the entrance.– If there is none left, ask the secretary to make

additional copies.– If the secretary does not have master copies, ask

me, and I will make new ones.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-8

(C) Anton Setzer 2002 (except for pictures)

Administrative Information (Cont.)

• Please don’t print out lecture notes ondepartmental printers – ask secretaries for copies.Help to save the tax payers money!!– Cost for printing is 10 times the cost of photocopying.

• Contact me if you have questions.

– Preferably in the afternoon.– My usual time table is available from my

homepage.

• Ask questions in lectures.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-8a

(C) Anton Setzer 2002 (except for pictures)

Web Page

• The webpage of the module is located at

http://www-compsci.swan.ac.uk/∼csetzer/langhardware/02/index.html

• For copyright reasons, some of the material ispassword protected. The password is

• The same password applies to related coursematerial.

• The password protection mechanism is very simple,therefore:Please put a link to the password protectedpage on any world-wide accessible page.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-8b

(C) Anton Setzer 2002 (except for pictures)

Recommended Literature

• Module is self-contained:No need to buy/borrow any book – but here aresome recommendations:

• W. Stallings: Computer organization andarchitecture. 6th Ed., Prentice Hall, 2000.Very rich book; main course text.

• D. Patterson and J. Hennessy: ComputerOrganization and Design. 2nd Ed.,Morgan Kaufmann, 1998.Good. Detailed calculations of performance.

• A. S. Tanenbaum: Structured computerorganization. Prentice Hall, 4th Ed., 1999.A little bit easier than the previous books.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-8c

(C) Anton Setzer 2002 (except for pictures)

Other Good Text Books

• J. Hennessy and D. Patterson: Computerarchitecture. A quantitative approach. 3rd Ed.Morgan Kaufmann, 2002.More advanced. Emphasis on calculation ofperformance.

• I. Englander: The architecture of computerhardware and systems software. 2nd Ed., JohnWiley, 2000. Lower level than the other books.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-8d

(C) Anton Setzer 2002 (except for pictures)

Books I haven’t studied yet

• R. Williams: Computer systems architecture. Anetworking approach. Addison-Wesley, 2001.

• C. Hamacher; Z. Vanesic; S. Zaky: ComputerOrganization. 5th Ed., McGraw Hill, 2001.

• Both books seem to be very good.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-8e

(C) Anton Setzer 2002 (except for pictures)

Related Topics

• R. H. Katz: Contemporary logic design.Benjamin/Cummings Publishing Company, 1994.Book on how circuits are actually designed on gatelevel.

• R. White: How computers work. Que,Indianapolis, 6th printing, 2001.Popular science book, illustrated guide on howcomputer components work.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-8f

(C) Anton Setzer 2002 (except for pictures)

Copyright Notice

Most pictures in these slides are copyrighted.They may not be used elsewhere, see the respectivebooks.All figures from Computer Organization and Design:The Hardware/Software Approach, Second Edition, byDavid Patterson and John Hennessy, are copyrightedmaterial (COPYRIGHT 1998 MORGAN KAUFMANNPUBLISHERS, INC. ALL RIGHTS RESERVED).Figures may be reproduced only for classroom orpersonal educational use in conjunction with the bookand only when the above copyright line is included.They may not be otherwise reproduced, distributed,or incorporated into other works without the priorwritten consent of the publisher.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-9

(C) Anton Setzer 2002 (except for pictures)

Exam

• You should aim to understand everything which istaught in this module.

• However, not all details will be relevant for theexam.

• In the revision lecture, the amount of material tobe learned will be cut down.

– No need to learn large lists of details.

• Some material to be understood will as well not berelevant for the exam.

– This will be indicated in the lecture.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-10

(C) Anton Setzer 2002 (except for pictures)

Overview

0. Introduction.

1. Historic Development and Basic Structure.

2. Combinatorial Circuits.

3. Sequential Circuits.

4. Computer Arithmetic and Representation of Data.

5. CPU and Interconnecting Structure.

6. Memory.

7. Machine Languages.

8. Procedures and Functions.

9. Operating Systems.

10. Exception Handling and Input/Output.

11. High Performance Architectures.

12. Revision Lecture.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-11

(C) Anton Setzer 2002 (except for pictures)

:::::::::::::::Levels

:::::::of

::::::::::::::::::::::::::::::Abstraction

1. Low level hardware (electronics).

2. Abstract hardware (digital logic).

3. Machine languages (assembly languages).

4. High-level languages: Java, Pascal, Delphi etc.

5. Application programs: word processors, databasesetc.

In this module:

• Machine languages and abstract hardware.

• A first glimpse at compilation from high levellanguages.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 0 0-12

(C) Anton Setzer 2002 (except for pictures)

1. Historic Developmentand Basic Structure

(a) Mechanical Era.

(b) The First Generation.

(c) Later Developments.

(d) Increase in Performance.

(e) Development of Languages.

(f) Basic Structure.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-1

(C) Anton Setzer 2002 (except for pictures)

(a) The Mechanical Era

• Old tools: Abacus.

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-2

(C) Anton Setzer 2002 (except for pictures)

The Mechanical Era (Cont.)

• Mechanical Calculators: Schickhardt (1623),Pascal (1642), Leibnitz (1673).

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-3

(C) Anton Setzer 2002 (except for pictures)

Punch Cards

• Invented 1801 by Joseph-Marie Jacquard (France)

• For control of patterns of an automatic loom.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-4

(C) Anton Setzer 2002 (except for pictures)

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-5

(C) Anton Setzer 2002 (except for pictures)

Charles Babbage (1791 - 1871)

Picture copied from book not included

Inventor of the first (mechanical) computer (≈ 1835):The

::::::::::::::::::::Analytical

:::::::::::::::::::::Machine.

• Memory.

• Arithmetic and logic unit.

• Punch cards, printer.

• Instructions contain

• Arithmetic and logic operations• Load/Store• Unconditional and conditional jumps.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-6

(C) Anton Setzer 2002 (except for pictures)

Charles Babbage (Cont.)

• Addition 1 second, multiplication 1 minute.

• Construction not carried out before 1991.

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-7

(C) Anton Setzer 2002 (except for pictures)

The Mechanical Area (Cont.)

• George Boole: Development of Boolean Logic.(1854).

• Herman Hollerith

• Construction of a machine based on punch cardsfor evaluation of the 1890 census in US.• Founder of the predecessor of IBM.

• Electromechanical computers.

• Vannevar Bush (MIT): Machine integration anddifferentiation (1925).• Zuse (1935-1938) Computer using binary

arithmetic.• J. A. Atanasoff (1936-1939) Special purpose

computer for solving sets of linear equations.• Mark I (1944). First general purpose electro-

mechanical computer.

• Turing (1936): Theoretical model of a computer.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-8

(C) Anton Setzer 2002 (except for pictures)

(b) The First Generation:Vacuum Tubes

ENIAC (completed 1946)

• 18 000 vacuum tubes.

• 5000 additions per second.

• Programmed through rewiring of connectionsbetween components.

• Decimal digits represented by 10 vacuum tubes.

• Originally developed for calculating ballisticequations for artilleries during world war II.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-9

(C) Anton Setzer 2002 (except for pictures)

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-10

(C) Anton Setzer 2002 (except for pictures)

The von Neumann Machine (1946)

Main

MemoryI/O

Equipment(ALU)Unit

Arithmetic−Logic

UnitControl

• Theoretical model of a computer.

• Basis for most computers developed up to now.

• However, in modern computers, the control unitcan communicate directly with I/O Equipment.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-11

(C) Anton Setzer 2002 (except for pictures)

Main Principlesof the von Neumann Machine

− Stored program-concept. Program in the memory.

− Main components: Memory, ALU, Control unit,I/O

− Both programs and data stored in the samememory.

− Sequential execution.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-12

(C) Anton Setzer 2002 (except for pictures)

(c) Later Developments

• Generation 1 (1945 - 1957)

− Vacuum tubes, Magnetic drums− Machine code, stored programs.− First Computer families (compatibility):

UNIVAC I, UNIVAC II.701/702 family (IBM).

− Performance: 2 KB memory, 10:::::::::::KIPS (Kilo

instructions per second).

• Generation 2 (1958 - 1964)

− Transistors.− High level languages.− Floating point arithmetic.− Performance: 32 KB memory, 200 KIPS.

• Generation 3 (1964 - 1971)

− Integrated circuits.− Semiconductor main memory.− Micro-programming− Multi-programming− Structured programming− 1 MB memory, 1

::::::::::::MIPS (Mega instructions per

second).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-13

(C) Anton Setzer 2002 (except for pictures)

• Generation 4 (1972 - 1977)

−:::::::::::Large

::::::::::::scale

::::::::::::::::::::::::integration

− Networks− CD−

::::::::::::::::::::::::::::::::Object-oriented

::::::::::::::::::::::languages.

−::::::::::::::Expert

:::::::::::::::::systems.

− 8 MB memory, 10 MIPS.

• Generation 5 (1978 - present)− Very large scale integration. (

:::::::::::VLSI)

− 128 - 512 MB memory, 100 MIPS - 500 MIPS.− World wide web.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-14

(C) Anton Setzer 2002 (except for pictures)

(d) Increase in Performance

Picture copied from book not included(Source: Stallings).Note that the scale is exponential.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-15

(C) Anton Setzer 2002 (except for pictures)

Gap between Processor Speed and Memory

Picture copied from book not included

(Source: Stallings).Note that the scale is exponential.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-16

(C) Anton Setzer 2002 (except for pictures)

Performance Increase of Workstations

Picture copied from book not included

Scale: number of times faster than the VAX-11/780Rate of performance: doubling every 1.6 years.(Source: Patterson and Hennessy)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-17

(C) Anton Setzer 2002 (except for pictures)

Moore’s Law

:::::::::::::::::Moore’s

::::::::law (1965):

• Number of transistors per Chip doubles every 18months.

• Similarly we can observe an exponential growth of

– density of memory,– speed of processors,– memory size.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-18

(C) Anton Setzer 2002 (except for pictures)

(e) Development of Languages

•::::::::::::::::::Machine

::::::::::::::::::::::Language.

– Programmed by sequences of zeros and ones.– Only basic operations - e.g. Addition.– A Program which adds memory locations 5 and

6 and stores result in location 7 might look likethis:100011101000101110010110000100110010100101011110

• Slightly more readable: Hexadecimal representation

– Hexadecimal = number system with base 16.A,B,C,D,E,F for digits10,11,12,13,14,15, respectively.

– 4 bits become one hexadecimal digit:1000︸︷︷︸

8

1110︸︷︷︸E

1000︸︷︷︸8

1011︸︷︷︸B

1001︸︷︷︸9

0110︸︷︷︸6

0001︸︷︷︸1

0011︸︷︷︸3

0010︸︷︷︸2

1001︸︷︷︸7

0101︸︷︷︸5

1110︸︷︷︸E

– this reads:8E 8B 9613 29 5E

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-19

(C) Anton Setzer 2002 (except for pictures)

Assembly Languages

• Assembly languages = symbolic language torepresent machine programsExample (above mentioned program):ADD (M5),(M6)STORE (M7)

•:::::::::::::::::::::Assembler= program to translate symboliclanguage to machine language.

– Direct translation (no optimization steps).

• Programs written in symbolic notation =

::::::::::::::::::assembly

::::::::::::::::::::language

:::::::::::::::::::::programs or

::::::::::::::::::::assembler

::::::::::::::::::::::programs.

• Addition of comments.

• Example:ADD (M5),(M6) ;Add the SUBTOTAL and

;the EXTRA value.STORE (M7) ;Store result in TOTAL.

• Assembler dominant for years. Lots of programsstill in assembler.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-20

(C) Anton Setzer 2002 (except for pictures)

High Level Languages

Next step – higher level of abstraction:

• First high-level language: FORTRAN, 1950s:TOTAL= SUBTOTAL + EXTRA.

• Meaningful maths-based syntax.

• Meaningful variable names.

• Other languages followed:Cobol (some predecessors), Lisp, Algol.

• Now lots of languages.

• Pascal – designed for teaching 1970;Structured programming.Above example:total:= subtotal + extra;

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-21

(C) Anton Setzer 2002 (except for pictures)

High Level Languages (Cont.)

• Even higher language paradigms:

– Object orientation, (Java, C++, . . . ).– Functional programming (Haskell,

Lisp, ML, . . . ).– Logic programming (Prolog).– Database languages (SQL, XML and XSLT,

. . . ).– Future: aspect-oriented languages?

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-22

(C) Anton Setzer 2002 (except for pictures)

Compilers

•:::::::::::::::::::::::::Compilation = translation of high-level languageinto machine language.

•::::::::::::::::::Compiler= program for compilation.

• Steps of a compiler (see module on compilers):

– Lexical analysis.– Syntax analysis.– Semantics analysis.– Code generation.– Optimisation.

•:::::::::::::::::::::::Interpreted

:::::::::::::::::::::::languages: Translation and execution

in one step.

– Faster development of programs.– Slow execution.– Useful when using scripting languages,

(useful for short programs)eg. Tcl/Tk, Awk, Perl, Python, Ruby . . .

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-23

(C) Anton Setzer 2002 (except for pictures)

Compilers (Cont.)

• Compilation/interpretation not one-to-one.

• Early efficiency worries.

• Old aim in design of machine languages:make it easy for human assembly programmers.

• New aim – make it easy for compilers.

⇒ Languages change machines!

• On modern machines (esp.:::::::::::RISC) difficult to write

assembly programs.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-24

(C) Anton Setzer 2002 (except for pictures)

(f) Basic Structure

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-25

(C) Anton Setzer 2002 (except for pictures)

Components of a Desktop Computer

• Box, keyboard, mouse, monitor, peripherals

• Box contains

–:::::::::::::::::::::::::::Motherboard∗ Memory and processor – directly or via slots.∗ More slots for· Extra memory (various types)· Expansion cards:

- Graphics cards- Peripheral interfaces.- Networking.-

::::::::PCI -

::::Peripheral

:::Component

::Interconnect.

-::::::::::AGP

::::Accelerated

:::Graphics

:::Port.

-::::::::ISA

::Industrial

:::Standard

:::Architecture bus.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-26

(C) Anton Setzer 2002 (except for pictures)

Components of a Desktop Computer(Cont.)

• Box contains also

– Power supply, fan, drives (hard disk, CD, floppy)

• Main focus in this module:

– Memory and processor.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-27

(C) Anton Setzer 2002 (except for pictures)

A Motherboard

• 1 = Ports / 2 = ISA slots

• 3 = PCI slots

• 4 = AGP slots

• 5 = CPU slots

• 6 = Chipset (Northbridge)

• 7 = Power connector

• 8 = Memory sockets

• 9 = I/O connectors

• 10 = Battery

• 11 = Chipset (Southbridge)

• 12 = BIOS chip

Picture can be found hereFrom languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-28

(C) Anton Setzer 2002 (except for pictures)

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-29

(C) Anton Setzer 2002 (except for pictures)

Embedded Systems

• Most processors not in desktops.

– They are in:::::::::::::::::::::embedded

::::::::::::::::::systems, eg.

cars, mobiles, planes, domestic appliances,medical equipment, industrial plants.

– Even:::::::::::smart

::::::::::::cards.

– Relatively low-powered.

• As well::::::::::main

:::::::::::::::frames.

• In this module main focus on desktop computers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 1 1-30

(C) Anton Setzer 2002 (except for pictures)

2. Boolean Logicand Combinatorial Circuits

(a) Combinatorial Circuits.

(b) Boolean logic.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-1

(C) Anton Setzer 2002 (except for pictures)

(a) Combinatorial Circuits

Main principle of a computer:Information represented by voltage on/off orpositive/negative.

• voltage on or voltage positive means 1 or true,voltage off or voltage negative means 0 or true.(The physical representation can be interchanged).

The following parts are used for internal manipulationof information:

• Voltage + ground.

• Clock.

• Capacitor. Temporary storage.(Needs regular refresh and refresh after informationtaken).

• Logic Gates.

• Wires.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-2

(C) Anton Setzer 2002 (except for pictures)

Logic Gates

Built using transistors and diodes.Transistor = electronic version of a relay.A

:::::::::relay is a electromagnetic switch:

spring

Solenoid 01

10

::::::::::::::::::::::“Negating

:::::::::::::::Relay”

::::::::::::::::::::p-channel

:::::::::::::::::::::::transistors operate like this.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-3

(C) Anton Setzer 2002 (except for pictures)

“Positive Relay”

1 0

1 0

::::::::::::::::::::n-channel

:::::::::::::::::::::::transistors operate like this.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-4

(C) Anton Setzer 2002 (except for pictures)

::::::::::::::::::::::NOT-Gate (

::::::::::::::::Inverter)

x

x

x

Symbol:

xnegating

relay

x ¬x0 11 0

• Often x written for ¬x.

• Don’t forget the circle in the symbol for NOT!!Without a circle, one obtains a buffer(output = input).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-5

(C) Anton Setzer 2002 (except for pictures)

::::::::::::::::::::::AND-Gate

x y

x /\ y

x /\ yx

y

Positive Relays

x y x ∧ y0 0 00 1 01 0 01 1 1

Often xy or x · y written for x ∧ y.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-6

(C) Anton Setzer 2002 (except for pictures)

Relationship toNatural Language “And”

• “And” corresponds to natural language “and”:Assume the sentence

– “we are in Swansea and it is raining”.

This sentence is true, if both

– “we are in Swansea” and– “it is raining”

get truth values “true”. If

– the truth value of “we are in Swansea” is x, and– the truth value of “it is raining” is y,

and one looks at the truth table of ·, one sees thatthe truth value of

– “we are in Swansea and it is raining”,

is x · y. Therefore · formalizes the way truth valuesare assigned to sentences linked by “and”.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-6a

(C) Anton Setzer 2002 (except for pictures)

::::::::::::::::::OR-Gate

x

x \/ y

x

y

x \/ y

y

relayspositive

x y x ∨ y0 0 00 1 11 0 11 1 1

Often x+ y written for x ∨ y.From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-7

(C) Anton Setzer 2002 (except for pictures)

Relationship toNatural Language “Or”

• “Or” corresponds to natural language “or”:Assume the sentence

– “we are in Swansea or it is raining”.

This sentence is true, if at least one of

– “we are in Swansea” and– “it is raining”

get truth value “true” (one often mixes this upwith the either . . . or. If both are true, then thetruth value of the result is true, which would befalse if we applied either or).

If

– the truth value of “we are in Swansea” is x, and– the truth value of “it is raining” is y,

and one looks at the truth table of +, one seesthat the truth value of

– “we are in Swansea and it is raining”,

is x + y. Therefore + formalizes the way truthvalues are assigned to sentences linked by “or”.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-7a

(C) Anton Setzer 2002 (except for pictures)

NOR Gate

:::::::::::NOR =

::::NOT

:::::::OR

=

x

y

x + y¬ (x + y)

x

y

¬ (x + y)

x y x+ y0 0 10 1 01 0 01 1 0

Exercise: Verify that this is the same as x · y:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-8

(C) Anton Setzer 2002 (except for pictures)

NAND Gate

::::::::::::::NAND =

::::NOT

:::::::::::AND

¬ (x y)x

y

x

y

x y¬ (x y)

=

x y x · y0 0 10 1 11 0 11 1 0

Exercise: Verify that this is the same as x+ y:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-9

(C) Anton Setzer 2002 (except for pictures)

Abbreviations

=

• © means always negation.

• Gates with more than two inputs:

xy

z

x(yz) = (xy)z =: xyz

– Similarly for +.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-10

(C) Anton Setzer 2002 (except for pictures)

Combination of Gates

With a combination of gates, more complicatedfunctions can be represented:(• means wires are connected.Crossing without • means crossing of wires with noconnection).

x

y

z

u

v

w

u = xyv = u+ zw = uv

Inputs ComputedValues

x y z u v w0 0 0 0 0 00 0 1 0 1 00 1 0 0 0 00 1 1 0 1 01 0 0 0 0 01 0 1 0 1 01 1 0 1 1 11 1 1 1 1 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-11

(C) Anton Setzer 2002 (except for pictures)

Exercise

• Simplify the last circuit (and further circuitspresented in this section).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-11a

(C) Anton Setzer 2002 (except for pictures)

(b) Boolean Logic

•:::::::::::::::::Boolean

::::::::::::value = value true or false.

• Often (and in this module) written as 1 (= true)and 0 (= false).

•:::::::::::::::::Boolean

:::::::::::::::::::::::::connectives

::

– not (¬), and (∧), or (∨).

•:::::::::::::::::Boolean

:::::::::::::terms = expressions formed from

– variables,– 0, 1,– Boolean connectives ¬, ∧, ∨.

• We write

– x for ¬x (except in some pictures),– · for ∧,– + for ∨,– even xy for x · y.

• Examples of Boolean terms:(xy) + 1, x+ y, x+ y.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-12

(C) Anton Setzer 2002 (except for pictures)

Boolean Terms

• Order of precedence

– As in arithmetic: · (∧) binds more than + (∨).E.g. xy + z = (xy) + z.

– ¬ binds more than other connectives.E.g. ¬x+ y · z = (¬x) + (y · z).

– Since (x+ y) + z and x+ (y+ z) have the sameresult for choices of x, y, zsimilarly for (xy)z and x(yz)we can omit parenthesis in products and sums.

• Defined connectives

– x NAND::::::::::::::::

y := xy (= ¬(x ∧ y)).

– x NOR:::::::::::

y := x+ y (= ¬(x ∨ y)).

– x XOR:::::::::::

y := xy + xy.

::::::::::XOR =

::::::::::::::::::exclusive

:::::or: either x or y

(x or y but not both).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-13

(C) Anton Setzer 2002 (except for pictures)

Representation of Circuits by Boolean Terms

• A Boolean term corresponds to a circuit with inputsfor each variable in it.

• Example: (xy + xy) + z represents the followingcircuit:

yx

z

xy ¬ (xy)

xy + ¬ (xy) + z

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-14

(C) Anton Setzer 2002 (except for pictures)

Optimization of Circuits

• If the same term occurs twice in a Boolean term,we can construct a circuit which uses this subtermdirectly twice.

• Example:In xy(xy + z), xy occurs twice.xy(xy + z) can be implemented by the followingcircuit:

z

y

x xy

xy+z

xy(xy+z)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-15

(C) Anton Setzer 2002 (except for pictures)

Combinatorial Circuits and Boolean Algebras

• A::::::::::::::::::::::::::::combinatorial

::::::::::::::circuit is a circuit which can be

represented by a Boolean term.

• This means that we can systematically construct itfrom the inputs by connecting inputs of gates tothe outputs of already introduced elements.

• Therefore we can calculate its output from theinputs by systematically calculating the results forall intermediate steps.

• A circuit which has a loop is non-combinatorial.

• The circuit on the last slide was combinatorial.

• The following circuit is not combinatorial(try to represent it by a Boolean term):

x

y

y = x+ y, a recursive equation.From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-16

(C) Anton Setzer 2002 (except for pictures)

Boolean-Valued Functions

A Boolean valued function is given by a truth table:

x y z f(x,y,z)0 0 0 00 0 1 10 1 0 00 1 1 11 0 0 01 0 1 01 1 0 11 1 1 0

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-17

(C) Anton Setzer 2002 (except for pictures)

Boolean-Valued Functions (Cont.)

A Boolean term in variables x, y, z represents afunction depending on x, y, z.E.g. xy + z represents the function with truth table(an additional column for the intermediate term xyhas been inserted):

x y z xy f(x,y,z)=xy+z0 0 0 0 00 0 1 0 10 1 0 0 00 1 1 0 11 0 0 0 01 0 1 0 11 1 0 1 11 1 1 1 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-18

(C) Anton Setzer 2002 (except for pictures)

Representation of Boolean-Valued Functions

Any Boolean valued function can be represented bya Boolean term (and therefore by a combinatorialcircuit).Consider the following example:

x y z f(x,y,z)0 0 0 10 0 1 00 1 0 10 1 1 01 0 0 01 0 1 01 1 0 01 1 1 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-19

(C) Anton Setzer 2002 (except for pictures)

Representation of Boolean-ValuedFunctions (Cont.)

Now take all rows, where f(x, y, z) has value 1.

x y z f(x,y,z)0 0 0 10 0 1 00 1 0 10 1 1 01 0 0 01 0 1 01 1 0 01 1 1 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-20

(C) Anton Setzer 2002 (except for pictures)

Representation ofBoolean-Valued Functions (Cont.)

For each row in which f(x, y, z) = 1, we construct aterm, by taking the

::::::::::::::::::::::::conjunction (= the “and”) of

• x, if the argument x is 1 in this row,x, if the argument x is 0 in this row,

• y, if the argument y is 1 in this row,y, if the argument y is 0 in this row,

• similarly for z

In the example we get:

x y z f(x,y,z) introduced term0 0 0 1 x · y · z0 0 1 00 1 0 1 x · y · z0 1 1 01 0 0 01 0 1 01 1 0 01 1 1 1 x · y · z

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-21

(C) Anton Setzer 2002 (except for pictures)

Representationof Boolean-Valued Functions (Cont.)

Now take the::::::::::::::::::::::disjunction (= the “or”) of all these

terms:

x y z f(x,y,z) introduced term0 0 0 1 x · y · z +0 0 1 00 1 0 1 x · y · z +0 1 1 01 0 0 01 0 1 01 1 0 01 1 1 1 x · y · z

The resulting term is x · y · z + x · y · z + x · y · zThis term represents the function f(x, y, z).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-22

(C) Anton Setzer 2002 (except for pictures)

Why is this Correct?

The constructed intermediate terms are 1 if and onlyifthe truth values of the variables are exactly those inthe row, from which the term was derived:For instance for the row with entries

x = 0, y = 1, z = 0,

the corresponding intermediate term is

xyz

This term is 1 if and only if

x = 1 and y = 1 and z = 1

which is if and only if

x = 0 and y = 1 and z = 0

ie. if x, y, z have the values for this row.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-25

(C) Anton Setzer 2002 (except for pictures)

• Now the resulting term is the disjunction of theseintermediate terms:

• It is 1 if and only if

one of the intermediate terms is 1 which is if andonly if

(x, y, z) is one of the triples for which f(x, y, z) = 1ie. if and only if

f(x, y, z) = 1

• and it is 0 otherwise, i.e. if and only if

f(x, y, z) 6= 0 i.e. if and only if

f(x, y, z) = 0.

• So the resulting term and f(x, y, z) have the samevalue for all choices of x, y, z.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-26

(C) Anton Setzer 2002 (except for pictures)

Disjunctive Normal Form

In logic, the resulting term is called the

::::::::::::::::::::::disjunctive

:::::::::::::::normal

::::::::::::form for this Boolean function.

This is because the result is a

• disjunction (::::::::::::::::::::::disjunction means “an or”)

• of a conjunction (::::::::::::::::::::::::conjunction means “an and”)

• of terms x or x; y or y; z or z.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-27

(C) Anton Setzer 2002 (except for pictures)

Example

The above mentioned function f is represented by

x · y · z + xyz + xyz

which corresponds to the circuit

yx z(¬ x )(¬ y)(¬ z)

(¬ x)y(¬ z)

xyz

f(x,y,z)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-28

(C) Anton Setzer 2002 (except for pictures)

Exercise

• Take any other Boolean valued functions with3 arguments by choosing arbitrary values forf(x, y, z).Compute its disjunctive normal form (ie. a Booleanterm representing it as defined above).

• (Typical exam question).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-28a

(C) Anton Setzer 2002 (except for pictures)

Logic Array

•:::::::::::Logic

::::::::::::array with inputs x, y z is a circuit

– which represents all possible intermediate termsfor arbitrary functions

– i.e. all products of x vs. x, y vs. y, z vs. z.– all connected with an or-gate to the output– but with possibility to interrupt connections from

the and-gates to the or-gate.

• By interrupting appropriate connections allpossible functions can be represented.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-29

(C) Anton Setzer 2002 (except for pictures)

x y zPossibility to interrupt connection

(¬ x )(¬ y)(¬ z)

(¬ x )(¬ y )z

x (¬ y)z

x y(¬ z)

x y z

(¬ x) y (¬ z)

(¬ x) y z

x (¬ y )(¬ z)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-30

(C) Anton Setzer 2002 (except for pictures)

Example: f as above

x y z

Interrupted connection

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-31

(C) Anton Setzer 2002 (except for pictures)

Equality between Boolean Terms

• We say: two Boolean terms are equal, if they forall choices of truth values for variables occurring inthem both have the same value.

• In order to verify that two terms are equal, builda truth table (depending on all variables occurringin one of the terms) for both terms, and show thattheir results are the same.

• Example 1: Verification of x+ y = y + x:

x y x+ y y + x0 0 0 00 1 1 11 0 1 11 1 1 1

• Example 2: Verification of x · x = 0:

x xx 00 0 01 0 0

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-32

(C) Anton Setzer 2002 (except for pictures)

Laws of a Boolean Algebra

• Simplification of Boolean terms (and theircorresponding circuits) by using equations betweenBoolean terms.

• = means: for all choices of x, y, z . . . the value ofthe term is the same.

• The following laws are due to Boole:

–::::::::::::::::::::::::::::::::Commutativity:∗ x+ y = y + x,∗ xy = yx.

–:::::::::::::::::::::::::::Associativity:∗ x+ (y + z) = (x+ y) + z,∗ x(yz) = (xy)z.

–:::::::::::::::::::::::::::Distributivity:∗ x(y + z) = (xy) + (xz),∗ x+ (yz) = (x+ y)(x+ z).

–::1

:::::is

::::::::::::::::neutral

::::::::for

::::·,

::::0

::::::::::::::::neutral

:::::::for

:::::+:

∗ x1 = x,∗ x+ 0 = x.

–:::¬

:::::is

::::::::::::::::inverse

:::::::for

:::::+

:::::::::and

:::·:

∗ x+ x = 1,∗ xx = 0.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-33

(C) Anton Setzer 2002 (except for pictures)

Laws of a Boolean Algebra (Cont.)

• Remark: The following laws can be derived fromthe above ones:

x+ x = x xx = x (:::::::::::::::::::::::::::idempodence)

x+ 1 = 1 x · 0 = 0x = xand the de Morgan laws:x+ y = x · y x · y = x+ y

• Based on the laws of an Boolean algebra there aregood techniques for simplifying circuits (see e.g.Stallings).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-34

(C) Anton Setzer 2002 (except for pictures)

Exercise

• Verify some or all laws of a Boolean algebra.

• Derive the derived laws from the laws of a Booleanalgebra directly (without using truth tables).

– For deriving x + x = x, derive 1 = 1 + 1 andthen use

x = x · 1 = x · (1 + 1) .

For deriving 1 = 1 + 1 derive first

1 = 1 + 1 · 1

and use the second distritutive law.– x = x is slightly tricky, start with

x = x(x+ x) .

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-34a

(C) Anton Setzer 2002 (except for pictures)

Exercise (Cont.)

• For derivingx+ y = x · y ,

derive firstx · y(x+ y) = 0

andxy + (x+ y) = 1

Now start with

x+ y = x+ y · (x+ y + x · y) .

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 2 2-34b

(C) Anton Setzer 2002 (except for pictures)

3. Sequential Circuits

(a) Why we need Sequential Circuits.

(b) Latches and Flip-Flops.

(c) How Data is Moved.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-1

(C) Anton Setzer 2002 (except for pictures)

(a) Why we need Sequential Circuits

Example:

x

y

z

u

v

w

Represented program:

u := xyv := u+ zw := uv

Combinatorial circuits allow to define

• arbitrary functions,

• therefore complex programs,

• but no loops.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-2

(C) Anton Setzer 2002 (except for pictures)

Representation of Loops

Consider the following (useless) toy program:(jmp = jump = goto)

loop : x := ¬xjmp loop

First Unsuccessful Attempt

x yz

Unclear, what is meant by the connection of x, y, z.Therefore not a correct way of drawing circuits.Correct diagram: Use an orgate.

x

z

y

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-3

(C) Anton Setzer 2002 (except for pictures)

Behaviour for x=1:

• y is constant 1.

• z is constant 0.

⇒ Not intended behaviour.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-4

(C) Anton Setzer 2002 (except for pictures)

Orgate = Union of Flows

• If a bit arrives in one of the two connecting rivers,one gets a bit at the output.

• However, if one bit arrives in both rivers, we onlyget one bit at the output.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-4a

(C) Anton Setzer 2002 (except for pictures)

Modifications

x

z

uM

Sy

1. Insertion of switch S.

2. Separation into program steps: cycles.However: output of the not-gate z should be fedinto S at the next cycle.

3. Therefore:Insertion of memory element M.

S outputs x at cycle 1,u at later cycles.

M stores input andoutputs it in the next cycle.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-5a

(C) Anton Setzer 2002 (except for pictures)

Modifications

x

z

S

u

y

M

1. Insertion of switch S.

2. Separation into program steps: cycles.However: output of the not-gate z should be fedinto S at the next cycle.

3. Therefore:Insertion of memory element M.

S outputs x at cycle 1,u at later cycles.

M stores input andoutputs it in the next cycle.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-5b

(C) Anton Setzer 2002 (except for pictures)

Modifications

x

z

S

u

y

M

1. Insertion of switch S.

2. Separation into program steps: cycles.However: output of the not-gate z should be fedinto S at the next cycle.

3. Therefore:Insertion of memory element M.

S outputs x at cycle 1,u at later cycles.

M stores input andoutputs it in the next cycle.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-5c

(C) Anton Setzer 2002 (except for pictures)

Modifications

x

z

S

u

y

M

1. Insertion of switch S.

2. Separation into program steps: cycles.However: output of the not-gate z should be fedinto S at the next cycle.

3. Therefore:Insertion of memory element M.

S outputs x at cycle 1,u at later cycles.

M stores input andoutputs it in the next cycle.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-5d

(C) Anton Setzer 2002 (except for pictures)

Behaviour for x = 1:

Cycle x y z u

1 1 → 1 → 0

2 0

0 → 1↘

3 1

1 → 0↘

4 0

0 → 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-6

(C) Anton Setzer 2002 (except for pictures)

Behaviour for x=1 (Cont)

• M needs to store one bit and make use of it in thenext cycle.

• S needs to store as well one bit, containing theinformation, whether it is in the first or a laterclock cycle.

• We need a circuit which can store bits.

• Done using latches and flip flops.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-7

(C) Anton Setzer 2002 (except for pictures)

(b) Latches and Flip-Flops

An:::::::S-R

::::::::::::::Latch is the following circuit:

S

R Q

Q’

Q = R+Q′.Q′ = S +Q.

• Non-combinatorial because of feed-back loop.

• Input: R, S (kept fixed).Output: Q, Q’.

• Full analysis hard (mathematical technique used:differential equations).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-8

(C) Anton Setzer 2002 (except for pictures)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-9

(C) Anton Setzer 2002 (except for pictures)

Calculation of Movementsof Two Fighters

• In reality, two fighters react continously over timedepending on their own movement and that of theother.

• We can approximate this as follows:

– Divide time into discrete steps.– Steps of time are now numbered 0,1,2,3, . . .– Now assume, each figher makes at time k + 1

one movement depending on∗ his posture (state)∗ and that of the other fighter∗ both at time k.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-10

(C) Anton Setzer 2002 (except for pictures)

Calculation of the Behaviourof an S-R-Latch

• Similarly as for the fighters, when analyzing anS-R-latch, we divide time into discrete steps.

• Steps correspond to the gate delay.

– Time it takes for the gate to change its outputdepending on a new input.

– We can assume that this delay is the same forall gates (almost true).

• So we assume that at time k + 1, the output of agate is calculated depending on its inputs at timek.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-11

(C) Anton Setzer 2002 (except for pictures)

S

R Q

Q’

•::::::::::::::::::::Notation:

– Let Q(k) be the value of Q in the kth step.– Let Q′(k) be the value of Q′ in the kth step.

• Assume R, S constantly for some time.

• Then

– Q(k + 1) = R NOR Q′(k) = R+Q′(k).– Q′(k + 1) = S NOR Q(k) = S +Q(k).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-12

(C) Anton Setzer 2002 (except for pictures)

• The above equations don’t define Q(0) and Q′(0).

• When the circuit is switched on, Q(0) and Q′(0)are unknown.

– By applying reset to it (R = 0, S = 1 asdiscussed below) one obtains a defined state.

• At later stages, Q and Q′ will initially have thevalue obtained by the previous input.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-12a

(C) Anton Setzer 2002 (except for pictures)

Case R = 0, S = 1 (Set)

• Assume R = 0, S = 1 constant for some time.

• Initially Q(0), Q′(0) are assumed to be unknown(indicated by ?).

• ThenQ(k + 1) = R+Q′(k) = 0 +Q′(k) = Q′(k).Q′(k + 1) = S +Q(k) = 1 +Q(k) = 1 = 0.So:

• Q′(1) = 0.

• Q′(2) = 0, Q(2) = Q(1) = 0 = 1.

• Q′(3) = 0, Q(3) = Q(2) = 0 = 1.

• The following table shows the values of Q, Q′, R,S after 0, 1, 2, 3 gate delays (steps):

Step 0 1 2 3R 0 0 0 0S 1 1 1 1Q(k) ? ? 1 1 constantQ′(k) ? 0 0 0 constant

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-13

(C) Anton Setzer 2002 (except for pictures)

Case R = 0, S = 1 (Cont.)

• From step 3 onwards, Q(k), Q′(k) remain constant.

• So whatever Q, Q′ initially are, Q′ become aftersome time 0, Q becomes 1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-13a

(C) Anton Setzer 2002 (except for pictures)

Case R = 1, S = 0 (Reset)

• Q(k + 1) = R+Q′(k) = 1 +Q′(k) = 1 = 0.

• Q′(k + 1) = S +Q(k) = 0 +Q(k) = Q(k).

• We get the table:

Step 0 1 2 3R 1 1 1 1S 0 0 0 0Q(k) ? 0 0 0 constantQ′(k) ? ? 1 1 constant

• Whatever Q,Q′ initially are, Q becomes 0,then Q′ becomes 1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-14

(C) Anton Setzer 2002 (except for pictures)

Case R = 0, S = 0, Stable Case

• Assume initially

– Q(0) = 0, Q′(0) = 1– or Q(0) = 1, Q′(0) = 0.

• Then Q(0) = Q′(0).Q(k + 1) = R+Q′(k) = 0 +Q′(k) = Q′(k).Q′(k + 1) = S +Q(k) = 0 +Q(k)= Q(k). So:

• Q(1) = Q′(0) = Q(0) = Q(0),Q′(1) = Q(0),

• Q(2) = Q′(1) = Q(0) = Q(0),Q′(2) = Q(1) = Q(0).

• We get the table:

Step 0 1R 0 0S 0 0Q(k) Q(0) Q(0) constant

Q′(k) Q(0) Q(0) constant

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-15

(C) Anton Setzer 2002 (except for pictures)

Case R = 0, S = 0, Stable Case (Cont.)

• Q, Q′ keep (store) their initial value.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-15a

(C) Anton Setzer 2002 (except for pictures)

R = 0, S = 0, Unstable Case

• Assume initially Q(0) = 0, Q′(0) = 0.

• AgainQ(k + 1) = Q′(k), Q′(k + 1) = Q(k).

• Q(1) = Q′(0) = 0 = 1,Q′(1) = Q(0) = 0 = 1.

• Q(2) = Q′(1) = 1 = 0,Q′(2) = Q(1) = 1 = 0.

• We get the table:

Step 0 1 2 3R 0 0 0 0S 0 0 0 0Q(k) 0 1 0 1 · · ·Q′(k) 0 1 0 1 · · ·

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-16

(C) Anton Setzer 2002 (except for pictures)

R = 0, S = 0, Unstable Case (Cont.)

• The latch oscillates (toggles) between 1 and 0.

• Same behaviour if initially Q(0) = 1, Q′(0) = 1.

• In reality, the delay of the two gates is not exactlythe same.

– Therefore the gates are not exactly syncronized.– After some toggling steps, it will be the case

that one is 0 while the other is 1.– Then the latch stabilizes.– But in which state, is unpredictable.

Q

Q’

Time

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-16a

(C) Anton Setzer 2002 (except for pictures)

R = 1, S = 1: Forbidden Case

• Q(k + 1) = R+Q′(k) = 1 +Q′(k) = 1 = 0.Q′(k + 1) = S +Q(k) = 1 +Q(k) = 1 = 0.

Step 0 1 2R 1 1 1S 1 1 1Q ? 0 0 constantQ′ ? 0 0 constant

• Q, Q′ become both 0.

• If we then release R, S, ie. set R = S = 0, weobtain the unstable case.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-17

(C) Anton Setzer 2002 (except for pictures)

S-R-Latch as a Memory Element

• If the output is Q = 0, Q′ = 1 or Q = 1, Q′ = 0,the latch is in a stable state.

• A stable state can be achieved by initially settingR = 1, S = 0, long enough.

• Q = 1, Q′ = 0 means memory value 1,Q = 0, Q′ = 1 means memory value 0.

• If R = S = 0 in a stable state,the memory value is preserved.

• If we set R = 0, S = 1 in any state,we obtain memory value 1.(S =

:::::::Set).

• If we set R = 1, S = 0 in any state,we obtain memory value 0.(R =

::::::::::::Reset).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-18

(C) Anton Setzer 2002 (except for pictures)

Unstable State of an S-R-Latch

• If the output is

– Q = 0, Q′ = 0 or– Q = 1, Q′ = 1,

the latch is in a potentially unstable state.

• If R = S = 0 in this state,the latch becomes unstable and oscillates(toggles).This should be avoided.

• If we set R = S = 1 in any state,we obtain such a potentially unstable state.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-19

(C) Anton Setzer 2002 (except for pictures)

Exercise

Analyze the following circuit in a simlar way:

S

R Q

Q’

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-20

(C) Anton Setzer 2002 (except for pictures)

D-Flip-Flop

clock

D

R

Q’S

Q

S−R−latchCombinatorialCircuit

• D-flip-flop consists of a combinatorial circuit andan S-R-latch.

• If the clock is 1 then:

– If D = 1, then R = 0, S = 1, and therefore inthe S-R latch memory value 1 is stored.

– If D = 0, then R = 1, S = 0, and therefore inthe S-R latch memory value 0 is stored.

• If clock is 0, then independently of D we haveR = 0, S = 0, and the S-R-latch keeps thereforeits previous value.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-21

(C) Anton Setzer 2002 (except for pictures)

Clock Cycles

• The clock signal is a signal, which periodicallyalternates between 0 and 1.Generated by an external clock.

• The clock signal will be slower than the time ittakes for S-R-latches to stabilize (ie. for Q, Q’to become constant), if we are not in an unstablesituation.

– For practical purposes one tries to make theclock as fast as possible.

– As an approximation, we assume that we canneglect the time it takes for the S-R-latches tostabilize.

10

10

Time

Clock

S−R−latch

Q

Q’

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-22

(C) Anton Setzer 2002 (except for pictures)

Clock Cycles (Cont.)

• By one clock cycle we mean the period

– starting when the clock signal becomes 1– ending when the clock signal is 0 and is just

about to switch back to 1 again.

• By half a clock cycle we mean the period when theclock cycle is constant either 1 or 0.

Clock ︸ ︷︷ ︸ ︸ ︷︷ ︸clock clock

cycle 1 cycle 2︸ ︷︷ ︸ ︸ ︷︷ ︸12 clock 1

2 clockcycle cycle

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-23

(C) Anton Setzer 2002 (except for pictures)

Clock Cycles (Cont.)

• By “at clock cycle 1 signal R is 0” we mean:

– when the clock is the first time 1, R should be0.

– This remains constant as long as the clock is 1.– What happens when the clock is 0 doesn’t

matter.

• By “at second half clock cycle S is 0” we meanthat as long as this half clock cycle persists, S is 0.

Clock

The following signal is 1 at clock cycle 1:

The following signal is as well 1 at clock cycle 1:

The following signal is 1 at the 2nd half clock cycle:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-24

(C) Anton Setzer 2002 (except for pictures)

Behaviour of a D-Flip-Flop

• Assume clock alternates between 1 and 0.

• Assume further that clock signal lasts much longerthan the time needed for the S-R-latch to stabilize.

• The following table illustrates the value of Q, Q′

in response to a (random) sequence of input valuesfor D:

Clock 1 0 1 0 1 0 1 0D 1 1 0 1 0 0 1 1Q 1 1 0 0 0 0 1 1Q’ 0 0 1 1 1 1 0 0

• So Q adapts to the value of D when the clock is 1,and stores this value, independent of D, when theclock is 0.

• Q’ is the negation of Q.

• Note that Q, Q’ react on D with a short (hereneglected) delay.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-25

(C) Anton Setzer 2002 (except for pictures)

Symbol

The following symbol denotes a D-Flip-Flop, withinput Ck (for clock) and D and output Q and Q′.

D

CkQ’

Q

Symbol:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-26

(C) Anton Setzer 2002 (except for pictures)

(c) How Data is Moved

A Storage Cell

D Enable

D

CkClock

Q’

Q

• If Enable = 1 and Clock = 1, then Q is updated tothe value of D.

• If Enable = 0 the value of Q is kept.

• If Clock = 0, the value of Q is kept as well.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-27

(C) Anton Setzer 2002 (except for pictures)

A Falling Edge D-Flip-Flop

D D’ Q

Q’

Clock

• Behaviour, for alternating clock signal:

– If Clock = 1, then D′ is updated to D.– If Clock then switches to 0, then∗ Value of D′ remains the same.∗ Further Q is updated to D′,· which is the value of D when clock was 1.

– If Clock then switches back to 1 again, then∗ Value of Q remains the same.

– Therefore the output at Q, when the clock is 1,is the input of D when clock was previously 1.∗ which means at the previous clock cycle.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-28

(C) Anton Setzer 2002 (except for pictures)

• The following table illustrates the behaviour of afalling edge D-flip-flop in response to a (random)sequence of inputs on D:

Clock 1 0 1 0 1 0 1 0 1D 1 0 0 1 0 0 1 1 0D’ 1 1 0 0 0 0 1 1 0Q ? 1 1 0 0 0 0 1 1

• The falling edge D-flip-flop is a buffer, which delaysits output by one clock cycle.

– The output arrives already after half a clockcycle, but is not stable from the beginning.

– When the clock is 1 again, the output is stableand repeats the input from the previous clockcycle.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-29

(C) Anton Setzer 2002 (except for pictures)

Moving Bits Around

D D’

Clock

SignalQ

Q’

• If Signal is 1, the value saved in the first flip-flop isstored the second flip flop and can be read off atQ in the next clock cycle.

• If Signal is 0, the second flip flop keeps its value.

• Output at D′ could be fed into several flip flops,each with an individual control signal.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-30

(C) Anton Setzer 2002 (except for pictures)

Output of Last Circuit

• D’ follows D when clock = 1 and keeps this valueif clock = 0.

• Q updates to D’ when clock = 0, provided Signal= 1.If clock = 1 or Signal = 0, it keeps the previousvalue

Clock 1 0 1 0 1 0 1 0 1D 1 1 0 0 0 0 1 1 0Signal 0 1 0 0 1 1 0 0 1D’ 1 1 0 0 0 0 1 1 0Q ? 1 1 1 1 0 0 1 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-30a

(C) Anton Setzer 2002 (except for pictures)

Multiplexer

CB

A

S

A

CB

C

S = 0 S = 1

A

B

• A multiplexer is a combinatorial circuitcorresponding to the Boolean term

C = AS +BS

• If S = 1, thenC = A1 +B · 1 = A · 0 +B · 1 = B.

• If S = 0 thenC = A0 +B · 0 = A · 1 +B · 0 = A.

• So:if S = 0, the output is A,if S = 1, the output is B.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-31

(C) Anton Setzer 2002 (except for pictures)

Multiplexer (Cont.)

• S controls, which of the two inputs is taken asoutput.

• Can be extended:

– with 2 Signals, decisions between 4 incomingsignals can be made,

– with 3 between 8 signals, etc.

• Exercise:

– Write down the Boolean formula for a 4-to-1multiplexer (ie. one with 4 inputs + two controlsignals, which decide, which of the inputs is theoutput).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-32

(C) Anton Setzer 2002 (except for pictures)

Making Decisions

1 3

2

Clock

Q

Q’

D2

D1

control signal

• Depending on the control signal, the third flip-flopis updated to the value stored in the first or secondone.

• Now we are able to

– move around bits,– store them in registers, and read them again.

• Required: Generation of control signals.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-33

(C) Anton Setzer 2002 (except for pictures)

Main

Mem

ory

ALU

PC

IR

MD

R

AB

Con

stants

Registers

Intern

al Address L

ines

Intern

al Data L

ines

R#1in

R#2inR

2out

R1out

C

Data B

us (in

)

Data B

us

(out)

Address B

us

Rdatain

CPU

with

som

e CU

contro

lpath

s

CU

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-34

(C) Anton Setzer 2002 (except for pictures)

Controlling the Flow of Datain a Circuit

• No need to understand the previous slide (shows aCPU).

• Need to understand:

– A CPU consists of lots of units.– Each unit has an output depending on its input

at the previous clock cycle.– The

:::::::::::::::Control

::::::::::::Unit (

:::::::CU) controls the flow of

data (bits) in the circuit.– It gets as input signals from various points in the

CPU.– It outputs signals, which via multiplexers control

the flow between these units.– How does the Control Unit operate?

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-35

(C) Anton Setzer 2002 (except for pictures)

Operation of the Control Unit

State of Control Unit

in1 in2

s0 s1

out0

out1

out2

combinatorial

circuit

in0

• Small boxes above = falling edge D-flip-flops(connected to a clock).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-36

(C) Anton Setzer 2002 (except for pictures)

Operation of the Control Unit

• The control unit has an internal state, encoded byseveral bits.

– Essentially the lines in a little program.

• Initially the state will be reset to 0.

• Then in each clock cycle, dependending on

– inputs from various points in the circuit,– and its own state from the previous clock cycle,

• the CU computes for the next clock cycle

– control signals which control the data flow– and its own next state.

• This computation is a Boolean function, which canbe done by a (very complicated) combinatorialcircuit.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 3 3-37

(C) Anton Setzer 2002 (except for pictures)

4. Computer Arithmetic andRepresentation of Data

(a) Unsigned Integers.

(b) Arithmetic Operations on Unsigned Integers.

(c) Signed Integers.

(d) Fixed Point Numbers.

(e) Floating Point Numbers.

(f) Other Scalar Data Types.

(g) Compound Data Types.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-1

(C) Anton Setzer 2002 (except for pictures)

Three Types of Data Types

•::::::::::::Scalar

:::::::::::data

::::::::::::::types.

– Hold a single data item.– Each data item has a unique representation.– Main examples:∗ Number types (integer, floating point

numbers).∗ Characters.∗ Booleans.∗ Finite data types (enumerations types, void).

•::::::::::::::::::::::Compound

:::::::::::data

::::::::::::::types.

– Hold several data items.– Examples:∗ Strings (some books treat them as scalar).∗ Records, lists, arrays, maps.

•::::::::::::::::::::Reference

::::::::::and

::::::::::::::::::function

::::::::::::::types.

– Have no unique representation.– Examples:∗ Pointers.∗ Function types (in C, C++, functional

programming).∗ Classes in object-oriented programming.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-2

(C) Anton Setzer 2002 (except for pictures)

Number of Values Representable

• With one bit we can represent two different values,“1” and “0”:e.g.: 1 represents true, 0 represents false;or: 1 represents blue, 0 represents red.

• With two bits we can represent four different values:00, 01, 10, 11.

– So we can represent for instancered as 00, blue as 01, green as 10 and yellow as11.

• With three bits we can represent eight differentvalues:

– 4 values can be represented, using first bit 0:000, 001, 010, 011.

– 4 values can be represented, using first bit 1:100, 101, 110, 111.

– In total we can represent 2 · 4 = 8 values.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-2a

(C) Anton Setzer 2002 (except for pictures)

Number of Values Representable (Cont.)

• With every additional bit, the number of values wecan represent doubles.

• With l bits we can represent 2 · · · · · 2︸ ︷︷ ︸l

= 2l different

values.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-2b

(C) Anton Setzer 2002 (except for pictures)

(a) Unsigned Integers

•::::::::::::::Signed integers = integers, which can be positiveor negative.

•:::::::::::::::::::Unsigned integers = integers, which are alwayspositive.

• Representation of unsigned integers easier.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-3

(C) Anton Setzer 2002 (except for pictures)

Unsigned Integers (Cont.)

• Representation in ENIAC: One decimal digitrepresented by 10 vacuum tubes, i.e. by 10 bits.(Bit = binary digit = digit 0 or 1)

• Better approach: With 4 bits we can represent24 = 2 · 2 · 2 · 2 = 16 objects.Represent each digit by 4 bits.

– Appropriate for financial data (commercialrounding): Binary coded decimal (BCD):135 coded as 0001︸︷︷︸

1

0011︸︷︷︸3

0101︸︷︷︸5

.

– Otherwise∗ Waste of space (with 4 bits 16 symbols can

be stored of which only 10 are used).∗ Difficult to compute by a circuit.∗ Better: Use binary representation of

numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-4

(C) Anton Setzer 2002 (except for pictures)

Exponential Function

• Remember from school:100 = 1.101 = 10.102 = 10 · 10 = 100.103 = 10 · 10 · 10 = 1000.104 = 10 · 10 · 10 · 10 = 10000.etc.

• Similarly:70 = 1.71 = 7.72 = 7 · 7 = 49.73 = 7 · 7 · 7 = 343.74 = 7 · 7 · 7 · 7 = 2401.etc.

• Important to know the powers of 2:20 = 1 27 = 12821 = 2 28 = 25622 = 4 29 = 51223 = 8 210 = 102424 = 16 211 = 204825 = 32 212 = 409626 = 64 213 = 8192

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-5

(C) Anton Setzer 2002 (except for pictures)

Different Number Systems

• Decimal representation of numbers:

What’s the meaning of 4053?

We can assign a::::::::::::::weight to each digit as follows

4 0 5 3 = 4 · 1000↓ ↓ ↓ ↓ +0 · 100

103 102 101 100 +5 · 10= = = = +3 · 1

1000 100 10 1

Now the represented number is obtained by takingthe sum of the product of each digit with its weight:

4053 represents4 · 103︸︷︷︸

1000

+0 · 102︸︷︷︸100

+5 · 101︸︷︷︸10

+3 · 100︸︷︷︸1

.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-6

(C) Anton Setzer 2002 (except for pictures)

Different Number Systems (Cont.)

• Other bases: Replace 10 by other numbers, e.g.7.

Notation (4053)7 stands for 4053 in the::::::::::::::::number

::::::::::::::system

:::::::::::with

:::::::::::basis

::::7.

( 4 0 5 3 )7

↓ ↓ ↓ ↓73 72 71 70

= = = =343 49 7 1

(4053)7 = 4 · 73︸︷︷︸343

+5 · 71︸︷︷︸7

+3 · 70︸︷︷︸1

= 4 · 343 + 5 · 7 + 3 · 1= 1410 .

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-7

(C) Anton Setzer 2002 (except for pictures)

Different Number Systems (Cont.)

• Binary representation means: basis is 2:

(101101)2 = 25+23+22+20 = 32+8+4+1 = 45:

1 0 1 1 0 1↓ ↓ ↓ ↓ ↓ ↓25 24 23 22 21 20

= = = = = =32 16 8 4 2 1

Long binary numbers are difficult to read, e.g.100110001100001101101001

Therefore use of hexadecimal and octalrepresentation.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-7a

(C) Anton Setzer 2002 (except for pictures)

Different Number Systems (Cont.)

• Hexadecimal representation: Basis 16.Lack of digits:New digits A,B,C,D,E, F :

digit valueA 10B 11C 12D 13E 14F 15

– Example:(AF03)16 = 10︸︷︷︸

A

·163 + 15︸︷︷︸F

·162 + 0 · 161 + 3

( A F 0 3 )16

↓ ↓ ↓ ↓163 162 161 160

= = = =4096 256 16 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-8

(C) Anton Setzer 2002 (except for pictures)

Notations

• Hexadecimal:0xAF03 (sometimes $AF03) for (AF03)16,Binary:0b110110 (sometimes %110110) for (110110)2.The notation 1b · · · does not occur in this lecture.

• MSB. In binary representation with fixed length,the bit corresponding to the highest power of 2 iscalled the

::::::::::most

::::::::::::::::::::::significant

::::::::bit,

:::::::::::MSB.

– In standard Western writing it is the bit most tothe left.

– On most machines it is the bit with the lowestnumber.

– On some machines this bit will be stored as theone with the highest number.The bit corresponding to the lowest power oftwo is called the

::::::::::least

::::::::::::::::::::::significant

::::::::bit,

:::::::::LSB.

• Example:0b 10101010

↑ ↑MSB LSB

bit No. 0 bit No. 7on most on most

machines machines

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-9

(C) Anton Setzer 2002 (except for pictures)

Plural of MSB and LSB

We use as well the plural of MSB and LSB:

• By:::::::the

::::::::::::three

:::::::::::::MSBs (

:::::::::::three

:::::::::::most

::::::::::::::::::::::significant

:::::::::bits)

the three bits with the highest weight are meant.

•:::::::::::LSBs is used similarly.

Octal representation

Sometimes::::::::::octal

::::::::::::::::::::::::::::::::representation of numbers is used

as well: this means that basis 8 is used.In this representation only digits 0, . . . ,7 occur.Sometimes denoted by a leading zero (not in thismodule).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-10

(C) Anton Setzer 2002 (except for pictures)

Example of use of Hexadecimal

(From a GameBoy Advance program, written in C)

/***** REG DISPCNT defines */

#define MODE 0 0x0#define MODE 1 0x1#define MODE 2 0x2#define MODE 3 0x3#define MODE 4 0x4#define MODE 5 0x5

#define BACKBUFFER 0x10#define H BLANK OAM 0x20

#define OBJ MAP 2D 0x0#define OBJ MAP 1D 0x40

#define FORCE BLANK 0x80

#define BG0 ENABLE 0x100#define BG1 ENABLE 0x200#define BG2 ENABLE 0x400

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-11

(C) Anton Setzer 2002 (except for pictures)

Conversion from Binary to Hexadecimal

Trivial transformation:A binary number is transformed into hexadecimal, bypacking from the right four bits into one hexadecimaldigit:Example: 0b1001110111111

= 0b 1︸︷︷︸1

0011︸︷︷︸3

1011︸︷︷︸B

1111︸︷︷︸F

= 0x13BF

.

Conversion from Hexadecimal to Binary

Write every digit as a binary number with four bits.Example: 0x13BF

= 0x 1︸︷︷︸0001

3︸︷︷︸0011

B︸︷︷︸1011

F︸︷︷︸1111

= 0b0001001110111111

.

Leading 0’s can be omitted.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-12

(C) Anton Setzer 2002 (except for pictures)

Conversion from Binary to Octal

Similar to conversion binary to hexadecimal:A binary number is transformed into octal, by packingfrom the right three bits into one digit:Example: 0b1001110111111

= 0b 1︸︷︷︸1

001︸︷︷︸1

110︸︷︷︸6

111︸︷︷︸7

111︸︷︷︸7

= (11677)8

.

Conversion from Octal to Binary

Write every digit as a binary number with three bits.Example: (11677)8

= ( 1︸︷︷︸001

1︸︷︷︸001

6︸︷︷︸110

7︸︷︷︸111

7︸︷︷︸111

)8

= 0b001001110111111

.

Leading 0’s can be omitted.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-13

(C) Anton Setzer 2002 (except for pictures)

Conversion from Binary/Hexadecimal/Octalto Decimal

• Done using the above calculations. E.g.

– 0xF39 = 15 · 162 + 3 · 161 + 9 · 160.– 0b1011 = 23 + 21 + 20.

Conversion from Decimal to Binary

• For small numbers practical method:

– Find highest power of 2 you can subtract.– Continue with the rest, till you arrive at 0.– Form a binary number with 1 at the positions

corresponding to the powers of 2 used:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-14

(C) Anton Setzer 2002 (except for pictures)

Example

15321 into binary:

Calculation Posit.15321− 213 = 15321− 8192 = 7129 137129− 212 = 7129− 4096 = 3033 123033− 211 = 3033− 2048 = 985 11985− 29 = 985− 512 = 473 9473− 28 = 473− 256 = 217 8217− 27 = 217− 128 = 89 789− 26 = 89− 64 = 25 625− 24 = 25− 16 = 9 49− 23 = 9− 8 = 1 31− 20 = 1− 1 = 0 0

If we describe positions by subscripts we get

15321= 213 + 212 + 211 + 29 + 28 + 27 + 26

+24 + 23 + 20

= 0b 1 1 1 0 1 1 1 1 0 1 1 0 0 1↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑

13 12 11 10 9 8 7 6 5 4 3 2 1 0

= 0x3BD9

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-15

(C) Anton Setzer 2002 (except for pictures)

Better Algorithm

• If we divide a decimal number by 10 with remainder,we obtain the number shifted to the right by 1, andas remainder the least significant digit:

Result Remainder153÷ 10 15 3 Least significant digit15÷ 10 1 51÷ 10 0 1 Most significant digit

• The remainder are the digits of the decimalrepresentation.

• Do the same to convert to binary representation.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-16

(C) Anton Setzer 2002 (except for pictures)

Example

Result Remainder15321÷ 2 7660 1 LSB!7660÷ 2 3830 03830÷ 2 1915 01915÷ 2 957 1957÷ 2 478 1478÷ 2 239 0239÷ 2 119 1119÷ 2 59 159÷ 2 29 129÷ 2 14 114÷ 2 7 07÷ 2 3 13÷ 2 1 11÷ 2 0 1 MSB!

15321 = 0b11101111011001.

Conversion from Decimal to Hexadecimal

Probably easiest by hand: convert to binary, then tohexadecimal.Otherwise: similar to the above method (divide by16).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-17

(C) Anton Setzer 2002 (except for pictures)

Some Facts about Shifting Binary Numbers

Fact

Let y be a binary sequence,y0 the same sequence shifted once to the left withone new 0 inserted at the right.Then

0by0 = 0by · 2 .

• Example:

0b 1 0 1 = 22 + 20 = 4 + 1 = 5↓ ↓ ↓22 21 20

= = =4 2 1

Shifted once to the left:

0b 1 0 1 0 = 23 + 21 = 8 + 2 = 10↓ ↓ ↓ ↓ = 5 · 223 22 21 20

= = = =8 4 2 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-18

(C) Anton Setzer 2002 (except for pictures)

Correctness

• If we shift a binary number once to the left, each“1” gets just twice the weight as in the originalnumber.

• The new number representsthe sum of the weights of the “1” in it,which is twice the sum of the weights of the “1” inthe original number,which is twice the value of the original number.

• So 0by0 = 0by · 2.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-19

(C) Anton Setzer 2002 (except for pictures)

Some Facts aboutShifting Binary Numbers (Cont)

Fact

Let y be a binary sequence,and y 0 · · · 0︸ ︷︷ ︸

l

be the same sequence shifted l times

to the left, with new zeros inserted at the right.Then 0by 0 · · · 0︸ ︷︷ ︸

l

= 0by · 2l.

• 0b010 = 2.Shifted twice to the right we get0b01000 = 2 · 22 = 2 · 4 = 8.

• Proof of the fact:

– Each shift has the effect of multiplying thenumber by 2.

– l shifts have the effect of multiplying it by2 · · · · · 2︸ ︷︷ ︸

l

= 2l.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-20

(C) Anton Setzer 2002 (except for pictures)

Some Facts about Shifting Binary Numbers

Fact

Shifting a binary number l times to the right andomitting the old LSB is the same as dividing it by2l with remainder.The l LSBs form the remainder with this division.

Examples:

• 0b110 = 22 + 21 = 6.Shifted once to the right:0b11 = 21 + 20 = (22 + 21)÷ 21 = 3.Remainder is 0.

• 0b111 = 22 + 21 + 20 = 7.Shifted once to the right:0b11 = 21 + 20 = (22 + 21 + 20)÷ 21 = 3.Remainder of this division is 1.

• 0b11010 = 24 + 23 + 21 = 16 + 8 + 2 = 26.Shifted twice to the right:0b110 = 22+21 = 6 = 26÷2 = (24+23+21)÷22.The remainder is 0b10 = 21 = 2.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-21

(C) Anton Setzer 2002 (except for pictures)

Correctness

• Write the original sequence of bits as 0byz, wherez are the l LSBs and y are the remaining bits.

• Example:For 0b 111︸︷︷︸

y

01︸︷︷︸z

shifted twice to the right,

y = 111, z = 01 .

• Now 0byz = 0by 0 · · · 0︸ ︷︷ ︸l

+0bz.

In the example above:

0by 0 · · · 0︸ ︷︷ ︸l

= 0b 1 1 1 0 0

0bz = + 0b 0 10byz = 0b 1 1 1 0 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-22

(C) Anton Setzer 2002 (except for pictures)

Correctness (Cont.)

• The result of shifting 0byz l times to the right is y.

• The remaining bits are 0bz.

• Now

0byz = 0by 0 · · · 0︸ ︷︷ ︸l

+0bz

= 0by · 2l + 0bz

and0bz < 0b1 0 · · · 0︸ ︷︷ ︸

l

= 2l .

• Therefore

– 0by is the result of dividing 0byz by 2l,– 0bz is the remainder.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-23

(C) Anton Setzer 2002 (except for pictures)

(b) Arithmetic Operationson Unsigned Integers

(i) Addition

Addition of Decimals:

1298+ 751Carry 11

2049

Note: Carry is at most 1:If we add two decimal digits, i.e. two numbers in theinterval 0 . . . 9 we get at most 18.It might be that we have to add carry one, then weobtain at most 19.Addition of Binary Numbers:Do just the same:

101101+ 111110Carry 1111

1101011

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-24

(C) Anton Setzer 2002 (except for pictures)

Addition of Decimals in Detail

• Reconsider example from previous slide:

1298+ 751Carry 11

2049

• Step 1: 8 +1 = 9, result 9, carry 0(carry 0 is not written down):

129 8+ 75 1Carry

9

• Step 2: 9 + 5 = 14, result 4, carry 1:

12 98+ 7 51Carry 1

4 9

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-24a

(C) Anton Setzer 2002 (except for pictures)

Addition of Decimals in Detail (Cont.)

• Step 3: 2 + 7 + 1 = 10, result 0, carry 1:

1 298+ 751Carry 11

0 49

• Step 4: 1 + 1 = 2, result 2, carry 0:

1298+ 751Carry 11

2049

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-24b

(C) Anton Setzer 2002 (except for pictures)

Addition of Binaries in Detail

• Reconsider example from above:

101101+ 111110Carry 1111

1101011

• Step 1: 1 + 0 = 1 = 0b01. Result 1, carry 0Again carry 0 not written down:

10110 1+ 11111 0Carry

1

• Step 2: 0 + 1 = 1 = 0b01. Result 1, carry 0

1011 01+ 1111 10Carry

1 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-24c

(C) Anton Setzer 2002 (except for pictures)

Addition of Binaries in Detail (Cont.)

• Step 3: 1 + 1 = 2 = 0b10. Result 0, carry 1

101 101+ 111 110Carry 1

0 11

• Step 4: 1 + 1 + 1 = 3 = 0b11. Result 1, carry 1

10 1101+ 11 1110Carry 11

1 011

• Step 5: 0 + 1 + 1 = 2 = 0b10. Result 0, carry 1

1 01101+ 1 11110Carry 111

0 1011

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-24d

(C) Anton Setzer 2002 (except for pictures)

Addition of Binaries in Detail (Cont.)

• Step 6: 1 + 1 + 1 = 3 = 0b11. Result 1, carry 1

101101+ 111110Carry 1111

1 01011

• Step 7: 0 + 0 + 1 = 1= 0b01. Result 1, carry 0

101101+ 111110Carry 1111

1 101011

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-24e

(C) Anton Setzer 2002 (except for pictures)

Half-Bit-Adder

Addition of two bits:We have the following table for addition

A B Carry Sum Corresponds to0 0 0 0 0b0 + 0b0 = 0b000 1 0 1 0b0 + 0b1 = 0b011 0 0 1 0b1 + 0b0 = 0b011 1 1 0 0b1 + 0b1 = 0b10

So we getCarry is 1, if both A and B are 1:

Carry = A ∧B ,

Sum is 1, if at least one of A and B is 1, but not both:

Sum = (A ∨B) ∧ ¬(A ∧B) .

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-25

(C) Anton Setzer 2002 (except for pictures)

Implementation of a Half-Bit-Adder

A A + B Sum

ABB Carry

(A+B)¬(AB)

AB

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-26

(C) Anton Setzer 2002 (except for pictures)

Full One-Bit-Adder

A full one-bit adder allows to add two bits, plus acarry from the previous addition(which can be 1 or 0).We have the following table:

A B C Carry Sum0 0 0 0 0 0 + 0 + 0 = 0b000 0 1 0 1 0 + 0 + 1 = 0b010 1 0 0 1 0 + 1 + 0 = 0b010 1 1 1 0 0 + 1 + 1 = 0b101 0 0 0 1 1 + 0 + 0 = 0b011 0 1 1 0 1 + 0 + 1 = 0b101 1 0 1 0 1 + 1 + 0 = 0b101 1 1 1 1 1 + 1 + 1 = 0b11

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-27

(C) Anton Setzer 2002 (except for pictures)

Full One-Bit-Adder (Cont.)

We obtain:Sum is 1 if exactly one of A, B and C, or all threeare 1:

Sum = (A ·B · C)+(A ·B · C)+(A ·B · C)+(A ·B · C)

Carry is 1 of at least two of A, B, C are 1:

Carry = (A ·B) + (A · C) + (B · C)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-28

(C) Anton Setzer 2002 (except for pictures)

Implementation of a Full Bit Adder

� �� � ��� �� �

���

� �

� ��

� �� �

��

� �� ���

��

��

��� �� �

!" "# #

A B

Sum

Carry

C

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-29

(C) Anton Setzer 2002 (except for pictures)

Implementation using Two Half-Bit-Adders:

Half

Half

Carry

Sum

B

C

A Carry1adder1 Sum1

adder2

Carry2

Sum2

Correctness:

• Easiest: write a truth table, with inputs A,B,C.

Very good exercise !!!

• Or see next slide

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-30

(C) Anton Setzer 2002 (except for pictures)

Correctness

• If we add A, B we get result Carry1, Sum1.

• Now we add Sum1 and C, with result Carry2,Sum2.

• Sum2 is now the final Sum.

• If the first addition has Carry1=0, the carry of thecomplete sum will be Carry2.

• If the first addition has Carry1=1, then Sum1=0.Then the second addition can have only Carry2 =0.So the carry of the complete sum is 1 or Carry1.

• In both the cases we get:Carry = Carry1 ∨ Carry2

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-31

(C) Anton Setzer 2002 (except for pictures)

Correctness (Revised Version)

• Adding A, B, C can be achieved by first adding A,B and then adding C to the result:

A+ B

Carry1 Sum1+ C

Carry2

Carry Sum2

• Addition of A, B is done by Half-Bit-Adder1.

• Sum2, Carry2 is computed by Half-Bit-Adder2.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-31a

(C) Anton Setzer 2002 (except for pictures)

Correctness (Revised Version, Cont.)

• Carry is the result of adding Carry1 and Carry2.

– We could use a half-bit-adder for this.

– However observe:∗ If Carry1 = 1, then Sum1 = 0.

(If a half-bit-adder has result Carry = 1, thenSum = 0.)

∗ If Sum1 = 0, then Carry2 = 0.(If we add 0 to another bit, the carry is 0).

∗ So we never have that both Carry1, Carry2are 1.

∗ But then the sum of Carry1, Carry2 is simplyCarry1 ∨ Carry2.(A∨B is the sum of A and B, if not both A,B are 1).

– Therefore Sum = Sum2, Carry = Carry1 ∨Carry2.That’s the implementation.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-31b

(C) Anton Setzer 2002 (except for pictures)

Comparison of the Two Implementations

• Advantage of second Implementation:Fewer gates needed (7 instead of 9).

• Disadvantage of second Implementation:Delay, because the signal has to pass through moregates:

– In a half bit adder,carry passes through 1 gate,sum through 2 gates.

– So sum in the second implementaiton passesthrough

2︸︷︷︸First Adder

+ 2︸︷︷︸Second Adder

gates,

– carry passes through2︸︷︷︸

First Adder

+ 1︸︷︷︸Second Adder

+ 1︸︷︷︸∨−gate

gates.

– In first implementation each signal has to passthrough 2 gates only.

• Addition is the most frequent operation!!

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-32

(C) Anton Setzer 2002 (except for pictures)

Addition of l bit numbers

Remember:

101101+ 111110Carry 1111

1101011

• Add the two LSBs with carry.

– Sum is LSB of result.

• Add next two LSBs plus carry of previous addition.

– Sum is next LSB of result.

• Etc.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-33

(C) Anton Setzer 2002 (except for pictures)

Carry A

BA

B A

BA

B

Carry

Carry

Carry

Possible C

arry fromprevious A

ddition

SumC

arrySum

Carry

SumC

arrySum

Full adder

Full adder

Full adder

Full adder

Bit 2

Carry

Bit 2

MSB

Bit 0

Bit 1

LSB

Bit 3

Bit 0

Bit 1

Bit 3

MSB

LSB

Output

Input

A F

our Bit A

dder

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-34

(C) Anton Setzer 2002 (except for pictures)

Overflow

• If there is carry of the addition of MSBs plus carry,we have an overflow:Sum requires one more bits than the summands.

• Example:

1 1 1+ 1 1 1Carry 1 1 1

1 1 1 0

Result cannot be represented with 3 bits.

• This extra bit is called::::::::::carry

:::::::::flag.

(In general::::::::flag are one-bit-registers, which are

usually set automatically in response to operations).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-35

(C) Anton Setzer 2002 (except for pictures)

Carry Flag

• When adding long numbers, one might carry outaddition in two steps:

01 1001 11

Carry 111 01

– First add 0b10 + 0b11 = 0b101,by using one two-bit-adder.Result is 0b01 plus Carry 1.

– Now add 0b01 + 0b01+ previous carryusing the same two-bit-adderResult is = 0b01 + 0b01 + 0b1 = 0b11.

– Result is result of second and first addition insequence.

• In practice, one has for instance a 32-bit adder,and can use it in order to add in two steps two64-bit-numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-36

(C) Anton Setzer 2002 (except for pictures)

Carry Flag

• In machine language, addition of two l-bit numbershas as result usually a l-bit number plus a carry,corresponding to the carry of the addition of theMSBs (plus previous carry).

– Carry might be taken over to the next addition,– or might indicate an overflow.– Depends on the context (has to be decided by

the assembler programmer).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-37

(C) Anton Setzer 2002 (except for pictures)

Carry Lookahead

• Problem with n-bit adders:The carry-input for the kth bit has to pass in worstcase through k − 1 full bit-adders, which causesdelay.

• Solution::::::::::::Carry

:::::::::::::::::::::::Lookahead:

– Compute carries directly from the bits of theinputs (passes through less gates).

– Note that the kth carry is a function from theleast significant k bits of the addends).

– Becomes complicated with increasing k, severaltechniques for optimization.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-38

(C) Anton Setzer 2002 (except for pictures)

(ii) Multiplication

Multiplication of Decimal Numbers

345 · 123 =

1035+ 6 9 0+ 345

1 1

42 4 3 5Steps in this example:

• Multiply 345 by 3. Result is1035.

• Multiply 345 by 2. Result is 690 .Shift result once to the left.

• Multiply 345 by 1. Result is 345.Shift result twice to the left.

• Add the three numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-39

(C) Anton Setzer 2002 (except for pictures)

Naive Multiplication of Binary Numbers

• Do the same with binary numbers.

0b1101 · 0b1011 =

1101+ 1 1 0 1+ 11010b 1000 1 1 1 1

• Steps in this example:

– Multiply 0b1101 by 1. Result is 0b1101.– Multiply 0b1101 by 1. Result is 0b1101 .

Shift result once to the left.– Multiply 0b1101 by 0. Result is 0.

Shift result twice to the left(or ignore it because it is zero).

– Multiply 0b1101 by 1. Result is 0b1101.Shift result three times to the left.

– Add the results above.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-40

(C) Anton Setzer 2002 (except for pictures)

Naive Multiplicationof Binary Numbers (Cont.)

• We will in the following interchange the role ofmultiplier and multiplicand.

0b1101 · 0b1011 = 1011+ 1011+ 10110b 10001111

– Since multiplication is commutative, order offactors doesn’t matter.

– This order is better for the presentation.

• In the algorithm below we will make use of avariable sum.

– sum is initially zero.– Whenever we have to add the shifted multiplier,

we add it to sum.– Then at the end, sum contains the result.

• It’s easier to shift the multiplier successively to theleft, and add it to sum

– (rather than shifting it when needed several timesto the left).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-41

(C) Anton Setzer 2002 (except for pictures)

Naive Multiplicationof Binary Numbers (Cont.)

• When calculating by hand, we write blanks in frontand add the end of the number to be added.Those blanks stand for the value 0 and have, whenworking on the computer, to be replaced by zeros.So the previous multiplicaton reads as follows:

0b1101 · 0b1011 = 0b00001011+ 0b00101100+ 0b01011000

0b10001111

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-41a

(C) Anton Setzer 2002 (except for pictures)

Naive Algorithm forthe Multiplication of m and m′

Assume m, m′ are k-bit numbers.

sum := 0for i = 1 to k do

beginif ith LSB of m is 1 then sum := sum +m′

Shift m′ left one bitend

sum is result

• Note that the result of the multiplication of two kbit numbers requires up to 2k bits!

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-42

(C) Anton Setzer 2002 (except for pictures)

Example (Naive Algorithm)

m m′ sumInitialization 011 000 101 000 000Step 1a1st LSB of m =1 011 000 101 + 000 101Add m′ to sum 000 101Step 1bShift m′ 011 001 010 000 101Step 2a2nd LSB of m =1 011 001 010 + 001 010Add m′ to sum 001 111Step 2bShift 011 010 100 001 111Step 3a3rd LSB of m =0 011 010 100No action 001 111Step 3bShifts 011 101 000 001 111

0b011 · 0b101 = 3 · 5 = 15 = 0b1111.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-43

(C) Anton Setzer 2002 (except for pictures)

Slightly Better Algorithm

• Start with sum 0.

• Shift successively

– multiplier (m) to the right (n times if we have nbits).

– as before multiplicand (m’) to the left,

• Whenever the LSB of multiplier (m) is 1, addmultiplicand (m’) shifted appropriately to the sum.

• Result is sum.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-44

(C) Anton Setzer 2002 (except for pictures)

Second Algorithm forthe Multiplication of m and m′

(m is multiplicand, m′ is multiplier).Assume m, m′ are k-bit numbers.

sum := 0for i = 1 to k do

beginif LSB(m) = 1 then sum := sum +m′

Shift m right one bitShift m′ left one bitend

sum is result

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-45

(C) Anton Setzer 2002 (except for pictures)

Example (Second Algorithm)

m m′ sumInitialization 011 000 101 000 000Step 1aLSB(m) =1 011 000 101 + 000 101Add m′ to sum 000 101Step 1bShifts 001 001 010 000 101Step 2aLSB(m) =1 001 001 010 + 001 010Add m′ to sum 001 111Step 2bShifts 000 010 100 001 111Step 3aLSB(m) =0 000 010 100No action 001 111Step 3bShifts 000 101 000 001 111

0b011 · 0b101 = 3 · 5 = 15 = 0b1111.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-46

(C) Anton Setzer 2002 (except for pictures)

Problem

• We need a 2k-bit adder when multiplying two k bitnumbers.

• However the number we add has always k zeros(in a block left and right of the original multiplier).

• Further before the addition, sum has to the left ofthe original multiplier only zeros.

– So the addition will affect only the bits whichpossibly are occupied by the multiplier (m’).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-47

(C) Anton Setzer 2002 (except for pictures)

Steps towards the Third Algorithm

• Let the sum in the new algorithm be

– the sum of the original algorithm,– but shifted to the left,– so that the multiplier to be added has to be

added to the k MSBs of the new sum.

• In our example the new sum is the old sum,

– in step 1a three bits shifted to the right;– in step 2a two bits shifted to the right;– in step 3a one bit shifted to the right.

• Then addition takes place only in the k MSBs bitsof sum.

– Requires only an k-bit adder.

• However, now the new sum has to be adjustedwhen moving from one step to the next.

– After the step, the new sum is one bit less shiftedto the left than before.

– So the new sum has to be shifted one bit to theright after step 1a, 2a, 3a etc.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-48

(C) Anton Setzer 2002 (except for pictures)

Difficulty

• After adding the multiplier, we might obtain oneoverflow bit.

– This corresponds to the original sum having onebit to the left of the multiplier.

• Solution:

– Let sum have 2k + 1 instead of 2k bits.– We add the multiplier to the k+1 MSBs of sum.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-49

(C) Anton Setzer 2002 (except for pictures)

Third Algorithm for the Multiplicationof m and m′

Assume m, m′ are k-bit numbers.

sum is a (2k + 1)-bit number.sum := 0for i = 1 to k do

beginif LSB(m) = 1 then add m′ to the

k+1 most significant bits of sumShift m right one bitShift sum right one bitend

sum is result

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-50

(C) Anton Setzer 2002 (except for pictures)

Example (Third Algorithm)

m m′ sumInitialization 011 101 0 000 | 000Step 1aLSB(m) =1 011 101 + 101Add m′ 0 101 | 000Step 1bShifts 001 101 0 010 1|00Step 2aLSB(m) =1 001 101 + 101Add m′ 0 111 1|00Step 2bShifts 000 101 0 011 11|0Step 3aLSB(m) =0 000 101No action 0 011 11|0Step 3bShifts 000 101 0 001 111|

0b011 · 0b101 = 3 · 5 = 15 = 0b1111.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-51

(C) Anton Setzer 2002 (except for pictures)

• Observe that in the last example at the end ofStep l,

– the 4 + l MSBs of sum (written in italic, incolour blue) are the same as

– the 4 + l LSBs of sum in the previous algorithm.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-52

(C) Anton Setzer 2002 (except for pictures)

Example, in which One Extra Bit is Needed:

m m′ sumInitialization 111 111 0 000 | 000Step 1aLSB(m) =1 111 111 + 111Add m′ 0 111 | 000Step 1bShifts 011 111 0 011 1|00Step 2aLSB(m) =1 011 111 + 111Add m′ 1 010 1|00Step 2bShifts 001 111 0 101 01|0Step 3aLSB(m) =1 001 111 + 111Add m′ 1 100 01|0Step 3bShifts 000 111 0 110 001|

0b111 · 0b111 = 7 · 7 = 49 = 0b110001.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-53

(C) Anton Setzer 2002 (except for pictures)

Fourth Algorithm for the Multiplicationof m and m′

We can store m in the least significant bits of thesum. Then we have only to shift sum to the right.(Saves shift logic).

Assume m, m′ are k-bit numbers.

Algorithm:sum is a (2k + 1)-bit number.sum := 0Set the k LSBs of sum to mfor i = 1 to k do

beginif LSB(sum) = 1 then add m′ to the

k+1 MSBs of sumShift sum right one bitend

sum is result

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-54

(C) Anton Setzer 2002 (except for pictures)

Example (Fourth Algorithm)

m m′ sumInitialization 011 101 0 000 |011Step 1aLSB(sum) =1 011 101 + 101Add m′ 0 101 |011Step 1bShift 011 101 0 010 1|01Step 2aLSB(sum) =1 011 101 + 101Add m′ 0 111 1|01Step 2bShift 011 101 0 011 11|0Step 3aLSB(sum) =0 011 101No action 0 011 11|0Step 3bShift 011 101 0 001 111|

0b011 · 0b101 = 3 · 5 = 15 = 0b1111.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-55

(C) Anton Setzer 2002 (except for pictures)

• In the last example, the bar | separates

– the bits identical with the sum in the previousalgorithm (written in italic, in colour blue)

– from m′ (written in boldface, in colour green)– which are both shifted to the right

simultaneously.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-56

(C) Anton Setzer 2002 (except for pictures)

Implemented Multiplication

• Implementations will use a fifth algorithm,::::::::::::::::Booth’s

::::::::::::::::::::algorithm.

• Booth’s algorithm best explained by referring tosigned integers.

• Therefore we break off here, and continue withmultiplication algorithms in the subsection aboutsigned integers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-57

(C) Anton Setzer 2002 (except for pictures)

(iii) Subtraction

Subtraction

Naive Subtraction of Decimal Numbers

• Example:1743

− 34511

1398

• Step 1:3− 5 is 8 with

::::::::::::::borrow 1.

(since 13− 5 = 8).1743

− 3451

8

• Step 2: 4 − (4 + 1) (+1 is the borrow) is 9 withborrow 1.(since 14− (4 + 1) = 9).

1743− 345

1 1

98

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-58

(C) Anton Setzer 2002 (except for pictures)

• Step 3: 7− (3 + 1) is 3.1743

− 34511

398

• Step 4: 1− 0 is 1.1743

− 34511

1398

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-59

(C) Anton Setzer 2002 (except for pictures)

Naive Subtraction of Binary Numbers

• Naive subtraction of binary numbers similarly:0b10101

− 0b01111111

0b00110

• Steps of the computation:

– Step 1:1− 1 = 0.

0b10101− 0b01111

0b 0

– Step 2: 0− 1 = 1 borrow 1,(since 0b10− 1 = 1)

0b10101− 0b01111

1

0b 10

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-60

(C) Anton Setzer 2002 (except for pictures)

– Step 3: 1− (1 + 1) = 1− 0b10 = 1 borrow 1.(since 0b11− 0b10 = 1)

0b10101− 0b01111

1 1

0b 110

– Step 4: 0− (1 + 1) = 0− 0b10 = 0 borrow 1.(since 0b10− 0b10 = 0)

0b10101− 0b01111

1 11

0b 0110

– Step 5: 1− 1 = 0.

0b10101− 0b01111

111

0b00110

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-61

(C) Anton Setzer 2002 (except for pictures)

Implemented Subtraction

• Subtraction in implementation will make use ofsigned numbers.

• So we will break off here and continue after theintroduction of signed numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-62

(C) Anton Setzer 2002 (except for pictures)

Observations about Binary Numbers

• We have

– 0b1 = 1 = 20,– 0b10 = 2 = 21,– 0b100 = 4 = 22, etc.– In general 0b1 0 · · · 0︸ ︷︷ ︸

k

= 2k.

• Further

0b 1 · · · 1︸ ︷︷ ︸k

= 0b1 0 · · · 0︸ ︷︷ ︸k

−0b1

= 2k − 1 :

0b10· · ·000− 0b 1

11 ···11

0b 1· · ·111

• 2k−1−1 = 0b 1 · · · 1︸ ︷︷ ︸k

is as well the largest number

representable as an unsigned number.Therefore the range of numbers representable asunsigned numbers with k bits is 0, 1, 2, . . . , 2k−1−1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-63

(C) Anton Setzer 2002 (except for pictures)

Notations

• If y is a finite sequence of bits with fixed numberof bits,

– 0y is y, extended by one new MSB 0,– 1y is y, extended by one new MSB 1

• E.g. if y = 1010, then

– 0y = 01010,– 1y = 11010.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-64

(C) Anton Setzer 2002 (except for pictures)

(c) Signed Integers

• Goal: Representation of positive and negativenumbers.

• Oldest Method:

::::::::::::::::::::::::::::::::Sign-magnitude

:::::::::::::::::::::::::::::::representation.

– One more additional MSB for representation ofthe sign.

– MSB = 0 means +, MSB = 1 means −.– The remaining bits represent the absolute value.– So, if y unsigned represents l, then∗ 0y represents +l,∗ 1y represents −l.

• Example:0011 represents +3,1011 represents −3.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-65

(C) Anton Setzer 2002 (except for pictures)

Disadvantages ofSign-Magnitude Representation

• Two representations of 0 (+0, −0).

• Arithmetic complicated.Especially in case of addition one needs 4 differentalgorithms depending on the sign of the first andsecond argument.

• So in the following we will use a differentrepresentation, called

::::::::::two’s

::::::::::::::::::::::::::::::::complement

:::::::::::::::::::::::::::::representation.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-66

(C) Anton Setzer 2002 (except for pictures)

Two’s Complement Representation

• Idea: If we naively subtract 1 from 0, we obtain anumber consisting of infinitely many ones:

0- 1· · ·1111111111

· · ·11111111111

• Similarly 0− 10 = “ · · · 11110′′:

0- 10· · ·111111111

· · ·11111111110

• 0− 11 = “ · · · 11101′′:

0- 11· · ·1111111111

· · ·11111111101

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-67

(C) Anton Setzer 2002 (except for pictures)

Two’s Complement Representation (Cont.)

• Similarly, we can consider positive numbers assequences of numbers (growing to the left) withinfinitely many zeros to the left:

• 0b10 can be written as · · · 00010,

• 0b101 is written as · · · 000101.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-67a

(C) Anton Setzer 2002 (except for pictures)

Two’s Complement Representation (Cont.)

• Since we have only finitely many bits available, wehave to cut the number of bits off at some position.

• We fix the number k of bits in our representation.

• In this representation

– an MSB 1 means that there are infinitely many1s to the left,

– an MSB 0 means that there are infinitely many0s to the left.

• So we have for k = 3

– 011 represents · · · 0011, or 3.– 010 represents · · · 0010, or 2.– 001 represents · · · 0001, or 1.– 000 represents · · · 0000, or 0.– 111 represents · · · 1111, or −1.– 110 represents · · · 1110, or −2.– 101 represents · · · 1101, or −3.– 100 represents · · · 1100, or −4.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-68

(C) Anton Setzer 2002 (except for pictures)

Two’s Complement Representation (Cont.)

• However, not all numbers can be represented.

• In case k = 3 we have:

∗ 4 cannot be represented.(Cutting off · · · 000100 after 3 bits yields 100.But 100 represents · · · 11100 or −4 and not 4).∗ −5 cannot be presented

(Cutting off · · · 111011 after 3 bits yields 011.But 011 represents · · · 00011 or 3 and not −5).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-69

(C) Anton Setzer 2002 (except for pictures)

Two’s Complement RepresentationPositive Numbers

• Assume for simplicity we cut off after 3 bits.We can represent the following numbers:

+3 = 011+2 = 010+1 = 001

0 = 000−1 = 111−2 = 110−3 = 101−4 = 100

• Positive numbers are 2-bit unsigned integers,extended by a new MSB 0.

• With k bits, positive numbers are k−1-bit unsignedintegers, extended by new MSB 0.

• The largest number representable with k bits is thelargest unsigned number representable with k − 1bits namely 2k−1 − 1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-70

(C) Anton Setzer 2002 (except for pictures)

Two’s Complement RepresentationNegative Numbers

+3 = 011+2 = 010+1 = 001

0 = 000−1 = 111−2 = 110−3 = 101−4 = 100

• Negative numbers are of the form 1y.

• When we increase x, the number increases.

– Different from sign-magnitude representation.There 1y denotes −0by:If one increases y, the number decreases.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-71

(C) Anton Setzer 2002 (except for pictures)

Two’s Complement RepresentationNegative Numbers (Cont.)

+3 = 011+2 = 010+1 = 001

0 = 000−1 = 111 = −4 + 3 = −4 + 0b11−2 = 110 = −4 + 2 = −4 + 0b10−3 = 101 = −4 + 1 = −4 + 0b01−4 = 100 = −4 + 0 = −4 + 0b00

• 1x represents −4 + 0bx.

• Least number we can represent is −4.

• With k bits, 1y represents −2k−1 + 0by.The least number we can represent is −2k−1.

• So in total, with k bits, we can represent numbersin the range −2k−1,−2k−1 + 1, . . . , 2k−1 − 1(with 3 bits the range is −4,−3,−2,−1, 0, 1, 2, 3with 4 bits it is −8,−7, . . . , 6, 7).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-72

(C) Anton Setzer 2002 (except for pictures)

Weight of MSB

• Notation:

–::::::::::::::::(y)signed is the value of y in two’s complementform

– As before 0by stands for the value of thesequence y as a binary unsigned number.

• (0y)unsigned = 0by,(1y)unsigned = −2k−1 + 0by.

• So the contribution of the MSB in two’scomplement form with kbits to the value is −2k−1:The MSB has weight −2k−1.

– This is the negative value of the weight it wouldhave as a signed number (i.e. 2k−1).

• All other bits have the same weight as forunsigned numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-73

(C) Anton Setzer 2002 (except for pictures)

Translation of Two’s Complementinto Decimal Notation

• The number represented by finite bit sequence two’scomplement form is the sum of the weights of the1s in it:

∗ (101101)signed

= −25 + 23 + 22 + 20

= −32 + 8 + 4 + 1 = −19:

1 0 1 1 0 1↓ ↓ ↓ ↓ ↓ ↓−25 24 23 22 21 20

= = = = = =−32 16 8 4 2 1

∗ (001101)signed

= 23 + 22 + 20

= 8 + 4 + 1 = 13:

0 0 1 1 0 1↓ ↓ ↓ ↓ ↓ ↓−25 24 23 22 21 20

= = = = = =−32 16 8 4 2 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-74

(C) Anton Setzer 2002 (except for pictures)

• Another example:

• (10001110)unsigned = −128 + 8 + 4 + 2 = −114:

1 0 0 0 1 1 1 0↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓−27 +26 +25 +24 +23 +22 +21 +20

= = = = = = = =−128 +64 +32 +16 +8 +4 +2 +1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-75

(C) Anton Setzer 2002 (except for pictures)

Observe

• In two’s complement form, MSB = 1 means thenumber is negative.

• MSB = 0 means the number is positive.

• So the MSB indicates whether the number ispositive or negative.

• But two’s complement form is not sign-magnituderepresentation:For instance 101 denotes not −0b01 but −4+1 =−3.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-76

(C) Anton Setzer 2002 (except for pictures)

Observation

• (This observation is only intended for thoseinterested in understanding the proofs in thefollowing).

• Lemma.Let z be a k-bit number. Then

0bz ={

(z)signed if (z)signed ≥ 0(z)signed + 2k otherwise.

• Proof:

– z = 0x or z = 1x, x a k − 1-bit number.– If z = 0x, then∗ 0bz = 0bx = (z)signed.∗ (z)signed ≥ 0.

– If z = 1x, then∗ MSB in z gets weight· 2k−1 as an unsigned number,· −2k−1 as a signed number .Therefore∗ 0bz = 2k−1 + 0bx.∗ (z)signed= −2k−1 + 0bx

= −2k−1 + 0bz − 2k−1

= 0bz − 2k

.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-77

(C) Anton Setzer 2002 (except for pictures)

Algorithm for NegatingTwo’s Complement Numbers

• Assume m = (z)signed, where z is a k-bit binarynumber.

• Assume z 6= 1 0 · · · 0︸ ︷︷ ︸k−1

.

– (The negation of (1 0 · · · 0︸ ︷︷ ︸k−1

) = −2k−1 is 2k−1,

which cannot be represented with k bits).

• In order to compute −m, do the following:

– Form the bitwise complement of z as a k-bitnumber.I.e.: replace every 0 by a 1, every 1 by a 0.

– Add one (as if it were an unsigned number), andignore any carry.

– The result represents −m.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-78

(C) Anton Setzer 2002 (except for pictures)

Example

• (All numbers on this slide are in two’s complementform with 3 bits.)

• −2 = −(010)signed = (110)signed:

– Bitwise complement of 010 is 101.– Addition of one yields 110.

• −(−2) = −(110)signed = (010)signed:

– Bitwise complement of 110 is 001.– Addition of 1 yields 010

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-79

(C) Anton Setzer 2002 (except for pictures)

Algorithm for Translating DecimalIntegers into Two’s Complement

• With k bits we can represent the range−2k−1, . . . , 2k−1 − 1.(eg. with 3 bits, this range is−4,−3,−2,−1, 0, 1, 2, 3).

• If l is not in this range,we cannot represent l,i.e. we have overflow or underflow.

• Assume l is in the range.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-80

(C) Anton Setzer 2002 (except for pictures)

Algorithm for Translating DecimalIntegers into Two’s Complement (Cont.)

• Case l ≥ 0.

– Represent l as a (k − 1)-bit unsigned binarynumber y,i.e. l = 0by.

– Then l = (0y)signed.

• Case l < 0:

– Represent −l as a k − 1-bit unsigned binarynumber y, i.e.−l = 0by.Then −l = (0y)signed.

– Form the negation of 0y using the abovealgorithm, i.e.:∗ Carry out the bitwise complement of 0y

(i.e. interchange bits 0 and 1).∗ Add 1 to the result as k-bit unsigned number,

ignoring a carry.∗ The result represents l as a k-bit signed

integer.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-81

(C) Anton Setzer 2002 (except for pictures)

Example

• Calculation of the representation of 22 with 8 bits

∗ 22 = 0b0010110 = (00010110)signed.

• Calculation of the representation of −22 with 8bits:

∗ 22 = 0b0010110 = (00010110)signed

∗ Formation of the bitwise complement:

0001011011101001

Result is 11101001.

∗ Add one. Result is 11101010.∗ −22 = (11101010)signed.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-82

(C) Anton Setzer 2002 (except for pictures)

Expansion of Two’s Complement Numbers

• Assume m = (z)signed, z has k bits.

• The representation of m with k+ 1 bits is obtainedby adding in front of z the MSB of z.

• Examples:

∗ z = 0101 is expanded by adding a 0 in front otit:(0101)signed = (00101)signed = (000101)signed.∗ z = 1101 is expanded by adding a 1 in front ot

it:(1101)signed = (11101)signed = (111101)signed.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-83

(C) Anton Setzer 2002 (except for pictures)

Observe

• If we add a 0 in front of a negative number, weobtain a positive number, not the same number.

• Example:

– (11101010)signed = (−128) + 64 + 32 + 8 + 2 =−22(as a signed 8 bit number):

1 1 1 0 1 0 1 0↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓−128 64 32 16 8 4 2 1

– (011101010)signed = 128+64+32+8+2 = 234as a 9 bit number.

– The correct expansion of the signed number(11101010)signed to 9 bits is (111101010)signed.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-84

(C) Anton Setzer 2002 (except for pictures)

Warning

• Don’t mix up sign-magnitude representation withtwo’s complement.

• 1011 in two’s complement representation is not tobe interpreted as −0b011, but as −0b101:(1011)signed = −8 + 2 + 1 = −5.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-85

(C) Anton Setzer 2002 (except for pictures)

Addition ofTwo’s Complement Numbers

• Addition of two k-bit numbers m, m′ in two’scomplement form is carried out as follows:

– Add m, m′ using the same algorithm as forunsigned numbers, with result as a k-bit number.Ignore a possible carry.

– Interpret the result as a a signed number n.– Case 1: m, m′ are positive, n is negative as a

two’s complement number.∗ Then there is an overflow.

Result too big to be represented.– Case 2: m, m′ are negative, n is positive as a

two’s complement number.∗ Then there is an underflow.

Result is negative and too small to berepresented.

– Otherwise there is no under/overflow,n as a k-bit number in two’s complementrepresentation is the sum.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-86

(C) Anton Setzer 2002 (except for pictures)

Examples

• (0110)signed + (0100)signed yields an overflow,since 0110 + 0100 = 1010,which appears to be negative.

• (0100)signed + (0010)signed = (0110)signed,since 0100+0010 = 0110 and the result is positive.

• (1001)signed + (1101)signed yields an underflow,since 1001+1101 = 0110 (ignoring a carry), whichis positive.

• (1101)signed + (1011)signed = (1000)signed,since 1101+1011 = 1000 (ignoring a carry!) whichis negative.

• (1000)signed + (0101)signed = (1101)signed,since 1000 + 0101 = 1101.

• (1001)signed + (0111)signed = (0000)signed,since 1001 + 0111 = 0000 (ignoring a carry).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-87

(C) Anton Setzer 2002 (except for pictures)

Subtraction ofTwo’s Complement Numbers

• m−m′ = m+ (−m′).

• Therefore in order to subtract m′ from m, add thenegation of m′ to m.

• Alternatively use naive subtraction, which can belifted directly to two’s complement.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-88

(C) Anton Setzer 2002 (except for pictures)

Multiplication ofTwo’s Complement Numbers

(i) Multiplication of two unsigned numbers, withresult signed.

(ii) Optimization.

(iii) Multiplication of signed numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-89

(C) Anton Setzer 2002 (except for pictures)

(i) Mult. of Unsigned NumbersResult Signed

• A block of ones, 0b 1 · · · 1︸ ︷︷ ︸k

, unsigned, represents

2k − 1.

• So multiplication of 0b 1 · · · 1︸ ︷︷ ︸k

with x has result

(2k − 1)x = 2kx− x.

• 2kx is x shifted k bits to the left.

• Therefore 0b 1 · · · 1︸ ︷︷ ︸k

·x is the result of subtracting

from x shifted k bits to the left x not shifted.

• For instance0b111 · 0b101 = (0b1000 · 0b101)− (0b1 · 0b101)= 0b101000− 0b101 = 0b100011is calculated as follows:

+0b101 +0b101 · 0b1000- 0b 101 −0b101

0b100011

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-90

(C) Anton Setzer 2002 (except for pictures)

Mult. of Unsigned NumbersResult Signed (Cont.)

• Reconsider the example above:

– Before adding 0b101 we shifted it, so that theLSB of it is in the first bit to the left of 0b111.

– Since 0b111 = 0b0111, this is at the first bitafter the end of the block of ones.

– So we add first 0b101, shifted so that its LSB isat the the end of the block of ones, and subtractfrom it 0b101 so that its LSB is at the beginningof the block of ones.

• 0b1110 · 0b101 = (0b10000− 0b10) · 0b101 can becomputed as follows:

+0b101 +0b101 · 0b10000- 0b 101 −0b101 · 0b10

0b1000110

• Again: we add 0b101 so that it is shifted to the endof the block of ones, and subtract from it 0b101shifted to the beginning of the block of ones.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-90a

(C) Anton Setzer 2002 (except for pictures)

Mult. of Unsigned NumbersResult Signed (Cont.)

• If we start from the right when detecting blocksof ones in the multiplicand, we first detect thebeginning of the block of ones, then the end of it.Therefore we have to subtract first 0b101, and thenadd it (shifted accordingly).

• If we subtract 0b101 first, we get a negativenumber. In order to deal with that, we treatthe result as signed numbers in two’s complement.Then we have to convert 0b101 into a signednumber before subtracting or adding it – can bedone by adding at least one 0 on the left side.Further, in order to represent the result, a 6-bitsigned number, we need 7 bits in order to representit as an unsigned number.

The calculation is as follows (all calculations arecarried out as signed numbers):

0b111 · 0b101 =

0- 0101

1111011+ 0101

0100011

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-91

(C) Anton Setzer 2002 (except for pictures)

Mult. of Unsigned NumbersResult Signed (Cont.)

• Any binary number can be split into several blocksof ones.

∗ Example: 0 111︸︷︷︸ 000 1︸︷︷︸ 000 111︸︷︷︸.• A number is the sum of numbers, which consist of

selecting one of these blocks only.

∗ 01110001000111 = 01110000000000+00000001000000+00000000000111

• Multiplying x with the previous number is theresult of multiplying it with each of0b01110000000000,0b00000001000000,0b00000000000111,and adding the results.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-92

(C) Anton Setzer 2002 (except for pictures)

Mult. of Unsigned NumbersResult Signed (Cont.)

So when multiplying the above number with x we

• start with 0,y

• subtract x shifted 0 bits to the left,

∗ (beginning of the first block of ones)

• add x shifted 3 bits to the left,

∗ (after the end of the first block of ones)

• subtract x shifted 6 bits to the left,

∗ (beginning of the 2nd block of ones)

• add x shifted 7 bits to the left,

∗ (after the end of the 2nd block of ones)

• subtract x shifted 10 bits to the left,

∗ (beginning of the 3rd block of ones)

• add x shifted 13 bits to the left

∗ (after the end of the 3rd block of ones).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-92a

(C) Anton Setzer 2002 (except for pictures)

Mult. of Unsigned NumbersResult Signed (Cont.)

Example:

0b11011 · 0b110 =

0− 0110

111111010+ 0110

000010010− 0110

111100010+ 0110

010100010

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-93

(C) Anton Setzer 2002 (except for pictures)

Booth’s Algorithm for the Multiplicationof m and m′ (as Unsigned Numbers,

Non-Optimized Version)

• Let m, m′ be k bit-unsigned numbers.sum is in the following a 2k + 1 bit-signed number.

sum := 0for i = 1 to (k + 1) do

beginif ith LSB of m is 1 and

(i = 1 or (i− 1)th LSB of m is 0)then

subtract m′ shifted (i− 1) bits leftas a (2k + 1)bit two’s compl. numb.

if ith LSB of m is 0 andi > 1 and (i− 1)th LSB of m is 1

thenadd m′ shifted (i− 1) bits leftas a (2k + 1)-bit two’s compl. num.

endsum is result

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-94

(C) Anton Setzer 2002 (except for pictures)

Example for Mult. of Unsigned Numb.with Non-Optim. Booth’s algorithm

m m′ sum(un- (un- (signed)signed) signed)

Initialization 101 110 0000 000Step 11st LSB(m) =1 101 110 - 0000 110Subtract m′ 1111 010Step 22nd LSB(m)=01st LSB(m) =1 101 110Add m′ to sum + 0001 100shifted once 0000 110Step 33rd LSB(m)=12nd LSB(m) =0 101 110Subtract m′ - 0011 000shifted twice 1101 110Step 44th LSB(m)=03rd LSB(m) =1 101 110Add m′ + 0110 000shifted 3 times 0011 110

0b101 · 0b110 = 5 · 6 = 30 = 0b11110.From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-95

(C) Anton Setzer 2002 (except for pictures)

(ii) Optimization

• Similar steps as for the naive algorithm:

– Shift sum to the left, so thatsubtraction/addition happens only in the(k + 1) MSBs of sum.

– Therefore in intermediate steps sum has to beshifted to the right.

– Store m′ in the LSBs of sum which are not used.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-96

(C) Anton Setzer 2002 (except for pictures)

Optimization (Cont.)

• The following differs from what was done for naivemultiplication:

– We need to consider the LSB of m′ and theprevious bit.∗ Therefore additional bit sum−1 required,

which keeps the previous LSB.– We have to carry out the complete procedure

k + 1 times.∗ Therefore we need to shift m (k+ 1) times to

the right, and treat an implicit 0 to the left ofm.∗ This requires one extra bit in sum.⇒ Sum needs to be treated as a (2k + 2)-bit

number.– Whenever we subtract, we obtain a sum with a

block of ones to the left.∗ A block of ones is represented by a MSB 1.∗ A block of zeros is represented by a MSB 0.

– Therefore when shifting sum to the right, thosehidden zeros and ones turn up.

– Arithmetic shift required:When shifting sum to the right, the new MSB isthe old MSB.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-97

(C) Anton Setzer 2002 (except for pictures)

Arithmetic Shift

•::::::::::::::::::::::Arithmetic

:::::::::::shift of x to the right means:

– We shift x to the right one bit.– But new MSB is the same as the old MSB.– Examples:∗ 1 0 0 1 0 1↓↘ ↘ ↘ ↘ ↘1 1 0 0 1 0

∗ 0 1 0 0 1 0↓↘ ↘ ↘ ↘ ↘0 0 1 0 0 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-98

(C) Anton Setzer 2002 (except for pictures)

Booth’s Algorithm for the Multiplicationof m and m′ (as Unsigned Numbers)

Let m, m′ have k bits.sum has in the following 2k + 2 (not 2k!!!) bits.

sum := 0Set the k LSBs of sum to msum−1 := 0for i = 1 to k + 1 do

beginif LSB(sum) = 1 and sum−1 = 0 then

subtract m′ as a (k + 1)-bit numberfrom the k + 1 MSBs of sum

and ignore a borrowif LSB(sum) = 0 and sum−1 = 1 then

add m′ as a (k + 1)-bit numberto the k + 1 MSBs of sum

and ignore a carrysum−1 := LSB(sum)

Arithmetic shift right of sum by one bitend

sum is result

Two examples follow.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-99

(C) Anton Setzer 2002 (except for pictures)

m m′ sum sum−1

(un- (un- (signed)sign) sign)

Initialization 011 101 0000| 0011 0Step 1aLSB(sum) =1sum−1=0 011 101 - 0101Subtract m′ 1011| 0011 0Step 1bArithmetic shift 011 101 1101 1|001 1Step 2aLSB(sum) =

sum−1 011 101No action 1101 1|001 1Step 2bArithmetic shift 011 101 1110 11|00 1Step 3aLSB(sum) =0sum−1=1 011 101 +0101add m′ 0011 11|00 1StepbArithmetic shift 011 101 0001 111|0 0Step 4aLSB(sum)=sum−1 011 101No action 0001 111|0 0Step 4bArithmetic shift 011 101 0000 1111| 0

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-100

(C) Anton Setzer 2002 (except for pictures)

Remark on Previous Slide

• Subtraction of 0b101 on previous slide can bedone using the naive algorithm (but ignore theborrow!!!).

• Or instead of subtracting 0b101, add -0b101 as a4-bit number, which is (1011)signed.

– Bitwise complement of 0101 is 1010.– Adding one yields 1011.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-101

(C) Anton Setzer 2002 (except for pictures)

m m′ sum sum−1

Initialization 111 011 0000|0111 0Step 1aLSB(sum) =1sum−1=0 111 011 -011Subtract m′ 1101|0111 0Step 1bArithmetic shift 111 011 1110 1|011 1Step 2aLSB(sum) =

sum−1

No action 111 011 1110 1|011 1Step 2bArithmetic shift 111 011 1111 01|01 1Step 3aLSB(sum) =

sum−1

No action 111 011 1111 01|01 1Step 3bArithmetic shift 111 011 1111 101|0 1Step 4aLSB(sum)= 0sum−1 = 1 111 011 +011Add m′ 0010 101|0 1Step 4bArithmetic shift 111 011 0001 0101| 0

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-102

(C) Anton Setzer 2002 (except for pictures)

(iii) Multiplication ofSigned Numbers

• If m′ is now a signed number (but m unsigned),the algorithm works as before.

– Except that sum can now be a (2k + 1)-bitnumber.

– Carry can be ignored.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-103

(C) Anton Setzer 2002 (except for pictures)

Multiplication ofSigned Numbers (Cont.)

• If both m and m′ are signed numbers, a leadingblock of ones in m has to be treated differently:Compare

∗ 0b1110011 = 27 − 24 + (22 − 1), and∗ (1110011)signed = −24 + (22 − 1).

• When multiplying m′ with m = (111011)signed weomit the addition of m′ 7 times shifted to theright, which we carry out when multiplying m with0b111011.

• In general the addition of m′ (k + 1) times shiftedto the left has to be omitted.

• So the loop has to be carried out k times, not(k + 1) times.

• Then sum requires only 2k bits (we don’t needspace for the 0 in front of m).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-104

(C) Anton Setzer 2002 (except for pictures)

Booth’s Algorithm for the Multiplicationof Signed Numbers m and m′

Let m, m′ be k-bit signed numbers.Let sum be a 2k bits signed number.

sum := 0Set the k LSBs of sum to msum−1 := 0for i = 1 to k do

beginif LSB(sum) = 1 and sum−1 = 0 then

subtract m′ as a k-bit signed numberfrom the k MSBs of sum

and ignore any borrowif LSB(sum) = 0 and sum−1 = 1 then

add m′ as a k-bit signed numberto the k MSBs of sum

and ignore any carrysum−1 := LSB(sum)Arithmetic shift right of sum by one bitend

sum is result

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-105

(C) Anton Setzer 2002 (except for pictures)

Example for Multiplication of SignedNumbers with Booth’s algorithm

m m′ sum sum−1

Initialization 101 110 000 |101 0Step 1aLSB(sum) =1sum−1=0 -110Subtract m′ 101 110 010 |101 0Step 1bArithmetic shift 101 110 001 0|10 1Step 2aLSB(sum) =0sum−1 = 1 +110Add m′ 101 110 111 0|10 1Step 2bArithmetic shift 101 110 111 10|1 0Step 3aLSB(sum) =1sum−1=0 -110Subtract m′ 101 110 001 10|1 0Step 3bArithmetic shift 101 110 000 110 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-106

(C) Anton Setzer 2002 (except for pictures)

• Verification that result on last slide is correct:

– 101 represents −4 + 1 = −3.– 110 represents −4 + 2 = −2.– (−3) · (−2) = 6 is represented by 000110 (with

6 bits!)Note that subtraction of 110 means addition of010.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-107

(C) Anton Setzer 2002 (except for pictures)

(d) Fixed Point Numbers

• In decimal representation, real numbers can bewritten as numbers with potentially infinite digitsafter the point:

– π = 3.1415926 . . ..

• Analysis:

– All digits get a weight in terms of powers of 10.– Left to the point, this is a positive power of 10,– right of it a negative one:√

111 = 1 0 . 5 3 5 5 . . .↓ ↓ ↓ ↓ ↓ ↓

101 100 10−1 10−2 10−3 10−4

= = = = = =10 1 1

101

1001

10001

10000

= 1 · 101 + 0 · 100 + 5 · 10−1

+3 · 10−2 + 5 · 10−3 + 5 · 10−4 + · · ·

• Fixed point numbers:

– Round or cut off after a fixed number of digitsafter the point.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-108

(C) Anton Setzer 2002 (except for pictures)

Binary Fixed Point Numbers

• With basis 2, similar representation:

0b 1 0 . 1 0 1 0↓ ↓ ↓ ↓ ↓ ↓21 20 2−1 2−2 2−3 2−4

= = = = = =2 1 1

214

18

116

= 1 · 21 + 0 · 20 + 1 · 2−1

+0 · 2−2 + 1 · 2−3 + 0 · 2−4 + · · ·

= 1 · 2 + 0 · 1 + 1 · 0.5+0 · 0.25 + 1 · 0.125 + 0 · 0.0625 + · · ·

= 2.6875

• With the above technique we can convert frombinary to decimal.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-109

(C) Anton Setzer 2002 (except for pictures)

Conversion from Decimal to Binary

• Convert the whole-numbered part (the part beforethe point) into a binary number.

– E.g. whole-numbered part of 13.25 is13 = 0b1101

• Convert the fractional part (the part after thepoint)

– E.g. fractional part of 13.25 is 0.25 = 0b0.01.

into the fractional part of a binary number with kbits after the point as follows:

– Multiply the number by 2.– If the result is ≥ 1, the next bit is 1, subtract 1

from the result.– If the result is < 1, the next bit is 0– Iterate this k times.

• Add binary representation of whole-numbered andfractional part.

– E.g. 13.25 = 0b1101.01.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-110

(C) Anton Setzer 2002 (except for pictures)

Conversion from Decimal to Binary (Cont.)

• More formally, the following algorithm converts thefractional part

(k = number of bits after point required).

x := fractional part of the numberfor i = 1 to k do

beginx := x · 2if x < 1 then kth bit after point

of the result is 0else kth bit after point

of the result is 1x := x− 1end

end

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-111

(C) Anton Setzer 2002 (except for pictures)

Example

We write 10.5355 as a fixed point binary number.(This is not

√111, since we have rounded this number

already).10 = 0b1010.

Fraction Digit0.5355

· 2 = 1.0710 1 first bit after point−1 = 0.0710· 2 = 0.1420 0· 2 = 0.2840 0· 2 = 0.5680 0· 2 = 1.1360 1−1 = 0.1360· 2 = 0.2320 0 6th bit after point

10.5350 = 0b1010.100010 with 6 bits after the point.

Remark: Note that the first bit obtained is thefirst bit after the point, the next onethe second etc.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-112

(C) Anton Setzer 2002 (except for pictures)

Remark

• The full (not-fixed-point) binary representation ofthe decimal number 0.2 is0.00110011001100110011 . . ..

• So numbers might have exact representation as adecimal fixed point number, but not as a binaryfixed point number.

• Consequence: Rounding errors occur.

• A calculation with decimal fixed-pointrepresentation and with binary fixed-pointrepresentation might have different results.

• Important when working with financial data.

• However: There is no optimal number system.Fractions a

b can be represented exactly:

– In binary if b is of the form 2n.– In decimal if b is of the form 2n5m.– In a number system with basis 30, if b is of the

form 2n3m5k.– etc., there we can always get a better one.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-113

(C) Anton Setzer 2002 (except for pictures)

Facts about Shifting Fixed Point Numbers

Fact

Shifting the point in a fixed point number once tothe right is the same as multiplying the denotednumber by 2.Shifting the right is the same as dividing thedenoted number by 2.

• Examples:

∗ 0b10.01 = 22 + 2−2.0b100.1 = 23 + 2−1 = (22 + 2−2) · 2.∗ 0b10.01 = 22 + 2−2.

0b1.001 = 21 + 2−3 = (22 + 2−2)/2.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-114

(C) Anton Setzer 2002 (except for pictures)

Facts about Shifting Fixed Point Numbers

• Proof:

– Let y be the original fixed point number,z the number shifted once to the right.

– If a bit in y has weight 2l, where l is∗ positive, if occurring left of the point,∗ negative, if occurring right of the pointthen the weight of the corresponding bit in z is2l+1

– 0by is the sum of the weight of ones in y,0bz the sum of the weight of ones in zwhich is the sum of twice the weight of the onesin z,which is 2 times 0bz.

– Shifting a number to the left is just the inverseof the previous operation, resulting in dividing itby 2.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-115

(C) Anton Setzer 2002 (except for pictures)

Facts about Shifting Fixed Point Numbers

Fact

Shifting the point in a fixed point number l bitsto the right is the same as multiplying the denotednumber by 2l.Shifting it l bits to the left is the same as dividingit by 2l.

• Examples:

– 0b10.01 = 22 + 2−2.0b1001 = 24 + 20 = (22 + 2−2) · 22.

– 0b10.01 = 22 + 2−2.0b0.1001 = 2−1 + 2−4 = (22 + 2−2)/22.

• Proof: l times shifting the point to the left/rightresults in l times dividing/multiplying the numberby 2 which is the same as dividing/multiplying itby 2l.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-116

(C) Anton Setzer 2002 (except for pictures)

Operations on Fixed Point Numbers

General Method:

• Write the operands as fixed point numberswith the same number of bits to the right ofthe point.

• Perform the operation as for integers.

• Now reintroduce the point in the result:

– Case addition and subtraction:If it initially was after the kth bit from the right,it should be in the result after the kth bit fromthe right.

– Case multiplication:If it initially was after the kth bit from the right,it should be in the result after the 2k-th bit fromthe right.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-117

(C) Anton Setzer 2002 (except for pictures)

At the end one might omit

• leading zeros

– e.g. convert 010.01 into 10.01;

• zeros as least significand bits after the point

– e.g.. convert 0.010 into 0.01.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-118

(C) Anton Setzer 2002 (except for pictures)

Examples (all in binary)

Fixed Point Numbers Corresponding Integers1.01 + 0.01 = 1.10: 101 + 001 = 110:

1.010.011.10

101001110

Example with overflow in the addition10.01 + 1.11=100: 1001 + 0111=10000:

10.011.11

100.00

1001111

10000

10 - 1.11= 0.01: 1000 -111 = 1:

10.00- 1.1100.01

1000- 1110001

1.01 · 1.11= 10.0011: 101 · 111=100011:1.01

.101101

10.0011

101101

101100011

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-119

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Algorithms

• Assume m, m′ represented with k bits after thepoint.

• Let n, n′ be the result of omitting the point in m,m′.

• Then n = m · 2k, n′ = m′ · 2k.

– For instance, if k = 2, m = 1.01, thenn = 101 = 22 ·m.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-120

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Algorithms (Cont.)

• Case addition:

– Sum of n, n′ is 2k · (m+m′).– Reintroducing the point after k-bits means

division by 2k.– Result is m+m′, the correct result.

• Case subtraction: Similarly.

• Case multiplication:

– Product of n, n′ is 2k · 2k ·m ·m′ = 22k ·m ·m′.– Reintroducing the point after 2k bits means

division by 22k.– Result is m ·m′, the correct result.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-121

(C) Anton Setzer 2002 (except for pictures)

(e) Floating Point Numbers

• In decimal representation numbers representedlike0.23578 · 105, −0.99998 · 10−5, 0.0 · 100.

• In general we obtain the form

s · a · 10k ,

where– s = +1 or s = −1; s is called the sign.– a is a fixed point number (in decimal

representation) s.t. 0.1 ≤ a < 1 or a = 0;a is called significand or mantissa;

– k is a signed integer called the exponent.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-122

(C) Anton Setzer 2002 (except for pictures)

Floating Point Numbers

• Note that0.349 · 108 = 3 4900000

↑107

• So 10exponent (in the example 108)is 10 times the weight of the digit in decimalrepresentation most to the left that is not equal to0 (in the example 107).

• The significand is formed by the sequence of digitsstarting with this digit(in the example 349).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-123

(C) Anton Setzer 2002 (except for pictures)

Binary Floating Point Numbers

• Similarly, only basis 2:We write numbers as

s · a · 2e

– where s is the sign as before,– 0b0.1 = 1

2 ≤ a < 1 or a = 0

• If a is chosen as above, we call this a::::::::::::::::::::::normalized

::::::::::::::::floating

::::::::::::point

:::::::::::::::::::::::::::::::representation.

• If we omit the condition on a, s · a · 23 is calleda

:::::::::::::::::::::::::::::::non-normalized

:::::::::::::::::floating

:::::::::::::point

:::::::::::::::::::::::::::::::representation

of this number.

• There are several non-normalizedrepresentations of the same number, e.g.

– 0b0.1 · 25

– 0b0.01 · 26

– 0b1 · 24

represent the same number, of which the first oneis the normalized representation.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-124

(C) Anton Setzer 2002 (except for pictures)

Omission of Leading Ones

• If a 6= 0 then a is always of the form 0b0.1xyz · · ·,with x, y, z ∈ {0, 1}.

– So the first 1 has not to be stored, except fora = 0.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-125

(C) Anton Setzer 2002 (except for pictures)

Biased Exponents

• Instead of storing positive and negative exponentsas signed integers, one does the following:

– We add to the exponent a fixed number b > 0.

– The result is called the:::::::::::::biased

::::::::::::::::::::exponent.

∗ If the biased exponent is positive, the exponentis represented by the biased exponent as anunsigned number.∗ Otherwise the exponent cannot be represented.

– So a biased exponent of a represents theexponent a− b.

– With k bits we can represent unsigned numbersand therefore biased exponents in the range0, . . . , 2k − 1.

∗ Therefore we can represent unbiasedexponents in the range −b, . . . ,−b+ (2k− 1).· If k = 8, 2k − 1 = 255, and a good choice

of b is 127 or 128.(For historic reasons, 126 is used).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-126

(C) Anton Setzer 2002 (except for pictures)

Example

• Let k = 8, 2k = 256.

• If we fix the bias 126, then we have the followingrepresentations:

Exponent Biased Binary RepresentationExponent (with 8 bits)

-100 26 000110100 126 01111110

10 136 10001000-126 0 00000000129 255 11111111

With 8 bits we can represent exponents in the range−126, . . . , 129.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-127

(C) Anton Setzer 2002 (except for pictures)

The IEEE 754Floating Point Standard

We consider only the single format with 32 bits, thereexists as well a double format with 64 bits.Floating points are written in the form

Bit positions 0 1 – 8 9– 31Function sign biased exponent significand

• Sign 0 means a positive, 1 a negative number(so s ∈ {0, 1} represents sign (−1)s).

• The exponent is biased with bias 126, andrepresented by 8 bits.

– So exponents in the range −126, . . . , 129 couldbe represented.

– However exponent 129 used for infinity (∞)and NaN, see below, giving an effective rangeof −126, . . . , 128 for the exponent.

– If the exponent is > −126, the significand isinterpreted as having an additional leading 1, sothe effective significand has 24 bits, not 23 bits.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-128

(C) Anton Setzer 2002 (except for pictures)

Exponent -126

• If the exponent is −126 (so the biased exponent is0), a leading 1 is not implied.

– Especially significand 0 and biased exponent 0means value 0.∗ Because of the sign bit, two representations

of zero (+0, −0) exist.– This allows to represent numbers which in

normalized form have exponent less than −126as non-normalized numbers with exponent−126.∗ E.g. 0b0.1 · 2−128 = 0b0.001 · 2−126.

– Therefore the area of numbers close to 0 isrepresented in uniformly

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-129

(C) Anton Setzer 2002 (except for pictures)

Exponent -126 (Cont.)

• Therefore there is no gap between 0 and 0b0.1 ·2−126, the smallest normalized number greater > 0with exponent −126:

||||||||||||||||||||||||||||||||||||||||||||||||Using this approach0 ︸ ︷︷ ︸

non-normalized numbers

0b0.1 · 2−126

| ||||||||||||||||||Without it0b.1 · 2−126

.

• Without this approach, the smallest number > 0we can represent is0b0.1 · 2−126 = 2−127.In IEEE 754, it is0b0. 0000000000000000000000︸ ︷︷ ︸

22

1 · 2−126 = 2−149.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-129a

(C) Anton Setzer 2002 (except for pictures)

Infinity and NaN

• Exponent of all ones (0b11111111) with significand0 expresses positive or negative infinity, dependingon the sign (denoted by +∞, −∞).Used when overflow occurs:

– If n > 0 then n/0 = +∞.– If n < 0 then n/0 = −∞.– If n > 0 then n · (+∞) = +∞.– If n < 0 then n · (−∞) = +∞.– If n,m > 0, n · m cannot be represented, the

result is +∞.

• Exponent of all ones and non-zero significandrepresents NaN, not a number, for undefined (error,which cannot be associated to positive or negativeinfinity). Examples:

– 0/0 yields NaN.– (+∞) + (−∞) yields NaN.– But (+∞) + (+∞) yields +∞.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-130

(C) Anton Setzer 2002 (except for pictures)

Table for IEEE 754

Sign Biased Signi- ValueExpon. ficand

Positive 0 0 0 0Zero

Negative 1 0 0 −0Zero

Positive 0 0 f 6= 0 +0.fdenormalized ·2e−126

Negative 1 0 f 6= 0 −0.fdenormalized ·2e−126

Plus 0 0xFF 0 ∞Infinity =255Minus 1 0xFF 0 −∞Infinity =255

NaN 0 or 0xFF 6= 0 NaN1 =255

Positive 0 0 < e f +0.1fnormalized < 255 ·2e−126

Negative 1 0 < e f −0.1fnormalized < 255 ·2e−126

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-131

(C) Anton Setzer 2002 (except for pictures)

Remarks on the Bias in theIEEE Floating Point Standard

• In the literature, one often sees the bias 127 insteadof 126 as on the last slides.

• The reason for this difference is that they expandsignificand f to 1.f instead of 0.1f as we did it.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-132

(C) Anton Setzer 2002 (except for pictures)

Examples

−0.75 = (−1) · 0b0.11 · 20

= (−1) · 0b0.11 · 2−126+126

= (−1) · 0b0.11 · 2−126+0b1111110

is represented by(first 1 of the significand omitted!)

1︸︷︷︸sign

01111110︸ ︷︷ ︸exponent

10000000000000000000000︸ ︷︷ ︸significand

.

0.4375 = 0b0.111 · 2−1

= 0b0.111 · 2−126+125

= 0b0.111 · 2−126+0b1111101

is represented by(first 1 of the significand omitted!)

0︸︷︷︸sign

01111101︸ ︷︷ ︸exponent

11000000000000000000000︸ ︷︷ ︸significand

.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-133

(C) Anton Setzer 2002 (except for pictures)

• 0.5 · 2−126 = 0b0.1 · 2−126+0

is represented by(first 1 of the significand not omitted!)

0︸︷︷︸sign

00000000︸ ︷︷ ︸exponent

10000000000000000000000︸ ︷︷ ︸significand

.

• 0.25 · 2−126 = 0b0.01 · 2−126+0

is represented by0︸︷︷︸

sign00000000︸ ︷︷ ︸exponent

01000000000000000000000︸ ︷︷ ︸significand

.

• 0 is represented by0︸︷︷︸

sign00000000︸ ︷︷ ︸exponent

00000000000000000000000︸ ︷︷ ︸significand

.

and1︸︷︷︸

sign00000000︸ ︷︷ ︸exponent

00000000000000000000000︸ ︷︷ ︸significand

.

• Infinity is represented by0︸︷︷︸

sign11111111︸ ︷︷ ︸exponent

00000000000000000000000︸ ︷︷ ︸significand

.

• 0︸︷︷︸sign

11111111︸ ︷︷ ︸exponent

10001000000000000000000︸ ︷︷ ︸significand

.

is one (of many) representation of NaN.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-134

(C) Anton Setzer 2002 (except for pictures)

Arithmetic Operationson Floating Point Numbers

• Simplification here:

– We use here the un-biased (original) exponents.– We don’t omit in the representation of the

significand a leading 1.

• Addition:

– Represent both numbers in the form s · a · 2k,where k is the maximum of the exponents of thenormalized form of both numbers.

– Let the:::::::::::::signed

:::::::::::::::::::::::significand be s · a.

– Add the two signed significands.– Write the result as s · a where s ∈ {−1, 1}, a is

positive.– A non-normalized form of the result is s · a · 2k.– Normalize the result, i.e. write it as s · a′ · 2k′

s.t. this representation is in normalized form.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-135

(C) Anton Setzer 2002 (except for pictures)

Examples

0b0.1011 · 20b110︸︷︷︸

=6 + 0b0.101 · 20b011︸︷︷︸

=3

= 0b0.1011 · 20b110 + 0b0.000101︸ ︷︷ ︸=0b101

3 times shifted

to the right

·20b110︸︷︷︸

=6

= 0b0.110001 · 20b110

0b0.10 · 20b000 + 0b0.10 · 20b000

= 0b1.0 · 20b000

= 0b0.1 · 20b001

0b0.1011 · 20b110 + (−1) · 0b0.101 · 20b011

= 0b0.1011 · 20b110 + (−1) · 0b0.000101 · 20b110

= (0b0.1011− 0b0.000101) · 20b110

= 0b0.100111 · 20b110

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-136

(C) Anton Setzer 2002 (except for pictures)

Examples (Cont.)

(−1) · 0b0.1011 · 20b110 + (−1) · 0b0.101 · 20b101

= (−1) · 0b0.1011 · 20b110 + (−1) · 0b0.0101 · 20b110

= (−0b0.1011− 0b0.0101) · 20b110

= −0b1.0 · 20b110

= −0b0.1 · 20b111

Subtraction of Floating Point Numbers

• Subtraction is carried out similarly, carry outsubtraction instead of addition for the signedsignificands.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-137

(C) Anton Setzer 2002 (except for pictures)

• Multiplication:

(s0 · a0 · 2k0) · (s1 · a1 · 2k1)

= (s0 · s1) · (a0 · a1) · 2k0+k1

• Therefore the sign of the result is s0 XOR s1.

• The significand of the result is the product of thesignificands.

• The exponent is the sum of the exponents.

– (when working with biased exponents, one wouldadd both biased exponents and subtract from thesum the bias).

• If one does not obtain a number in normal form,adapt the result.

• Examples:(0b0.1 · 20b01) · (0b0.1 · 20b01) = 0b0.01 · 20b10 =0b0.1 · 20b01

((−1) · 0b0.11 · 20b10) · ((−1) · 0b0.11 · 20b10) =0b0.1001 · 20b100.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-138

(C) Anton Setzer 2002 (except for pictures)

From the JavaTM Tutorialhttp://www.java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

Primitive Data Types

Keyword Description Size/Format

(integers)

byte Byte-length integer 8-bit two’s complement

short Short integer 16-bit two’s complement

int Integer 32-bit two’s complement

long Long integer 64-bit two’s complement

(real numbers)

float Single-precision floating point 32-bit IEEE 754

double Double-precision floating point 64-bit IEEE 754

(other types)

char A single character 16-bit Unicode character

boolean A boolean value (true or false) true or false

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-139

(C) Anton Setzer 2002 (except for pictures)

(f) Other Scalar Data Types

Characters, Texts

Standard Representation of Characters: ASCII,uses 7 bits which represent numbers between 0 and127 to represent on character. Table:

AS- Char AS- Char AS- Char AS- CharCII CII CII CII

0 NUL 16 DLE 32 SP 48 0

1 SOH 17 DC1 33 ! 49 1

2 STX 18 DC2 34 ” 50 2

3 ETX 19 DC3 35 # 51 3

4 EOT 20 DC4 36 $ 52 4

5 ENQ 21 NAK 37 % 53 5

6 ACK 22 SYN 38 & 54 6

7 BEL 23 ETB 39 ′ 55 7

8 BS 24 CAN 40 ( 56 8

9 HT 25 EM 41 ) 57 9

10 LF 26 SUB 42 ∗ 58 :

11 VT 27 ESC 43 + 59 ;

12 FF 28 FS 44 , 60 <

13 CR 29 GS 45 − 61 =

14 SO 30 RS 46 . 62 >

15 SI 31 US 47 / 63 ?

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-140

(C) Anton Setzer 2002 (except for pictures)

AS- Char AS- Char AS- Char AS- CharCII CII CII CII

64 @ 80 P 96 ‘ 112 p

65 A 81 Q 97 a 113 q

66 B 82 R 98 b 114 r

67 C 83 S 99 c 115 s

68 D 84 T 100 d 116 t

69 E 85 U 101 e 117 u

70 F 86 V 102 f 118 v

71 G 87 W 103 g 119 w

72 H 88 X 104 h 120 x

73 I 89 Y 105 i 121 y

74 J 90 Z 106 j 122 z

75 K 91 [ 107 k 123 {76 L 92 \ 108 l 124 |77 M 93 ] 109 m 125 }78 N 94 ˆ 110 n 126 ∼79 O 95 111 o 127 DEL

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-141

(C) Anton Setzer 2002 (except for pictures)

• IBM has extended ASCII (::::::::::::::::::extended

::::::::::::::::ASCII)

by adding 128 characters, especially symbolsoccurring in other European languages, as wellsome mathematical symbols.

• In total therefore 8 bits (= 1 byte) are needed toencode them.

• Characters 0 - 127 are identically with ASCII and128 - 255 are new.

• Unfortunately variants of this set of charactersexist, partially due to the fact that IBM’s set isnot allowed to be used on non-IBM-licensed PCs.

• Characters 128 - 255 are not standardized.

• Some characters are control symbol, which cannotbe displayed or printed (eg. ASCII 7 is an acousticsignal, “bell”; ASCII 10 is a line feed).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-142

(C) Anton Setzer 2002 (except for pictures)

Other Character Encodings

• Unicode (allows to represent internationalcharacters including Chinese, Japanese and Korean,with 16 bits.)– Standard in Java, XML.– Problem: requires twice as much space.– Variants exist, which encode the standard

Western alphabet using 1 bytes, non-Westernalphabets using 2 bytes, and control charactersto switch between 1-byte and 2-byte characterencodings.

– Unicode still doesn’t accomodate all existingalphabets (especially not the full range ofChinese/Korean/Japanese-Chinese characters).

• ISO/IEC 10646-1 (Universal Multiple-Octet CodedCharacter Set): system, which uses 32 bitencodings (although only 31 bits are effectivelyused).– Allows to accomodate for all alphabets.

• Lots of other encodings exist.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-143

(C) Anton Setzer 2002 (except for pictures)

Representation of Texts

• Text represented as sequences of characters.

• Advanced formats: Programming languagesfor storing text with formatting information(postscript, pdf, TeX, RTF, Word, OpenofficeXML-format).

• Various compressing algorithms in order to savespace.

Representation of Graphics and Multimedia

• Graphics stored as bit maps for representing pixelwith colours.

– Graphics interchange format, gif), with somecompression.

• Music, video data in various formats.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-144

(C) Anton Setzer 2002 (except for pictures)

Compression of Multimedia

• Problem when storing multi-media: data requirea lot of space.Good compression algorithms needed.

• Use of the fact that small changes betweenconsecutive images, sounds.

• Some compression algorithm lead to loss of data(

:::::::::lossy algorithms).

• For instance omission of invisible colour changes inareas where texture is particularly vivid.

• Examples:JPEG (images), MPEG (video, audio;computationally complex to code and decode),Quicktime (video).

– MPEG-2 achieves compression rates of morethan 100:1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-145

(C) Anton Setzer 2002 (except for pictures)

(g) Compound Data Types

• Compound data types are data types, where eachunit consists of several units.

• Main examples:

– Arrays.– Strings.– Records.– Classes (not treated in this lecture).

• Main ideas: Store data items in sequence.

• Strings stored as arrays of characters.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-146

(C) Anton Setzer 2002 (except for pictures)

Storage of Records

• Records are stored by saving the elements in asequence:

– Example:studentMark= record

studentID: integermark: double

end– Stored as one integer followed by one double

precision floating point number.E.g., if an integer is stored as 4 bytes, a double-precision floating point number is stored as 8bytes, and addresses referring to byte level:∗ Addresses 0x0000 to 0x0003 containstudentID,∗ 0x0004 to 0x000B contain mark,∗ Each entry requires 12 bytes.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-147

(C) Anton Setzer 2002 (except for pictures)

Storage of Arrays

• Assume a:array[0...n] of intand int requires 4 bytes.

• a can be stored by storing a[0], a[1], a[2] etc. insequence.

• Assume that memory addresses refer to bytes,and that a[0] is to be stored at location 0x1000.

• Then we store

– a[0] at addresses 0x1000− 0x1003.– a[1] at addresses 0x1004− 0x1007.– a[2] at addresses 0x1008− 0x100B.– a[3] at addresses 0x100C − 0x100F– etc.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-148

(C) Anton Setzer 2002 (except for pictures)

Storage of Arrays (Cont.)

}}}}}}

a[0]

a[1]

a[2]

a[3]

a[5]

a[4]

1014

1010

100C

1008

1004

1000

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-149

(C) Anton Setzer 2002 (except for pictures)

Two-Dimensional Arrays

• Assume

– a: array[0 .. 199,0..99] of int,– int stored as 4 bytes,– addresses up to byte level,– a stored beginning with memory location A.

• Then the address of the first byte

– of a[0,0] is A,– of a[0,1] is A+ 4, etc.– of a[0,k] is A+ 4 · k,– of a[0,99] is A+ 4 · 99,– of a[1,0] is A+ 4 · 100,– of a[1,1] is A+ 4 · 100 + 4– of a[i,0] is A+ 4 · 100 · i and– of a[i,j] is A+ 4 · 100 · i+ 4 · j.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-150

(C) Anton Setzer 2002 (except for pictures)

• So the picture is like this(elem = element of the array, addr = address):

elem. a[0, 0] a[0, 1] · · · a[0, i] · · · a[0, 99]addr. A A+ · · · A+ · · · A+

4 · · · 4i · · · 396

elem. a[1, 0] a[1, 1] · · · a[1, i] · · · a[1, 99]addr. A+ A+ · · · A+ · · · A+

400 400+ · · · 400+ · · · 400+4 · · · 4i · · · 396

· · ·elem. a[j, 0] a[j, 1] · · · a[j, i] · · · a[j, 99]addr. A+ A+ · · · A+ · · · A+

400j 400j+ · · · 400j+ · · · 400j+4 · · · 4i · · · 396

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-151

(C) Anton Setzer 2002 (except for pictures)

Exercise: What is the address of a[i,j], if

• a: array[k ..l, m .. n] of B,

• each element of B requires f bytes

• a[k,m] is stored at address A?

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4 4-152

(C) Anton Setzer 2002 (except for pictures)

Supplementary Materialfor Sec. 4.

(a) Correctness proofs of algorithms on signed numbers

(b) Signed fixed point numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-153

(C) Anton Setzer 2002 (except for pictures)

(a) Correctness Proofsfor Algorithms on Signed Numbers

Correctness of the Algorithmfor Negating Numbers

in Two’s Complement Form

• First observe: forming the bitwise complement of ak-bit unsigned number is the same as subtractingit from 0b 1 · · · 1︸ ︷︷ ︸

k

= 2k − 1:

– Example:

0b1111− 0b1010 =0b 1 1 1 1

− 0b 1 0 1 00b 0 1 0 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-154

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Algorithm (Cont.)

• Case 1: m > 0.

– Then z = 0x where x is a k − 1-bit number,m = (z)signed = 0bx = 0bz.

– Bitwise complement of z represents the unsignednumber (2k − 1)− 0bz = 2k − 1−m.

– Adding one yields an y s.t.0by = 2k − 1−m+ 1 = 2k −m.

– That number has MSB 1, so it is negative as asigned number.

– By the lemma above (y)signed = 0by − 2k =(2k −m)− 2k = −m.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-155

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Algorithm (Cont.)

• Case 2: m = 0.

– Then x = 0 · · · 0︸ ︷︷ ︸k

, Bitwise complement of x is

1 · · · 1︸ ︷︷ ︸k

. Adding one and ignoring carry yields

0 · · · 0︸ ︷︷ ︸k

, which is as signed number 0 or −m.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-156

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Algorithm (Cont.) (Cont.)

• Case 3: m < 0.

– Then z = 1x where x is a (k − 1)-bit number,and m = −2k−1 + 0bx.

– Bitwise complement of 1x is 0y where y is thebitwise complement of x.∗ (0y)signed = 0by

= (2k−1 − 1)− 0bx.

∗ If x = 0 · · · 0︸ ︷︷ ︸, then y = 1 · · · 1︸ ︷︷ ︸k−1

and adding one

to 0b0y yields 1 0 · · · 0︸ ︷︷ ︸k−1

which is not −m.

(−m = 2k−1 is out of the range ofrepresentable numbers and can therefore notbe represented.)∗ Otherwise, adding one to 0y yields a number

0u and we have(0u)signed = 0bu

= (2k−1 − 1)− 0bx+ 1= 2k−1 − 0bx= 2k−1 − (m+ 2k−1)= −m

.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-157

(C) Anton Setzer 2002 (except for pictures)

Some Facts about Shifting Numbersin Two’s Complement Form

(The following facts are only intended for thoseinterested in proofs).

Fact

Let y be a sequence of k bits.Therefore (y)signed is the value it denotes as asigned number in two’s complement form.y 0 · · · 0︸ ︷︷ ︸

l

is the number shifted l times to the left,

(y 0 · · · 0︸ ︷︷ ︸l

)signed the number it denotes.

Then (y 0 · · · 0︸ ︷︷ ︸l

)signed = (y)signed · 2l.

• Examples:

– (011)signed = 21 + 20.(01100)signed = 23 + 22 = 22(21 + 20).

– (101)signed = −22 + 20.(1010)signed = −23 + 21 = 2(−22 + 20).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-158

(C) Anton Setzer 2002 (except for pictures)

Some Facts about Shifting Numbersin Two’s Complement Form

• Proof:A bit in y in y 0 · · · 0︸ ︷︷ ︸

l

has 2l times the weight in y

as signed numbers:

– If it is the MSB, it has weight −2k−1 in y, andweight −2k−1+l = −2k−1 · 2l in y 0 · 0︸︷︷︸

l

.

– Otherwise, it has weight 2j in y, and weight2j+l = 2j · · · 2l in y 0 · 0︸︷︷︸

l

.

– The l LSBs don’t contribute to the value of0by 0 · · · 0︸ ︷︷ ︸

l

.

– So (y 0 · · · 0︸ ︷︷ ︸l

)signed is the sum of the weight of

the ones in y, each multiplied by 2l, which is(y)signed · 2l.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-159

(C) Anton Setzer 2002 (except for pictures)

Some Facts about Shifting Numbersin Two’s Complement Form

Fact

Let yz be a sequence of k bits, with z being the lLSBs (l < k).So y is the sequence of bits shifted l times to theright, z the bits omitted. Then (y)signed is theresult of dividing (yz)signed by 2l with remainder,0bz is the remainder.

• Examples:

– (0110)signed = 22 + 21.(01)signed = 20 = (22 + 21)÷ 22.(0b10 = 21 is the remainder.

– (1110)signed = −23 + 22 + 21.(11)signed = −21 + 20 = (−23 + 22 + 21)÷ 22.(0b10 = 21 is the remainder.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-160

(C) Anton Setzer 2002 (except for pictures)

Some Facts about Shifting Numbersin Two’s Complement Form

• Proof:

– (yz)signed = (y 0 · · · 0︸ ︷︷ ︸l

)signed + 0bz.

– (y 0 · · · 0︸ ︷︷ ︸l

)signed = (y)signed · 2l.

– 0bz < 2l

(follows as in the proofs for unsigned numbersabove).

– So (yz)signed = (y)signed · 2l + 0bz with 0 ≤0bz < 2l.

– This means that (y)signed = (yz)signed ÷ 2l, 0bzis the remainder.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-161

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Algorithmfor Adding Integers

in Two’s Complement Form

• Many Cases!!!

• Case 1: m, m′ are both positive.

– So m = (0x)signed, m′ = (0y)signed, where– m = 0bx, m′ = 0by,x, y (k − 1)-bit numbers.

– Case sum of x, y as unsigned (k−1)-bit numbersgives overflow.∗ Then sum of 0x and 0y as signed numbers is

1z:

0x+ 0y

carry:11z

∗ This is negative interpreted as two’scomplement numbers!∗ Further sum of 0b0x and 0b0y is at least 2k−1,

too big to be represented.∗ So we have overflow, as is the result of the

algorithm.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-162

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Addition Algorithm (Cont.)

– Case sum of x, y as unsigned (k−1)-bit numbersgives no overflow.∗ Let sum be z.∗ Then sum of 0x and 0y as signed numbers is

0z:

0x+0y

0z

∗ This is the sum of 0x and 0y.∗ The algorithm computes the correct result.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-163

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Addition Algorithm (Cont.)

• Case m, m′ are both negative.

– So m = (1x)signed, m′ = (1y)signed.– (1x)signed = −2k−1 + 0bx,

i.e. 0bx = (1x)signed + 2k−1 = m+ 2k−1.– 0by = m′ + 2k−1.– Sum of 0bx and 0by is

2k−1 +m+ 2k−1 +m′ = 2k +m+m′.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-164

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Addition Algorithm (Cont.)

– Case sum of x, y as unsigned (k−1)-bit numbershas an overflow.∗ Then sum is of the form z (z is a (k − 1)-bit

number) plus carry.∗ Since· z ignores this carry,· carry would have weight 2k−1,

∗ it follows that0bz= 2k +m+m′︸ ︷︷ ︸

x+y

−2k−1

= 2k−1 +m+m′

.

∗ Sum of 1x, 1y as unsigned number is 1z pluscarry.

1x+ 1y

carry:11z

∗ (1z)signed= −2k−1 + (2k−1 +m+m′)︸ ︷︷ ︸z

= m+m′

.

∗ Algorithm gives result 1z (carry ignored), thecorrect result.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-165

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Addition Algorithm (Cont.)

– Case sum of x, y as unsigned (k−1)-bit numbershas no overflow.∗ 0bz= 2k−1 +m︸ ︷︷ ︸

x

+ 2k−1 +m′︸ ︷︷ ︸y

= 2k +m+m′

.

∗ Since we have no overflow,2k +m+m′ < 2k−1.∗ Therefore m+m′ < −2k−1.∗ m+m′ cannot be represented, underflow!.∗ Further, sum of 1x and 1y is 0z plus carry:

1x+1y

0z

∗ This is a positive number.∗ Therefore algorithm signals underflow, the

correct result.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-166

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Addition Algorithm (Cont.)

• Case m positive, m′ negative.(Case m′ positive, m negative will be treated inthe same way).

– So m = (0x)signed, m′ = (1y)signed, where– x, y are k − 1-bit-numbers,

– 0bx = m,– 0by = 2k−1 +m′.– Sum of 0bx and 0by is

2k−1 +m+m′.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-167

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Addition Algorithm (Cont.)

• Case sum of x, y as unsigned (k − 1)-bit numbershas an overflow.

– Then sum is of the form z(z is a (k − 1)-bit number) plus carry.

– Since∗ z ignores this carry,∗ carry would have weight 2k−1,

– it follows that0bz= 2k−1 +m+m′ − 2k−1

= m+m′.

– Sum of 1x, 0y as unsigned number is 0z pluscarry:

1x+ 0y

carry:10z

– (0z)signed = m+m′.– Algorithm gives result 0z (carry ignored), the

correct result.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-168

(C) Anton Setzer 2002 (except for pictures)

Correctness of the Addition Algorithm (Cont.)

– Case sum of x, y as unsigned (k−1)-bit numbershas no overflow.∗ 0bz = 2k−1 +m+m′.∗ Sum of 1x, 0y as unsigned number is 1z:

1x+0y

1z

∗ This represents−2k−1 + 2k−1 +m+m′︸ ︷︷ ︸

z

= m+m′,

the correct result.∗ Algorithm has as result 1z, the correct result.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-169

(C) Anton Setzer 2002 (except for pictures)

(b) Signed Fixed Point Numbers

•::::::::::::::Signed

:::::::::::fixed

:::::::::::::point

::::::::::::::::::::numbers.

– Similarly to fixed point numbers,the MSB gets weight −2k−1, if k bits to the leftof the point.

• Conversion:

– Convert whole-numbered part as a signed binarynumber and the fractional part as before.

• Examples (k= 2):

– (1001.0)signed:∗ (1001)signed = −23 + 20 = −8 + 1 = −7.∗ (0.01)unsigned = 0.25.∗ Therefore

(1001.01)signed = −7 + 0.25 = −6.75.– (0010.11)signed:∗ (0010)signed = 2∗ (0.11)unsigned = 0.5 + 0.25 = 0.75.∗ Therefore

(0010.11)signed = 2 + 0.75 = 2.75.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-170

(C) Anton Setzer 2002 (except for pictures)

Operations on Signed Fixed Point Numbers

• Addition/subtraction/multiplication of signedfixed point numbers m, m′ carried out as forunsigned fixed point numbers:

– Assume we have k bits to the left of point and lbits to the right of the point.

– Write m, m′ with this number of bits l.(Important, that both have the sameformat).

– Omit the point.– Add/subtract/multiply m, m′.– Reintroduce the point with k bits after point

(addition/subtraction) and 2k bits after point(multiplication).

• Correctness of the above:

– Multiplication of a signed number l with k bitsafter the point by 2n is the same as shifting thepoint n bits to the right.∗ MSB which had weight −2k

′gets new weight

−2k′+n.

∗ Any of the other bits, with weight say 2k′′,

gets new weight 2k′′+n.

– Now correctness follows exactly as for unsignednumbers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-171

(C) Anton Setzer 2002 (except for pictures)

Operations on Signed Fixed Point Numbers

• Note that in the signed case, we apply the rules fordetermining overflow/signed of the result used forintegers.

• E.g. addition: only if both have the same sign andthe result the opposite, we have an over/underflow.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-172

(C) Anton Setzer 2002 (except for pictures)

Examples (Addition, Signed Numbers)

Fixed Point Numbers Corresponding Integers

Two positive integers(01.01)signed

+ (00.10)signed

= (01.11)signed

(0101)signed

+ (0010)signed

= (0111)signed

(decimal: 1.25 + 0.5 = 1.75)

Two negative integers (Carry ignored)(11.01)signed

+ (11.11)signed

= (11.00)signed

(1101)signed

+ (1111)signed

= (1100)signed

(decimal: -0.75 + (-0.25) = -1)

Opposite signs (Carry ignored)(01.01)signed

+ (10.11)signed

= (00.00)signed

(0101)signed

+ (1011)signed

= (0000)signed

(decimal: 1.25 + (-1.25) =0)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-173

(C) Anton Setzer 2002 (except for pictures)

Examples (Addition, Signed Numbers,Cont.)

Overflow(01.01)signed

+ (01.01)signed

= (10.10)signed

(0101)signed

+ (0101)signed

= (1010)signed

Overflow Overflow(decimal: 1.25 + 1.25 = 2.5 ≥ 2)

Underflow(10.00)signed

+ (10.00)signed

= (00.00)signed

(1000)signed

+ (1000)signed

= (0000)signed

Underflow Underflow(decimal: -2 + (-2) = -4 < -2)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-174

(C) Anton Setzer 2002 (except for pictures)

Example (Multiplication, Signed Numbers)

• Calculation of (10.1)signed · (11.1)signed as fixedpoint numbers.

– Booth’s algorithm yields:(101)signed · (111)signed = (000011)signed.

– So result of the above is (0000.11)signed.– This is correct, since∗ (10.1)signed = −2 + 0.5 = −1.5.∗ (11.1)signed = −2 + 1 + 0.5 = −0.5.∗ (0000.11)signed = 0.75 = (−1.5) · (−0.5).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 4, Suppl. 4-175

(C) Anton Setzer 2002 (except for pictures)

5. CPU and InterconnectingStructure

(a) Overview.

(b) Execution of Commands.

(c) The Fetch-Decode-Execute Cycle.

(d) Registers.

(e) The ALU.

(f) Buses

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-1

(C) Anton Setzer 2002 (except for pictures)

(a) Overview

Remember the von Neumann Architecture (withadditional connections CU ↔ I/O):

Main

MemoryI/O

Equipment(ALU)Unit

Arithmetic-Logic

ControlUnit

• Main memory stores information:

– Data,– programs.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-2

(C) Anton Setzer 2002 (except for pictures)

Control Unit

• The Control Unit sends control signals to

– memory,– registers,– ALU,– I/O.

• These signals control

– the flow of data (read, write, from which unit towhich)

– operations carried out on data (shifts, logicoperations, addition, etc.)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-3

(C) Anton Setzer 2002 (except for pictures)

(b) Execution of Commands

Execution of High Level Commands

• We will consider the interaction between

–:::::::::::::::::::::::::::::::::::Arithmetic-Logic

::::::::::Unit, (

:::::::::ALU).

– Main memory.– Basic registers (temporary storage).

during the execution of programs.

• Consider the high level language commandA := (B + C) * (C+D)

– Too complicated in order to be executed directly.

• Decompose it into 3 commands:Aux1 := B + CAux2 := C + DA:= Aux1 * Aux2

– Variables are usually stored in main memory.– During executions, most commands deal with

registers.– Registers = small storage cells in the CPU.– Allow fast storage and retrieval.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-4

(C) Anton Setzer 2002 (except for pictures)

Execution of High Level Commands (Cont.)

• Initially, we will use in the following only one uservisible register,

::::::AC (

::::::::::::::::::::::::::Accumulator).

• Use of very simple instructions with simpleinstruction codes (little man computer):

– LOAD A“Load AC with content at memory address A”.

– ADD A“Add to AC content at memory address A, storeresult in AC”.

– MULT A“Multiply AC by content at memory address A,store result in AC”.

– STORE A“Store content of AC at memory address A”.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-5

(C) Anton Setzer 2002 (except for pictures)

Execution of High Level Commands (Cont.)

• The above command translates then into thefollowing(A, B, C, D, AUX1, AUX2 are the addresses wherethe corresponding variables are stored, “;” indicatescomments):

LOAD B ; AC = BADD C ; AC = B +CSTORE AUX1 ; AUX1 = B+CLOAD C ; AC = CADD D ; AC = C + DSTORE AUX2 ; AUX2 = C + DLOAD AUX1 ; AC = (B+C)MULT AUX2 ; AC = (B+C) * (C+D)STORE A ; A = (B+C) * (C+D)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-6

(C) Anton Setzer 2002 (except for pictures)

Execution of Machine Instructions

Consider the following piece of program code:

Address Instruction Code0x300 LOAD 0x940 0x19400x301 ADD 0x941 0x59410x302 STORE 0x941 0x2941

• Codes for the instructions in this toy exampleconsist of

– a 4 bit:::::::::::::::::::operation

:::::::::::::::code (or

:::::::::::::::opcode),

representing the instruction (the 1 in 1940).– a 12 bit

:::::::::::::::::operand, which is here the address of

an element in main memory.

• Execution will use the following::::::::::::::::internal

:::::::::::::::::::registers:

–::::::PC =

:::Program

:::Counter.

∗ Number of next instruction to be executed.–

:::::IR =

::Instruction

:::Register.

∗ Contains next instruction to be executed.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-7

(C) Anton Setzer 2002 (except for pictures)

Execution of Machine Instructions (Cont.)

0. Initially, the::::::::::::::::::Program

:::::::::::::::::::Counter (

::::::PC) points to

address 300.

1a. Fetch instruction from address 300.Result stored in IR.Simultaneously, increment PC by 1.

1b. Load AC with content of memory location 940.

2a. Fetch instruction from address 301.Result stored in IR.Increment PC by 1.

2b. Add contents of AC and contents of location941 and store result in AC.

3a. Fetch instruction from address 302.Result stored in IR.Increment PC by 1.

3b. Store contents of AC at location 941.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-8

(C) Anton Setzer 2002 (except for pictures)

Execution of Machine Instructions (Cont.)

� �� �

� �� �

� �� �

300301302

940941

PCACIR

1 9 4 05 9 4 12 9 4 1

0 0 0 30 0 0 2

3 0 0Memory CPU Registers

Step 0: Initial State

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-9

(C) Anton Setzer 2002 (except for pictures)

� ��

� ��

� ��

� �� �

� �

� �

300301302

940941

PCACIR

1 9 4 05 9 4 12 9 4 1

0 0 0 30 0 0 2

1 9 4 0

Memory

300301302

940941

PCACIR

1 9 4 05 9 4 12 9 4 1

0 0 0 30 0 0 2

1 9 4 0

Memory CPU Registers

Step 1a: Fetch Instruction 300

0 0 0 33 0 1

CPU Registers

+ 13 0 1

Step 1b: Execute LOAD 940

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-10

(C) Anton Setzer 2002 (except for pictures)

� ��

� ��

� ��

� �� �

� �

� �

300301302

940941

PCACIR

1 9 4 05 9 4 12 9 4 1

0 0 0 30 0 0 2

Memory

300301302

940941

PCACIR

1 9 4 05 9 4 12 9 4 1

0 0 0 30 0 0 2

Memory CPU Registers

CPU Registers

0 0 0 35 9 4 1

5 9 4 1

3 0 2

+

00030 0 0 5

Step 2a: Fetch Instruction 301

+ 13 0 2

Step 2b: Execute ADD 941

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-11

(C) Anton Setzer 2002 (except for pictures)

� ��

� ��

� ��

� �� �

� �

� �

300301302

940941

PCACIR

1 9 4 05 9 4 12 9 4 1

0 0 0 3

Memory

300301302

940941

PCACIR

1 9 4 05 9 4 12 9 4 1

0 0 0 30 0 0 2

Memory CPU Registers

CPU Registers

0 0 0 5

Step 3a: Fetch Instruction 302

2 9 4 10 0 0 5

3 0 3

0 0 0 5

+ 13 0 3

Step 3b: Execute STORE 941

2 9 4 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-12

(C) Anton Setzer 2002 (except for pictures)

(c) The Fetch-Decode-Execute Cycle

• Three phases carried out for each instruction:

–::::::::::::Fetch

::::::::::::::Cycle:

Get next machine instruction from main memory.–

:::::::::::::::::::Decoding

:::::::::::::::Cycle:

Decode the fetched instruction.–

::::::::::::::::::::Execution

:::::::::::::Cycle

Carry instruction out.Done by sending control signals to the units.

– Once this is done, repeat this cycle:fetch next instruction etc.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-13

(C) Anton Setzer 2002 (except for pictures)

Fetch nextInstruction

DecodeInstruction

ExecuteInstruction

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-14

(C) Anton Setzer 2002 (except for pictures)

The Fetch Cycle

• Need to know address in main memory, to fetchinstruction from.

• Done via Program Counter (PC).

• Instruction stored in instruction register (IR).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-15

(C) Anton Setzer 2002 (except for pictures)

Realization of Fetch Cycleon a Digital Level

• The following diagrams shows the layout for afictitious architecture with

– Only 4 memory cells.– Therefore a 2 bit PC used.– Only the connections for the first bit of the IR

are shown.(For the other bits, there are similar connections,which operate in parallel).

– Connection to PC, assertion of read/writecontrolled by the CU.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-16

(C) Anton Setzer 2002 (except for pictures)

EnableMem 0

Mem 1

Mem2

Mem3

IR

PC

0

0

10

1

1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-17

(C) Anton Setzer 2002 (except for pictures)

Here you see how the instruction stored in memorylocation 01 is loaded:

EnableMem 0

Mem 1

Mem2

Mem3

IR

PC10

11

0

0

0

1

1

1

1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-18

(C) Anton Setzer 2002 (except for pictures)

The Decoding Cycle

• Identify the bits representing

– the::::::::::::::::::::operation

:::::::::::code (

::::::::::::::::op-code)

∗ what kind of instruction:addition, multiplication, storage etc.

– and the::::::::::::::::::operands

∗ addresses for memory to be loaded, stored∗ register numbers∗ next instructions∗ constants

• Might not require any memory cycles, if decodingis easy (as in the above example).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-19

(C) Anton Setzer 2002 (except for pictures)

RISC vs. CISC

•:::::::::::RISC (

::::::::::::::::reduced

:::::::::::::::::::::::instruction

::::::::set

:::::::::::::::::::::::computers) have

simple instruction formats.

– More modern concept.– Decoding very simple and does usually not take

any memory cycle.– Examples: PowerPC (Macintosh), SUN SPARC

and descendants

•:::::::::::CISC (

:::::::::::::::::complex

:::::::::::::::::::::::instruction

:::::::set

:::::::::::::::::::::::computers) have

very complex instruction formats

– Older concept.– Length of op code can vary very much.– Decoding might take several cycles.– Example: Intel Pentium.

• In the 80s, 90s expectation RISC will eventuallyoutperform CISC.

• But still CISC very successful.

– Trend: mixture between CISC and RISC.

• More later.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-20

(C) Anton Setzer 2002 (except for pictures)

The Execution Cycle

• Execute instruction.

– Example 1: LOAD Rm,Mn.∗ Meaning: load memory location n into register

m.∗ Execution:· Connect bits in the instruction containing

address n as before PC with memory.· Connect lines from memory at the CPU with

register m.· Assert read to main memory· Assert write to register m.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-21

(C) Anton Setzer 2002 (except for pictures)

Mem3

Mem2

Mem 1

Mem 0in IR

Register m(selected by

Operand 1

Operand 2in IR)

Enable

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-22

(C) Anton Setzer 2002 (except for pictures)

The Execution Cycle (cont.)

• Example 2: ADD Rk,Rl,Rm

– Meaning: Add register Rl, Rm; store result inRegister Rk.

– Execution:∗ Cycle 1:· Connect registers Rl, Rm with the ALU,

assert Addition.· Store result temporarily in some temporary

register.∗ Cycle 2:· Connect that temporary register with

register Rk.

• This takes 2 cycles.More complex instructions can take much morecycles.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-23

(C) Anton Setzer 2002 (except for pictures)

RmALU

Tmp.Register

RegisterUnit

RkRl

Control Signals

Control Signal

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-24

(C) Anton Setzer 2002 (except for pictures)

Refined Instruction Cycle

• More refined instruction cycle see next slide.

– Address of instruction might need to becalculated.

– Execution cycle split into∗ operand address calculation

(might be more complicated, see section onaddressing modes).∗ operand fetch

(operands might be fetched separately)∗ data operation (e.g. ADD)∗ operand store address calculation,∗ operand store.

– For most instructions some of these sub-steps ofthe execution cycle might be trival or omittedaltogether.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-25

(C) Anton Setzer 2002 (except for pictures)

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-26

(C) Anton Setzer 2002 (except for pictures)

(d) Registers

• Two kinds of registers

–:::::::::User

::::::::::::::::Visible Registers.

–:::::::::::::::Control

:::::::::and

:::::::::::::::status Registers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-27

(C) Anton Setzer 2002 (except for pictures)

User Visible Registers

•::::::::::::::::General

::::::::::::::::::purpose

::::::::::::::::::::registers.

– Use of more expensive but faster technology thanfor main memory.

– General purpose registers used for frequentlyused data(e.g. the i in a loop “for i=1 to 10000 . . . ”).Can be read and written by the machinelanguage.

– Sometimes specific ones for∗ data vs. programs∗ floating point vs. integer data.

– This distinction allows to save address space.

•::::::::::::::::::::Condition

:::::::::::::::codes, often referred to as

:::::::::flags.

– Are one-bit registers.– Examples are overflow or result zero flags from

last ALU operation.– Condition codes are usually set by the ALU and

(sometimes) by the CU and only read by theuser.

– However some might be changed (in exceptionalcases) by the machine language.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-28

(C) Anton Setzer 2002 (except for pictures)

Control and Status Registers

• Registers, used while carrying out the instructionsin the CPU.

• Typical ones are:

–::::::::::::::::::Program

::::::::::::::::::Counter

::::::::::(PC)

–::::::::::::::::::::::Instruction

:::::::::::::::::::Register

:::::::::(IR)

–:::::::::::::::::Memory

:::::::::::::Data

:::::::::::::::::::Register

:::::::::::::::(MDR) or

:::::::::::::::::Memory

:::::::::::::Buffer

::::::::::::::::::Register

:::::::::::::::::(MBR):

Stores data received from main memory, whichcan not directly be passed on to a register.

–:::::::::::::::::Memory

::::::::::::::::::Address

::::::::::::::::::Register (

:::::::::::MAR):

∗ Contains addresses of memory locations, fromwhich data is to be fetched or into which datais to be stored.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-29

(C) Anton Setzer 2002 (except for pictures)

(e) ALU

• Carries out operations like

– addition, subtraction– bitwise and, or, negation.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-30

(C) Anton Setzer 2002 (except for pictures)

(f) Buses

• A:::::::bus consists of 50 - 100 separate lines,

– each of which assigned with a particular meaningor function,

– each line carries one bit.

• Most buses have

–::::::::::Data

:::::::::::::lines for transfer of data (including

instructions).∗ Collection of data lines is called

::::::::::data

::::::::bus.

∗ Data buses have typically 8, 16, 32, 64separate lines.∗ This number is called

::::::::::::width of the data bus.

–::::::::::::::::Address

::::::::::::lines.

Carry∗ memory address,∗ possibly as well addresses to I/O ports.· Collection of address lines is called

:::::::::::::::address

:::::::bus.· Usually higher order bits select particular

module on the bus.· Lower order bits select memory location or

I/O port within that module.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-31

(C) Anton Setzer 2002 (except for pictures)

Buses (Cont.)

•:::::::::::::::Control

:::::::::::::lines. For instance:

– Collection of control lines is called:::::::::::::::control

::::::::bus.

– Memory write/read.– I/O write/read.– Transfer acknowledgment.– Request of bus and grant of bus access.– Interrupt request and acknowledgment.– Clock.– Reset

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-32

Picture copied from book not included

(C) Anton Setzer 2002 (except for pictures)

Dedicated and Multiplexed Lines

•:::::::::::::::::::::Dedicated

:::::::::::lines.

– Lines of the bus which serve only one particularpurpose.

– Example: transfer of data bit 4.

•::::::::::::::::::::::::Multiplexed

:::::::::::lines.

– Carry many signals multiplexed through time.– Suitable for I/O (memory has much higher traffic

which needs to be treated much faster).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-34

(C) Anton Setzer 2002 (except for pictures)

Arbitration

•:::::::::::::::::::::::Arbitration

:::::= process of determining

– which component may write to the bus.

• Two methods:

–:::::::::::::::::::::::::Centralized, by one piece of hardware∗ (

:::::::bus

:::::::::::::::::::::controller or

:::::::::::::arbiter).

–::::::::::::::::::::::::Distributed.∗ Each module has

:::::::::::::access

::::::::::::::::control

:::::::::::logic,

∗ modules act together to share the bus.

• With both methods,

– a::::::::::::::master is determined,

– which initiates a data transfer with some otherdevice,

– called::::::::::slave.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-35

(C) Anton Setzer 2002 (except for pictures)

Timing

• Synchronous:

– All bus operations synchronized by a clock.– Advantage: Simpler to implement and test.– Disadvantage:∗ Tied to a fixed clock rate,∗ no advantage if one device is replaced by a

faster one.

• Asynchronous:

– No clock for the bus.– Use special control signals to synchronous them.– Very difficult to verify.

• Example:

– With synchronous timing it is clear that afterone clock signal, one bit put on the data bus hasbeen received by the recepient.

– With asynchronous timing, an acknowledgementsignal is required by the recepient to indicatethat the bit has arrived.∗ So the protocoal is more complicated and

requires extra control lines.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-36

(C) Anton Setzer 2002 (except for pictures)

Multiple-Bus Hierarchies

• Bus forms the::::::::::::::::::::::::::::::::::::::::::::::::::::von-Neumann-bottleneck:

– Relatively slow.– Blocks increase in performance.

• Methods taken for solving this problem:

– A cache is placed between processor andmemory.∗ Frequently used memory contents do not have

to be moved via the system bus.∗ Sometimes cache integrated into the

processor.– Formation of hierarchies of buses∗ s.t. some memory traffic does not go via the

system bus.– Design of I/O modules with controllers,

buffers,∗ so that memory access is needed only, when

bus is free.∗ Memory transfer carried out in blocks.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-37

Picture copied from book not included

Explanation of the modules on last slide:

• SCSI = small computer system interface = typeof bus which supports local disk drives and otherperipherals.

• LAN = local area network, for instance Ethernet.

• Fire Wire = high speed bus arrangement forsupport of high-capacity I/O devices.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5 5-39

Supplementary Materialfor Sect. 5.

(a) The PCI Protocol.

(b) Synchronous vs. Asynchronous Timing.

(c) Data Transfer Types

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-40

(a) The PCI Protocol

• PCI example of a protocol with multiplexed lines.(PCI is an interface between processor and I/O).

– Processor first puts on AD the I/O address fromwhich he wants to receive data.

– Later the I/O module puts in response the dataat this location on the AD lines.∗ (CLK = clock,∗ the other lines are control lines,

indicating type of request, whether data hasbeen received, etc.)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-41

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-42

Description of the PCI-Protocol (Last Slide)

• If a signal is at the top line means deasserted, atthe bottom line asserted.

• Protocol is synchronized by a clock.

• In this protocol, no treatment of bus arbitration

– (i.e. decision who is::::::::::::::master, who is

::::::::::slave).

∗ In case of PCI, master is called::::::::::::::::initiator,

slave is called::::::::::::target.

– Separate protocol (via a centralized PCI arbiter).– We assume, initiator has gained control over

the bus.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-43

Functionality of the Bus Lines

•:::::::::CLK = clock.

•:::::::AD

– Multiplexed:::address/

::data lines.

32 bits.– An extension to 64 bits exists– In case of a write operation, AD controlled by

the initiator.– In case of a read operation, address controlled

by the initiator, data controlled by the target.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-44

Bus Lines Controlled by the Initiator

•:::::::::::::::::FRAME asserted indicates

– initiator has started a request and not receivedall data yet.

•::::::::::::::::C/BE#

– When address given, indicates whether read orwrite requested.

– When data given, tells which bytes of the wordon AD are valid.

– (Any of the four bytes of a word can be selectedindividually.

– Master/slave might not be able to receive/senda full word of 4 bytes in one step).

•:::::::::::IRDY = indicates whether initiator is ready toreceive data.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-45

Bus Lines Controlled by the Slave

•:::::::::::::::::TRDY#

– During read operation: Valid data is on AD.– During write operation: Slave ready to receive

data.

•::::::::::::::::::::::DEVSEL#

– Slave has received address.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-46

Description of the Stepsin The PCI Protocol

• a.

– Initiator asserts FRAME.– Initiator puts requested address on address bus.– Initiator asserts read on C/BE#.

• b.

– Target has received the address.

• c.

– Initiator assumes that address has been received,– removes therefore address from the bus.–

::::::::::::::::::::::::Turnaround

:::::::::::::cycle required, since a different

device (target) controls now those bus lines.– Initiator indicates on C/BE#, which bytes of AD

should contain valid data.– Initiator asserts on IRDY# that it is ready to

receive data.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-47

Description of the Stepsin the PCI Protocol Example (Cont.)

• d.

– Target asserts DEVSEL#, since it has recognizedits address and will respond.

– It replaces requested data on AD.– Target asserts IRDY, since valid data is on AD.

• e.

– Initiator reads data.– Changes C/BE# and indicates which bytes it

wants to read next.

• f.

– As an example, here we assume that target needstime before second data item is ready.

– Therefore TRDY# is deasserted.– When target is ready, it asserts TRDY# again,

puts data on AD.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-48

Description of the Stepsin the PCI Protocol Example (Cont.)

• g.

– As an example, here we assume that initiatorneeds time before it can received 3rd data item.

– Therefore it deasserts IRDY#.– Data item will remain on AD until IRDY# is

asserted again.

• h.

– Initiator will now receive its last data item(which it knows is already there because ofIRDY# on AD).

– It therefore deasserts FRAME#.– it asserts IRDY#, since it is now ready to receive

data.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-49

Description of the Stepsin the PCI Protocol Example (Cont.)

• i.

– Initiator deasserts IRDY#, since it has receivedits data.

– Target deasserts TRDY# and DEVSEL#, andremoves data from AD.

– FRAME#, AD, C/BE# have now turnaroundtime, so that another initiator can use them oneclock cycle later.

– IRDY#, TRDY#, DEVSEL# contain still validinformation (“deasserted”);

– they have their turnaround time afterwards.– Note the turnaround time for these signals during

a., b.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-50

(b) Synchronous vs.Asynchronous Timing

• Consider the following timing diagram.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-51

Picture copied from book not included

The SynchronousTiming Protocol

• Figure 3.19 (a).

– Description of a simple CPU/memory readrequest.

• Master puts a read signal, the requested addresson the bus.

• It further asserts a start signal.

• In the next cycle, slave puts data on the data lines.

• Slave asserts an acknowledgment signal, to indicatethat it has received the address and put data onthe data lines.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-53

The AsynchronousTiming Protocol

• Figure 3.19 (b).

• Master places address + read signal on the bus.

• It takes some time, before address has stabilized.Known by the master. (With synchronous timing,this time is 1 clock cycle).

• When this is the case, master asserts

::::::::::::::MSYN (

::::master

::synchronization) line.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-54

The AsynchronousTiming Protocol (Cont.)

• When address is stable and slave has received it, itputs data on data lines.

• As soon as data is stable, slave asserts

::::::::::::SSYN (

::slave

:synchronization) line.

• When master has read data, it deasserts MSYN.

• Slave responds by deasserting SSYN.

• It then removes data from data lines.

• Master removes address from address bus.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-55

(c) Data Transfer Types

Several data transfer types allowed (used in order todecrease the amount of time for switching betweendifferent modes).

• Read.

– Master puts address on the bus.– Then slave puts data on it.– In case of multiplexed lines delay for stabilization

of the bus in between.

• Write.

– Master puts address and data on the bus– (if multiplexed, consecutively).

• Read-Modify-Write.

– Read followed by a write.– Master puts address on the bus.– Slave puts data on it.– Master puts then data on it

(no second address transfer needed).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-56

Data Transfer Type (Cont.)

• Read-After-Write.

– Write followed by a read transfer.– Master puts address and data on the bus.– Slave puts afterwards the received data on the

bus.– Used for checking purposes.

• Block transfer.

– Only once the address is put on the bus.– Then follow several data transfers from adjacent

memory locations,– controlled by control lines.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 5, Suppl. 5-57

6. Memory

(Addressing modes moved to Sect. 7).

(a) Internal Memory.

(b) External Memory.

(c) Types of Memory.

(d) The Memory Hierarchy.

(e) Stacks.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-1

(a) Internal Memory

•:::::::::::RAM.

– Abbreviates::::Random-

:::Access

:::::Memory.

– Abbreviation is mainly historic; RAM should becalled∗ erasable (volatile) random access memory.

–::::::::::::::::::::::::::::::::Random-access means: any memory addresscan be read and written instantly.

–::::::::::::::::Volatile means: when power is switched off,data stored will be lost.

– Two sorts:∗

::::::::::::Static

:::::::::::::RAM (

::::::::::::::SRAM):

· Use of flip-flops· Holds data as long as power is supplied

(no refresh necessary).· More expensive per bit but faster than

dynamic RAM.· Used for small portions of memory, which

require fast access:(registers, cache).

∗::::::::::::::::::Dynamic

:::::::::::::RAM (

::::::::::::::DRAM).

· Cheaper.· Can be packed more densely

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-2

Realization of DRAM

• Current main technique: small capacitors.

• When storing memory, voltage is set on thecapacitor, and it is charged.

• Capacitor keeps charge for a few milliseconds.

• When reading it, the capacitor is discharged.Refreshing is necessary.

• As well, charge in capacitor fades away.

– Regularly (after some milliseconds), memory hasto be refreshed.∗ The part to be refreshed for a moment

unavailable.∗ But refreshing is done very fast.∗ Only small percentage of time used for

refreshing.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-3

Core Memory

• Old technique for what is now DRAM:

– Ferrite core.– Rings of magnetic cores

(⇒ terminology:::::::::core

::::::::::::::::::memory).

– Advantages:∗ Robust.∗ Non-volatile.But∗ Slow and expensive.∗ Remains (or remained?) in use for military

applications and in space.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-4

Other Types of Semiconductor Memory

•:::Read-

::::Only

::::Memory (

:::::::::::ROM).

– Non-erasable.– Created like a circuit chip with data stored using

gates.– Large cost in development, no room for error.– Used for∗ Microprogramming of the CPU.∗ Function tables, frequently wanted functions,

system programs.

•:::Programmable

::::::::::::ROM (

::::::::::::::PROM).

– Nonvolatile, can be written only once.– Writing process performed electrically, can be

performed after the chip is produced.– Special equipment for programming needed.– Still attractive for high-volume productions.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-5

Other Types of Semiconductor Memory (Cont.)

•::::::::::::::::::::::::::Read-mostly

::::::::::::::::::Memory:

– Write operation difficult, but can be performedseveral times.

–:::::::::::::::::EPROM.∗

:::Erasable

::::Programmable

:::Read-

::::Only

::::Memory.

∗ Read and written electrically.∗ One transistor per chip, therefore high density.∗ Erasure possible using UV radiation.∗ Erasure takes 20 minutes.

–::::::::::::::::::::EEPROM∗

:::Electrically

:::Erasable

:::Programmable

:::Read-

::::Only

::::Memory.

∗ Can be written to without erasing priorcontents.∗ Write operation takes approx several hundred

microseconds per byte.∗ More expensive and less dense than EPROM.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-6

Read Mostly Memory (Cont.)

•:::::::::::Flash

::::::::::::::::::memory.

– Electrical erasing technology like EPROM,EEPROM.

– Entire memory can be erased in one or a fewseconds.

– Blocks of memory can be erased.– No byte-level erasure.– Density as for EPROM.– Slower and more expensive than the hard disk –

therefore no substitute for hard disk.– Used in digital cameras, voice recorders, PDAs,– Sometimes used in order to store the BIOS

(as a substitute for EPROM).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-7

Memory Errors

•:::::::::Soft

:::::::::::::errors = spontaneous changes between 0 and

1 in memory.

– Memory not permanently damaged.– Caused mainly by radioactive nuclei in the

packaging of the chip.– Was rare.– Because of higher density of memory more and

more frequent.– Occurs in all kinds of memory (both internal and

external memory).

•::::::::::Hard

:::::::::::::error.

– Permanent damage due to∗ wear∗ harsh environmental use∗ manufacturing defects.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-8

Error Correction

• Error checking scheme in the memory systemrequired.

• Attach to a memory word extra bits, called::::::::::::check

::::::::::code

• Simplest example: parity bit.

– Attach to a memory word (say 8 bits) one bit,which is 1 iff the number of bits in the word isodd.(So if the words have length 8bit, we need 9 bitsto store them).

– If one bit is changed (could be the parity bit!!),the error is discovered.

::::::::::::Single

::::::::::::Error

:::::::::::::::::::::::Detection.

• Better codes allow::::::::::error

:::::::::::::::::::::::correction (recovery of

original value), if only one bit is damaged.

– For 8 bits, 3 extra bits required.

• Typically two kind of codes used:

–::::::::::::::::::::::::Single-error

:::::::::::::::::::::correction.

–::::::::::::::::::::::::::Double-error

::::::::::::::::::::detection,

::::::::::::single

::::::::::error

::::::::::::::::::::correction.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-9

(b) External Memory

• Hard disk.

– Principal storage device.– Considerable technological improvement.– Currently 5 - 180 GB (Giga-Byte) (but always

increasing).– Platters (one or more) stacked up in sealed

environment.– Coated with magnetic material.– 3600 rpm, 5400 rpm, 7200 rpm, 10 000 rpm

(rpm = rounds per minute).– Read/write heads – ride close to surface.– Nowadays usually sealed, since small articles

could cause catastrophic damage (disk crash).– Aerodynamic systems exist (Winchester drives)

Head rests on disk when disk is motionless.Head is lifted by air pressure generated by thespinning disk.∗ Allows to get head closer to the platter and

therefore to achieve higher density.– Data stored in concentric rings on tracks divided

into sectors.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-10

Hard Disk (Cont.)

• Fixed vs. movable head.

– Fixed head: one head per track – head can readonly this track.∗ For high performance disks.

– Movable head: one head which can move.More common.∗ Access time higher, since head has first to

move to find the track.

• Single-sided vs. double-sided disks.

• Multi-platter disks: Unit containing several harddisks, driven by one engine only.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-11

Fixed Head Disks

(One head per track)Tracks

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-11a

Movable Head

Tracks

Head

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-12

::::::::::::::::::::::::::Multi-platter

:::::::::::Disk

Head Motion

Surface 7

Surface 6

Surface 5

Surface 4

Surface 3

Surface 2Surface 1

Surface 0Read/Write Head

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-13

Disk Data Layout

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-14

Floppy Disk

• Similar in principal to hard disks.

• One [flexible] platter, no sealed environment, headrides on surface

• Very slow, low capacity.

• First ones 8 inch - not intended for “normal” data.

– Cardboard sleeve.– Adopted by early microcomputers.

• Replaced by similar 5.25 inch floppies.

• Now 3.25 inch, plastic sleeve.

• Really obsolete (too small – 1.44 MB (Mega Byte);slow, unreliable).

– but large installed user base.

• Better alternatives (e.g.:::::::Zip (100 MB) and others).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-15

Tapes

• Similar to tape recorder.

• Can store large amount of data reliable.

• Mainly used for backups and long term storage ofdata.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-16

:::::::::::::::::::CD-ROM

• Data stored as pits on the surface of a disk.Imprinted at manufacture time.

• To read data, laser directed to surface.Reflection of beam alters by presence/absence ofpits.

•:::Constant

:linear

::velocity used (

:::::::::CLV)

(eg. the speed of the track passing by the head isconstant, therefore for inner tracks the rotationalspeed is faster than for outer tracks). Data densityper angle on inner and outer areas of the diskdifferent (Different from magnetic disks).

• Data stored along one spiral, that originates fromthe centre of the disk.

• To locate sector:

– Disk head moves to approximate vicinity.– Minor adjustments to find and read desired

sector.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-17

Recording Structure of a CD

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-18

Digital Video Disk (:::::::::::DVD)

• As CD-ROM but

– Bits packed more closely than in CD– Two layers of pits one on top of the other.– Two-sided.

• Can store 8.5 GB (single sided), 17 GB (double-sided).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-19

(c) Types of Memory

• Most important distinction:

– External Memory.Involves some mechanics.Peripheral storage devices, like hard disk, floppy,CD-ROM.Can in principal be moved from one computer tothe other without loosing data.

– Internal Memory.Semi-conductor memory, non-mechanical.Some can be moved around, but the main formof internal memory (RAM) cannot be moved.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-20

Methods of Access

• Sequential Access:

– Access of memory sequentially.– Long seek time, before information is accessed.– But then following block accessed relatively fast.– Main example: tape units.∗ Nowadays mainly used for backups.

• Direct Access:

– Blocks have a direct address.– Location within such a block is accessed

sequentially.– Example: disks, CDs, DVDs.

• Random Access:

– Each addressable location of the memory can beaddressed and accessed directly.

– Example: Main memory, some cache memory,registers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-21

Methods of Access (Cont.)

• Associative:

– Random-access type memory.– Used for cache.∗ Cache stores data which is expected to be used

in the near future.Faster access to cache than to main memory.

– In order to look up data, one has to checkwhether its address is in cache.

– One part of the address selects directly asequence of addresses in the associative memory.

– The associative memory verifies in one step,∗ if the rest of the address (

:::::::tag) coincides with

one of the tags of the addresses stored in it.– If success, the value stored is retrieved.– If unsuccessful, data has to be retrieved from

main memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-22

Types of Memory by Permanence

• Volatile Memory: information lost after powerswitched off.

• Non-volatile: information remains.

– (e.g. magnetic surface memory, ROM, PROM,EPROM, EEPROM, flash memory, old corememory; see below).

• Erasable memory: memory can be overwritten.

• Nonerasable memory.

– Nonerasable memory is necessarily nonvolatile.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-23

(d) The Memory Hierarchy

VeryFast

Fast Cache (sometimes different levels)

Medium Main memory DRAM

Slow

Off-line Storage

Outboard StorageHard Disk

Expensive Fast

Registers

CD/DVD

Floppy

SRAM

Cheap Slow

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-24

Memory Hierarchy (Cont.)

• Different kinds of storage devices:

• Expensive (per bit), but fast ones.

– Very fast: Registers.– Fast: Used for cache.– Based on SRAM.

• Medium price/speed: Main memory

– DRAM

• Cheap, slow:

– Outboard storage∗ Not in main memory.∗ But fast access.

– Offline storage∗ Can be carried around.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-25

(e) Stacks

• If we look at a stack of paper:

we see the following:

– Only the top sheet is accessible.– We can add a sheet on top of it (PUSH (p)).

Then the new sheet is the top one.– we can take a sheet from its top (POP) and use

it.Then the sheet below it is new top one.

• A stack is now a data structure which is derivedfrom this example.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-26

Stack Addressing (Cont.)

• A::::::::::stack is a finite linearly ordered sequence of

elements, of which only the last element can beaccessed at a time.

• For the ordering one uses the analogy ofabove/below/top element:

– The last element is called the:::::::top of the stack.

– The element immediately before one element iscalled the element

::::::::::::below it.

– The first element is called the::::::::::::::::bottom element

of the stack.

• Usually we have only the following operations:

–:::::::::::::PUSH, which moves a new element on top of astack (at the end of it).

–::::::::::POP, which removes an element from the topof a stack. Afterwards the element below it isnew top element.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-27

Stack Addressing (Cont.)

Stacks which are used for arithmetic operations haveadditionally operations:

•::::::::::::Unary

::::::::::::::::::::::::operations, which pop the top element,

apply a unary operation to it (like negation, shiftoperations, bitwise NOT) and push the result onthe stack.

•:::::::::::::Binary

:::::::::::::::::::::::operations, which pop two elements from

the stack, apply a binary operation (like addition,multiplication), and push the result on the stack.

–:::::::ADD denotes this operation for addition,

– MULT for multiplication,– SUB for subtraction,– DIV for division.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-28

Use of Stacks

• Almost all architectures have a stack for dealingwith function and procedure calls (see later).

• Machines built using stacks as main principle (:::::::::::stack

:::::::::::::::::::machines).

– Used in some simple machine (not verycommon).

•:::::::::Java

:::::::::::::::virtual

:::::::::::::::::::machine (JVM) is based on a stack

machine.

– JVM is a real machine language, but it is mainlyinterpreted on other machines.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-29

Stacks and Evaluation of Expressions

• Stacks can be used for evaluating expressions.

• Assume we want to calculate (2 + 3) · 6 with astack.Can be done as follows:

Instruction Resulting StackPUSH 2

2

PUSH 332

ADD5

PUSH 665

MULT30

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-30

Stack Implementation

• Usually the items in the stack are stored in somespecial area of main memory.

• In registers one stores:

–::::::::::::Stack

:::::::::::::Base. Points to the bottom element of

the stack.–

::::::::::::Stack

::::::::::::::::::Pointer. Points to the top element.

(Problem if the stack is empty; stack shouldpoint to an address below the stack base!).To avoid this problem (eg. if Stack Base = 0),sometimes one has stack pointers pointing to thefirst address above the stack.

–::::::::::::Stack

:::::::::::::Limit. Points to the topmost element of

the stack (alternatively: to the first address notpart of the stack).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-31

#32#50#90

Stackpointer

InitiallyA

fter

#32#50#90#40

Push(#40)

After

Pop

(#40 was

retrieved)

#90#50#32

After

AD

D(using stackaddressingm

ode)

#140#32

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-32

• Variations:

– Stacks might grow downwards instead ofupwards – so the element below an element getsa higher rather than a lower memory address.∗ This is the case in the Pentium.

– Stack base and/or stack limit might behardwired.

– For efficiency reasons one might have registerscontaining the content of the top element andof the second element of the stack.

Error messages are issued if the CPU wants to

– Pop an element from an empty stack.– Push an element on a stack which is full.

(This often indicates that one has a procedurewhich is infinitely often calling itself).

• The next two slides show two implementations ofa stack

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-33

Stack-- Limit

- Pointer

-Base

04A50245138545903489FF033FA9

freefreefreefreefreefree

Reservedforstack

Memory:Registers:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-34

Stack-- Limit

- Pointer

-Base

04A50245138545903489FF033FA9

freefreefreefreefreefree

Reservedforstack

TopElement

2nd Element04A5

0245

Registers: Memory:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6 6-35

Supplementary Materialfor Sec. 6.

(i) Organization of Main Memory.

(ii) Organization of Associative Memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-36

(i) Organization of Main Memory

Two extremes:

• Addressable up to to word level.Cheaper, more dense, higher access time.

• Addressable up to bit level.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-37

Example: 16-Mbit DRAM (next slide but one).

• Organized as four arrays of2048 · 2048 = 211 · 211 bits each.

• Requires two 11-bit addresses to access a single bit.

• Organized in rows and columns.

• Address is sent::::::::::::::::::::::::multiplexed,

ie. sent in two cycles:

– First an 11 bit address is sent to identify a row.– Then an 11 bit address is sent to identify the

column.

• Whether address identifies row or column done viasignals:

–:::::::::Row

:::::::::::::::::address

:::::::::::::select (

:::::::::RAS) and

–::::::::::::::::Column

:::::::::::::::::address

:::::::::::::select (

::::::::::CAS).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-38

• Refresh circuity:

– When refresh is done,∗ Address buffers are disabled,∗ Refresh counter steps through all rows.∗ For each row, the complete column is refreshed

in one step.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-39

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-40

Next but one slide: EPROM and DRAM chips.

• A0 - A19: Address lines.

• D0- D7: data lines.

• Vcc: Power supply.

• Vss: Ground pin.

• CE (chip enable): indicates, whether address isvalid for the chip or not. (since there are morememory chips in use).

• Vpp: program voltage used during programming(write operation, for EPROM).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-41

• WE: Write enable (for DRAM chip).

• OE: Output enable (for DRAM chip).

• RAS: row access select (for DRAM chip).

• CAS: column access select (for DRAM chip).

• NC: not connected (so that pins of the chip matchpins of the socket).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-42

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-43

Module Organization:

• Next slide: Organization of 256 KByte memory,

– using eight 256 kilobit chips.

• Each chip stores 1 bit of the word.

• (Word length = 8 bit = 1 byte).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-44

� �� �

� �� �

� �� �

� ��

� �

Chip #1

512 x 512 bits

512 x 512 bits

Chip #8

Column-Address

Row

a-Adress

Row

a-Adress

Column-Address

1

8

MemoryAddressRegister(MAR) Memory

BufferRegister(MBR)

9

9

(Chip #2 - #7)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-45

(ii) Organization ofAssociative Memory

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-46

(b = 1 bit)

Requested A

ddressIm

plementation of A

ssociative Mem

ory

Com

pare

Tag of 1st line

Com

pare

Tag of 2nd line

Data of 1st line

Data of 2nd line

From

other cache linesF

rom other cache lines

From

other cache linesH

it/Miss

bbb

b

bb

Read D

atab

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-47

(b = 1 bit)

Requested A

ddress

Com

pare

Tag of 1st line

Com

pare

Tag of 2nd line

Data of 1st line

Data of 2nd line

From

other cache lines

From

other cache linesH

it/Miss

bb

Read D

ata

Exam

ple: Hit on F

irst Line

10

e

e0

e

e

f

f0F

rom other cache lines0

0

0

11 f

f

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 6, Suppl. 6-48

7. Machine Languages

(a) Basic Types of Instructions.

(b) Basic Form of Instructions.

(c) Translation of Higher Level Instructions into Assembly Language.

(d) Addressing Modes.

(e) Endianess.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-1

Assembly Lanugages

• Commands executed by the machine are words inbinary.

• Too difficult to program by human beings.Instead use of

::::::::::::::::::assembly

::::::::::::::::::::::languages:

– Like a programming language.– But each assembly language instruction

corresponds exactly to one machine instruction.– Assembly languages are translated by a program,

called::::::::::::::::::::assembler into machine code.

– Only small and simple calculations (eg. ofaddresses) are carried out by the assember,especially no optimizations.∗ Assembly language code is essentially identical

to machine code.∗ Compilation of higher level languages into

machine code is more complicated and theprogramer has no direct control over, whichmachine instructions are actually used.∗ When writing time-critical code, assembly

language code is usually much faster (typicallytwice as fast).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-1a

Typical Assembly Language Instructions

• Typical instructions in assembly languages have theform:INSTR A1, A2, A3, ... Anwhere

– INSTR is an instruction, typically a word in uppercase of 3 or more characters like MOVE, SUB,ADD;

– A1, ... , An are operands or destinations of theoperation.

• For instance ADD R1,R2,R3 meansadd contents of registers R2, R3 and store result inR1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-1b

(a) Basic Types of Instructions

There are essentially four groups of instructions:

(i) Instructions for data processing.

(ii) Instructions for data transfer.

(iii) Instructions for Input/Output (I/O).

(iv) Control Instructions.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-2

(i) Instructions for Data Processing

• Perform common arithmetic and logic operations,and

:::::::::shift operations (see below).

• On RISC architecture all arithmetic/logicoperations have a simple format:

Name destination, source1, source2 ,

where destination, source1 and source2 areregisters (eg. R7) or constants.

• On CISC machine, much more sophisticatedformats available.

• Advantage of RISC instruction set:

– More uniform execution (same number of cyclesper instruction).

– More uniform and shorter instruction format.– Expected to be much faster.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-3

Examples

• ADD R10,R1,#5: ≈ R10:= R1 + 5.Add 5 to content of R1, store result in R10.

• SUB R1,R0,R3: ≈ R1:= R0 - R3.Subtract R3 from R0, store result in R1.

• Similarly MUL, DIV (multiplication, division).

• XOR RO,R1,#0xC8:Bitwise XOR of R1 and 0xC8, store result in R0.Eg. bitwise XOR of 0b0110 0110 and 0b1100 1000is:

0b 0 1 1 0 0 1 1 00b 1 1 0 0 1 0 0 00b 1 0 1 0 1 1 1 0

• Similarly AND, OR, NOT (bitwise).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-4

Examples (Cont.)

• Shift left, shift right:

– 0b0100 0000 shifted right becomes 0b0010 0000.– 0b0100 0010 shifted left twice becomes 0b0001

0000

• Arithmetic shift.

• Rotate operations(rotate left: new LSB is old MSB;rotate right: new MSB is old LSB):ROTATELEFT #0b10101 has result 0b01011ROTATERIGHT #0b10101 has result 0b11010

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-5

(ii) Instructions for Data Transfer

• Movement of data.

• On RISC Machines only these instructions can movedata between registers and main memory.

• Two main instructions on RISC

– Load instructions (from main memory into someregister):LOAD R20,[15]: Load memory location 15 intoregister R20.

– Store instructions (from some register into mainmemory):STORE [12],R1: Store content of register R1into memory location 12.

• On CISC machines instructions for arbitrarymovements, for instance from main memory tomain memory; often called MOVE.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-6

Comparison with High-Level Languages

• Above mentioned instructions correspond to highlevel statements:

i:= 15; STORE Ri,#15i:= j+10; ADD Ri,Rj,#10k:= l-i; SUB Rk,Rl,Ri

• Here we assume that i,j,k,l are stored in registerRi, Rj, Rk, Rl.

• If they are in main memory, they have first to beloaded (or other adressing modes be used).

• More complex statements like

i:= (j+k) * l

have to be decomposed:

aux:= j+k;i:= aux * l.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-7

(iii) I/O Instructions

• Transfer data to and from I/O.

• Instructions for control of I/O(ie. start/stop execution of I/O).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-8

(iv) Control Instructions

•::::::::::::::::::::::::::::Unconditional

::::::::::::::::::jumps (goto in high level

languages).

•:::::::::::::::Branch

:::::::::::::::::::::::::instructions (conditional jumps).

•:::::::::::::::::::::::Conditional

::::::::::skip of next instruction.

•:::::::::::Jump

::::::to

:::::::::::::::::::::::subroutine

::::::or

::::::::::::::::::::::procedure (see later).

•::::::::::::::Return

:::::::::::from

:::::::::::::::::::::::subroutine (see later).

•::::::::::Stop program execution.

•::::::::::Wait until condition (clock or I/O) is satisfied.

•::::::No

:::::::::::::::::::::operation.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-9

Unconditional Jump

• Unconditional Jump written as JMP A.

– Jump to address A. (Corresponds to goto inhigher level languages).

– Address usually indicated by a label in assemblylanguages, eg.:

JMP LabelA· · ·

Label A: Next Instruction to be executed.– The

:::::::::::::::::::::assembler (which translates assembly

language code into machine languages)calculates the actual address.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-10

Branch Instructions

• An instruction likeif <condition> then <instruction>requires three steps to be carried out:

– Calculation of <condition>.– Depending on <condition>, either jump at the

end of <instruction> or not.– Execution of <instruction> (in case<condition> was true).

• For the second step machine languages provideusually

::::::::::::::branch

::::::::::::::::::::::::::instructions.

– Typical mnemonic like BEQ (branch if equal), BNQ(branch if not equal) etc.

– BEQ A means: branch, if the result of the lastcalulation was zero, to address A.Address A is given usually in relative addressing(see later).In assembly languages usually given by a label(as for unconditional jumps).

– The condition, depending on which onebranches, is determined by flags.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-11

Flags

• Flag registers are one-bit registers, which areusually set automatically depending on the lastarithmetic/logic operation performed.

• Main flags are

–:::Z flag∗

::::::::::Zero – flag.∗ Set if result (of last arithmetic/logic operation)

was zero.–

::::N – flag.∗ Set if result was negative.

–:::C – flag.∗ Set if result had carry/borrow.

(= Overflow for arithmetic operations onunsigned numbers).

–:::V – flag.∗ Set if result gave over/underflow.

for arithmetic operations on signed numbers.(Note that arithmetic operations on signed andunsigned numbers coincide. The difference ishow to determine when overflow occurs).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-12

Conditional Jumps

• Branch instructions advice to jump, if one flag isset (or not set). Typical branch instructions are

–:::::::BEQ labelA∗ Branch, if Z flag is set, to labelA.∗ (If compared values were equal).

–:::::::BNQ labelA∗ Branch, if Z flag is not set, to labelA.∗ (If compared values were not equal).

–:::::::BRN labelA∗ Branch if N flag is set to labelA.∗ (If first compared argument less than the

second).–

:::::::BRP labelA∗ Branch if N flag is not set (positive result) to

labelA.∗ (If first compared argument greater or equal

to second).–

:::::::::::::::::BCS/BCC labelA∗ Branch, if C flag is set/not set to labelA.

–:::::::::::::::::BVS/BVC labelA∗ Branch, if V flag is set/not set to labelA.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-13

Comparison Instructions

• For interpretation of a high-level languageinstructionif r=s then · · · else · · ·one needs to

– subtract r from s,– this sets zero flag, if the result is zero,– then depending on this flag make the decision.– Result of subtraction can be thrown away.

• Therefore CMP instruction:

– CMP R1,R2∗ calculates R1 - R2,∗ but throws result away.∗ Only result of flags relevant.

– So if R1 < R2, then the negative flag is set,otherwise not.If R1 = R2, then the zero flag is set, otherwisenot.

• CMP is a data processing instruction.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-14

Conditional Jumps and “Spaghetti-Code-If”

• The two instructions:

CMP R0,R1BEQ label1

correspond to the “Spaghetti-Code”-Instruction:

if R0 = R1 then goto label1.

• The two instructions:

CMP R0,R1BNQ label1

correspond to the “Spaghetti-Code” Instruction:

if R0 6= R1 then goto label1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-15

Conditional Jumps and“Spaghetti-Code-If” (Cont.)

• Similarly

CMP RO,R1BRN label1

corresponds to

if R0 < R1 then goto label 1.

• CMP RO,R1BRP label1

corresponds to

if R1 ≤ R0 then goto label 1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-15a

(b) Basic Form of Instructions.

• An instruction consists of an:::::::::::::::::::operation and

:::::::::::::::::::operands.

• The CPU needs to obtain up to four kinds ofinformations from the instruction:

– The::::::::::::::::::::operation the CPU has to perform:

∗ Which instruction,∗ including size of the data

(eg. bit, byte, word, double word),∗ format of data (eg. unsigned integer,

floating-point etc).– The

:::::::::::::source of the operation:

Not needed in case of unconditional and flag-dependent control operations,

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-16

Basic Form of Instructions. (Cont.)

- The:::::::::::::::::::::::destination of the operation.

Not needed in case of unconditional and flag-dependent control operations,

- The:::::::::::::::::location

:::::::of

::::::::::the

::::::::::::next

:::::::::::::::::::::::::instruction to be

fetched.Usually the instruction following the current one,except for control instructions.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-17

Number of Addresses

• Most arithmetic and logic functions have one ortwo arguments.

• Together with the destination, this would requiretwo or three addresses.

• Four addresses needed if we have an address ofthe next instruction (rarely used).

• Formats used where one or more addresses canbe omitted.(Register numbers considered here as an address aswell).This is done in order to save space needed forsaving operations.(Increases speed, since fetching of instructionsrequires time).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-18

Reduction of Number of Addresses

In case of functions with two arguments we couldhave:

•::::::::::::Three

:::::::::::::::::::::::addresses.

Eg. SUB A,B,C expresses: A:= B-C.

•:::::::::Two

:::::::::::::::::::::::addresses.

Eg. SUB A,B expresses: A:= A-B.

•::::::::One

:::::::::::::::::::address.

Used if one has one specific register in the CPU,called

:::::::::::::::::::::::::accumulator,

:::::::AC.

Eg. SUB A expresses: AC:= AC - A.

•::::::::::Zero

::::::::::::::::::address.

Used if one has one specific stack.SUB expresses:

– Pop two uppermost elements from stack,– subtract first popped from the second popped,– push result on the stack.

(Push and Pop however require at least oneaddress).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-19

Example: A = (B+ C)/(D-(E · F))with 3/2/1/0 Address Instructions

• With three-address instructions:

Instruc- Inter- Commenttion pretationADD A,B,C A:= B + CMULT Z,E,F Z:= E · F ; Z= E · FSUB Z,D,Z Z:= D-Z ; Z= D- (E · F)DIV A,A,Z A:= A/Z

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-20

Example A = (B+ C)/(D-(E · F))(Cont.)

• With two-address instructions:

Instruc- Inter- Commenttion pretationLOAD A,B A:= BADD A,C A:= A + C ; A= B + CLOAD Z,E Z:= EMULT Z,F Z:= Z · F ; Z = E ·FLOAD U,D U:= DSUB U,Z U:= U-Z ; U = D -

; (E ·F)DIV A,U A:= A/U ; A= (B+C)/

; (D-(E ·F))

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-21

Example A = (B+ C)/(D-(E · F))(Cont.)

• With one-address instructions:

Instruc- Inter- Commenttion pretationLOAD B AC:= BADD C AC:= AC + C ; AC = B + CSTORE A A:= AC ; A = B+CLOAD E AC:= EMULT F AC:= AC · F ; AC = E · FSTORE Z Z:=AC ; Z = E · FLOAD D AC:= DSUB Z AC:= AC- Z ; AC = D - (E · F)STORE Z Z:= AC ; Z = D - (E · F)LOAD A AC:= A ; AC = B+CDIV Z AC:= AC/Z ; AC = (B+C)/

; (D - (E · F))STORE A A:= AC ; A = (B+C)/

; (D - (E · F))

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-22

Example A = (B+ C)/(D-(E · F))(Cont.)

• With zero-address instructions(except for PUSH and POP):

– PUSH A means push content at memory locationA on stack.

– POP A means pop uppermost element from stackand store it in A.

Instruction Stack (rightmost= top element).

PUSH B ; BPUSH C ; B,CADD ; B+CPUSH D ; B+C,DPUSH E ; B+C,D,EPUSH F ; B+C,D,E,FMULT ; B+C,D,E·FSUB ; B+C,D-E·FDIV ; (B+C)/(D-E·F)POP A ; Empty

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-23

Warning

• When translating high level commands intomachine language, one should usually not makeconversions like

– replacing A+ (B + C) by (A+B) + C.– replacing A · (B + C) by A ·B +A · C.

• Since we cannot represent floating point numbersprecisely, floating point arithmetic does not followthose laws.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-24

Example

• For simplicity assume that

– floating point numbers have a two bit significandonly,

– we don’t ignore a leading 1 in the significand,– have no biased exponents,– we round towards 0 (i.e. extra bits which cannot

be represented in our significand are omitted).

• Then the machine will calculate as follows:

(0b0.1 · 20 + 0b0.1 · 2−2) + 0b0.1 · 2−2

= (0b0.1 · 20 + 0b0.001 · 20) + 0b0.1 · 2−2

= 0b0.1 · 20 + 0b0.1 · 2−2 (0.101 rounded to 0.1)

= 0b0.1 · 20 + 0b0.001 · 20

= 0b0.1 · 20 (0.101 rounded to 0.1)

0b0.1 · 20 + (0b0.1 · 2−2 + 0b0.1 · 2−2)

= 0b0.1 · 20 + 0b1.0 · 2−2

= 0b0.1 · 20 + 0b0.01 · 20

= 0b0.11 · 20 6= 0b0.1 · 20

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-25

(c) Translation of High-LevelInstructions into Assembly Language

Interpretation of If-Clauses

• Interpretation of

if x=y then begin <action> end;<actionInAllCases>

– Assume x, y in registers R0, R1.– First we translate the statement into “Spaghetti-

code”:

if x 6=y then goto Label1<action>

Label1: <actionInAllCases>– Replace the if-statement by the instructions

CMP R0,R1BNQ Label1

– We obtain as assembly code:

CMP R0,R1BNQ Label1<action>

Label1: <actionInAllCases>

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-26

Translation of If-Then-Else

• Translation of

if x=y then begin <ifAction> end;else begin <elseAction> end;

<actionInAllCases>

– Assume x, y in registers R0, R1.– First we translate the statement into “Spaghetti-

code”:

if x 6=y then goto Label1<ifAction>goto Label2

Label1: <elseAction>Label2: <actionInAllCases>

– Note that the goto-statement is necessary.– Then we replace the if-statement by the

instructions:

CMP R0,R1BNQ Label1

and goto by JMP

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-27

Translation ofIf-Then-Else (Cont.)

• We obtain as assembly code:

CMP R0,R1BNQ Label1<ifAction>JMP Label2

Label1: <elseAction>Label2: <actionInAllCases>

• Crucial: The JMP instruction (in boldface).Frequent Mistake to omit it.

• Note as well that no JMP at the end ofelseAction required.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-28

Interpretation of a While Loop

• Interpretation of

while x 6= 0 do begin <whileAction> end;<actionAfterwards>

– Assume x in register R0.– First we translate the statement into “Spaghetti-

code”:

Label1: if x=0 then goto Label2<whileAction>goto Label1

Label2: <actionAfterwards>– Note that the goto-statement is necessary.– Then we replace the if-statement by the

instructions:

CMP R0,R1BEQ Label2

– Further replace goto by JMP.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-29

Interpretation of aWhile Loop (Cont.)

• We obtain as assembly code:

Label1: CMP R0,#0BEQ Label2<whileAction>JMP Label1

Label2: <actionAfterwards>

• Note the JMP instruction.

• For-loops, repeat-until-loops can be interpreted aswhile-loops.

• Good exercise: interpret them directly yourself.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-30

Optimization (While Loop)

• Body of while/for/repeat loops are usually the mosttime consuming parts of a program.

– Might be executed for instance a million timeswhereas the initialization is only executed once.

⇒ worth to safe every instruction.

• If we check the condition at the end and then usethe branch as a jump we can safe one instruction.

• We have only initially to jump to the test so that,in case initially the condition is not fulfilled, thewhile-loop is never executed.

• The result is as follows:

JMP Label2Label1 <whileAction>Label2 CMP R0,#0

BNQ Label1<actionAfterwards>

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-30a

(d) Addressing Modes

• Question:

– Where is the source and target of an operationlocated?∗ E.g. in an instructionADD A, B, CWhat does a hexadecimal value A mean:· a constant,· a memory address,· a register number?

– Where is the next instruction (in case of jump,branch)?

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-31

Address vs. Operand

• When we want to address the target of anoperation (where the result of an operation is tobe stored), what matters is the

::::::::::::::::::effective

:::::::::::::::::address,

(::::::EA), ie. the address in main memory or the

register number, where the data is to be stored.

– EA might be a physical address. When pagingis used (see later), it is a virtual address,which is then mapped by a combination ofhardware/software to some physical address.

• When we want to use the addressing mode todetermine in case of branch or jump the addressof the next instruction to be executed, again,what matters is the effective address, since the PCis to be updated to this address.

• However, when we want to address the sourceof an operation (arithmetic/logic operation, thedata to be stored or loaded), what matters is theoperand, which is the content of the data at theEA.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-32

Remark on Example Instructions

• Use of pseudo assembler code (like ADDIMMEDIATE) in the following.

• In real assembler code, abbreviations used whichsubsume both operation and addressing mode.

• Main example will add one operand to theAccumulator (AC).

– Not realistic (since in complex architectures, weusually don’t have a specific register AC).

– Only for simplification.– Usually second operand arbitrary register content

or constant, but complicated addressing modesmight as well be possible.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-33

Syntax for Denoting Addressing Modes

In this module we use the following notation:

• When register numbers are meant, we writeexpressions

::::::::R#,

::::::::::R1#,

::::::::::R2# etc. This will be

used as well for registers with special names like

::::::::::SR#.

• When writing expressions like:::R,

:::::R1, the content

of the register is meant.So R := R+ 1 means that the content of registerR is increased by one.

• The previous two notations are specific for thismodule only. (Standard notations not used in asystematic way).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-34

Syntax for Denoting Addressing Modes (Cont.)

• The following is standard:

• When we want to refer to the content of a registergiven by its number, we put brackets around itand write for instance

:::::::::::::(R1#).

– So (R1#) and R1 mean the same.

• If A is an address in main memory,:::::::(A) is the

content of memory location A.

• More complex expressions are defined by iteratingthis syntax:

– E.g.::::::::::::::::::((R1#)) is the content of memory location

at the address which is the content of registernumber R#.

–:::::::::::::::::::::(((R1#))) is the content at memory locationgiven by the previous expression.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-35

Main Addressing Modes

(i) Immediate Addressing.

(ii) Direct Addressing.

(iii) Indirect Addressing.

(iv) Register Addressing.

(v) Register Indirect Addressing.

(vi) Displacement Addressing.

(vii) Stack Addressing.

(viii) More Advanced Addressing Modes.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-36

(i) Immediate Addressing

• Address = Operand(So operand is a constant, ie. a degeneratedaddress.This mode cannot be used for the target of astore/source of a load operation or for denotingjump/branch addresses).

Address Field A

Operand

Opcode

(Opcode specifies the instruction to be executed,eg. “ADD”).

• Notation: OPERAND = A.

• ADD IMMEDIATE 17 expresses:AC:= AC + 17.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-37

Notations

• We write ADD #17 for ADD IMMEDIATE 17.

• By default, 17 means (unless specified differently)decimal number 17.For denoting hexadecimal 17, we write 0x17.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-38

Advantages/Disadvantages ofImmediate Addressing

• Fast (not even necessary to look up in memory).

– However, when loading the instruction code, theaddress has to be looked up in main memory.

• Good for dealing with constants in programminglanguages(especially small constants like 2 in i:= i+2).

• Limitations:Since both operation and addresses have to bestored in the instruction code

– either limited range of data to be stored in theinstruction,∗ (both number and complete instructions stored

as one word)– or two or more words per instruction needed.∗ Loading of these words requires extra time.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-39

(ii) Direct Addressing

• Address = actual memory address.

Opcode Address Field

A

ContentMain Memory

CA

Memory Address(Operand is C)

• EA = A (Effective Address = A).

• Operand = (A).

• ADD DIRECT 0x17 means:AC:= AC + content at memory location 0x17,AC := AC + (0x17)(sometimes written as AC ← AC + (0x17)).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-40

•::::::EA is the

:::::::::::::::::Effective

::::::::::::::::::::Address.

• This might be the physical address,

• or might be a virtual address, which is thentranslated using paging tables or other mechanismsto a physical address.(Invisible to the programmer; see later).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-41

Advantages/Disadvantages ofDirect Addressing

• Fast, since no calculation of memory addressrequired.Suitable for reference to scalar variables in highlevel programming languages.

• However, range of address space limited orlonger instruction codes required, similarly asfor immediate addressing.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-42

(iii) Indirect Addressing

• If address in instruction code is A, effective addresscalculated as follows:

– Look up content B at memory location withaddress A.

– Effective address is B.

Opcode Address Field

B

Main Memory

C

BA

Memory Content(Operand is C)

Memory addressesA

A

• EA = (A)(EA = content of memory at address A).

• Operand = ((A)).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-43

• ADD INDIRECT 0x17 means:AC:= AC + content at memory location given atmemory location 0x17,AC := AC + ((0x17))

• Suitable for looking up pointer references(the pointer variable provides an address at whichthe content the pointer is pointing to can be lookedup).

• Suitable for looking up variables in a lookup table.

• Full range of addresses available for the address Blooked up.

• However, looking up of addresses is timeconsuming.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-44

(iv) Register Addressing

• Address is number of a register:

Opcode

Registers

Register number

���

Address Field

R#

A

(Operand is A)

• Register number R# acts as an address (referringto the register unit however).

• Operand = (R#).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-45

• ADD REGISTER 17 means:AC:= AC + content of register 17,AC := AC + R17(or AC:= AC + (R17#))

• Operand can be fetched very fast.

• Very small address space needed, so spaceavailable for complicated opcodes or multipleaddresses.

• RISC machines are optimized to use registers asmuch as possible.Arithmetic operations on RISC machines usuallyuse this addressing mode only.

• Suitable for simple variables (i.e. no arrays, stringetc.) which are used frequently.Eg. i in for i=1 to 10 do a[i]:= a[i+20].

• Limitation: Number of registers limited.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-46

(v) Register Indirect Addressing

• Effective Address is content of a register:

Registers

Opcode Address FieldR#

R#

Register number

A

(Operand is B)

A

Main Memory

B

• EA = (R#)(EA = content of register with number R#).

• Operand = ((R#)).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-47

• ADD REGISTER INDIRECT 17,or shorterADD (R17)means:AC:= AC + content of memory location at addressgiven in register 17,or AC := AC + (R17)(or AC:= AC + ((R17#)))

• Full range of main memory addresses available.

• Address calculation almost as fast as for directaddressing.

• Suitable for arrays.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-48

Example

• Assembler program which computes the sum ofelements in an array with 100 integer variables(enumerated from 0 to 99).

• In high level language the program is as follows:

sum:=0;for i=0 to 99 do sum:= sum + a[i];

• Translated into spaghetti code it reads:

sum := 0;i := 0;limit := 100;

L1: sum:= sum + a[i];i:= i+1;if i 6= limit goto L1;

• Assume now

– i not used afterwards,– each element of a[i] requires 4 bytes,– addresses refer to bytes.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-49

Example (Cont.)

• Store

– sum in register R1;– i in register R2;– index pointing to a[i] in R3;∗ initially R3 = A, where A is the address ofa[0];∗ in each step R3 increased by 4;∗ a[i] is now accessed as (R3) (short forREGISTER INDIRECT 3).

– limit stored in register R4.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-50

Example (Cont.)

LOAD R1,#0 ; R1:= 0; R1 stores sum.

LOAD R2,#0 ; R2 stores array index i.LOAD R3,#A ; R3:= A,

; A = address of a[0]; available at compile time.; R3 is pointer to a[i]

LOAD R4,#100 ; R4 stores 99 + 1L1: ADD R1,(R3) ; R1:= R1 + (R3).

; sum := sum + a[i]ADD R2,#1 ; i := i+1ADD R3,#4 ; Updating of pointer

; to a[i]CMP R2,R4BNE L1 ; Branch if R2 6= R4

; to L1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-51

Optimization

• Index R2 is not really needed.

• Instead use R3.

– When R3 has reached address of a[100], theprogram stops.

• Let A’ = A + 400 = address of a[100].

LOAD R1,#0 ; R1 = sumLOAD R3,#A ; R3 is pointer to a[i]LOAD R4,#A’ ;

L1: ADD R1,(R3) ; R1:= R1 + (R3); sum:= sum + a[i]

ADD R3,#4 ; Increment pointer to a[i]CMP R3,R4BNE L1 ; Branch if R3 6= R4

; to L1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-52

(vi) Displacement Addressing

• Address calculated by adding to one addresscontent contained at another address.

• Five main variants:

(vi.1) Relative Addressing(vi.2) Base-Register Displacement Addressing.(vi.3) Index Displacement Addressing.(vi.4) Post-Indexed Indirect Addressing(vi.5) Pre-Indexed Indirect Addressing.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-53

(vi.1) Relative Addressing

• Effective Address is result of addition of content ofaddress field to the program counter.

• Content will be considered as a signed number:both backwards and forwards references possible.

Opcode Address Field

A

PC+ PC + A B

Main Memory

• EA = PC + A(EA = program counter + A).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-54

Relative Addressing (Cont.)

• Main addressing mode used for jump addresses(conditional, unconditional and subroutine jumps).

– Whereas full jump address requires many bits,usually the distance to it from current addressis small and requires only few bits.

– However, one addition necessary.∗ This is still faster than using direct

addressing, since having to load longerinstructions from main memory takes longerthan performing this addition.∗ Further, one can reach addresses, which are

unreachable using direct addressing (unless thedirect address has the same length as ordinaryaddresses).

– JMP REL DISPL +0x10 means:jump 16 addresses forward.

– JMP REL DISPL -0x10 means:jump 16 addresses backward.

• Since this addressing mode is usually only used fordetermining jump/branch addresses, the operand(B in the last picture) is not important (it’s a codefor the next instruction to be executed), only theEA (PC + A) matters. The new PC value is, if thebranch/jump is taken, the EA.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-55

Relative Addressing (Cont.)

• Often, while fetching an instruction, the PC isalready incremented, so that it contains theaddress of the instruction following the currentone.

– Then the next instruction can be already fetchedand decoded.

– However, in this case the displacement in rel.addressing has to be added to the address of thenext instruction.

– Whether this takes place is architecturedependent.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-56

(vi.2) Base-Register DisplacementAddressing

• Effective address is result of addition of an offsetgiven in the address to the contents of a register.

• Two addresses needed:

– immediately given offset,– number of the register.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-57

Base-Register DisplacementAddressing (Cont.)

OpcodeAddressField 1 Address Field 2

AR#

Registers

BR#

Register number

+

B+A

Main Memory

C

(Operand is C)

• EA = (R#)+A(EA = content of register with number R# plusoffset A).

• Operand = ((R#)+A).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-58

Base-Register DisplacementAddressing (Cont.)

• Used for segmentation:

:::::::::::::::::::::::::::::Segmentation: Several processes (programs) runon the same machine interleaved.

• Variables used in these processes organized inmemory segments.

• Segments are allocated dynamically at run time,when the process is created (or requires morememory).

• Use of base-register displacement addressing:

– Store in (R#) the beginning of the address of asegment, known at run time.

– A contains the at compile time known offset.

• Usually (R#) is considered as a long address (eg.32 bit) whereas A is a small displacement (eg. 8bit) within the segment.

• Register might be implicit (a standard register forthis mode, eg. segment register SR in the Intelinstruction set).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-59

Base-Register DisplacementAddressing (Cont.)

Segment

Segment

Segment(Runtime)

Offsetknown atcompile time

previous

nextSegment

Beginning of

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-60

(vi.3) Index DisplacementAddressing

• Similar formula as before:EA = A + (R#).But now

– A is a possibly long starting address (eg. 32 bitnumber).

– (R#) is a small offset (eg. 8 bit number),

• Used for arrays.A contains starting address of the array,R the displacement.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-61

Index DisplacementAddressing (Cont.)

Begin ofArray

Displacement

a[i]

a[0]

(fixed)

small)(variable,

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-62

Auto Incrementing

• Index displacement addressing often combined withauto increment:

•:::::::::::::::::::::::::::::::Post-increment After carrying out the addresscalculation, as part of the instruction the registeris incremented by one:

EA = A + (R#)(R#) := (R#)+1

•:::::::::::::::::::::::::::::::::Post-decrement:

EA = A + (R#)(R#) := (R#)-1

•:::::::::::::::::::::::::::::Pre-increment Register incremented beforecalculating the address:

(R#) := (R#)+1EA = A + (R#)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-63

Auto Incrementing (Cont.)

• Similarly::::::::::::::::::::::::::::::Pre-decrement.

• Useful when iterating over all elements of an arrays.

• Even higher post/pre-in/decrement is used (forinstance by 2, 4, 8).

• All the above used for loops through an array, eg.

for i=1 to 10000 do a[i]:= a[i] ∗ 32

– But less efficient then using register indirectaddressing.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-64

(vi.4) Post-Indexed IndirectAddressing

• Effective Address is result of addition of the contentof a register to the content of an address given inthe address field.

• Of limited use.

AddressField 1 Address Field 2

AR#

Registers

R# B

Register number(Operand is D)

+C + B D

AMain Memory

C

Opcode

• EA = (A) + (R#)

• Operand = ((A) + (R#)).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-65

Post-Indexed IndirectAddressing (Cont.)

• Used for:::::::::::::::process

:::::::::::::::::control

:::::::::::::::blocks,

:::::::::PCBs (which

provide control information for one individualprocess, like priority or time it was started)

– A contains a pointer to the beginning of thePCB of the current process.∗ So when switching to another process, (A) will

be modified.∗ Process control is not carried out often,

therefore one does not use a register for storing(A).

– (R#) contains the displacement within thatblock, which allows to access individual fieldsin the PCB.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-66

Post-Indexed IndirectAddressing (Cont.)

A

(R#)

(A)

Pointer to Beginning

Process Control Block

of a Process Control Block

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-67

(vi.5) Pre-Indexed IndirectAddressing

• EA = (A+(R#))

• Operand = ((A+(R#))).

OpcodeAddressField 1 Address Field 2

R# A

Registers

R# B

Register number(Operand is D)

+A + B C

C D

Main Memory

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-68

Pre-Indexed IndirectAddressing (Cont.)

• Used for:::::::::::::::::::multiway

:::::::::::::::branch

::::::::::::::tables:

– A contains the address of the beginning of abranch table.

– Branch table contains addresses, to one of whichone jumps.

– Depending on some calculation, one of theseaddresses is to be chosen.

– Result of this calculation will provide an offsetstored in (R#).

– Next instruction to be executed will be theaddress contained at A + (R#).

• Usually not both pre-indexing and post-indexingindirect found in the same architecture.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-69

Pre-Indexed IndirectAddressing (Cont.)

A(R#)

Branchtable

Next Instruction executed

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-70

(vii) Stack Addressing

• Operations refer to stack.

– Eg. ADD: Pop two topmost elements from thestack, add them store result on the stack.

– No explicit address needed, implicitly one refersto the stack.

• PUSH, POP have

– one implicit address referring to the stack and– one epxlicit address using another addressing

mode, denoting the element to be pushed orpopped.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-71

(viii) More AdvancedAddressing Modes

More advanced addressing modes are used.Example: Pentium II has the following addr. modes(SR = segment register, I = index register, B = baseregister):

• Standard Addressing modes:

– Immediate Addressing.– Register Addressing.– Relative Addressing.

• Standard Addressing modes, but with one extraaddition of SR# (segment register).

– Relativized direct:∗ EA = (SR#) + A∗ This is base register displacement with a

special register.– Relativized register indirect:∗ EA = (SR#) + (B#).∗ This is post-indexed addressing with a special

register.– Relativized base register displacement:

EA = (SR#) + (B#) + A.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-72

More AdvancedAddressing Modes (Cont.)

• Advanced Adressing Modes:

– Scaled index with displacement (Scalingfactors: S=1,2,4,8;eg. for arrays, where each element requires1,2,4,8 bytes):EA = (SR#) + (I#) · S +A.

– Base with index and displacement (eg. fortwo-dimensional arrays, or arrays on a stack):EA = (SR#) + (B#) + (I#) +A.

– Base with scaled index and displacement(eg. for arrays on a stack):EA = (SR#) + (I#) · S + (B#) +A.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-73

(e) Endianess

• If an address or number consists of several bytes,what is the ordering of the individual bytes?

• Example: 0x1234 5678 can be stored at address0x0256 in the following to ways:

Big Endian Little EndianAddress Value Address Value

0x256 0x12 0x256 0x78

0x256 0x34 0x256 0x56

0x256 0x56 0x256 0x34

0x256 0x78 0x256 0x12

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-74

Endianess (Cont.)

• Left way is called::::::big

:::::::::::::::endian.

– The most signficand byte has the lowest address.– Used by IBM 370/390, Motorola 680x0, Sun

SPARC, most other RISC machines.

• Right way is called:::::::::little

:::::::::::::::endian.

– The least signficand byte has the lowest address.– Used by Intel 80x86, Pentium, VAX, Alpha.

• PowerPC is:::::::::::::::::::bi-endian: it supports both modes

(selected by a flag).

• Differences in performance.

– On big endian machines, the most signficandbyte is accessed first, on little endian machinesthe least significand byte is accessed first.

– Difference in performance minor.

• However the layout of data stored different.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-75

Endianess (Cont.)

• Terminology goes back to Part I, Chapter 4 of“Gulliver’s Travels” by Jonathan Swift.

– It contains the story of a religious war betweentwo groups, one that breaks eggs at the big endand one that breaks them at the little end.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 7 7-76

8. Procedures and Functions

(a) Machine Language Instructions for Procedure Calls

(b) Parameters.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-1

(a) Machine Language Instructionsfor Procedure Calls

• Procedure/function calls occur quite often in highlevel languages.

• Many machine languages have a correspondingmechanism.

– One instruction for calling a procedure:∗ CALL A∗ Call procedure at memory address A.

– One instruction for returning from a procedure:∗ RETURN∗ Return to the program calling the current

instruction.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-2

��

��

��

��

��

��

��

��

��

CALL Sub2

CALL Sub1

ADD R10,R1,0x15

(at address labelled (add address labelled Sub1) Sub2)

Main P

rogram P

rocedure Sub1 Procedure Sub2

ADD R2,R2,R3

LOAD R2,[A9]

RETURN

RETURN

MUL R3,R3,0x12

MUL R2,R2,0x3

STORE R3,[A9]

STORE R10,[A4]

ADD R2,R2,[A9]

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-3

��

��

��

��

��

��

��

��

��

CALL Sub2

CALL Sub1

ADD R10,R1,0x15

(at address labelled (add address labelled Sub1) Sub2)

Main P

rogram P

rocedure Sub1 Procedure Sub2

ADD R2,R2,R3

LOAD R2,[A9]

RETURN

RETURN

MUL R3,R3,0x12

MUL R2,R2,0x3

STORE R3,[A9]

STORE R10,[A4]

ADD R2,R2,[A9]

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-4

Order of RETURN

• A procedure might have multiple entries:

– Program1:MULT R1,R1,#14CALL Label1

– Program2:ADD R1,R1,#34CALL Label2

– Subroutines:Label1 ADD R1,R1,#0x14Label2 MULT R1,R1,#0x3

· · ·RETURN

• or multiple RETURN-statements:

BNE Label 1RETURN

Label1 ADD R1,R2,#13RETURN

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-5

Order of RETURN (Cont.)

• Even a RETURN before the entry might occur:

CALL Sub1...

Label1 RETURNSub1 CMP R1,R2

BNE Label 1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-6

Execution of Procedure Calls

• When calling a procedure, the address which wouldbe executed after returning from the procedure

– address of the instruction immediately followingthe CALL statement

has to be stored somewhere.

• Problem of nested procedure calls:Unbounded many procedure calls might occur.

– Standard method:Push return address on a stack.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-7

Execution of Procedure CallsUsing a Stack

• Execution of CALL A:

– Address of next instruction to be executed ispushed on the stack.

– Jump to the address A is executed.(A usually specified in assembly language by alabel).

• Execution of RETURN:

– Pop address from the stack.∗ If stack is empty, error occurs.· Execute interrupt handler for this (see later).

∗ Otherwise, jump to the address retrieved fromthe stack.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-8

Picture copied from book not included

Picture copied from book not included

Example of Recursive Function Calls

• Procedures/functions might even call themselvesrecursively.

• Example:

::::::::::::::::::Factorial function n! defined by:

– 0! := 1– (n+ 1)! := n! · (n+ 1).

• So

– 1! = 1 · 1,– 2! = 1 · 1 · 2.– 3! = 1 · 1 · 2 · 3– n! = 1 · 1 · 2 · · · · · n

• Program:

function fac (i: integer) : integer;beginif i = 0 then fac:=1else fac:= i ∗fac(i-1)

end;

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-11

Example of Recursive Function Calls (Cont.)

• So

– fac(2) will call– fac(1) which will call– fac(0).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-12

(b) Parameters

• When calling a procedure, parameters have to bepassed on to the new procedure.

• Could be done via registers or main memory.

– However one needs to keep track of multiplenested function callsParameters for each function call have to bestored.

– Easiest way of organization by storing parameteron the stack.

• So

– Before calling a procedure, parameters arepushed on the stack.

– Then CALL is executed,therefore address of next instruction of the callingprogram is pushed as well on the stack.

– Address of the parameters is result of subtractingfrom the new stack pointer a certain offset,known when compiling the called procedure.

– Additional information is stored on the stack.See appendix to this section.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-13

Stackpointer

A

additionlinform

.additional

inform.

A

Call of f(1)

Call of f(0)

11 0

additionalinform

.

Main P

ro-gramC

allingf(1)

in f(1)A

fter termination

of f(1)A

fter termination

of f(1)

Exam

ple Call of f(1) from

Main P

rogram

additionalinform

.B

A = A

ddress following call of f(1) in m

ain programB

= Address follow

ing call of f(o) in procedure f

A1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-14

Types of Parameters

• Two kinds of parameters.

–::::::::Call

::::::by

::::::::::::::value.

∗ The value of the parameter is passed to theprocedure.∗ Changes to the parameter in the called

procedure, doesn’t affect the originalparameter.Therefore such changes will be lost.∗ Modelled by pushing the actual content of the

variable on the stack.∗ Inefficient, when dealing with arrays.· Complete array has to be copied.· Arrays can be extremely big.· In Java, for this reason arrays are always

passed by reference.–

::::::::Call

::::::by

::::::::::::::::::::::reference.

∗ Only the address to the original variable ispassed to the procedure.So address of the parameter stored in the listof parameters.∗ Only possible, if parameter is actually a

variable(not an expression like i− 1 in fac).∗ Changes to this parameter will affect the

original variable.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-15

Examples for Types of Parameters

• Consider the following program:var i: integer;...procedure p([var] i: integer);begin i:= i+1 end;...i:=5;p(i);writeln(i);

• If i is passed by reference, result is 6(“var”-parameter).

• If i is passed by value, result is 5.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8 8-16

Supplementary Materialfor Sect. 8.

(a) Parameter passing and local variables.

(b) Call by Name.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-17

(a) Parameter Passingand Local Variables

Local Variables

• A procedure might have local variables.

– When calling another procedure, local variableshave to be saved.

– When returning, local variables have to beretrieved again.

• Best way to store local variables on the stack.

– Can be done just before executing CALL.– More efficient to use the stack as the place

where these variables are kept throughout theprocedure.

– In order to access them we use a register,containing the address on the stack, where thecurrent local variables start.

– All registers can be accessed by adding to thisregister a certain offset.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-18

Frame Pointer

• So when a procedure is called, the following isdone:

– The location of the stack pointer is saved in aregister

:::::::::::::Frame

:::::::::::::::::Pointer.

– Then space is created on the stack for all localvariables.

– Can be done by just increasing the stack pointer.– Then∗ the frame pointer points to the beginning of

the local variables for the current procedure,∗ the stack pointer points to the top element

of the stack, which is the last of the localvariables.

– Later we will see, that we need one extra fieldon the stack below the local variables, to whichthe frame pointer points.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-19

}

Fram

e Pointer

Stack Pointer

Return A

ddress of calling program

xlocal variables

Local variable x located at address "F

rame P

ointer + 2"

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-20

Parameter Passing

• Parameters have to be passed on to the calledprocedure.

– Can be done via main memory or registers.– However one needs to keep track of multiple

nested function callsParameters for each function call have to bestored.

– Therefore better to store the parameter on thestack.∗ Before calling a new procedure, push the

parameters for the procedure on the stack.∗ Then call the procedure.∗ The called procedure can access the

parameters by subtracting from the framepointer a certain offset.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-21

}}

local variables

Fram

e Pointer

Stack Pointery

Param

eters forP

rocedure

Return A

ddress of calling program

y located at address "Fram

e Pointer - 3"

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-22

Old Frame Pointer

• When returning from a procedure, we need toretrieve the old frame pointer.

• So when calling a procedure, the old frame pointerhas to be pushed on the stack as well.

– Done in the called procedure.– Old Frame Pointer will be pushed first on the

stack.– The new frame pointer will point to this pointer.– Local variables will be stored on top of it.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-23

}}

local variables

Fram

e Pointer

Stack Pointer

Param

eters forP

rocedure

Return A

ddress of calling programO

ld Fram

e Pointer

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-24

Complete Procedure Call

• So the complete high level language procedure callresults in the following operations:

• In the calling procedure:

– The parameters for the called procedure arepushed on the stack.

– CALL is executed, which results in pushing thereturn address on the stack.

• In the called procedure:

– The register containing the (old) frame pointeris pushed on the stack.

– The frame pointer is updated to to the currentstack pointer.∗ Therefore it points to the location of the old

frame pointer.– Stack pointer is increased to make space for the

local variables.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-25

Stack Frame

• Notion of a:::::::::::stack

:::::::::::::frame.

– Stack frame of procedure is the part of the stackcontaining∗ parameters passed on to this procedure,∗ the return address to the calling program,∗ the frame pointer of the calling program,∗ the local variables for the called procedure.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-26

}}}

}}

Fram

e Pointer

Stack Pointery

Param

eters forP

rocedure

Stack Fram

e

local variablescalling procedure

Stack Fram

e

called Procedure

calling Procedure

local variablescalled P

rocedureO

Id Fram

e Pointer

Return A

ddress of calling program

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-27

(b) Call by Name

• A third kind of parameter type are:::::::call

:::::::by

:::::::::::::name

parameters.

• It occurs mainly in functional programming.

– The called expression is passed on to the calledprocedure.∗ In the example of fac, call by need for the

parameter i would mean that the expressioni− 1 is passed on to the called. procedure.

– Whenever the parameter is accessed, theexpression is evaluated.

– Advantages:∗ Efficient, if the parameter might not be

accessed at all, and if it is accessed, it willbe accessed at most once.∗ Especially, the expression used might not

terminate at all.If the parameter is not needed, the call of theprocedure might still terminate.

– Inefficient, if the parameter is accessed morethan once.

– If the expression for computing the parameterhas some side effect, the result might differ fromwhen using call by value.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-28

Example of Call By Name

• Consider the following program:

var i: integer

function f: integer;begini:= i+1;f:=1;end;

function p(j: integer);begini:= i+j;f:=j;end;

i:= 1;p(f(0));writeln(i);

• With call by value for the parameter of p, f isexecuted only once, the program prints 2.

• With call by name, f is executed twice, the programprints 3.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-29

Example 2 of Call By Name

• Replace in the above example p by the following:

function p(j: integer);begini:= i+1;f:=1;end;

• Now p doesn’t make use of j.

– With call by value, f(0) has still to be executed,the program prints as before 2.

– With call by name f(0) is never executed, theprogram prints 1.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 8, Suppl. 8-30

9. Operating Systems

(a) Overview.

(b) Processes.

(c) Memory Management.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-1

(a) Overview

•:::::::::::::::::::::Operating

::::::::::::::::System (

::::::OS)

= Program which provides the the following 2functions:

–:::::::::::::::::::::::::::::::::::::::(i) User Functions:∗ The OS provides an interface between the user

and the hardware.–

::::::::::::::::::::::::::::::::::::::::::::::(ii) System Functions:∗ The OS manages the resources of the machine.∗ This part essentially forms the

::::::::::::kernel of the

operating system.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-2

System Functions (Kernel)

User Functions

Hardware

Application Programs

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-3

(i) User Functions

• (α) Program Control.

– The OS allows the user to write, change andexecute programs with particular data.

– It allows to start several programs at the sametime and to execute them so that it appears asif they were running in parallel.

• (β) Input/Output Operations.

– The OS provides the user with an relatively easyinterface for communicating with I/O devices.

– Includes access to the network.

• (γ) File Handling

– Allows the user to manage and store informationlong-term, using symbolic names.(No need to find the physical location of dataon the hard disk).

• (δ) Provide a user interface

–:::::::::::::::::::::Command

:::::::::::::Shell.

–:::::::::::::::::::Graphical

:::::::::::User

:::::::::::::::::::::Interaces (

::::::::GUI).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-4

User Functions (Cont.)

• (ε) Provide an interface between applicationprograms and the system functions

– Under Unix via::::::::::::::system

::::::::::calls.

– Under Windows via the::::::::::::::::::::::application

::::::::::::::::::::program

::::::::::::::::::interface

:::::::::::::(API)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-5

Command Shell

• Program which:

– allows the user to type in commands;– interprets them;– and executes them.

• Example (“Command Prompt” under Windows):

– Prompt has the form:C:\WINDOWS >

– User can type in a command (e.g. dir).– The OS looks for the program typed in in its

search path.– In case of dir, DOS will find this program.– If the program is found, it is executed.

(In case of dir, the directory is listed).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-6

Advantages of the Command Shell

• Shell very popular amongst UNIX users.

– Allows to execute different programs fast.– The UNIX shell is particularly powerful.

Allows easily to write programs like:∗ Execute program prog applied to all files in

the current directory which contain the string“dbase”:

for file in ∗dbase∗ doprog $file

done.

∗ Show all occurrences in Pascal programs inthe current directory of the word “MyProg”at the beginning of a line.

· egrep ’^MyProg’ ∗.pas

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-7

Advantages of the Command Sheell (Cont.)

• Shell programs can be written both for

– routine tasks, executed frequently∗ (e.g. compile 5 special files and move the

compiled result to some directory)– tedious tasks, executed only once∗ (e.g. replace in all 50 pascal programs in this

directory the string ’ProgrWithOldName’ by’ProgrWithNewName’.)

• Special programming languages have beendeveloped for more advanced tasks.

– awk, sed (Unix specific).– Perl, Python (available on most platforms).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-8

Graphical User Interfaces

• Provides menus, icons.

• Several windows for different applications (as wellseveral shells possible).

• Using the mouse (or other devices) one can pointto menus and icons and execute programs attachedto them.

•::::::::::::::WIMP interface (

:::::Windows,

::Icons,

::::Menus,

:::Pointing Devices).

• (Pointing Devices = Mouse and related devices).

• Easier to use if one doesn’t know the system verywell.

– But much slower than using the shell.– Not many possibilities for automatization.

• GUIs are good interfaces for features of a programwhich one doesn’t use often.

– but very slow for features which are frequentlyused (like search or query/replace facilities of aneditor).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-9

(ii) System Functions

• Commands given by the user are translated intocommands sent to the kernel.

• System functions are:

– Memory Management.∗ Allocation of memory to the different

processes (= different programs) running onthe computer.∗ Provides the processes with a much bigger

address space than is actually available(

:::::::::::::virtual

::::::::::::::::::memory).

∗ Gives the possibility to write programs forprocesses as if they were the only programrunning (except of communication calls withother processes).∗ More details in (c).

– Process Management.∗ Different programs are running simultaneously

on the machine.∗ OS schedules them in an interleaved way, so

that it appears as if the processes were runningsimultaneously.∗ OS allocates memory to them.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-10

System Functions (Cont.)

– Protection.∗ Prevents programs from· damaging each other

(especially when running astray).∗ or getting access to protected data· eg. passwords.

– Accounting.∗ On multi-user systems – users are charged for

resources used (like printing of paper, CPUtime) and possibly excluded from using them.

– File System and I/O.∗ Actual handling of files (reading, saving,

listing) and other I/O interaction is carriedout by the system functions.(Interface is part of the user functions).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-11

(b) Processes

•::::::::::::::::Process

– = Separate program running, most of the timeindependent of others.

– Basis of modern operating systems.

• Most processes are I/O dependent.

– Most of the time they will be waiting for an I/Odevice to be ready to exchange data.

– While it is waiting, other tasks could be carriedout by the processor.

– Therefore more efficient to use::::::::::::::::::::::::::::::::multiprocessing

machines, which support several processessimultaneously.∗ As well called

::::::::::::::::::::::::::::::::::::::multiprogramming,

:::::::::::::::::::::::::multitasking machines.

– On single processor machines, the differentprocesses are run by the OS in an interleavedway.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-12

Process 1 Process 2 Process 3

Time

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-13

Process State

• To every process the OS associates a state.Typical states are:

–:::::::::New: Process is admitted, but not yet ready toexecute.OS initializes it, so that it becomes “ready”.

–:::::::::::::Ready: Process is ready to execute, awaitingaccess to CPU.

–:::::::::::::::::Running: Process is executed by the processor.

–::::::::::::::::Blocked: Process is suspended from execution,waiting for some system resource, eg. I/O.

–::::::::::::::Halted: Process has terminated, will bedestroyed by the operating system.

– Under UNIX there exists as well the:::::::::::::::Zombie

state:Occurs, when process is ready to exit, but thereis no parent waiting for it.If the parent finally signals that it is waiting forthis process, then the process will terminate.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-14

New Ready Running Halted

Waitingfor Event

Event occurred

Process Creation Process Termination

Blocked

Scheduler

Dispatch

Timeout

Admit Release

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-15

Example: Output of Unix-Command “ps”

USER PID %CPU %MEM VSZ RSSroot 2 0.0 0.0 0 0root 4 0.0 0.0 0 0csetzer 26103 0.0 0.4 2572 1300

TTY STAT START TIME COMMAND? SW Nov26 0:02 [keventd]? SWN Nov26 0:00 [ksoftirqd CPU0]? S 11:30 0:00 /bin/bash –login

• “ps” outputs a list of active processes (here brokeninto two blocks).

– PID = process ID.– %CPU, %MEM = percentage of CPU/memory

used.– VSZ = size of virtual memory.– RSS = resident set size.– TTY = terminal from which it was started.– STAT = status∗ S = sleeping∗ R = runnable∗ D = uninterruptible sleep∗ T = traced or stopped∗ Z = defunct (zombie) process∗ Additional letters N = low priority, W = has

no resident pages, < = high priority task, L =has pages locked into memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-16

Process Context

•::::::::::::::::Process

::::::::::::::::::::Context = collection of information

necessary for the process to run properly.

•::::::::::::::::Context

:::::::::::::::::::::::Switching = act of switching from one

process to another.

•::::::::::::::::Process

:::::::::::::::::Control

:::::::::::::Block (

:::::::::PCB)

= information about the process context stored ina compact way.

– Used by the operating system for scheduling andrestarting a process which has been halted.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-17

Process Control Block

• The process control block contains the following:

– Process identifier.– Process state (New, Ready, . . .)– Process and priority level.– Program counter (pointing to next instruction

to be executed).– Memory pointers

Beginning and end of the process in memory.– Context data

Content of registers, status of flags etc.– I/O status information

Outstanding I/O requests, I/O devices assignedto the process etc.

– Accounting information(CPU time used, time limits, account numbersetc.)

– Time when process was last running.Important to guarantee fair scheduling – that noprocess is permanently locked out, ie. not givena chance to run.

– Entries PC, context data, I/O status informationare only kept up-to-date while the process is notrunning (Needed for reactivating it).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-18

Process Table

•::::::::::::::::Process

:::::::::::::::Table (

:::::::PT) stores all current active

process control blocks (PCBs).

• Context switching from process x to process yamounts to the following:

– Copy the current values of the CPU registers(including PC) of x to x’s PCB.

– Update process x’s PCB.– Copy the values of CPU registers stored in y’s

PCB to the CPU (excluding PC).– Change the currently active PCB from x to y.– By updating PC to the entry in y’s PCB the

process y starts to resume running.

• Processors often provide support for contextswitching:For instance by having special registers, which keepthe context of the most recently used processesinside the CPU.(⇒ switching to those processes can be done inone clock cycle).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-19

Modes of Operation

• Processes are assigned:::::::::::::modes

::::::::of

::::::::::::::::::::::::operation

(:::::::::::::::process

:::::::::::::levels).

– Usually at least the following two modes occur:∗

:::::::::User

:::::::::::::::mode.

Mode in which ordinary programs are written.· E.g. no access to the file system or I/O not

allowed; only done by calling a routine of thekernel.· E.g. no direct control over

paging/segmentation.∗

:::::::::::::::::::::Supervisor

::::::::::::::mode(or

:::::::::::::::system

:::::::::::::mode,

::::::::::::::Kernel

:::::::::::mode).· Used by the

::::::::::::kernel of the system.

– Other modes:∗ e.g. for I/O handling processes (part of the

kernel, time critical)∗ e.g. for exception handling (highest priority,

commands for masking exceptions; masking =restriction of the exceptions which cause aninterrupt).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-20

(c) Memory Management

• Usually address space bigger than availablememory.

– E.g. with 32 bit addresses referring to byte level,one can address 232 bytes = 4 Gigabytes,

– with 64 bits 264 bytes we can access 16 Exabytes– Far beyond memory physically available

currently.– Instead the system behaves as if it had such a

lot of memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-21

Remark on Units

External and main memory measured in terms ofBytes:1 byte = 8 bits.

– 1:::::::::::::::::Kilobyte (

:::::::::::::KByte)= 210 byte = 1024 byte,

– 1::::::::::::::::::::Megabyte (

::::::::::::::MByte)= 220 byte = 1024

Kilobyte.– 1

::::::::::::::::::Gigabyte (

:::::::::::::GByte)= 230 byte = 1024

Megabyte.– 1

::::::::::::::::::Terabyte (

:::::::::::::TByte)= 240 byte = 1024

Gigabyte.– 1

:::::::::::::::::::Petabyte (

:::::::::::::PByte)= 250 byte = 1024

Terabyte.– 1

:::::::::::::::::Exabyte (

::::::::::::EByte)= 260 byte = 1024 Petabyte.

• Sometimes one needs to work with Kilobits,Megabits etc.:

–::::::::::::::Kilobit = 1024 bits.

–:::::::::::::::::Megabit = 1024 Kilobits.

– etc.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-22

Metric Units

• When dealing with metric units we have:

–::::::::Kilo means 103 = 1000.

–::::::::::::Mega means 106 = 1000 000 = 1000 kilo.

–:::::::::Giga means 109 = 1000 000 000 = 1000 Mega.

–:::::::::Tera means 1012 = 1000 Giga.

–::::::::::Peta means 1015 = 1000 Tera.

–::::::::Exa means 1018 = 1000 Peta.

• This applies to meter, seconds, Hertz (see nextslide).

• For bytes, bits one uses kilo for 1024 instead of1000, since 1000 bytes is not a natural unit –addresses are given in binary.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-23

Hertz

•::::::::::::Hertz (

::::::Hz) = 1 signal per second.

– 1 Hz = 1 (signal) per second.– 2 Hz = 2 (signals) per second.– 1 KHz = 1000 Hz. (not 1024 Hz!!).– 1 MHz = 1000 kHz = 1 million (signals) per

second.– 1 GHz = 1000 MHz.

• Note that 1kHz is not 1024 Hz!!!So the period between two impulses is in case of

Hertz duration1Hz 1s (second)2Hz 0.5s (seconds)1KHz 1

1000s = 1ms (milisecond)1MHz 1

1000 000s = 1µs (microsecond)1GHz 1

1000 000 000s = 1ns (nanosecond)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-24

Virtual Memory

::::::::::::::Virtual

:::::::::::::::::::Memory means:

• One uses a virtual address space, which is muchbigger than the actual main memory available.

• Some parts of this address space correspond tophysical addresses in main memory.

• Other parts corresponds to data stored on the harddisk or have not yet been intialized.

– A translation map is used which maps those

:::::::::::::virtual

:::::::::::::::::::::::addresses (sometimes called

:::::::::::::logical

::::::::::::::::::::addresses) to

::::::::::::::::physical

::::::::::::::::::::::addresses.

• As new processes are created and deleted, thistranslation map will be modified.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-25

Virtual Memory (Cont.)

– If an address is called which corresponds to anarea of data, which is on the hard disk or whichhasn’t been addressed yet, the following hasto be carried out:∗ If there is no space left in main memory, some

part of virtual memory is· thrown out of main memory· if necessary, written back to hard disk (called

(:::::::::::::::::::swapping).

∗ Then a area of memory including the currentaddress is· if it was stored on the hard disk, loaded back

into the free area of main memory,· or, if this area hasn’t been used yet, the

data in the free area is possibily set to intialvalues (e.g. 0).

∗ The translation mechanism is adapted.∗ Now the virtual address can be looked up in

main memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-26

Paging

•::::::::::::::Paging = main mechanism for organization ofvirtual memory (the other one is

::::::::::::::::::::::::::::segmentation).

– Both virtual and physical address space dividedinto

:::::::::::pages.

Physical pages are called::::::::::page

:::::::::::::::frames.

∗ Each page has same size, typically between512 byte and 64 Kbyte.∗ Some pages of the virtual address space are

mapped to pages in main memory.∗ The other pages are yet untouched or mapped

to one page (a segment of size one page) onthe hard disk.

– Virtual pages and physical pages are usually notin the same order.∗ A translation table, called

::::::::::page

:::::::::::::::table,

between virtual and physical pages ismaintained.

•:::::::::::::::::Demand

:::::::::::::::paging = a page is only loaded into main

memory, if access to it is actually needed(so one doesn’t load, if a process is activated, allits pages into main memory.Allows to have processes, which are bigger thanmain memory).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-27

Virtual

Page

Num

ber

88 − 95k80 − 87k

0 − 7k

72 − 79k64 − 71k56 − 63k48 − 55k40 − 47k

8 − 15k

XXXXXXXX

Virtual A

ddressSpace

1110 9 8 7 6 5 4 3 2 1 0

Physical

Page N

umber

76543210

24 − 31k16 − 23k

32 − 39k

96 − 103k104 − 112k113 − 119k120 − 127k

12 13 14 15

0 − 7k

56 − 63k48 − 55k

8 − 15k

Physical

Mem

ory Space

24 − 31k16 − 23k

32 − 39k40 − 47k

(All num

bers are in decimal)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-28

Looking up an Address in Virtual Memory

• When looking up an address in virtual memory, thefollowing is done:

– First the virtual page number of the address isdetermined and its offset within this page.

– Then one looks up in the page table, whether thevirtual page corresponds to a physical page.∗ If this is the case, the physical address is

the result of adding to the beginning of thecorresponding physical page the offset.∗ If this is not the case, one has a

::::::::::page

:::::::::::fault.

Then the following has to be done:· If all physical pages are used, one physical

page has to be selected and to be loadedback into the hard disk.· Otherwise, one chooses one free page

∗ Then the page stored on the hard disk, thedata belongs to, is loaded into the (now) freepage· or, if there is no such page on the disk, the

page is intialized.∗ Now the address can be looked up as before.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-29

The Role of Hardware and Software in Paging

• The translation of virtual pages to physical pagesis usually done completely in hardware.

– Not even visible to the assembly languageprogrammer.

– The assembly language programmer uses virtualaddresses as if they were real ones.

• However, if there is a page fault, the swapping ofpages is done completely by sofware, ie.in case of a page fault

– an exception is raised, and a software routineof the operating system is called.

– This software routine does now the∗ determination of a page to be replaced,∗ swapping of the pages,∗ updating of the page table.

– Once this is finished, the program continues withexecution as before.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-30

Relationship between Virtualand Physical Address

• Assume an address A is located in virtual page k,which is mapped to physical page k′.

• Let B be the address of the first element of virtualpage k.

• Then A = B + offset, where offset = A−B.

• Let B′ be the physical address address of the firstelement of phsical page k′.

• Then the virtual address A corresponds to thephysical address A′, which is obtained by adding toB′ the offset: A′ = B′ + offset.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-31

Offset

Virtual

Page

Physical

Page

Virtual

Page

Offset

Corresponding

physical page

First

address of corresponding Physical A

ddress:

Virtual

Address

A

B

B’

B’

A’

A’ = B

’ + offset

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-32

Calculation of Page Number and Offset

• For simplicity assume, addresses refer to bytes.

– (Often addresses refer to larger units: words.Typical sizes of a word are 2, 4, 8 , 16 or morebytes)

• Assume a page size of 2k bytes, and an addresssize of l bits.

• Then new pages start at addresses 0, 2k, 2 · 2k,3 · 2k etc.

• So virtual page number i starts at address i · 2k,and ends at address i · 2k + (2k − 1).

• If we have an address A in this page, i · 2k ≤ A <(i+ 1) · 2k, then

– i is the result of dividing A by 2k with remainder.– The offset of A is A− i · 2k,– which is the remainder with respect to this

division.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-33

Calculation of Virtual Page Numberand Offset (Cont.)

• Division of a binary number by 2k is the same asshifting it k bits to the right.

• When shifting a l-bit number k bits to the right,one obtains the MSB l − k bits.

• So the virtual page number is formed by the l − kMSBs of the address. The remaining k LSBs formthe remainder with respect to this division, whichis the offset.

Virtual Page Number Offset︸ ︷︷ ︸l-k bits

︸︷︷︸k bits︸ ︷︷ ︸

l bits

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-34

Corresponding Physical Address

• Assume the physical addresses have l′ bits.

• Physical addresses are divided into physical pagenumber and offset in just the same way as virtualaddresses are:

Physical Page Number Offset︸ ︷︷ ︸l’-k bits

︸︷︷︸k bits

• Now in order to obtain the physical address, wejust have to replace the virtual page number by thephysical page number, and then we are done.

• The page table maps some virtual page numbersto their physical page numbers.

– In the simplest implementation, page table has∗ for each virtual page number· one entry with a possible physical page

number· plus a

::::::::::::::::::::::::valid-invalid

:::::::bit, indicating, whether

the physical page is valid for this virtualpage.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-35

}

Virtual Address

111

000001010011100101110

00

100

1011

Offset

1 11 01 1 10 1 10 1

01

10

11

PhysicalPagenum.

Virtual OffsetPagenum.

0 00

0

0 1 0 1 1 0 1 1 1 1 0 0 1 1 0 1

Page exists in

main mem?

Page Table

(For longer addresses, e.g. 32 bits, virtual page−number is longer than the offset)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-36

Replacement Algorithms

If all pages in main memory are used and we need toload a new page into main memory, one page has tobe moved back to disk.Several algorithm for choosing the page to be replaced:

•:::::::::First

::::::in

:::::::::first

:::::::::out

:::::::::::::::::(FIFO). Replace page which has

been in memory longest.

•:::::::::::Least

::::::::::::::::::recently

:::::::::::used

:::::::::::::::::(LRU). Replace page which

has been in memory longest without a read or writeaccess.

•:::::::::::Least

::::::::::::::::::::::::frequently

:::::::::::::used

::::::::::::::::::(LFU). Replace page

which had the least number of read or writeaccesses.

• No best solution, for every of the above algorithmsthere is a sequence of calls for memory, whichbehaves worst with this algorithm.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-37

Thrashing

•:::::::::::::::::::::Thrashing happens if one

– throws a page out of memory, and loads itshortly afterwards back.

– Costs a lot of time (since hard disk is relativelyslow).

– In well-designed systems thrashing happensrarely.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-38

Writing Back

• When throwing a page out of main memory, it neednot be written back to the disk.

– Only needed when it has been changed.

• Therefore best to keep track of whether a page hasbeen modified or not.One bit needed for this. Called

:::::::::dirty

::::::::bit.

– “Dirty Bit = 1” means page is dirty, not identicalwith content on the disk.

• In order to optimize writing back, some systemswrite back pages before it has to be thrown out.Called

::::::::::::::sneaky

::::::::::::::::writing.

– Might be of course too early.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-39

Translation Lookaside Buffer

• Looking up a page entry is quite complicated andrequires two (sometimes even more) main memoryaccesses(looking up the page table entry and the physicaladdress).

• Better performance by using a:::::::::::::::::::::::Translation

::::::::::::::::::::Lookaside

:::::::::::::::Buffer (

:::::::::TLB):

• TLB =block of special, fast, cache memory, in which themost recently used page table entries are stored,in order to speed up the looking up of virtualaddresses.

• Now in most cases only one memory access (andif the data is already in cache no memory access)is required for looking up an address.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 9 9-40

10. Exception Handlingand Input/Output

(a) Some I/O Devices.

(b) Parallel vs. Serial Ports

(c) I/O Modules.

(d) Control Methods for I/O.

(e) Exception Handling.

(f) I/O Channels and I/O Processors

(g) Memory Mapped vs. Isolated I/O

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-1

(a) Some I/O Devices

• External devices are:

–::::::::::::Input: keyboard, mouse, microphone, scanner.

–::::::::::::::::Output: Printer, display.

–:::::::::::Input

:::::::::and

:::::::::::::::::output: Networks.

–::::::::::::::::Storage (functions structurally as I/O devices):Magnetic disk/tape, optical disk/tape.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-2

Display

• Monitor

– TV - technology or–

:::::::::LCD (Liquid Crystal Display) or

–::::::::Gas

::::::::::::::::Plasma

:::::::::::::::::Display (currently > £2000)

• Screen contents stored in video:::::::::::RAM.

(RAM =:::Random

:::Access

::::Memory,

= of same type as ordinary memory).

• Contents normally decided by processor.

• Sometimes [3D]:::::::::::::::::graphics

::::::::::::::::::::processor.

(“Graphics card”).

– Take over rendering:Mapping polygons, colours, textures to screen.

– Hardware faster than software,processor freed from this task.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-3

Interfaces

• Peripherals – Minimum keyboard and mouse.

• Also printer, scanner, network, disk drives etc.

• Transfer data to/from – speed etc. will vary.

• Keyboard slow, disk fast.

• Classify as::::::::::slow,

::::::::fast,

::::::::::::::::network.

– Slow – e.g.:::::::::USB. (Universal serial Bus).

– Fast – e.g.::::::::::SCSI (Small computer system

interface).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-4

Network – Phone etc.

• Analogue phone via via modem

– Currently 56.6 Kbps downstream (fromprovider),

– 33.6 Kbps upstream (to provider).– Slow, unreliable.–

::::::::::Kbps =

:::Kilo

:::bits per

::second.

• Digital::::::::::::ISDN. 144 Kbps.

•:::::::::DSL.

:::Digital

:::Subscriber

::Line.

From 784 Kbs to 8 Mbps.

• Satellite.

– Downstream via satellite.– Upstream via Telephone.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-5

Network (LAN)

•::::::::::LAN =

:::Local

:::Area

::::Network.

– Main technology::::::::::::::::::Ethernet.

∗ Standards:·

:::::::::::::::::10baseT (10 Mbps),·

::::::::::::::::::::100baseT (or

::::::::fast

::::::::::::::::::::Ethernet, 100 Mbps),

·:::::::::::::::::::::::::::GigaEthernet (1 Gbps).·

:::::10

:::::::::::::::::::::::::::::GigaEthernet (10 Gbps)

was standardized 2002∗ Shared Connection.∗ Protocols for arbitration, collision detection

and recovery.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-6

(b) Parallel vs. Serial Ports

•:::::::::::::::Parallel

::::::::::::ports allow simultaneous transfer of many

bits (usually a word) to/from the I/O module.

– Broad cables, port with many pins.– Used for devices to/from which large amounts of

data per request are transfered, eg. connectionto monitor, printer.

•:::::::::::Serial

::::::::::::::ports transfer only one bit at a time in

sequence.

– E.g. connection to mouse, keyboard.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-7

Connection to Parallel Port

Connection to Serial Port

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-8

(c) I/O Modules

• Computer systems communicate with externaldevices through an intermediate I/O module.

• An:::::::I/O

:::::::::::::::::module (or

:::::::::::::::::::controller) serves both as

– interface between CPU via system bus and as– interface with one or more external devices.

• I/O module often part of the I/O device.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-9

Picture copied from book not included

Tasks of the I/O Module

• Tasks of the I/O Module:

–:::::::::::::::::::::Command

::::::::::::::::::::::decoding. Decoding of commands

from CPU.–

::::::::::Data

::::::::::::::::::::Transfer. Transfer of data via the system

bus.–

:::::::::::::Status

::::::::::::::::::::::::Reporting. Report of status to CPU:

::::::::::Busy or

::::::::::::ready.

–::::::::::::::::Address

::::::::::::::::::::::::::::::recognition. Identification of

addresses of associated peripherals and sendingof control signals.

–::::::::::::::::Sending

:::::::of

::::::::::::::::::::::interrupt

::::::::::::::::::signals. (in case of

interrupt driven I/O and DMA; see later).

• Sending of requests from CPU to I/O modulesunproblematic.

• Problem, to deal with data supplied byI/O modules, since waiting time is usuallyunpredictable.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-11

(d) Control Methods for I/O

Three types of control methods used

(i) Programmed I/O.

(ii) Interrupt Driven I/O.

(iii) Direct Memory Access I/O.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-12

(i) Programmed I/O

• Oldest method.

• CPU controls directly I/O.

• When I/O module has received information to bepassed on to the CPU, it sets appropriate flags inits status register.

–:::::::::Flag = register with one bit only.

• It is the task of the CPU to check status of the I/Omodule until it finds operation is complete.

• Disadvantages

– Processor has quite often check the status of allI/O modules or

– processor has to wait, if request is not dealt with.– Complicated to program efficiently.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-13

Issue Read

Com

mand to

I/O M

odule

Read Status

of I/OM

odule

Check

Status

CP

U I/O

I/O C

PU

Not

Ready

Read W

ordfrom

I/OM

odule

into Mem

oryW

rite Word

Done?

Next Instruction

I/O C

PU

CP

U M

emory

Error

condit.

Ready

No

Yes

(Main P

rogram)

Instruction"R

ead I/O"

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-14

(ii) Interrupt Driven I/O

• I/O modules have now possibility to send interruptsignals, once they have data available or are readyto receive data.

• Causes the CPU to interrupt its current programand call and exception handler.

– The exception handler (a program) processesthe I/O request and exchanges data with theI/O modules.

– Once it has finished, the original programcontinues to be executed.

• Interrupt handling is treated similarly as a call of asubroutine (using a stack).

– Note that while handling an interrupt, anotherinterrupt might occur (similarly to recursivefunction calls).

• Note that the CPU still has to control every byteof data passed from I/O to and from memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-15

Instruction"R

ead I/O"

in main P

rogram

Issue Read

Com

mand to

I/O M

odule

Read Status

of I/OM

odule

Check

Status

CP

U I/O

I/O C

PU

Read W

ordfrom

I/OM

odule

into Mem

oryW

rite Word

Done?

Next Instruction

I/O C

PU

CP

U M

emory

Error

condit.

Ready

No

Yes

else

Interrupt

Do som

eth.

Reading D

ata from I/O

(Interrupt−Driven I/O

)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-16

Identification of the Origin of an Interrupt

• Problem with multiple interrupts, since usuallynot one interrupt bus line for every module:

– Identification of requesting module necessary.– Several modules might send interrupt at the

same time.

• Methods used:

– Multiple Interrupt lines.– Software polling.– Daisy chains.– Bus arbitration.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-17

Multiple Interrupt Lines

• Use one interrupt line for each I/O module.

• Only possible if there are few I/O modules.

• Method used for PCs, if there are not too many(typical 16) modules attached to it.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-18

Software Polling

• Processor asks each I/O module (or its statusregister) whether it issued the interrupt.

• Disadvantage: time consuming.

Daisy Chain

• All modules have direct access to interrupt lines.

• Modules chained together subsequently.

• When processor senses interrupt, it sends interruptacknowledgment signal.

• Signal propagates through the series of I/O module.

• First module, which has sent an interrupt, sendsback a vector, which identifies this module.(Priority of modules ordered by their position in thechain).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-19

First Module

(highest priority)

Second Module

Interrupt request linesB

us Term

inator

Third M

odule

(lowest priority)

Data lines

Address lines D

aisy Chain

From the System Bus

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-20

Bus Arbitration.

• I/O module must gain control over bus when raisinginterrupt request.

• Therefore only one module at a time can raiseinterrupt.

• When processor detects request, it responds viainterrupt acknowledgment line.

• Module places a vector identifying it on the dataor address lines.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-21

(iii) Direct Memory Access (DMA)

• Additional module on the system bus:The

::::::::::::::::::::::::::::DMA-module.

• DMA module can transfer data from an I/O moduleto memory without CPU interaction.

• Processor sends I/O request to DMA module, andthen continues with other tasks.

• When transfer complete, DMA module sendsinterrupt signal.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-22

DMA (Cont.)

• DMA must use bus only, when processor does notuse it.

– Either use the bus when processor does not needit.

– Or force processor to suspend operationtemporarily (

::::::::::cycle

:::::::::::::::::stealing; usual method).

• Three main kinds of configurations (see next slide).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-22a

Picture copied from book not included

Issue ReadCommand to

Read Status

DMA Module

Module

CPU DMA

of DMA

Interrupt

DMA CPU

Next Instruction

InstructionRead I/O

in main Program

Do something else

Reading Data from I/O (DMA)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-24

(e) Exception Handling.

• There are 2 kinds of::::::::::::::::::::::Exceptions:

–::::::::::::::::::::Interrupts

–:::::::::::::Traps.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-25

Interrupts

•::::::::::::::::::::Interrupts = exceptions caused by external events.Usually because I/O handling is done via interruptsor DMA.

– I/O devices signal that they have some dataavailable or that they have completed some task.

• Other reasons:

– Error when handling I/O request.– Reset is activated.

• Warning: Terminology not uniform:Sometimes “interrupt” taken as synonymous with“exception”.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-26

Traps

•:::::::::::Traps = exceptions caused by internal events.Typical reasons:

– Program errors:∗ Eg. division by zero,∗ arithmetic overflow,∗ accessing memory out of bounds.

– Traps invoked by special instructions.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-27

Handling of Exceptions

• Detection:

– At the end of an instruction cycle (see next slide),processor checks whether an interrupt was raised.

– Traps are detected during execution ofinstructions.

• When an exception is detected, the processor doesthe following:

• In case of an interrupt it identifies the source ofthe interrupt, and signals acknowledgment of theinterrupt.

• For both traps and interrupts, the processor pushesthe

:::::::::::PSW on the

:::::::::::::::control

::::::::::::stack.

–:::::::::::PSW =

:::Process

::status

::::word:

– Contains information about status of the currentprocess.∗ flags indicating carry, result zero from last

arithmetic operation,∗ flags for interrupt, supervisor mode.∗ Possibly content of some other registers or

flags.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-28

Picture copied from book not included

Handling of Exceptions (Cont.)

• Then a jump to a software program, the::::::::::::::::::::exception

:::::::::::::::handler is made.

– This means that the current PC is pushed on thestack,

– and the program jumps to the location of theexception handler.

• The exception handler is a software program, whichdoes the following:

– It stores further process state information onthe stack.

– Then it deals with the exception.∗ Program might run in a special mode with

access to more privileged instructions (eg.supervisor mode).∗ (However the user should not be able to control

execution in such a mode).– At the end it restores its original status,– and returns.∗ This means that the original PC is recovered.∗ Further the status according to the PSW is

recovered,∗ and execution of the original program is

resumed.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-30

Handling of Exceptions (Cont.)

• The next two slides give a flow chart for thehandling of interrupts

• Traps are handled similarly.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-31

Device controller or

other system hardw

areissues an interrupt

Processor finishes execution of currentinstruction

Processor signalsacknow

ledgement

of interrupt.

Processor pushes PSWand PC

onto controlstack

Processor loads new

PC value based on the

interrupt

Continue on N

ext Slide

(In this part, all steps done by hardware)

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-32

state information (done by

interrupt handler-software)

Interrupt handler dealsw

ith interrupt(softw

are)

Restore process state

information

(software)

Restore old PSW

and PC (hardw

are)

Continue w

ithoriginal program

Save remainder of process

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-33

Priority

• Exceptions are assigned::::::::::::::priority

:::::::::::::::levels.

– Typically, there are 2 - 16 priority levels.

• Higher priority exceptions can interrupt lowerpriority exceptiony handlers, but not vice versa.

• In some cases (e.g. if the exception is pending fora long time) this might not apply.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-34

Masking of Interrupts

• Sometimes, it’s desirable to inhibit interrupts.

– E.g. during time-critical parts of a program.

• Possibility, to temporarily disable or:::::::::::mask

::::::::::::::::::::interrupts:

•::::::::::::::::::Interrupt

:::::::::::::::mask = sequence of bits held in

some special register, which tells, which kind ofexceptions are to be ignored.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-35

(f) I/O Channels and I/O Processors

I/O Channels

• I/O modules get more and more processing power.

– CPU sends more general commands to I/Omodules, which performs tasks like processingof data, polling I/O modules etc.

•:::::::::::::::::::::::::::I/O-channels = I/O modules with DMA whichacts as a processor on its own, but without separatememory.

–::::::::I/O

:::::::::::::::::selector selects I/O devices and effects data

transfer with it.–

::::::::I/O

::::::::::::::::::::::::::multiplexors handles I/O with multiple I/O

modules simultaneously.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-36

I/O Processors

•::::::::I/O

:::::::::::::::::::::::processors = as I/O channels, but having

their own local memory.

–:::::::::::::::::::::::::::Coprocessors = not fully functional processors.

::::::::::::::::::Graphics

::::::::::::card = coprocessor for graphics

combined with an I/O module for the monitor.∗ Hardwired program, which reads sequences

of graphics drawing instructions stored inmain memory accessible via DMA and createscontrol signals for the monitor from it.∗ Carries out the relatively simple but intensive

processing of graphics data.– General purpose processors could as well be

used for I/O processing∗ Note for instance the addition of special

instructions for graphics manipulation to thePentium instruction set.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-37

(g) Memory Mapped vs.Isolated I/O

•:::::::::::::::::Memory

::::::::::::::::::mapped

::::::::::I/O: I/O devices and memory

share the global address space.

– No I/O instructions for load and store needed.

•::::::::::::::::Isolated

::::::::::I/O. I/O device addresses and memory

addresses separated.

– Additional control signal indicate whether anaddress is valid for memory or I/O.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 10 10-38

11. High PerformanceArchitectures

(a) Pipelining.

(b) RISC and CISC Systems.

(c) Superscalar Processors.

(d) Explicit Instruction-Level Parallelism.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-1

(a) Pipelining

Laundry Analogy: Assume several students want todo their laundry. They have to do the following 4steps:

1. Wash the clothing using the washing machine.

2. Dry it in a dryer.

3. Fold clothing.

4. Put them away.

This can now be done in a pipeline:

• When one has washed his/her clothing, the nextone can use the washing machine.

• Similarly for the dryer and the table for foldingclothing.

The following slide illustrates the speed up.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-2

Wash

Dry

FoldStore

Wash

Dry

FoldStore

Wash

Dry

FoldStore

StoreW

ashD

ryFold

Wash

Dry

FoldStore

Tim

e (hrs)3.5

44.5

55.5

6

Wash

Dry

FoldStore

Tim

e (hrs)0

Wash

Dry

FoldStore

11.5

22.5

33.5

44.5

55.5

6

00.5 0.5

11.5

22.5

3

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-3

Pipelining of the Instruction Cycle

The Instruction Cycle can now be divided into similarpieces.Assume a sequence of instructions of the form:

• LOAD R1,(R1 + A)

ie. the second argument has addressing mode base-register displacement.We divide it into 5 stages:

1. Instruction fetch (1 cycle).

2. Register load (R1; 1/2 cycle).

3. ALU operation (calculation of R1+A; 1 cycle)

4. Data access (load of (R1+A); 1 cycle).

5. Register save (storage of result; 1/2 cycle).

All 5 stages can be carried out independently.(For the increase of the PC by one in 1. one needs anextra adder). The next slide demonstrates the speedup. It is ideally by the factor 4.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-4

Instrfetch

store

(a) Non-Pipelined

Data

Reg

AL

UR

eg

Reg

Instr. fetch

Instr. fetch

Instr. fetch

Reg

AL

UR

eg

Reg

AL

UD

atastore

Reg

AL

UD

atastore

Instr. fetchR

egA

LU

Data

store

(b) Pipelined

fetchInstr

AL

UstoreD

ataR

eg

Reg

storeD

ata

Reg

Reg

Reg

01

42

35

712

1110

98

6

Instrfetch R

egAL

UstoreD

ata

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-5

Remark

• No increase in speed of a single operation.

• But increase of throughput of instructions.

– In the laundry example, everybody needs thesame amount of time as before for his laundry.

• The Pentium IV has a 20 stage pipeline.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-6

Problems

• The stages require different amount of time. Thepipeline is only as fast as the slowest device.Can be improved, but not avoided by using assmall stages as possible.

• Data dependencies between the stages.Eg. the second instruction requires a register, inwhich a value was stored by the first register:

– Example: First instruction increments registerR1 by 1.Second instruction loads data register indirectfrom memory using register R1.

– Solutions:∗ Either second instruction has to be delayed till

first instruction is ready.∗ Or (nowadays standard) pass on the data

from one unit to the other if possible.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-7

Problems (Cont.)

• Most difficult problem are branches, since wedo not know, whether the next instruction is theone at the next location or we have to follow thebranch.

The next slide illustrates the additional delay, weget, if we have to wait until the condition of a branchis computed.

• Only after loading the registers one knows whetherone should follow the branch or not (equality canthen be determined immediately).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-8

Reg

Fetch R

egA

LU

Data

Reg

Fetch R

egA

LU

Data

Reg

Fetch R

egA

LU

Data

Tim

e

SUB

R2, R

5, R9

BE

Q R

4, R5, R

el. 0x40

AD

D R

1, R2, R

3

Delay

Fetch = Instruction Fetch

Reg =

Register A

ccess (with direct com

putation of comparisons)

Data =

Data A

ccess

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-9

Solutions for Problemwith Branches

• Delay next instruction till decision is taken.Takes much time (branch instructions occurfrequently).

• Always continue both with next instruction andfollow the branch.If it is determined which was the next instruction,throw away the other one.Problem: might require unnecessary data accesswith removal of data from cache.

• Branch Prediction:Guess the next instruction. If the guess was wrong,throw away the instruction(and don’t change memory or registers before thisdecision is taken).

• Delayed Branches: Used on RISC machines. SeeAppendix (ii).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-10

Conflict of Resources

• In general there will be conflict of resources.

• Most resources can be doubled in order to avoidthis problem.

– Except of access for memory.Required for instruction fetch, data load anddata storage.

– Solution, to solve this:∗ Divide cache into instruction cache and data

cache.∗ Since usually instructions are not rewritten,

unproblematic.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-11

(b) RISC and CISC Systems

(i) CISC Systems.

(ii) Analysis of CISC Systems.

(iii) RISC Systems.

(iv) CISC vs. RISC and Current State.

•:::::::::::CISC =

::complex

:instruction

::set

::computers.

•:::::::::::RISC =

::reducedx

:instruction

::set

::computers.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-12

(i) CISC Systems

•:::::::::::::::::::Semantic

::::::::::Gap: Gap between constructs in higher-

level programming languages:::::::::::::(HLL) and assembly

languages instructions.

• Problems of increasing use of higher-level programconstructs:

– Decreasing efficiency.– Increasing size of the compiled program.– Increasing complexity of compilers.

• Attempt to close it by introducing more and morecomplex instructions. This resulted in:

– Increase of number of machine instructionsand addressing modes.

– Instructions with long execution time whichrequire complicated hardware.

– Long instruction formats in order toaccommodate a lot of instructions.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-13

(ii) Analysis of CISC Systems

Studies carried out in the late 70’s and early 80’s onusage of machine instructions in computers.Results:

• Complex instructions are not exploited bycompilers.Possible reasons:

– They are too specific.– Optimizing generated code easier with simple

instructions.

• Assignment statements dominate, thereforesimple movement of data is of high importance.

• Conditional and unconditional branches occuras well frequently.

• Procedure calls and returns are most time-consuming operations in HLL.

• Most references to variables are to simple scalarvariables (integer, floating point or strings whichstore one item only as opposed to arrays, lists etc.).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-14

Analysis of CISC Systems (Cont.)

• Most such references refer to local variables.

• Procedures have usually only few parameters(rarely more than 6) and only few local scalarvariables.

• During execution for a long time one remainswithin a rather narrow sequence of instructions(window).This window moves only slowly over time.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-15

(iii) RISC Systems

From the above the following conclusions were drawn:

• Instead of creating complex instructions one shouldoptimize performance of most time-consumingfeatures of HLL.

• The following main principles of RISC Systemswere suggested in order to achieve this:

– Use of a large number of registers. Try to usethem as much as possible.Effective since variables are often local andregisters are accessed much faster.

– Use:::::::::::::::delayed

:::::::::::::::::::::branches and

::::::::::::::::delayed

:::::::::::::loads to

make pipelining easier and avoid the choice ofthe wrong branch (see appendix).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-16

Main Principlesof RISC Systems, (Cont.)

– Reduce the number of instructions andinstruction formats and fix the instructionlength. The reason for this is:∗ Simple instruction format and fixed instruction

length makes pipelining more easier, moreuniform and therefore faster.∗ Simple instruction format allows to carry out

opcode decoding and register operandaccessing simultaneously.(From the format it is clear which bits form theregister numbers, even before we have decodedthe instruction).∗ Simple instructions are faster executed and

pipelined and the hardware can be moreoptimized.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-17

Characteristics of RISC Instruction Sets

• One instruction per machine cycle.(Machine cycle = length it takes to fetch twooperands from registers, perform an ALU operationand to store the result in a register).That results in a uniform pipeline.

• Most operations should be register-to-register, only load and store operations accessmemory.This simplifies the instruction set and therefore thecontrol unit.

• Use of simple addressing modes, so thataddresses can be computed fast.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-18

(iv) RISC vs. CISC and theCurrent State

• Examples of full RISC processors are MIPS R4000(used by NEC, Nintendo, Silicon Graphics, Sony),Sun SPARC, Power PC.

• Not clear whether RISC systems are so much faster.Examples of directly comparable RISC and CISCsystems are missing.

• Tendency towards mixture of CISC and RISCfeatures:

– Addition of some CISC instructions to RISCarchitectures.

– For instance the Pentium has an initialinterpretation procedure, in which the fetchedCISC instructions are interpreted as internalRISC instructions which are then evaluated ina RISC pipeline. Instructions are cached in theform of the interpreted RISC instructions.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-19

RISC vs. CISC and theCurrent State (Cont.)

• Difficulty of Intel to move from the CISC instructionset (would destroy compatibility with previousprocessors).

• It seems to be easier to market sophisticatedCISC instruction sets.(Compare with advertisement of cameras, stereoequipment etc. in which one points out the newswitches they have, even so they are rarely used).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-20

(c) Superscalar Processors

•::::::::::::::::::::::::::::::::Superpipelining =Use the fact that if data is to be passed from oneregister to the next one, it arrives half a clock cyclelater instead of a full clock cycle later.Use pipeline stages so that every half clock cyclethe next instruction is fetched. See next slide.

• Above = traditional usage, which is changing.Nowadayas superpipelining means often only avery long pipeline with very small individual units(e.g. for Pentium IV has a 20 stages pipeline).

•::::::::::::::::::::::::Superscalar

:::::::::::::::::::::::::::Architecture =

– Execute several instructions fully in parallel.– Requires to have multiple functional units, all

of which are implemented as a pipeline.For instance one has two integer ALUs, twofloating point ALUs, but usually only one accessto memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-21

Picture copied from book not included

Superscalar Processors (Cont.)

• Origin of name superscalar because superscalarprocessors are optimized for processing scalardata, as opposed to

:::::::::::::vector

::::::::::::::::::::::::processors, which

are optimized for processing vector data (arrays)in parallel.The latter are important for numerical applications(eg. weather forecast, other simulations of physicalsystems, engineering).

• Superscalar is now the technical standard forprocessors in computers (there are as wellprocessors in cars, calculators . . ..Power PC from RS/6000 onwards, all Pentiumprocessors, MIPS R10000, Sun’s UltraSPARC-IIare examples of it.

• Superscalar approach is an instance of

:::::::::::::::::::::::::::::::::instruction-level

:::::::::::::::::::::::parallelism:

multiple instructions in sequence, which areindependent, are executed in parallel.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-23

Limitations ofSuperscalar Architectures

Instructions can not always executed in parallelbecause of:

• True data dependency between multipleinstructions (the next instructions depends on theresult of a previous one).Example:R1 := R3 + 3;R2 := R1 + 4.Partial solution: pass result of one computationdirectly to the next instruction (in the example, assoon as the result R1 + 3 of the first instruction isobtained, it can be used in the second one at thesame time as storing it in R1).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-24

Limitations ofSuperscalar Architectures (Cont.)

• Procedural dependency.

– Dependencies on branching instructions.– Example:BEQ R1, R2, Label2ADD R3, #4;Second instruction executed only if R1 6= R2.

– Can be solved as in pipelining by branchprediction.(Delayed branch strategy for RISCarchitectures turns out to be less effective).

• Resource conflict. (Two instructions require thesame functional unit of the CPU).Two instruction may use the same resource. Canbe resolved by adding more resources. However,access to memory and registers is limited.

– E.g.:LOAD R1, DIRECT ALOAD R2, DIRECT BBoth loads require access to main memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-25

Limitations ofSuperscalar Architectures (Cont.)

• Output dependency. If two instructions storevalues in the same register, the later one has to becompleted after the first one:E.g. in case ofR1 := R2 +R3;R1 := 3,if the second instruction is completed before thefirst one, at the end a wrong result might be storedin R1.Solution:

:::::::::::::::::Register

::::::::::::::::::::::renaming. Register numbers

in the instructions are virtual, which are mappedby some logic to actual registers.In the above example, the R1 in thethird instruction (and in later instructions) ismapped to a different actual register than the R1used in the first instruction.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-26

Limitations ofSuperscalar Architectures (Cont.)

• Antidependency. If one instruction reads aregister which is overwritten by a later instruction,the later one shouldn’t be completed before thefirst one.Example:R1 := R2 + 3;R2 := 3.If R2 := 3 is completed before R2 is fetched in thefirst one, the first instruction has a wrong result.Can be solved again by register renaming.Register renaming turns out to be crucial in orderto obtain any substantial speed-up.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-27

Superscalar Execution

In the next slide an overview over superscalar executionof programs is shown.

• In the instruction fetch process, a dynamicstream of instructions is created.Branch prediction logic is applied simultaneously.Dependencies are examined and if possibleremoved.

• The instructions are then dispatched into awindow of execution.This is done in the order of dependency instead ofthe order in the program.

• At the end instructions are issued to the:::::::::::::::retiring

::::::::unit.

• The retiring unit guarantees that data conflictswhen writing data to memory and registers areresolved, so that the result is as in an executionin true order. Results not yet retired are kept intemporary storage.If a wrong branch was taken, wrong results have tobe thrown away.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-28

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-29

(d) Explicit Instruction-LevelParallelism

• New processor of Intel, developped together withHP:

::::::::::IA64, with code name for first processor

::::::::::::::::Itanium.

• 64 bit processors.

• As for superscalar processors

– large number of registers (256 64-bit-register and64 1-bit predicate registers).

– multiple execution units.

• Starts completely new family of processors withcompletely new instruction sets.(Break with an over 20 years long history, startingin the late 1970s with the x86 family).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-30

Predicated Execution

•:::::::::::::::Explicit

:::::::::::::::::::::::::parallelism.

– Called::::::::ILW =

:Instruction

::Level

:::Parallelism.

• New instruction format, which holds 3 instructions,stored as 128 bits, which can be executed in parallel.

– Because of the long instruction format, this iscalled

:::::::::very

:::::::::::large

::::::::::::::::::::::::instruction

:::::::::::word,

::::::::::::VLIW.

• Additional information in the instruction, whichindicates that several of these instructionbundles can be executed in parallel.

• Dependencies between instructions are given byadditional flags (predicates).

– Therefore resolving of dependencies anddetermining of order done at compile timerather than at execution time.Compiler has much more time to resolve suchissues.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-31

Current Development

• Explicit parallelism seems to be the right way.

• Problems with backward compatibility.

– Itanium in compatibility mode slower thanPentium.

– Full speedup only, when compilers and (partly)operating systems have been specially rewritten.

– Implies that software has to be adapted as well(new compilers will not always behave in exactlythe same way).

• AMD is developing 64 bit architectures, whichextend the old x86-family in a traditional way.

– Market forces might favour this approach, evenso this means to continue with not very welldesigned architectures.

• Degree of parallelism is expected to increase inthe near future.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11 11-32

Supplementary Materialfor Sec. 11.

• Supplementary Material on(a) Pipelining.

(i) Branch Prediction and Loop Buffers.

• Supplementary Material on(b) RISC and CISC systems.

(ii) Delayed Loads and Branches.(iii) Register Windows on RISC Systems.(iv) RISC Pipelining.

• Supplementary Material on(c) Superscalar processors.

(iv) Design Issues for Superscalar Processors.(v) Units Required in Superscalar Architectures.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-33

Supplementary Materialfor Sec. 11. (Cont.)

• Supplementary Material on(d) Explicit Instruction-level Parallelism.

(vi) Predicated Execution (Itanium).(vii) Speculative Load.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-34

(i) Branch Predictionand Loop Buffers

Strategies for Branch Prediction

• Branch never taken.Always fetch the next instruction.

• Branch always taken.Always follow the branch (ie. assume temporarilythat the branching condition is true).

• Predict by opcode Decide by the kind of branchwhether to follow the branch or not.

• Taken/not taken switch. Add to the instructionbits which reflect history taken.Usually stored when instruction is in cache orspecial buffer.

• Branch history table. Store information in thistable.Information not lost if instruction removed fromcache.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-35

Loop Buffers

•::::::::::Loop

:::::::::::::::Buffer = Some sort of cache, but

– only for instructions,– stores always a sequence of instructions.

• Instructions are prefetched if possible so that evenif a small forward jump is done, often the nextinstruction is already in the buffer.

• Loop buffers are well suited for loops inprograms, if loop fits within the buffer

– Instructions in the loop need to be fetched frommain memory only once.

• When a branch is reached, the next instruction canoften be fetched from the loop buffer.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-36

(ii) Delayed Loads and Branches

•::::::::::::::::Delayed

::::::::::::::::branch means that a branch instruction

only takes place after the next instruction followingit.

– So in a language with delayed branches theprogram

BEQ R1 Label1LOAD R2,#13ADD R2,#3

is executed as the program in a language withoutdelayed branches

LOAD R2,#13BEQ R1 Label1ADD R2,#3

– Then in pipelining we can start executing theinstruction following the branch, even beforewe know whether the branch takes place.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-37

Delayed Branches (Cont.)

– If we cannot move the branch ahead, one hasto insert a NOP instruction (

::::::::::NOP means

:::no

:::::operation, an operation which does nothing):The program wihout delayed branches

BEQ R1 Label1ADD R2,#3

can be replaced by the program with delayedbranches

BEQ R1 Label1NOPADD R2,#3

– Depending on the architecture, one might delayexecution of branches even more by 2, 3 or 4instructions.

– Note that delayed branches means that thesemantics of the program is changed(program don’t have to be interpreted in theorder they occur).

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-38

Delayed Loads

• Similarly,:::::::::::::::delayed

::::::::::::::loads means that the effect

of load instruction (from main memory) takesplace only after the instruction following the loadinstruction.Before this the register value cannot be used.

– So

LOAD R1,#7LOAD R2,R1

would be illegal.– Instead, either move the first load ahead, or

insert NOP:

LOAD R1,#7NOPLOAD R2,R1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-39

(iii) Register Windowson RISC Systems

• Use of many registers in a::::::::::::::::register

::::::::file, which is

organized in a circular way. See Fig 12.2 below.

• When a new procedure called the followinghappens:

– Parameters which pass data from the callingprocedure to the called one and back again aresaved in new free registers of the register window.These parameters are

:::::::::::::::::::::temporary

:::::::::::::re-

:::::::::::::gisters for the calling procedure and

:::::::::::::::::::::parameter

:::::::::::::::::::registers for the called procedures.

– Further new::::::::::local

:::::::::::::::::::registers are assigned for the

local variables of the new register.– Parameter-, local and temporary registers of a

procedure together form the:::::::::::::::register

:::::::::::::::::window of

the procedure.– The register windows overlap.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-40

Picture copied from book not included

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-41

Register Windows

• The total number of registers is usually fixed.(Typical size is 16 or 32 registers per window).

• The window allows only to accommodate afew most recent procedure activations (limitednesting of procedure calls). Typical numbers are 8,16, 32.

• If a procedure call requires more registers thanavailable, the content of old procedures are storedback into main memory.

• When returning from a procedure, the registers ofwhich have been overwritten, the registers have tobe loaded back from main memory.

• Two pointers needed for organizing this:

– the:::::::::::::::::::::::::::::::current-window

:::::::::::::::::pointer

::::::::::::::::(CWP) points to

the end of the currently used register area in thewindow.

– the::::::::::::saved

:::::::::::::::::window

:::::::::::::::::pointer

:::::::::::::::::(SWP) points to

the beginning of the currently used register areain the window.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-42

Register Windows (Cont.)

• Because of the limited depth of recursions, thewriting back of registers into main memory isnot often needed.

• For global variables (common to several procedures)some additional global registers might beprovided.

On the next slide the circular register file is illustrated.

• A.in, B.in . . . are the parameter registers ofprocedure A, B . . .,

• A.loc, B.loc . . . are the local variables of procedureA, B, . . ., and

• w0, w1 . . . are the register windows of A, B, . . ..

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-43

Picture copied from book not included

Comparison Cache vs.Register Files

• Register windows and cache have similarfunctionality.

• Advantage of cache: It can store non-scalarvariables, instructions etc.

• Advantage of registers:

– Can be addressed with very few bits.– Address computation is simple and fast.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-45

Compiler Based RegisterOptimization

• Some RISC architectures don’t have registerwindows but still large number of registers.

• Then one instead optimizes compilers, so thatthey try to use registers in such a way that transfersto and from memory are minimized.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-46

(iv) RISC Pipelining

• RISC systems execute always the nextinstruction following an instruction, even in caseof a branch.

• In case of branches therefore instructions have tobe reordered. For instance the branch can bemoved forward, if it doesn’t depend on the previousoperation.

• In case there is no suitable next instruction, a NOP(no operation) instruction is to be inserted.

• Similarly, in case of dependency of oneinstruction on a previous one because of registeruse, reordering of instructions is applied.

• RISC pipelines are therefore very efficient, sincethe pipeline is most of the time in full use.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-47

(v) Design Issues forSuperscalar Processors

Usually instructions are not fully independent.Three orderings are important:

• Order in which instructions are fetched.

• Order in which instructions are executed.

• Order in which result of an instructions is stored inregisters or memory.

The terminology used is

•::::::::::Issue for the order in which fetched instructionsare passed on to the units executing it.

•::::::::::::::::::::::::Completion for the order in which their results arestored in registers and main memory.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-48

Three Policies forSuperscalar Architectures

•::::::::::::::::In-order

::::::::::::issue

::::::::::with

::::::::::::::::::in-order

::::::::::::::::::::::::completion.

Both issue and completion of instructions in theorder they occur in the program. If there is aconflict execution of later instructions is delayed.Rarely used.

•::::In

::::::::::::::::order

:::::::::::::::issue

:::::::::::::::with

::::::::::::::::::::::::::::::out-of-order

::::::::::::::::com-

::::::::::::::pletion.Now completion can be out of order.However decoding of instructions only done up tothe point of dependency or conflict.More complex instruction-issue logic required.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-49

Three Policies forSuperscalar Architectures (Cont.)

•::::::::::::::::::::::::::Out-of-order

:::::::::::::::::::issue

:::::::::::::::::with

::::::::::::::::::::::::::::::::::out-of-order

::::::::::::::::::::::::completion.Addition of a new buffer, called

::::::::::::::::::::::instruction

:::::::::::::::window.

– When an instruction is decoded it is placed inthe instruction window.

– As long as this buffer is not full, new instructionswill be decoded.

– When a function unit in the execution stageis available, an instruction from the instructionwindow may be issued, provided no conflicts ordependencies block this instruction.

Therefore the processor has lookahead capability,ie. it can identify independent instructions aheadof the current one.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-50

(vi) Units Required inSuperscalar Architectures

• Good instruction fetch strategies combined withbranch prediction logic.

• Logics for determining and if possible solvingtrue dependencies.

• Mechanisms for issuing multiple instructions inparallel.

• Resources for parallel execution of instructions,both for functional units and for access to mainmemory (in parallel!).

• Mechanisms for designing the retiring unit.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-51

(vii) Predicated Execution (Itanium)

• In order to allow the execution of many instructionsin parallel, they are predicated (has to be done atcompile time).

– If one instruction contains a condition∗ eg. “if x = 3”,and other instructions depend on positive andnegative outcome of it∗ eg. the “then”- and “else” clause in an if-

then-else construct,two predicates (from the predicate registers) arechosen.

– As soon as the condition is resolved, onebecomes true, the other false.

– The instructions which are to be executeddepending on this condition will now be prefixedwith these predicates.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-52

Predicated Execution (Itanium; Cont)

Execution of instruction boundle can now be carriedout in parallel.

• However the prefixed instructions will retire onlywhen the condition is known and the correspondingpredicate is set to be true.

• If it is set to false or not solved they will bedismissed.

Example of Predication

If R1=R2 then R3:= R3+1 else R4:= R4+1

is translated into

P2,P3 := CMP R1,R2<P2> ADD R3,R3,#1<P3> ADD R4,R4,#1

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-53

(ix) Speculative Load (Itanium)

• Bottle neck is access to main memory.

– Therefore want to move load instructionsahead, so that they can be carried out as soonas access to main memory is free.

• Problem with moving load instructions over branchinstructions:

– Wrong load might raise an exception, since itrefers to an invalid memory address or we getpage fault.

– However this load might not be required and toraise an exception might be unnecessary.

• Solution:

– Introduction of new instructions, which replacean ordinary load instruction:

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-54

Speculative Load (Itanium; Cont.)

• Speculative load written for instance as

ld.s. R4, (R3)

– “load speculative (R3) into R4”.

Executed as follows:

– Load (R4) into a register R4, but– don’t deliver the result, and– don’t raise an exception.

• An additional checking instruction is required

– e.g. chk.s R4– Placed now where one would put the load

instruction without speculative loading.– chk.s R4 does the following∗ it delivers the result of a previous speculative

loading of R4 to R4,∗ it raises an exception, if an exception

occurred.– If there is a branch in between, chk.s might be

predicated, which means that the result of theloading is only delivered and an exception is onlyraised in case the predicate is true.

From languages to hardware, CS 113, Michaelmas term 2002, Sect. 11, Supplement11-55

Revision Lecture

0. Introduction.1. Historic Development and Basic Structure.2. Combinatorial Circuits.3. Sequential Circuits.4. Computer Arithmetic and Representation of Data.5. CPU and Interconnecting Structure.6. Memory.7. Machine Languages.8. Procedures and Functions.9. Operating Systems.

10. Exception Handling and Input/Output.11. High Performance Architectures.12. Revision Lecture.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-1

General Remarks

• Essentially everything needed should becontained in the notes.(No absolute guarantee can be given however.)

– Books are of course helpful∗ to widen your perspective,∗ to get a deeper understanding∗ understand what is meant by short

sentences on the slides.∗ But you probably don’t need to know topics

which only can be found in books.∗ Other lecturers might have different policies.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-2

Coursework from CS M33

• Coursework from CS M33 recommended exerciseand good preparation for the exam.

– Question 1 and 2 of coursework 2 not relevantfor this module.

• Solutions to the courseworks will be made availableon the course web page when no submissions areare possible any more.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-3

Structure of the Exam

• Some questions ask for bookwork (eg. 3 I/Ocontrol methods, configuration of an S-R-latch).

– Require an understanding of the ares taught, notonly the knowledge of some names.

• Most questions require working out/calculatingsomething rather than repeating what you havelearned by heart.

– In that case always write down how you obtainedyour solution.

– Most marks are given for the algorithm – smallmistakes in calculations will not count that much,if the way of solving it was correct.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-4

Structure of the Exam (Cont.)

• There is a seal you put over the name.

– I usually break the seal after the exam has beenmarked completely in order to cross-check thatthe names are correct.

– It’s a pain if you seal it heavly – one strip gluedover one corner suffices.

• I will visit the exam during the first 1/2 hourand will walk around. Please look for me in casesomething is unclear in a question.

• No calculators will be allowed but dictionary(applies to most exams).

– No complicated calculations will be demandedfrom you.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-5

Structure of the Exam (Cont.)

• Exams at the department (and probably otherdepartments at UWS) usually have usually 3questions;you can select 2 out of 3;each has 25 marks (in total therefore 50):so 1 mark counts 2%.

– If you answer all 3 questions, only the best 2will count.

– When trying 3 questions, in most cases thethird question is answered badly, due to timepressure – it’s usually better for you to spentmore time on answering the other questionscarefully.∗ Except if you realize when answering one

question that it didn’t go that well with it.

• (By the way: 1 mark of the coursework = 2%of the coursework = 0.2% of the module whichcontributes 0.2

18 % = 0.011% to your final result.)

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-6

Structure of the Exam (Cont.)

• Parts of a question will have increasing strength.

– Some very easy parts (mainly bookwork).– Some parts of medium difficulty.∗ Eg. multiply using Booth’s algorithm.

– One or two more tricky parts.

• Expect to be under time-pressure during the exam(might be different from what you are used to inyour home country).

• Exam might be slighly more difficult than previousone.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-7

1. Historic Developmentand Basic Structure

• No years, numbers to be learned.

• Basic periods with basic information.

– Mechanical era – from mechanical toelectromechanical computers∗ Schickhardt, Pascal, Leibnitz.∗ Babbage, analytical machine.

– 1st generation (vacuum tubes).– 2nd generation (transistors).– 3rd generation (integrated circuits).– 4th generation (very large scale integration).

• Moore’s law, increase in performance.

• Von Neumann machine. Design principles.

• Development of languages (notions like compilers,assembly languages, assemblers)

• Components. Motherboard. (ie. what is amotherboard?)

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-8

2. Boolean Logicand Combinatorial Circuits

• Gates (symbols – often symbols for + and · mixedup!!), truth tables (for a gate; for an expression likex · y + x · y).

– Draw circuits carefully (filled bullet =connection and bullets are required; circles =negation symbol).

– Note the difference between x · y and x · y. Becareful when writing such expressions.

• Truth tables (of gates, logic operations, Booleanexpressions).

• Conversion between circuits and Boolean terms.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-9

Boolean Logicand Combinatorial Circuits (Cont.)

• Circuits and Boolean terms for defining arbitraryBoolean functions. (How to really do it).

– Example: f given by a truth table.– Example: Define a circuit which computes f,

where f(x,y,z) is true if and only if exactly twoof x, y, z are true.

– Basic optimization (not using specifictechniques, using common sense, mathematicalintuition).

• How does one show that two Boolean expressionsare the same (using a truth table)?

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-10

Circuits for Boolean Functions

• Trick with constructing circuits for Booleanfunctions:

– Take inputs A,B,C, . . ..

– For every “1” in the result, take one AND gateand connect it with each of A,B,C , . . ..

– If A is 0 in the corresponding row, put a 0 at thegate.

– If B is 0 in the corresponding row, put a 0 at thegate.

– etc.– OR everything.

• A Boolean formula can now be read off.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-11

Example

A B C Result0 0 0 01 0 0 10 1 0 01 1 0 00 0 1 11 0 1 00 1 1 01 1 1 0

��� ��

� �� �

��� �

� �

� ��������������

�������������

(A ·B · C) + (A ·B · C).

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-12

3. Sequential Circuits

• Combinatorial vs. sequential circuits(definition). Why do we need sequential circuits?

• S-R-latches (called as well R-S-latches;configuration, stable/unstable state, inputs formodifying it, how does modification take place,memory values stored).

• Circuits built from latches like D-flip-flop, storagecells.

– How does output of a D-flip-flop and fallingedge D-flip-flop depend on its inputs?

• How does a clock operate?

• How are bits moved around?

• Multiplexers (What do they do? Implementation?How do they work?).

• How does the CU operate?

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-13

4. Computer Arithmeticand Representation of Data

• Binary representation of numbers vs. BCD (binarycoded decimals).

• Conversion binary ↔ hexadecimal ↔ decimal.(What is 19 in binary, hex?)

– 0x19 stands for 19 in hexadecimal, 0b0010 for0010 in binary.

• Other number systems (what is (14)7, what is(19)10 in the system with basis 7)?

• Octal numbers.

• Shifting means multiplying/dividing a binarynumber by 2.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-14

Computer Arithmetic (Cont.)

• Adders (half, full, n-bit; circuits for them. Whytwo implementations of full-bit adders? Which oneis better?).

• Carry lookahead.

• Addition (unsigned, signed). Subtraction (add thenegated number).

• Multiplication

– Mainly Booth’s algorithm, signed and unsigned(Concrete: carry out multiplication for 3 and−2 using Booth’s algorithm; note differencesbetween signed and unsigned version).

• Signed numbers (two’s complement; sign-magnitude, and why not used?)

– Conversion decimal ↔ binary (complement +add one).

– Addition, subtraction of signed numbers.

• Carry, overflow (how calculated; examples!)

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-15

Computer Arithmetic (Cont.)

• Unsigned fixed point numbers (convert binary 0.375into fixed point with 7 bits after the point).

– (Solution 0b0.0110000).

• Arithmetic operations on unsigned fixed pointnumbers.

• Floating point numbers. (Compute decimal ↔binary).Biased exponent. Cannot represent numbersprecisely.

• Specialities about IEEE floating point standard(eg. NaN, non-normalized/normalized; no needto remember the precise bias, number of bits)

• Arithmetic operations on floating point numbers.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-16

Representation of Data

• What are scalar vs. compound data types?

• How are texts represented (Ascii, Unicode)?

• Lossy compression.

• Compound data typs (how are records/arraysstored?; two-dimensional arrays).

– At which address is a[16,5] stored?

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-17

5. CPU and Interconnecting Structure

• Basic structure of the von Neumann Architecture(ALU, Control Unit (CU), I/O, main memory).

• PC, IR, AC.

• Execution of high level commands (section (b))

– How does the content of PC, IR, AC and mainmemory change during execution of concreteinstructions?

• Implementations on digital level.

• Instruction cycle.

• Registers (user visible vs. control/status registers;definition, name typical ones).

• ALU (what is an ALU?)

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-18

CPU and Interconnecting Structure (Cont.)

• Buses (3 groups: data/address/control lines;dedicated vs. multiplexed; arbitration; timing)

• Problems with simple structure, with busconnecting everything (von Neumann bottleneck);solution (cache; hierarchies of busses etc.).

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-19

6. Memory

• Internal memory.

– Realization of DRAM (capacitors), SRAM (flip-flops).

– Definition of core memory, RAM, DRAM,EPROM etc.

– What’s error correction (basics)?

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-20

Memory (Cont.)

• External memory.

– Basics of Hard disk. CD-ROM, DVD, Tapes.(no details like tracks and sectors).

• Types of memory:

– By access type:Sequential/direct/random/associative.

– By permanence (volatile/non-volatile;erasable/non-erasable).

– What’s read mostly memory?– What are soft/hard errors?

• Memory hierarchy (briefly; why not only SRAMused?).

• Stacks.

– What is a stack?– Stack addressing (write a small program).– Implementation of stacks (how does a concrete

implementation look like?).

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-21

7. Machine Languages

• Basic types and forms of instructions.

• Translation of high level instructions into assemblylanguages.

– Simple expressions using 3/2/1/0 addressinstructions.∗ Eg. translate u:=(x+y) · (y+u) into

machine code using 3/2/1/0 addressinstructions.

– Simple conditional/loop constructions(translation of simple expressions into machinecode).∗ (translate a concrete example likeif x=y then x:=3 else x:= 4).

• Flags (N/Z/C/O-flag)

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-22

Addressing Modes, etc.

• Description (eg.: How is the address calculated inrelative addressing mode?)

• Which operand is loaded in “Load register indirect3”, if R3 = 10, memory location 10 has content20?

• No need to know names for fancy addressing modes(vi.3 - 5, Pentium II addressing modes)

• Assembly language program for computing sum ofelements of an array; try out similar examples.

• Endianess.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-23

8. Procedures and Functions

• Assembly language notation (CALL, RETURN).

• How is it executed? (How does the stack changeduring execution?)

• How to deal with parameters in high levellanguages?

• Call by value vs. call by reference parameters.

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-24

9. Operating Systems

• What does it provide (2 groups: user functions +system functions)?

• What kind of user functions?

– Command shell vs. GUI (only defiition).– WIMP.

• System functions (which ones? eg. file processing,protection, accounting, process/memorymanagement)

• Processes.

– Why multiprocessing? (While process has towait for I/O, processor can execute another one)

– Execution (interleaving).– Some process states

(new/ready/running/blocked/halted; there aremore, depending on the OS).

– What is process context, context switching,process control block, process table?

– What’s happening during context switching?– Which is meant by process level? Name two

(supervisor/user mode).

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-25

Memory Management

• Units (kilo, mega etc.)

• What’s virtual memory?

• Paging: how is address translated? (Via pagetables; separate address into offset and virtualpage number, translate virtual page numberinto physical one; construct physical address).Hardware/software.

• 3 replacement algorithms (FIFO, LRU, LFU).

• Thrashing (what’s that?).

• What’s a dirty bit?

• What’s a translation lookaside buffer?

• What’s writing back, sneaky writing?

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-26

10. Exception Handling and I/O

• Basic I/O devices (only distinctioninput;output;input and output; storage andslow/fast/network).

• Parallel vs. serial ports.

• I/O modules (definition).

• Three control methods: programmed I/O, interruptdriven I/O, DMA.How do they work (roughly)?

• Memory mapped vs. isolated I/O?

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-27

Exception Handling and I/O (Cont.)

• Idnetification of origin of interrupts (4 methods andbrief characterization).

• How are traps/interrupts detected (traps causedby the CPU; interrupts tested at the end of theexecution cycle)?

• What means masking of interrupts?

• Basic way of dealing with interrupts (some partsdone by hardware, some by software).

• What are I/O channels, -selectors, -multiplexors,-processors?

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-28

11. High Performance Architectures

• Typical question: name three main methods usedin order to enhance performance (much to besaid; sufficient: pipelining, superscalar, RISC;but alternative answers possible as well like:use of many registers, vector processors, cache,instruction level parallelism etc.). etc.).

• Principle idea of pipelining (sketch it).

• Problems of pipelining (stages with differentamount of time; data dependencies; branches;conflict of resources).

• Solutions for problems with branches (delay nextinstruction; always follow both branches; branchprediction).

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-29

High Performance Architectures (Cont.)

• RISC vs. CISC. What do the acronyms stand for?Basic characterization of both (simple vs. complexinstruction sets) and why (simple ones faster to beexecuted). Why CISC (closing the gap machinelanguages/ high level languages).

• Examples of a typical RISC (eg. Sun SPARC,PowerPC) and CISC (eg. Pentium) machine.

• Super scalar: principal idea (double the units);problems (procedural dependency, resource conflictetc.).

• Explicit instruction-level parallelism: New idea(several instructions executable in parallel) andmain architecture (Itanium).

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-30

Merry Christmas

Good Luck in the Exam

From languages to hardware, CS 113, Michaelmas term 2002, Revision Lecture Rev-31