Computers are amazing machines Navigation fly aircraft and

  • Slides: 47
Download presentation

Computers are amazing machines Navigation fly aircraft and spaceships Entertainment Industry provide diverse entertainment

Computers are amazing machines Navigation fly aircraft and spaceships Entertainment Industry provide diverse entertainment such as computer games or computerized music control power stations and chemical plants Communication Business route and control millions of telephone calls in networks help companies to make decisions Mathematics perform immensely complicated and involved mathematical computations 2021/3/10 Medical sophisticated medical procedures Law serve lawyers and judges 2

What is Computer? Volume of Bits Even the most sophisticated computer is really only

What is Computer? Volume of Bits Even the most sophisticated computer is really only a large, well-organized volume of bits. Operations 2021/3/10 It can normally carry out only a small number of extremely simple operations on them, e. g. , zeroing, flipping, and testing 3

It’s amazing! Isn’t it? Computer is not that SMART!!! 2021/3/10 4

It’s amazing! Isn’t it? Computer is not that SMART!!! 2021/3/10 4

What is Computer? How to check the equality of two bits? 2021/3/10 5

What is Computer? How to check the equality of two bits? 2021/3/10 5

What is Computer? How to check the equality of two bits? 2021/3/10 Can you

What is Computer? How to check the equality of two bits? 2021/3/10 Can you extend it to check the equality of two 32 -bit binary numbers? 6

Another “complicated” example How to calculate the sum of two 1 -bit binary number

Another “complicated” example How to calculate the sum of two 1 -bit binary number ? 2021/3/10 Operations allowed to use flip, zero, test exit, goto add(x, y) 1. zero z 0 2. zero z 1 3. equality test(x, y) 4. test eq goto 7 5. flip z 0 6. exit 7. zero t 8. flip t 9. equality test(x, t) 10. test eq flip z 1 7

Another “complicated” example How to calculate the sum of two 2 -bit binary numbers?

Another “complicated” example How to calculate the sum of two 2 -bit binary numbers? 2021/3/10 add(x, y) 1. zero z 0 2. zero z 1 3. equality test(x, y) 4. test eq goto 7 5. flip z 0 6. exit 7. zero t 8. flip t 9. equality test(x, t) 10. test eq flip z 1 8

Encapsulation + Abstraction flip “Atomic” operations can be combined to generate “Compound” operations! 2021/3/10

Encapsulation + Abstraction flip “Atomic” operations can be combined to generate “Compound” operations! 2021/3/10 … zero test goto exit 2 -bit. Add(x, y) 9

Encapsulation + Abstraction Compound Operations “Atomic” operations can be combined to generate “Compound” operations!

Encapsulation + Abstraction Compound Operations “Atomic” operations can be combined to generate “Compound” operations! 2021/3/10 • Provide higher functional abstractions • Encapsulate underlying details of implemented by lower operations • Can be directly used as atomic operations • Can be further combined to provide new higher compound operations/functions 10

Encapsulation + Abstraction Different Instruction Set Different Computer (Type) Far more atomic operations are

Encapsulation + Abstraction Different Instruction Set Different Computer (Type) Far more atomic operations are provided by computers 2021/3/10 11

CISC VS RISC A=A*B A CISC processor would come prepared with a specific instruction

CISC VS RISC A=A*B A CISC processor would come prepared with a specific instruction (we call it “MULT”). When executed, this instruction 1. Loads the two values into separate registers 2. Multiplies the operands in the execution unit 3. Stores the product in the appropriate register. Thus, the task of multiplying two numbers can be completed with one instruction: MULT A, B <<<==this is assembly statement MULT is what is known as a “complex instruction. ” 2021/3/10 A RISC processor only use simple instructions that can be executed within one clock cycle. Thus, the “MULT” command could be divided into three separate commands: 1. “LOAD” moves data from memory to a register 2. “PROD” finds the product of two operands located within the registers 3. “STORE” moves data from a register to memory. The task of multiplying two numbers can be completed with four lines of assembly: LOAD R 1, A <<<==this is assembly statement LOAD R 2, B <<<==this is assembly statement PROD A, B <<<==this is assembly statement STORE R 3, A <<<==this is assembly statement 12

Encapsulation + Abstraction Program at different abstraction level 2021/3/10 13

