Introducing hardware and computer languages March 17.

38
Introducing hardware and computer languages March 17

Transcript of Introducing hardware and computer languages March 17.

Page 1: Introducing hardware and computer languages March 17.

Introducing hardware and computer languages

March 17

Page 3: Introducing hardware and computer languages March 17.

3

MotherboardInput/output

Processor

Memory slots

Processor

Power slot

Graphic card slot

Slots for Storage devices

Page 4: Introducing hardware and computer languages March 17.

4

Motherboard

data, or instructions

addr

ess

A bus is a set of wires that connect the computer’s components.

Buses are responsible for movement of data from input devices, to output devices and from/to CPU and memory.

Rand

om A

cces

s M

emor

y

Page 5: Introducing hardware and computer languages March 17.

Memory Memory is a large collection of circuits, each capable of

storing bit. Bit is a binary(base 2) digit: either 0 or 1 Cells (words): manageable units; Typical size is 8 bits (1 byte): Example: 01101011b Some machines are 16 bits (2 bytes) and some are 32 bits or

64 bits Byte (8 bits), KB (kilobyte, 103 ≈ 210 bytes), MB (Megabyte, 106 ≈ 220 bytes), GB (Gigabyte, 109 ≈ 230 bytes). Note: k ≠ K because 1000 ≠ 1024.

Page 6: Introducing hardware and computer languages March 17.

MemoryTo identify individual cells (bytes) in a machine’s main memory, each cell is assigned a unique name, called its address.

...01001000 01100101 01101100 0110111101101100 00101110

H e l l o ,ASCII

...Data

Address 0000 0101 0000 0110 0000 0111 0000 1000 0001 0001 0001 0010

Address BusData Bus

0 0 0 0 0 1 0 1 Low-order endHigh-order end

Least Significant Bit (LSB)Most Significant Bit (MSB)

The organization of byte-size memory cell

ASCII is symbolic coding table, where each symbol associated with a numerical code.

Page 7: Introducing hardware and computer languages March 17.

Instructions are represented as 32-bit numbers Programs can be stored in memory

Can be read/written just like numbers/data

Processor

Payroll program

C compiler

Word processor

Payroll data

Source C program

Term paper

Stored Program Concept

Memory

Page 8: Introducing hardware and computer languages March 17.

8

Processor

ALU This provides the basic operational units of the CPU

PC – program counterIR – instruction register

Registers are memory cells that work as scratch locations for storing intermediate results and values

A control unit: This unit is responsible for controlling flow of data and instructions.

#

Add 25 to A

Control Unit

IR

PC

Regi

ster

s

Page 9: Introducing hardware and computer languages March 17.

9

Program execution (you can skip)

0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111

...

Memory

program & data

Dat

a pa

th(b

us)

Fetch

Decode

Execute Cycle

Input / Output

Control Unit

Page 10: Introducing hardware and computer languages March 17.

Example of Processor Instructions Arithmetic

add, sub, mult, div Logical

and, or, ssl (shift left logical), srl (shift right logical) Data transfer

lw (load word), sw (store word) lui (load unsigned immediate constant)

Branches Conditional: beq, bne, slt Unconditional: j (jump), jr (jump register), jal

Page 11: Introducing hardware and computer languages March 17.

Computer Languages

temp = v[k];v[k] = v[k+1];v[k+1] = temp;

lw $t0,0($2)

lw $t1,4($2)

sw $t1,0($2)

sw $t0,4($2)0000 1001 1100 0110 1010 1111 0101 1000

1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111

High-level Language

Assembly Language

Machine Language

C/Java Compiler

Assembler

TEMP = V(K)V(K) = V(K+1)V(K+1) = TEMP

Fortran Compiler

Page 12: Introducing hardware and computer languages March 17.

Generations of Programming Languages1GL: machine codes (0110 1101 1100 1010, …)

2GL: symbolic assemblers (add A, B)

3GL: (machine-independent) imperative languages (C = A + B). Examples are FORTRAN, Pascal, C languages

4GL: domain specific application generators (Python, LabVIEW, R, Matlab)

Each generation is at a higher level of abstraction

Page 13: Introducing hardware and computer languages March 17.

Machine Languages Comprised of 1s and 0s The “native” language of a computer Difficult to program – one misplaced 1 or 0 will cause the

program to fail. Example of code:

11101000101010 01110101011100 10111010110100 10100011110111

Page 14: Introducing hardware and computer languages March 17.

