how computers compute
TRANSCRIPT
How Computers Compute
(6+2*3)/22–6/3
How we humans compute?
(6+2*3)/22–6/3
How we humans compute?
Operator Precedence:
3: POW (^)
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
12/4–2
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
12/4–2
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
12/4–2
3–2
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
12/4–2
3–2
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
12/4–2
3–2
1
How we humans compute?
3: POW (̂ )
2: MUL, DIV (*, /)
1: ADD, SUB (+,-)
0: Parenthesis
CPU
Memory
Program
VS
CPU
Memory
Program
This is actually pretty stupid
VS
● Get data from program/memory● Push data to memory
CPU
Memory
Program
What a CPU can do?
● Get data from program/memory● Push data to memory● Perform operations with data from memory
AdditionSubtractionMultiplicationDivision
CPU
Memory
Program
What a CPU can do?
● Get data from program/memory● Push data to memory● Perform operations with data from memory
AdditionSubtractionMultiplicationDivision
CPU
Memory
Program
What a CPU can do?
● Get data from program/memory● Push data to memory● Perform operations with data from memory
AdditionSubtractionMultiplicationDivision
CPU
Memory
Program
What a CPU can do?
“Variations” of Addition (Binary format perk)
● Get data from program/memory● Push data to memory● Perform operations with data from memory
AdditionSubtractionMultiplicationDivision
CPU
Memory
Program
What a CPU can do?
“Variations” of Addition (Binary format perk)
RISC Reduced Instruction Set Computing
CPU
Memory
Program
(6+2*3)/22–6/3
This should go there...
Running a program
CPU
Memory
Program
(6+2*3)/22–6/3
But the CPU won’t understand this program!!!
Running a program
(6+2*3)/22–6/3
(6+2*3)/4–6/3
(6+6)/4–6/3
(6+6)/4–2
12/4–2
3–2
1
Let’s flip our previous pyramid
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
Let’s flip our previous pyramid
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
-
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
-
/
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
-
/
+
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
-
/
+/
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
-
/
+/
*
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
-
/
+/
* ^
1
3–2
12/4–2
(6+6)/4–2
(6+6)/4–6/3
(6+2*3)/4–6/3(6+2*3)/22–6/3
And create a Graph with the steps taken
-
/
+/
* ^6
2 3 2 2
6 3
Let’s apply a Post-order Traversal
-
/
+/
* ^6
2 3 2 2
6 3
Algorithm:
Resolve left subtree
Resolve right subtree
Resolve node
Let’s apply a Post-order Traversal
-
/
+/
* ^6
2 3 2 2
6 3
Algorithm:
Resolve left subtree
Resolve right subtree
Resolve node
623*+22^/63/-
Let’s apply an Post-order Traversal
-
/
+/
* ^6
2 3 2 2
6 3
Algorithm:
Resolve left subtree
Resolve right subtree
Resolve node
this is what compilers do!
From: (6+2*3)/22–6/3To: 623*+22^/63/-
Abstract Syntax Tree (AST)
CPU -/36/^22+*32 6
Memory
623*+22^/63/-
reversed
Random Fact: HP48 calculators work with postfix notation only
CPU -/36/^22+*32 6
CPU -/36/^22+*3 2
6
CPU -/36/^22+* 3
62
CPU -/36/^22+ *
62
3
Take 2 items from memory, multiply them and leave the
result in memory
CPU -/36/^22 +
6 6
Take 2 items from memory, multiply them and leave the
result in memory
CPU -/36/^2 2
12
CPU -/36/^ 2
12
2
CPU -/36/ ^
12
22
CPU -/36 /
12
4
CPU -/3 6
3
CPU -/ 3
36
CPU- /
363
CPU-
32
CPU
1
End!Q&A