CS100: How Computers Work in 50 minutes - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS100: How Computers Work in 50 minutes

Description:

store(n): copy value from accumulator to nth memory location ... (n): add value in nth memory location to accumulator (store result in accumulator) ... – PowerPoint PPT presentation

Number of Views:235
Avg rating:3.0/5.0
Slides: 26
Provided by: howard2
Category:

less

Transcript and Presenter's Notes

Title: CS100: How Computers Work in 50 minutes


1
CS100 How Computers Work (in 50 minutes)
Professor Craig Zilles
2
Quick Question (1)
  • Are computers smart?

3
Quick Question (2)
  • How do we get them to do smart things ?

4
A thing Compute the nth triangular number
  • The triangular numbers are as follows    
  • 1     1
  • 3     1 2
  • 6       1 2 3
  • 10      1 2 3 4
  • 15     1 2 3 4 5
  • 21     1 2 3 4 5 6
  • etc.
  • Given an n, compute the nth triangular
    number.
  • (we will pretend this is a smart thing)

5
Why Didnt the Math Work?
  • The mathematical expression is declaritive
  • It specifies what we want, but not how to do it
  • To communicate with computers natively we must be
    imperative
  • We need to say step-by-step what we want the
    computer to do

Problem
Algorithm
6
Must express the algorithm in code
  • Algorithms are abstract solutions
  • Good for ignoring details when solving problems
  • Must make it concrete
  • The computer needs the complete solution

Problem
Algorithm
Code
7
21
8
But what is going on?
  • A computer doesnt really understand C
  • or any programming language for that matter
  • What does a computer understand?

9
A Machine Model
  • This is just one example of a possible computer

10
Instructions Getting the machine to do things
  • input copy value from input to accumulator
  • output copy value from accumulator to output
  • load(n) copy value from nth memory location to
    accumulator
  • store(n) copy value from accumulator to nth
    memory location
  • add(n) add value in nth memory location to
    accumulator (store result in accumulator)
  • subtract(n) subtract value in nth memory
    location from accumulator (store result in
    accumulator)

11
Getting a computer to computer diagonal numbers
  • First step allocating storage for variables
  • int diagonal(int num)
  • int result 0
  • int i 1
  • while (i lt num)
  • result result i
  • i i 1
  • return result
  • How many variables in the above code?

12
Getting a computer to computer diagonal numbers
  • First step allocating storage for variables
  • int diagonal(int num)
  • int result 0
  • int i 1
  • while (i lt num)
  • result result i
  • i i 1
  • return result
  • Allocate each a place in memory

13
Getting a computer to computer diagonal numbers
  • First step allocating storage for variables
  • int diagonal(int num)
  • int result 0
  • int i 1
  • while (i lt num)
  • result result i
  • i i 1
  • return result
  • Need to get the 1 from someplace.
  • Store it in a memory location

num
0
result
0
i
1
14
Second Step converting to instructions
  • int diagonal(int num)
  • int result 0
  • int i 1
  • while (i lt num)
  • result result i
  • i i 1
  • return result

15
Remaining questions
  • How do we handle the loop?
  • How do we give these instructions to the computer?

16
I lied.
  • Computers dont really understand input,
    load(n), add
  • They only understand numbers (bits).

17
I lied.
  • Computers dont really understand input,
    load(n), add
  • They only understand numbers (bits).
  • We must represent instructions as numbers.
  • This is called machine code
  • We will represent every instruction with two
    numbers ltopcode, addressgt
  • input 1, 0
  • output 2, 0
  • load(n) 3, n
  • store(n) 4, n
  • add(n) 5, n
  • subtract(n) 6, n

18
I lied.
  • Computers dont really understand input,
    load(n), add
  • They only understand numbers (bits).
  • We must represent instructions as numbers.
  • This is called machine code
  • We will represent every instruction with two
    numbers ltopcode, addressgt
  • input 1, 0
  • output 2, 0
  • load(n) 3, n
  • store(n) 4, n
  • add(n) 5, n
  • subtract(n) 6, n
  • Once we do this, we can store our program in the
    memory

19
I lied.
  • Computers dont really understand input,
    load(n), add
  • They only understand numbers (bits).
  • We must represent instructions as numbers.
  • This is called machine code
  • We will represent every instruction with two
    numbers ltopcode, addressgt
  • input 1, 0
  • output 2, 0
  • load(n) 3, n
  • store(n) 4, n
  • add(n) 5, n
  • subtract(n) 6, n
  • Once we do this, we can store our program in the
    memory

20
Executing instructions stored in memory
  • Must extend machine model to track the current
    instruction
  • Add 1 to instruction counter after each
    instruction

21
Control Flow (the order instructions are executed)
  • Implement loops and ifs by setting the
    instruction counter
  • Additional instructions
  • jump(n) write n into instruction counter
  • branch(n) if the value in the accumulator is
    greater than 0, write n into instruction
    counter (otherwise add one to it)

22
Control Flow (the order instructions are executed)
  • Implement loops and ifs by setting the
    instruction counter
  • Additional instructions
  • jump(n) write n into instruction counter
  • branch(n) if the value in the accumulator is
    greater than 0, write n into instruction
    counter (otherwise add one to it)

23
Example
  • int diagonal(int num)
  • int result 0
  • int i 1
  • while (i lt num)
  • result result i
  • i i 1
  • return result

24
Instruction Reference
  • input lt1, 0gt copy value from input to
    accumulator
  • output lt2, 0gt copy value from accumulator to
    output
  • load(n) lt3, ngt copy value from nth memory
    location to accumulator
  • store(n) lt4, ngt copy value from accumulator to
    nth memory location
  • add(n) lt5, ngt add value in nth memory location
    to accumulator
  • sub(n) lt6, ngt subtract value in nth memory
    location from accumulator
  • jump(n) lt7, ngt write n into instruction counter
  • branch(n)lt8, ngt if accumulator gt 0, write n into
    instruction counter

25
Obviously this is somewhat simplified
  • (what do you expect from 50 minutes?)
  • But, much of our core curriculum is intended to
    fill in the gaps

Problem
Algorithm
Code
Assembly
Mach. Lang.
Architecture
Circuits
Gates
Write a Comment
User Comments (0)
About PowerShow.com