Assembly Languages Assembly languages are a step towards easier programming. Assembly languages are comprised of a set of elemental

commands which are tied to a specific processor. Assembly language code needs to be translated to machine

language before the computer processes it. Example:

ADD AX, 10

Register (temporary memory) inside processor

Page 15: Introducing hardware and computer languages March 17.

High-Level Languages High-level languages represent a giant leap towards easier

programming. The syntax of HL languages is similar to English.

Historically, we divide HL languages into two groups: Procedural languages Object-Oriented languages (OOP)

Page 16: Introducing hardware and computer languages March 17.

Assembler vs High Level Languages A “semantic gap” exists between the amount of information

conveyed in assembly language vs high level languages. Consider the following single statement: x = x + 3;

This single statement may require many assembly language statements (operations): Load memory location 24 into accumulator Add a constant 3 to the accumulator Store accumulator in memory location 24

The number of executable statement expands greatly during the translation process from a high level language into assembly language.

Page 17: Introducing hardware and computer languages March 17.

Procedural Languages Early high-level languages are typically called procedural

languages. Procedural languages are characterized by sequential sets of

linear commands. The focus of such languages is on structure. Examples include C, COBOL, Fortran, LISP, Perl, HTML, VBScript

Page 18: Introducing hardware and computer languages March 17.

Object-Oriented Languages Most object-oriented languages are high-level languages. The focus of OOP languages is not on structure, but on modeling data. Programmers code using “blueprints” of data models called classes. Examples of OOP languages include C++, Visual Basic.NET and Java.

Page 19: Introducing hardware and computer languages March 17.

Languages’ similarities and differences

Common Constructs: basic data types (numbers,

etc.); variables; expressions; statements; keywords; control constructs; procedures; comments; errors ...

Uncommon Constructs: type declarations; special types (strings,

arrays, matrices, ...); sequential execution; concurrency constructs; packages/modules; objects; general functions; generics; modifiable state; ...

Page 20: Introducing hardware and computer languages March 17.

Machine code generation process All programs need to be translated to machine code so that a

computer can process the program.The machine code building process consist of compilation and linking

Source codes

Object codes

compilation

linking

Page 21: Introducing hardware and computer languages March 17.

21

Compiler Compiler is program that analyze source code and generates

intermediate code. A compiler consists of: The front end: Verifies syntax and semantics, and generates an

intermediate representation of the source code for processing by the middle-end.

The middle end: Performs optimizations, including removal of useless or unreachable code, discovery and propagation of constant values, etc. Generates another intermediate representation for the backend.

The back end: Generates the assembly code, performing register allocation in process.

Page 22: Introducing hardware and computer languages March 17.

22

Linker A linker is a computer program that takes one or more object

files generated by a compiler and combines them into a single executable program.

Page 23: Introducing hardware and computer languages March 17.

Interpreting Some programs are translated using an interpreter. Such programs are translated

line-by-line instead of all at once (like compiled programs). Interpreted programs generally translate quicker than compiled programs, but

have a slower execution speed. Examples are Java and Python.

Page 24: Introducing hardware and computer languages March 17.

A Brief Chronology

Language Application Area

Primitive assemblers (early 1950s)FORTRAN (1957) Scientific programming Formula TranslationCOBOL (1960) Business data Processing Common Business-Oriented LanguageLisp (1960) Artificial Intelligence (AI) List ProcessingC (1972) System Programming Predecessor BProlog(1970) Artificial Intelligence Logic ProgrammingAda (1980) Real-time distributed syst. Ada Augusta Byron & Charles Smalltalk(1983) GUI, OOP Objects “talk” via messageC++ (1986) Supports object & OOP C (++ is the increment operator)JAVA (1995) Uses virtual machine Originally named “Oak”

Origin of Name

Page 25: Introducing hardware and computer languages March 17.

Programming as Problem SolvingDeveloping a Program

Analyze the problem

Design the problem

Code the program

Test the program

Understand the problem

Devise a plan to solve

Carry out the plan

Review the results

Problem solving principles

Page 26: Introducing hardware and computer languages March 17.

(1) Analyze the Problem

Specify the problem requirementsImportant questions What are the inputs? (given data) What are the outputs? (required data) How will we calculate the required outputs from the given

inputs?

Page 27: Introducing hardware and computer languages March 17.

(2) Design the Program Create an outline of the program that solves the problem

An algorithm – a step by step procedure that will provide the required results from the given inputs.

