Title: CS100: How Computers Work in 50 minutes
1CS100 How Computers Work (in 50 minutes)
Professor Craig Zilles
2Quick Question (1)
3Quick Question (2)
- How do we get them to do smart things ?
4A 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)
5Why 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
6Must 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
721
8But what is going on?
- A computer doesnt really understand C
- or any programming language for that matter
- What does a computer understand?
9A Machine Model
- This is just one example of a possible computer
10Instructions 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)
11Getting 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?
12Getting 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
13Getting 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
14Second 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
-
15Remaining questions
- How do we handle the loop?
- How do we give these instructions to the computer?
16I lied.
- Computers dont really understand input,
load(n), add - They only understand numbers (bits).
17I 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
18I 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
19I 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
20Executing instructions stored in memory
- Must extend machine model to track the current
instruction - Add 1 to instruction counter after each
instruction
21Control 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)
22Control 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)
23Example
- int diagonal(int num)
- int result 0
- int i 1
- while (i lt num)
- result result i
- i i 1
-
- return result
-
24Instruction 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
25Obviously 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