Encapsulation + Abstraction Program at different abstraction level 2021/3/10 13

 int absdiff(int x, int y) { if (x < y) return y -

int absdiff(int x, int y) { if (x < y) return y - x; else return x - y; } 2021/3/10 movl 8(%ebp), %edx ; 取x的值 movl 12(%ebp), %eax; 取y的值 cmpl %eax, %edx ; 比较x和y的值 jl. L 3 ; 如果y<x 转到. L 3 subl %eax, %edx ; 计算y-x movl %edx, %eax ; 返回值 jmp. L 5 ; 跳转到. L 5. L 3: ; y<x subl %edx, %eax ; 计算x-y. L 5: ; 完成 14

The ESSENCE The outward appearance is of peripheral importance when compared to the bits

The ESSENCE The outward appearance is of peripheral importance when compared to the bits and their internal arrangement 2021/3/10 It is the bits that “sense” the external stimuli arriving from the outside world via buttons, levers, keys on a keyboard, electronic communication lines, and even microphones and cameras. It is the bits that “decide” how to react to these stimuli and respond accordingly by directing other stimuli to the outside via displays, screens, printers, loudspeakers, beepers, levers, and cranks. 15

冯·诺依曼体系结构 计算机之父 John Von Neumann 1903-1957 2021/3/10 16

冯·诺依曼体系结构 计算机之父 John Von Neumann 1903-1957 2021/3/10 16

Problem Solving 2021/3/10 Problem Instance Problem 17

Problem Solving 2021/3/10 Problem Instance Problem 17

Problem Solving Computer is usually not used to solve problem instances, but to solve

Problem Solving Computer is usually not used to solve problem instances, but to solve problems in a general way, following which all instances of the same problem can be solved. Problem 2021/3/10 18

Problem Solving How do people SOLVE a problem? 4 STEPS for solving a problem

Problem Solving How do people SOLVE a problem? 4 STEPS for solving a problem George Pólya 2021/3/10 19

Problem Solving STEP-4 Looking back Check the result, and… STEP-3 Carrying out Solve the

Problem Solving STEP-4 Looking back Check the result, and… STEP-3 Carrying out Solve the problem. STEP-2 Planning How will you attack the problem? STEP-1 Understanding 4 STEPS for solving a problem 2021/3/10 What you are given and what you are supposed to figure out 20

An example: decipher a message Assume we know the ciphering method X Y Z

An example: decipher a message Assume we know the ciphering method X Y Z A B C D … W X Y Z Given the ciphered message SUREOHP VROYLQJ What is the original message? ? ? ? ? 2021/3/10 X Y Z 21

An example: decipher a message STEP-4 Looking back PROBLEM SOLVING Cipher “PROBLEM SOLVING” with

An example: decipher a message STEP-4 Looking back PROBLEM SOLVING Cipher “PROBLEM SOLVING” with the ciphering method, and check whether the ciphered message is “SUREOHP VROYLQJ” STEP-3 Carrying out Apply the plan STEP-2 Planning STEP-1 Understanding 4 STEPS for solving the problem 2021/3/10 22

How does a computer solve a problem? STEP-4 Looking back • Why the output

How does a computer solve a problem? STEP-4 Looking back • Why the output is right? • Can we further improve it? STEP-3 Carrying out This is the only thing actually done by computer STEP-2 Planning • • • How to “digitalize” input and output? What “plan” can be carried out by computer? How to make computer understand the “plan”? STEP-1 Understanding 2021/3/10 • • What is the INPUT? What is the OUTPUT? Relation 23

How does a computer solve a problem? Characterization of all legal INPUTS Understanding Real

How does a computer solve a problem? Characterization of all legal INPUTS Understanding Real World Problem Characterization of all legal OUTPUTS Arithmetic Problem 2021/3/10 24

How does a computer solve a problem? Characterization of all legal INPUTS Planning Characterization

How does a computer solve a problem? Characterization of all legal INPUTS Planning Characterization of all legal OUTPUTS Arithmetic Problem 2021/3/10 Executable Program 25

How does a computer solve a problem? Planning Any legal input Characterization of all

How does a computer solve a problem? Planning Any legal input Characterization of all legal INPUTS Implementation Coding Characterization of all legal OUTPUTS Arithmetic Problem Executable Program Desired output Arithmetic Solution 2021/3/10 26

How does a computer solve a problem? Carrying out Executable Program 2021/3/10 27

How does a computer solve a problem? Carrying out Executable Program 2021/3/10 27

How does a computer solve a problem? Carrying out Hello World! 2021/3/10 28

How does a computer solve a problem? Carrying out Hello World! 2021/3/10 28

How does a computer solve a problem? Carrying out Look Back Is the result

How does a computer solve a problem? Carrying out Look Back Is the result correct as expectation? Hello World! Is the implementation acceptable? Is there any better solution? …… 2021/3/10 29

An example: Total Wage Given the wages of all employees Please calculate the TOTAL

An example: Total Wage Given the wages of all employees Please calculate the TOTAL WAGE Look Back Carrying out Planning Understanding 2021/3/10 30

An example: Total Wage Given the wages of all employees Please calculate the TOTAL

An example: Total Wage Given the wages of all employees Please calculate the TOTAL WAGE Look Back Carrying out Planning Understanding 2021/3/10 • Known: the wages of all employees • Target: Total Wage of the company 31

An example: Total Wage Given the wages of all employees Please calculate the TOTAL

An example: Total Wage Given the wages of all employees Please calculate the TOTAL WAGE Look Back Carrying out 2021/3/10 Planning • It’s a simple problem, which can be solved by aggregate all the wages. Understanding • Known: the wages of all employees • Target: Total Wage of the company 32

An example: Total Wage Given the wages of all employees Please calculate the TOTAL

An example: Total Wage Given the wages of all employees Please calculate the TOTAL WAGE Look Back Carrying out 2021/3/10 • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Planning • It’s a simple problem, which can be solved by aggregate all the wages. Understanding • Known: the wages of all employees • Target: Total Wage of the company 33

An example: Total Wage Given the wages of all employees Please calculate the TOTAL

An example: Total Wage Given the wages of all employees Please calculate the TOTAL WAGE Look Back Carrying out 2021/3/10 • Is the Total Wage similar to that of last month? • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Planning • It’s a simple problem, which can be solved by aggregate all the wages. Understanding • Known: the wages of all employees • Target: Total Wage of the company 34

An example: Total Wage • Is the Total Wage similar to that of last

An example: Total Wage • Is the Total Wage similar to that of last month? • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Look Back Carrying out • It’s a simple problem, which can be solved by aggregate all the wages. Planning • Known: the wages of all employees • Target: Total Wage of the company Understanding 2021/3/10 • Problem: given the wages of n employees of any company, calculate Total Wage of the company 35

An example: Total Wage • Is the Total Wage similar to that of last

An example: Total Wage • Is the Total Wage similar to that of last month? • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Look Back Carrying out • It’s a simple problem, which can be solved by aggregate all the wages. Planning • Known: the wages of all employees • Target: Total Wage of the company Understanding 2021/3/10 • • How to load? File or Stand input? How to store? Array or Linklist? How to sum? Iteration or Recursion? How to output? File or Display? • Problem: given the wages of n employees of any company, calculate Total Wage of the company 36

An example: Total Wage • Is the Total Wage similar to that of last

An example: Total Wage • Is the Total Wage similar to that of last month? • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Look Back Carrying out • It’s a simple problem, which can be solved by aggregate all the wages. Planning • Known: the wages of all employees • Target: Total Wage of the company Understanding 2021/3/10 • • How to load? How to store? How to sum? How to output? Coding and Implementation • Problem: given the wages of n employees of any company, calculate Total Wage of the company 37

An example: Total Wage • • How to load? How to store? How to

An example: Total Wage • • How to load? How to store? How to sum? How to output? 2021/3/10 Coding and Implementation 38

An example: Total Wage • • How to load? How to store? How to

An example: Total Wage • • How to load? How to store? How to sum? How to output? 2021/3/10 Coding and Implementation 39

An example: Total Wage • • How to load? How to store? How to

An example: Total Wage • • How to load? How to store? How to sum? How to output? 2021/3/10 Coding and Implementation 40

An example: Total Wage • • How to load? How to store? How to

An example: Total Wage • • How to load? How to store? How to sum? How to output? 2021/3/10 Coding and Implementation 41

An example: Total Wage • • How to load? How to store? How to

An example: Total Wage • • How to load? How to store? How to sum? How to output? 2021/3/10 Coding and Implementation 42

An example: Total Wage • Is the Total Wage similar to that of last

An example: Total Wage • Is the Total Wage similar to that of last month? • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Look Back Carrying out • It’s a simple problem, which can be solved by aggregate all the wages. Planning • Known: the wages of all employees • Target: Total Wage of the company Understanding 2021/3/10 • Execute the program • • How to load? How to store? How to sum? How to output? Coding and Implementation • Problem: given the wages of n employees of any company, calculate Total Wage of the company 43

An example: Total Wage • Is the Total Wage similar to that of last

An example: Total Wage • Is the Total Wage similar to that of last month? • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Look Back Carrying out • It’s a simple problem, which can be solved by aggregate all the wages. Planning • Known: the wages of all employees • Target: Total Wage of the company Understanding 2021/3/10 • Correct? • Efficient? • … • Execute the program • • How to load? How to store? How to sum? How to output? Coding and Implementation • Problem: given the wages of n employees of any company, calculate Total Wage of the company 44

An example: Total Wage • Is the Total Wage similar to that of last

An example: Total Wage • Is the Total Wage similar to that of last month? • Aggregate all the wages one by one, with a pencil and a piece of paper, even a calculator Look Back Carrying out • It’s a simple problem, which can be solved by aggregate all the wages. Planning • Known: the wages of all employees • Target: Total Wage of the company Understanding 2021/3/10 • Correct? • Efficient? • … • Execute the program • • How to load? How to store? How to sum? How to output? Coding and Implementation • Problem: given the wages of n employees of any company, calculate Total Wage of the company 45

An example: Vending Machine • CASH ONLY!!! • Can identify and accept 50 yuan,

An example: Vending Machine • CASH ONLY!!! • Can identify and accept 50 yuan, 10 yuan, 5 yuan, 1 yuan and 0. 5 yuan. • If CHANGE cannot be correctly returned, it refuses to provide service. • Please find a way to return changes, s. t. the machine could provide service as long as possible Look Back Carrying out Planning Understanding 2021/3/10 46

Importance of MATH The Arithmetic Problem must be unambiguously descripted. The language, used to

Importance of MATH The Arithmetic Problem must be unambiguously descripted. The language, used to implement the plan, has its mathematic foundation. The correctness of an algorithm has to be proved. The efficiency of an algorithm has to be analyzed with mathematic tools. 2021/3/10 47