Algorithm Examples: Instructions on how to make a cake, How to use the bank’s ATM, etc.

Flowchart is a good way to represent an algorithmstart

Get Data

Processes

Output Result

stop

Page 28: Introducing hardware and computer languages March 17.

(3) Code the Program

Once the design is completed, implement the program code. Code is written in some programming language such as BASIC,

Pascal, C++, Java, etc.

Page 29: Introducing hardware and computer languages March 17.

(4) Testing the program

Locate any errors (bugs) Testing is done throughout the development cycle Desk-checking, or code walkthrough is performed to locate

errors in the code. Pretend you are the computer and execute your own code.

Ultimate test is to run the program to see if the outputs are correct for the given inputs.

Page 30: Introducing hardware and computer languages March 17.

30

Modular programming Determine the major tasks that the program must accomplish.

Each of these tasks will be a module. Some modules will be complex themselves, and they will be

broken into sub-modules, and those sub-modules may also be broken into even smaller modules.

This is called top-down design

Page 31: Introducing hardware and computer languages March 17.

31

Example: Calculate Electricity Bill The rates that a company charges for electricity, based on

kilowatt-hours (kWh) The task is to develop a program that calculates energy costs. The following information is provided

First 360 kWh: $0.12589 per kWhNext 320 kWh: $0.17901 per kWh

Over 680 kWh: $0.20971 per kWhImportant questions What are the inputs?

Energy rates per kWh What are the outputs?

Total cost for a given kWh How will we calculate the required outputs from the given inputs?

Page 32: Introducing hardware and computer languages March 17.

Mapping ModulesInputs Processes Output

Input variables:Total used power kwhRATE1 RATE2 RATE3

Rate of Interest:BASE1 = (RATE1 * 360.0 );BASE2 = (BASE1 + (RATE2 * (680.0 - 360.0)))

Display:Write FinalBill

Final Bill:Select the plane based on kwhFinalBill = RATE1 * kwhFinalBill = BASE1 + (RATE2 * (kwh - 360.0))FinalBill = BASE2 + (RATE3 * (kwh - BREAK2));

Page 33: Introducing hardware and computer languages March 17.

Code Modules A module is an independent, self-contained section of code that

performs a single task. The main module is the module that drives the application. It

“controls” all other modules. When the main module calls another module, program control

transfers to the called module. Program control cedes back to the main module when the called module finishes.

Main Module

Input Data Perform Calculations

Output Results

Page 34: Introducing hardware and computer languages March 17.

34

Problem classification A programming language is a notational system for describing

computation in a machine-readable and human-readable form. Types of problems

Functional problems Decision problems Search problems Optimization problems

Page 35: Introducing hardware and computer languages March 17.

35

Programming problem examples Polynomial root finding

Category: Functional problem Input: A polynomial with real coefficients Output: One (or all) real roots of the input polynomial

Matrix inversion Category: Functional problem Input: A square matrix with rational entries Output: The inverse of the input matrix if it is invertible, or "failure”

Primality testing Category: Decision problem Input: A positive integer Output: The decision whether the input integer is prime or not

Page 36: Introducing hardware and computer languages March 17.

36

Programming problem examples Weather prediction

Category: Functional problem Input: Records of weather for previous days and years. Possibly

also data from satellites. Output: Expected weather of Seoul for tomorrow

Web browsing Category: Functional problem Input: A URL (abbreviation for "Uniform Resource Locator" which

is colloquially termed as "Internet site”) Output: Display (audio and visual) of the file at the given URL

Page 37: Introducing hardware and computer languages March 17.

37

Programming problem examples Traveling salesman problem (TSP)

Category: Optimization problem Input: A set of cities, the cost of traveling between each pair of

cities, and the criterion of cost minimization Output: A route through all the cities with each city visited only

once and with the total cost of travel as small as possible Chess : Can I win?

Category: Search problem Input: A configuration of the standard 8x8 chess board and the

player ("white" or "black") who is going to move next Output: A winning move for the next player, if existent, or "failure”

Page 38: Introducing hardware and computer languages March 17.

DocumentationDocumentation plays an important role for programmers as well as for users. Internal Documentation

Comments explain to the reader the logic and decision processes of the programmer. Comments are ignored by an interpreter or compiler.

Types of comments include code comments, documentation comments & module comments.

External Documentation External documentation includes a user’s guide and, typically, a

more technical system administrator’s guide.