From Algorithms to Architecture....a lightning introduction to computer architecture

Similar documents
Chapter 1 - Introduction. Copyright 2014 by John Wiley & Sons. All rights reserved. 1

ICOM 4015: Advanced Programming

2/9/2012. Chapter One: Introduction. Chapter Goals

Chapter Goals. Chapter One: Introduction. Prerequisites. What Is Programming?

CHAPTER INTRODUCTION. Final Draft Oct. 15, Slides by Donald W. Smith TechNeTrain.com. Copyright 2013 by John Wiley & Sons. All rights reserved.

Chapter Goals. Contents. 1.1 Computer Programs

Chapter Goals. Contents INTRODUCTION

Introduction to Software Development (ISD) David Weston and Igor Razgon

Which statement regarding computer programs is correct? Which statement best describes a computer program?

CHAPTER INTRODUCTION

CHAPTER 1 Introduction to Computers and Java

First Java Program - Output to the Screen

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

Lesson 01 Introduction

COSC 122 Computer Fluency. Computer Organization. Dr. Ramon Lawrence University of British Columbia Okanagan

Computer Components. Software{ User Programs. Operating System. Hardware

CS 231 Data Structures and Algorithms, Fall 2016

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Chapter 1 Introduction to Computers, Programs, and Java

Fundamentals of Programming (Python) Basic Concepts. Ali Taheri Sharif University of Technology Spring 2018

Course Outline. Introduction to java

Computer Components. Software{ User Programs. Operating System. Hardware

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CS 11 java track: lecture 1

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Outline. elements and behavior executing programs. developing programs use as a calculator converting strings to numbers

Computers in Engineering COMP 208. Computer Structure. Computer Architecture. Computer Structure Michael A. Hawker

Computer Hardware and Software Introduction. Mr. Smith AP Computer Science A

What did we talk about last time? Examples switch statements

Introduction. Introduction to OOP with Java. Lecture 01: Introduction to OOP with Java - AKF Sep AbuKhleiF -

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to MiniSim A Simple von Neumann Machine

Chapter 1 Introduction to Computers, Programs, and Java

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to Java Applications

Computer Architecture and Assembly Language. Spring

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java Basics Lecture: January 26, 2012 (On-line Lecture 1)

IB Computer Science Topic.2-

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Introduction to Computer Systems and Operating Systems

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

COMP 202 Java in one week

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 1 Introduction to Computers, Programs, and Java

2 rd class Department of Programming. OOP with Java Programming

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Chapter 1 Introduction to Computers, Programs, and Java

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Class List. Java Must Have. Class Goals. Class Goals. Schedule

Week 2: Data and Output

Pace University. Fundamental Concepts of CS121 1

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

COMP 202 Java in one week

Java and Software Design

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 1 INTRODUCTION

Full file at

CS11 Java. Fall Lecture 1

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 01, SPRING 2013

Chapter 1 Introduction to Java

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Java Language. Programs. Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs.

Introduction to Java

Introduction to Computers, Programs, and Java. CSE 114, Computer Science 1 Stony Brook University

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Advanced Parallel Architecture Lesson 3. Annalisa Massini /2015

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Advanced Computer Programming

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Introduction. Chapter CHAPTER GOALS. To understand the activity of programming. To learn about the architecture of computers

COMP Computer Basics. Yi Hong May 13, 2015

! Learn how to think like a computer scientist. ! Learn problem solving. ! Read and write code. ! Understand object oriented programming

Chapter 2 Exercise Solutions

Chapter 1 Introduction to Computers, Programs, and Java

Lab # 2. For today s lab:

COMP 202. Java in one week

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Chapter 3 Intro to Java

B.V. Patel Institute of BMC & IT, UTU 2014

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

CS111: PROGRAMMING LANGUAGE II

4. If the following Java statements are executed, what will be displayed?

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

AP Computer Science A: Java Programming

OCR GCSE (9-1) Computer Science J276 Accompanying Instructional Document

Introduction to Java

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Transcription:

From Algorithms to Architecture...a lightning introduction to computer architecture

Implementing Algorithms Now have a methodology for going from problem to program Next develop a mental model of a device that might actually execute our algorithm, i.e. a computer!

Computer Programs Computers are programmed to perform many different tasks. Computers execute very basic instructions in rapid succession. A computer program is a sequence of instructions and decisions. Programming is the act of designing and implementing computer programs. The physical computer and peripheral devices are collectively called the hardware. The programs the computer executes are called the software.

The Anatomy of a Computer Central processing unit (CPU) performs Program control Data processing Storage Memory (Primary storage) Secondary storage Peripherals To interact with human users Networks

Schematic Diagram of a Computer

Computer Memory Set of memory locations To identify easily, number or name them To minimise bugs, restrict type of content radius (positive integer) salary (positive real) username (string) address (string) holidaypic (image) age (integer 0-150) speedoflight (integer = 300) taxrate (real 0-27.5) Might also specify whether information can be changed or not i.e. is constant/variable

Area-Circumference Problem To find area & circumference of circle 1. Print welcome message 2. Ask for & get radius from user 3. Compute area as pi.radius.radius 4. Compute circumference as 2.pi.radius 5. Report area, circumference & radius Information flowing from one step to another must be stored & so needs a place in memory

Algorithm Envisage area/circumference algorithm in terms of computer memory model radius (positive integer) area (positive real) circumference (positive real) pi (constant 3.142) 2 (constant 2.0)

Data flow Only three sorts of instructions input - from external world to memory output - from memory to world assignment - from memory to memory memory input output ALU Arithmetic & Logic Unit

Control flow (1) Control mechanism implements algorithm sets up the data paths in appropriate sequence input Control memory output First computing devices had control mechanisms that were hardwired (fixed) or at best pluggable e.g ENIAC ALU Changing the function of the machine required literally changing its wiring!

How it works 2 memory 5 Switches control data flow into and out of memory. Sequence of switch settings determines function input 3 6 output 1 4 7 8 1 2 3 4 5 6 7 8 9 10 11 12 13 1 1 0 0 0 0 0 0 0 0 0 0 0 9 12 ALU 13 10 11 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0

Control flow (2) Recognising only three forms of control sequence required (sequence, decision & repetition) & instructions can be encoded in memory like data allows general control mechanism to be implemented Control Program memory Instructions & hence the machine s function can be changed quickly & easily. memory input ALU output Limitation: may be out of program memory, yet have free data memory or vice versa!

Control flow (3) Finally realise that program & data can share same memory Control PC Instructions stored in sequentially numbered locations in memory. input memory output Current position in program held in program counter. Memory now holds both program & data ALU Fetch Execute cycle Control fetches current instruction, decodes & executes it (by setting data paths), increments PC, then fetches next instruction, and so on.

Spaghetti Code? go to programming Default is next instruction 1. Read first number 2. If first number > 10 then go to 4 3. go to 1 4. Read second number 5. If second number <= first number goto 8 6. Print Well done 7. go to 10 8. Print sorry, no good! 9. go to 4 10. Print finished.

Von Neumann Architecture Stored-program computer architecture Credited to John von Neumann, circa 1946 Control PC CPU Central Processing Unit (note: ALU may be considered part of it too.) memory input ALU output Today 99.99999% of all computers still work like this!

Practical Considerations Memory crucial to system speed! Memory technology High-speed, volatile, expensive, so limited. Non-volatile, cheap, so plentiful, but slow! Speed differential >10,000x Semiconductor RAM, ROM Optical & Magnetic Disks, tapes & CDROMs

Memory Hierarchy Result of today s technological Long-term data & programs stored in secondary storage Moved into primary memory (RAM) when needed. When machine switched on, no program in memory Boot ROM load a program (usually the OS) into RAM and start it running. input Secondary memory (disks) Control Primary memory ALU Boot ROM PC output Primary memory also called RAM Random Access Memory

From problem to execution From requirements to algorithm From algorithm to program (something understandable to machine) Ultimately need machine code (1 s & 0 s representing switch patterns) but how do we get it? Directly write machine code Write assembly & translate Write high-level & translate Translation is a symbol manipulation task!

Language Hierarchy (1) Machine code Patterns of 1 s & 0 s ~ machine instruction Usually restricted, e.g. no real numbers Difficult & error-prone writing by hand! 10011000 11000011 01000000 01000110 11111000 10000110 00000011 11111100 : Note: machine dependent, same operation may require different pattern of 1 s & 0 s on different machines. Computer designer/manufacturer defines machine code.

Language Hierarchy (2) Assembly language Each machine instruction has mnemonic Easier to remember & understand so slightly less error-prone, but still difficult to do even simple things! One-to-one mapping so translation to machine code is trivial Use program to do translation (assembler) MOV #5, R1 ADD R1, R2 STR @R0 : Like machine code, assembly language is machine dependent and low-level

Language Hierarchy (3) High-level language Much more natural, e.g. has real numbers! Much easier to understand & write Language standards, so machine independent Translation to machine code is complex use program to do translation! input A Z = A * 3 + 1; print( Z=. Z); : Two approaches Interpret Compile This program must itself be able to be executed on the machine being used!

Interpreters Translate & immediately execute each instruction in turn Source code (stored in file) 10 rem Sum two numbers 20 input enter first number, $a 30 input enter second number, $b 40 $s = $a + $b 50 output sum is. $s interpreter machine code (generated & executed, never stored) 11100101001000100

Compilers Translate all instructions to machine code, save & execute as needed Source code (stored in file) 10 rem Sum two numbers 20 input enter first number, $a 30 input enter second number, $b 40 $s = $a + $b 50 output sum is. $s compiler machine code (generated once & stored in file) 0011000101010000100 11100101001000100 Stored machine code executed by OS as many times as required!

Java compile & interpret Compile & save to bytecode (machine independent) Execute by interpreting bytecode MyProg.java // Program MyProg // David, Oct 2002 import java.io.*; Import cs1.jrobo; MyProg.class public class MyProg { } public static void mai System.out.println( JRobo robby = new J robby.f(100); robby.rect( 150, 50); } Source code (stored in file) Java compiler (javac) bytecode (stored in file) 1100011100 0100010000 0111100111 0001010101 0001 Also known as the Java Virtual Machine (JVM) machine code (generated & executed, never stored) Java interpreter (java) 11100101001000100

Java language levels

Programs across Internet Java Applets run anywhere safely! Mypage.html // Program MyApplet // David, Oct 2002 import java.io.*; Import cs1.jrobo; public class MyApplet extends Applet { } public void paint( JRobo robby = new J robby.f(100); robby.rect( 150, 50) } Source code (stored in file) <html> <body> <applet class= MyApplet.class > </applet> </body> Java compiler (javac) html webpage containing applet (stored in file) bytecode (stored in file) INTERNET WebBrowser Netscape http://somewhere.com/mypage.html My WebPage Look at this applet Welcome to MyApplet MyApplet.java 1100011100 0100010000 0111100111 0001010101 0001 Java interpreter (java) MyApplet.class JVM in webbrowser creates machine code for client 11100101001000100

The Java Programming Language Safe Portable Platform-independent Distributed as instructions for a virtual machine Vast set of library packages Designed for the Internet

HelloPrinter.java 1 public class HelloPrinter 2 { 3 public static void main(string[] args) 4 { 5 // Display a greeting in the console window 6 7 System.out.println("Hello, World!"); 8 } 9 }

Analyzing Your First Program: Class Declaration Classes are the fundamental building blocks of Java programs: Declaration of a class called HelloPrinter public class HelloPrinter The name of the public class must match the name of the file containing the class: Class HelloPrinter must be contained in a file named HelloPrinter.java

Analyzing Your First Program: Methods Each class contains declarations of methods. Each method contains a sequence of instructions. A method contains a collection of programming instructions that describe how to carry out a particular task. A method is called by specifying the method and its arguments.

Analyzing Your First Program: main Method Every Java application contains a class with a main method When the application starts, the instructions in the main method are executed Declaring a main method public static void main(string[] args) {... }

Analyzing Your First Program: Statements The body of the main method contains statements. Our method has a single statement: System.out.println("Hello, World!"); It prints a line of text: Hello, World!

Analyzing Your First Program: Method Call A method call: System.out.println("Hello, World!"); A method call requires: 1. The method you want to use (in this case, System.out.println) 2. Any values the method needs to carry out its task enclosed in parentheses (in this case, "Hello, World!") The technical term for such values is arguments

Analyzing Your First Program: Printing You can print numerical values System.out.println(3 + 4); evaluates the expression 3 + 4 displays the number 7. System.out.println method prints a string or a number and then starts a new line. The sequence of statements System.out.println("Hello"); System.out.println("World!"); Prints two lines Hello World! There is a second method, System.out.print, that you can use to print an item without starting a new line

Self Check How would you modify the HelloPrinter program to print the word "Hello" vertically? Answer: System.out.println("H"); System.out.println("e"); System.out.println("l"); System.out.println("l"); System.out.println("o");

Self Check Would the program continue to work if you replaced line 7 with this statement? System.out.println(Hello); Answer: No. The compiler would look for an item whose name is Hello. You need to enclose Hello in quotation marks: System.out.println("Hello");

Self Check What does the following set of statements print? System.out.print("My lucky number is"); System.out.println(3 + 4 + 5); Answer: The printout is My lucky number is12. It would be a good idea to add a space after the is.

Self Check What do the following statements print? System.out.println("Hello"); System.out.println(""); System.out.println("World"); Answer: Hello a blank line World

Errors A compile-time error (syntax error) is a violation of the programming language rules detected by the compiler. System.ou.println("Hello, World!"); A run-time error (logic error) causes a program to perform an action that the programmer did not intend. System.out.println("Hello, Word!");

Errors Exception - a type of run-time error Generates an error message from the Java virtual machine This statement System.out.println(1 / 0) Generates this run-time error message "Division by zero"

Self Check Suppose you omit the "" characters around Hello, World! from the HelloPrinter.java program. Is this a compile-time error or a run-time error? Answer: This is a compile-time error. The compiler will complain that it does not know the meanings of the words Hello and World.

Self Check Suppose you change println to printline in the HelloPrinter.java program. Is this a compile-time error or a run-time error? Answer: This is a compile-time error. The compiler will complain that System.out does not have a method called printline.

Self Check Suppose you change main to hello in the HelloPrinter.java program. Is this a compile-time error or a run-time error? Answer: This is a run-time error. It is perfectly legal to give the name hello to a method, so the compiler won't complain. But when the program is run, the virtual machine will look for a main method and won't find one.

Self Check When you used your computer, you may have experienced a program that "crashed" (quit spontaneously) or "hung" (failed to respond to your input). Is that behavior a compile-time error or a run-time error? Answer: It is a run-time error. After all, the program had been compiled in order for you to run it.

Self Check Why can't you test a program for run-time errors when it has compiler errors? Answer: When a program has compiler errors, no class file is produced, and there is nothing to run.