Grundlagen und Methoden der Informatik
für Wirtschaftswissenschaftler
Woche 1: Computing Basics
Prof. Dr. Simon Mayer
simon.mayer@unisg.ch
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
2
Why Computers?
Predicting the Future!
https://www.bbc.com/reel/video/p09pcwnz/unlocking-the-secrets-of-the-world-s-oldest-computer
National Geographic Naked Science Star Clock aka Ancient Computer theblackpacket com
- YouTube
~200 B.C.E.
Making War!
~1953 C.E.
Doing… anything?
Navy Mechanical Fire Control Computers (1/2) - YouTube
~Today
DIGITAL COMPUTER TECHNIQUES & PRINCIPLES 1962 U.S. NAVY FILM UNIVAC IBM ELECTRODATA 90714 – YouTube (from 4:15-6:16)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
3
Basics of the Efficient Automation of Processes
The goal of this week’s lectures is to acquaint you with computer science as the science of the
efficient automation of processes. We will discuss what “information” is in a computer science context,
how it can be stored and processed by a computer system, and what basic hardware components this
depends on.
After this week, you should know and be able to explain how information can be represented and
computed on in principle, and what happens when a program is translated and executed on a
computer.
This week’s guided exercise has two main targets:
- Motivation: I demonstrate how to meaningful tasks can be efficiently automated already with little
programming knowledge.
- Exercise Introduction: I will introduce the first exercise assignment.
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
4
“Computing” Basics
The goal of this week’s lectures is to acquaint you with computer science as the science of the
efficient automation of processes. We will discuss what “information” is in a computer science context,
how it can be stored and processed by a computer system, and what basic hardware components this
depends on.
After this week, you should know and be able to explain how information can be represented and
computed on in principle, and what happens when a program is translated and executed on a
computer.
This week’s guided exercise has two main targets:
- Motivation: I demonstrate how to meaningful tasks can be efficiently automated already with little
programming knowledge.
- Exercise Introduction: I will introduce the first exercise assignment.
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
5
“Any sufficiently advanced technology
is indistinguishable from magic.”
- Arthur. C. Clarke
One of the “Big Three” of English Science Fiction (with Asimov and Heinlein)
2001: A Space Odyssey [1986]
2010: The Year We Make Contact [1984]
et al.
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
6
Card #2
Card #3
«Think of a number. Hand me all
cards that contain this number. I’ll tell
you your number in almost no-time.»
Card #1
Card #5
Card #4
Teams of 2, take 5 min
- Find the pattern
- Replicate the trick
- Build (in your mind) a “Magic Computer” for the numbers 0-13
Card #6
Questions
- Does the trick also work for the number 0 ?
- How many cards would be minimally required for a "Magic Computer" that works for the numbers 0-20 ?
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
8
Observations
(Not) handing over one of the cards is a binary decision
The volunteer makes six such decisions
Thereby, s/he encodes one out of 2^6 (= 64) states
That state is the number the volunteer was thinking of
The magician merely needs to sum up all top-left numbers
Examples
“4”
“7”
“63”
“0”
1
2
4
8
16
32
0
1
1
0
0
1
1
0
1
1
1
0
0
0
1
0
0
0
1
0
0
0
1
0
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
Card
#2
Card
#1
Card
#3
Card
#5
Card
#4
Card
#6
9
Today + Friday
Voilà! This is a Central
Processing Unit, or CPU!
Numbers
School and Assessment Math (Logic, Set Theory)
Information
Text
Context
Memory
Pixels / Colors
Instructions
Binary Digits
Instruction Set
Control Unit
Logic + Arithmetic
Arithmetic/Logic Unit
Electronic Components
(today: Transistors)
Logic Operations
General-Purpose Calculator!
(NOT, NOR, …, NAND)
Function Selector
Logic Gates
Adder Circuits
Circuits for other
arithmetic functions
Our Menu
Introduction and Administrative
Information Basics: Numbers, Bits, Bytes, Encodings
Computing Basics: Transistors and Circuits
Processor Basics
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
11
Stripping away the “Magic”
Goal: Understand the inner
workings of a computer system.
We’ll start with the simple (?)
concept of information and build
upon this foundation.
https://en.wikipedia.org/wiki/Pioneer_plaque
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
12
What‘s „Information“ in Computer Science
In CS, all Information = Bits + Context
Example: img.jpg
-> display as text (ANSI encoding)
-> display as hexadecimal numbers (in the demo, I use the HxD program)
-> display as binary code, i.e. as bits (https://mothereff.in/binary-ascii)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
13
Information in Computer Science
Interpret the bit string „11111111 00111111 11111111 00111111“ !
(this bit string is 4x8 = 32 bits long)
In CS, all Information = Bits + Context
Unsigned Decimal Number: 4‘282‘384‘191
Hexadecimal Number: FF 3F FF 3F
Floating-Point Number (IEEE 754): -2.5520786 x 10E38
Signed Decimal Number (2‘s Complement, 32 bits): -12583105
One pixel of an image (at 32bit color depth)
Text in ANSI encoding: ÿ?ÿ?
One pixel of one frame of a movie (at 32bit color depth)
A machine-level instruction in an x86 processor: e.g. addq %rax,%rbx
“Add the two numbers stored at locations %rax and
%rbx, and store the result in location %rbx.”
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
https://www.exploringbinary.com/twos-complement-converter/
https://www.h-schmidt.net/FloatConverter/IEEE754.html
https://mothereff.in/binary-ascii
14
Information in Computer Science
Interpret the bit string „11111111 00111111 11111111 00111111“ !
(this bit string is 4x8 = 32 bits long)
In CS, all Information = Bits + Context
Unsigned Decimal Number: 4‘282‘384‘191
Hexadecimal Number: FF 3F FF 3F
Floating-Point Number (IEEE 754): -2.5520786 x 10E38
Signed Decimal Number (2‘s Complement, 32 bits): -12583105
One pixel of an image (at 32bit color depth)
Text in ANSI encoding: ÿ?ÿ?
One pixel of one frame of a movie (at 32bit color depth)
A machine-level instruction in an x86 processor: e.g. addq %rax,%rbx
https://www.exploringbinary.com/twos-complement-converter/
https://www.h-schmidt.net/FloatConverter/IEEE754.html
https://mothereff.in/binary-ascii
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
15
Bits: Binary Digits
Logic: Basic representation of the logical values True and False
G.W. Leibniz: Using binary, arithmetic
and logic can be combined!
Practicality: Many easily feasible physical representations
-
Punched card:
Electrical switch:
SSD:
Fibre Optics:
Credit Card:
Bar Code:
Station Clock:
Hole vs. No Hole
On vs. Off
High vs. Low Electrical Charge
Light vs. No Light
Polarity of Magnetization
Thick Line vs. Narrow Line
Light vs. No Light
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
16
Bits: Binary Digits
At school, in 1966.
(courtesy of F. Mattern)
Here are all Binary Additions
0 2 + 02 = 0 2
1 2 + 02 = 1 2
0 2 + 12 = 1 2
12 + 12 = 102
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
17
Bits: Binary Digits
First Number (5 bits)
All Information = Bits + Context
Bits that represent three numbers…
Second Number (6 bits)
Third Number (6 bits)
In this context, that’s a clock!
What are the current hour,
minute, and second?
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
18
Bits: Binary Digits
i.e., in a base-10
number system!
A Decimal Clock…
Hour
Minute
Second
20
55
59
2 x 10^1 + 0 x 10^0
5 x 10^1 + 5 x 10^0
5 x 10^1 + 9 x 10^0
20 + 0
50 + 5
50 + 9
Hour = 20
Minute = 55
Second = 59
i.e., in a base-2
number system!
A Binary Clock…
Hour
Minute
Second
10100
110111
111011
1 x 2^4 + 0 x 2^3 + 1 x 2^2 + 0 x 2^1 + 0 x 2^0
1 x 2^5 + 1 x 2^4 + 0 x 2^3 + 1 x 2^2 + 1 x 2^1 + 1 x 2^0
1 x 2^5 + 1 x 2^4 + 1 x 2^3 + 0 x 2^2 + 1 x 2^1 + 1 x 2^0
Convention:
− We express decimals in the way we’re used to:
− Binary values get a leading ‘0b’:
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
16 + 0 + 4 + 0 + 0
32 + 16 + 0 + 4 + 2 + 1
32 + 16 + 8 + 0 + 2 + 1
20
0b10100
(i.e., decimal 20)
(i.e., decimal 20 as well)
19
Hexadecimal numbers work in the same way!
For reference
-
Decimal digit range:
Binary digit range:
Hexadecimal digit range:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]
i.e., in a base-16
number system!
A Hexadecimal Clock…
12 : 2A : 1F
Hour
Minute
Second
12
2A
1F
1 x 16^1 + 2 x 16^0
2 x 16^1 + 10 x 16^0
1 x 16^1 + 15 x 16^0
16 + 2
32 + 10
16 + 15
Why Hexadecimal Numbers?
-
Convenience!
Compact format!
Simple conversion!
Convention:
− We express decimals in the way we’re used to:
− Binary values get a leading ‘0b’:
− Hexadecimal values get a leading ‘0x’:
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
Hour = 18
Minute = 42
Second = 31
18
0b10010
0x12
(i.e., decimal 18)
(i.e., decimal 18 as well)
(i.e., decimal 18 as well)
20
A few notes (made via notepad during the lecture)
A few conversions…
-
Unraveling the decimal number “123”:
Unraveling the decimal number “321”:
Unraveling the decimal number “65536”:
Unraveling the binary number “1101b”:
1*10^2 + 2*10^1 + 3*10^0
3*10^2 + 2*10^1 + 1*10^0
6*10^4 + 5 *10^3 + 5*10^2 + 3*10^1 + 6*10^0
1*2^3 + 1*2^2 + 0*2^1 + 1*2^0
For any number system, the number “one-zero” in that system gives the basis of the system
-
Decimal:
Binary:
Hexadecimal:
A “10” is simply a decimal 10 (i.e., the basis of the system)
A “0b10” is simply a decimal 2 (i.e., the basis of the system)
A “0x10” is simply a decimal 16 (i.e., the basis of the system)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
21
Jokes?!
Actually, this should be 10b or 102 to signify that it’s binary!
Make sure you fully understand both
these jokes!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
22
Quick Exercises
What are the current hour,
minute, and second?
Represent the time 10:20:30 in binary!
What‘s the binary representation
of the largest hour (i.e., 23)?
What number range can be represented using n bits?
1 bit ?; 2 bits ?; 3 bits ?; n bits ?
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
23
Not Just Numbers: Encodings!
When we talk about data, we don’t talk about bits anymore, but about Bytes
-
1 Byte = historically, the number of bits used to encode a single character
Today de-facto standard of 8 bits in one Byte
8 bits permit encoding values from 0…255 (i.e., from 20 – 1 to 28 – 1)
Encodings
-
We’ve already seen that colors can be encoded using hexadecimal
In fact, anything can be represented in binary
Example: Windows-1252 Encoding
-
Typically given in hexadecimal
Most-used 8-bit encoding in the world
Example: Encoding of “©” in hexadecimal and in binary.
Example: Encoding emoticons. What happens when we send a smiley?
https://www.unicode.org/emoji/charts/full-emoji-list.html
https://www.gammon.com.au/unicode/ contains an interesting and easy to read historic account of encodings. Recommended!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
24
Our journey so far…
Voilà! This is a Central
Processing Unit, or CPU!
Numbers
School and Assessment Math (Logic, Set Theory)
Information
Text
Context
Memory
Pixels / Colors
Instructions
Binary Digits
Instruction Set
Control Unit
Logic + Arithmetic
Arithmetic/Logic Unit
Electronic Components
(today: Transistors)
Logic Operations
General-Purpose Calculator!
(NOT, NOR, …, NAND)
Function Selector
Logic Gates
Adder Circuits
Circuits for other
arithmetic functions
Any Questions / Comments /
Doubts / Concerns?
Take-Home
-
We can represent any information using bits, where the context is given by a standard
Floating-point Numbers:
IEEE 754-2019
Text (including exotic characters):
ISO 10646 „Unicode“
JPEG:
ISO 10918-1
145000 characters from 159 modern and ancient character sets
etc.
- You can also create your own mapping, but is this useful if you are the only person using it?
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
26
Today’s Menu
Introduction and Administrative
Information Basics: Numbers, Bits, Bytes, Encodings
Computing Basics: Transistors and Circuits
Processor Basics
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
27
Core Goal: Automatic Adding!
If we can add, we can also…
-
Subtract (that’s just adding a negative number)
Multiply (that’s just repeated adding)
Divide (that’s just multiplying with 1/number)
Draw roots (that’s just adding and dividing)
….
Do machine learning (that’s just combinations of the above)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
28
Computing on Bits: Transistors
Transistors are the key active components in practically all modern electronics. The
transistor is thus considered to be one of the greatest inventions of the 20th century.
Price, Robert W. (2004). Roadmap to Entrepreneurial Success
Why is it so useful in a computing context?
Because it can be used as an electronic switch!
Source
Gate
Gate
Source Drain
Global Production of
Transistors per Capita
Drain
Compare this to global car production!
(~ 2 per second)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
(~ 150bn per second in 2015)
https://www.darrinqualman.com/global-production-transistors/
29
„Economies of Scale“
https://spectrum.ieee.org/transistor-production-has-reached-astronomical-scales
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
30
Schematics of a Transistor
Source
Gate
Gate
Drain
Source
This is key!
What is it?
Drain
Base
Picture: https://en.wikipedia.org/wiki/Transistor
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
31
Now let‘s engineer a computer!
https://www.kickstarter.com/projects/babyengineering/computer-engineering-for-babies
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
32
Computing on Bits
George Boole (1815 – 1864): Boolean Algebra
NOT
AND
OR
XOR
~x => 0;
(x & y) => 0;
(x | y) => 1;
(x ^ y) => 1;
~y => 1;
(x & z) => 1;
(y | y) => 0;
(x ^ z) => 0;
Charles Sanders Pierce (1839 – 1914):
boolean x = 1
boolean y = 0
boolean z = 1
Logic Operations using Electronic Components
C. Pierce, letter to A. Marquand, 1886: Pierce suggests use of electrically
operated switches called relays to achieve “and” and “or” operations.
Back then: Relays!
“(…) it is by no means hopeless (…) to make a machine
for really very difficult mathematical problems.”
- Charles S. Pierce
Today: Transistors!
Good that we’re not dealing with electromechanics anymore ;-)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
33
Computing on Bits: Logic Gates
Symbols for Logic Gates
There is a voltage on the “output” Q
only if there is a voltage on both
“inputs” A and B.
Only if both inputs A and B are set to
TRUE, then output Q is set to TRUE.
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
34
Logic Gates and Truth Tables
OR Function: Q = (A OR B)
NAND Function: Q = (A NAND B)
AND Function: Q = (A AND B)
Input
Input
Output
Input
Input
Output
Input
Input
Output
A
B
Q
A
B
Q
A
B
Q
0
0
0
0
0
1
0
0
? 0
0
1
0
0
1
1
0
1
? 1
1
0
0
1
0
1
1
0
? 1
1
1
1
1
1
0
1
1
? 1
Q is the exact opposite
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
35
Computing on Bits: NAND
Henry Sheffer (1882 – 1964): Functional completeness of NAND
-
All operations over a binary number space can be represented using
combinations of gates that compute Not-AND („NAND“). This is equivalent to
saying that „all logic operations can be computed using combinations of NAND
gates“
-
Look here for the proofs: https://en.wikipedia.org/wiki/NAND_logic
A
Q
If a NAND gate receives the same
on both inputs, it computes a NOT
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
Input
Input
Output
A
B
Q
0
0
1
0
1
1
1
0
1
1
1
0
These do not matter (different inputs)
36
How to Build a NAND Gate?
(Line Voltage Power Supply, e.g. 5 Volts)
Q = A NAND B
A
So we need two transistors to
produce a NAND. We produce
billions of transistors per second.
We can compute lots of NANDs ;-)
B
(Ground)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
37
It‘s easier to see than you think!
(Line Voltage Power Supply, e.g. 5 Volts)
Case 1: Neither A nor B carry voltage (i.e., 0)
-
Q = A NAND B
A
Both T1 and T2 are open
Q is directly connected to the supply voltage
Q is 1
Case 2: Only A carries voltage
-
T1 is closed, T2 is open
Q is directly connected to the supply voltage
Q is 1
Case 3: Only B carries voltage
-
B
T1 is open, T2 is closed
Q is directly connected to the supply voltage
Q is 1
Case 4: Both A and B carry voltage
(Ground)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
-
Both T1 and T2 are closed
Supply voltage is connected to Ground
Q carries no voltage, i.e. Q is 0
38
That‘s what we wanted!!
Case 1: Neither A nor B carry voltage (i.e., 0)
-
Both T1 and T2 are open
Q is directly connected to the supply voltage
Q is 1
Case 2: Only A carries voltage
NAND Function: Q = (A NAND B)
-
Input
Input
Output
Case 3: Only B carries voltage
A
B
Q
0
0
1
0
1
1
1
0
1
1
1
0
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
-
T1 is closed, T2 is open
Q is directly connected to the supply voltage
Q is 1
T1 is open, T2 is closed
Q is directly connected to the supply voltage
Q is 1
Case 4: Both A and B carry voltage
-
Both T1 and T2 are closed
Supply voltage is connected to Ground
Q carries no voltage, i.e. Q is 0
39
Any Questions / Comments /
Doubts / Concerns?
Take-Home
- Through standard encodings, any symbol can be represented as a binary number
- We can (relatively easily and cheaply) create NAND gates
- Combinations of NAND gates can express all logic functions
- NAND gates can thus express all functions over a binary number space
- Any number can be represented as binary
Therefore, we have a universal calculator (or, “computer”)
At least in principle, we’ll build it next ;-)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
40
Our journey so far…
Voilà! This is a Central
Processing Unit, or CPU!
Numbers
School and Assessment Math (Logic, Set Theory)
Information
Text
Context
Memory
Pixels / Colors
Instructions
Binary Digits
Instruction Set
Control Unit
Logic + Arithmetic
Arithmetic/Logic Unit
Electronic Components
(today: Transistors)
Logic Operations
General-Purpose Calculator!
(NOT, NOR, …, NAND)
Function Selector
Logic Gates
Adder Circuits
Circuits for other
arithmetic functions
Remember: Logic Gates and Truth Tables
OR Function: Q = (A OR B)
NAND Function: Q = (A NAND B)
AND Function: Q = (A AND B)
Input
Input
Output
Input
Input
Output
Input
Input
Output
A
B
Q
A
B
Q
A
B
Q
0
0
0
0
0
1
0
0
0
0
1
0
0
1
1
0
1
1
1
0
0
1
0
1
1
0
1
1
1
1
1
1
0
1
1
1
Q is the exact opposite
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
42
Remember: Logic Gates and Truth Tables
XOR Function: Q = (A XOR B)
Input
Input
Output
A
B
Q
0
0
? 0
0
1
1
0
? 1
? 1
1
1
? 0
Exclusive Or: “Either A or B, but not both!”
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
43
Now let‘s build a Calculator!
You actually already know how to do this – however you need to transfer some knowledge from school.
How do we do pen-and-paper additions?
Carry = 0
6
+3
---9
«And 0 in
mind»
Carry = 1
6
+6
---12
«And 1 in
mind»
Carry = 02
02
+ 12
---12
«And 0 in
mind»
Carry = 12
12
+ 12
---1 02
«And 1 in
mind»
As we have seen, in binary, logic and arithmetic are equivalent.
So we should be able to perform this calculation using a circuit with only NAND gates.
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
44
Now let‘s build a Calculator!
And if we can add, we can also
-
Multiply (that’s just repeated adding)
Subtract (that’s just the «opposite» of adding)
Divide (that’s just the «opposite» of multiplying)
Draw roots (that’s just adding and dividing)
….
Do machine learning (that’s just combinations of the above)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
45
If a bit A and a bit B go in, then S should
hold the sum while C should hold the carry.
Now YOU build a Calculator!
Example: A=1 and B=1 should yield S=0 and C=1
A
S
B
C
The «1» is the
«carry» output
bit in 12 + 12
Carry = 12
12
+ 12
---1 02
«And 1 in mind»
The «0» is the «sum»
output bit in 12 + 12
5-min Teams of Two: What’s the meaning of «Sum» and «Carry» in binary?
-
You have two inputs, A and B, which are both binary digits
You have two outputs, S(um) and C(arry), which are both binary digits
1. Which value (0/1) does S need to assume if A/B assume the values 0/0, 0/1, 1/0, or 1/1?
2. With which logical expression can you express the operation S = A ?? B
3. With which logical expression can you express the operation C = A ?? B
4. Can you draw your calculator using the logic gate symbols?
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
46
Now YOU build a Calculator!
XOR
12
+ 12
---1 02
Carry = 12
AND
A so-called “Half-Adder” adds two bits and outputs the result.
- The Sum output should be 0 if both inputs are 0. It should also be 0 if an overflow occurs.
In other words, it should be 1 if either A or B is 1 (but not both). That’s an Exclusive OR!
- The Carry output should be 0 if no overflow occurs, i.e. if not both A and B are 1.
In other words, it should be 1 if both A and B are 1. That’s an AND!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
47
Now YOU build a Calculator!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
48
Do it yourself!
Buy transistors: https://www.jameco.com/z/2N3904BU-Major-Brands-Transistor-2N3904-NPN-General-Purpose-bulk-_38359.html
Build a simple 4-bit computer: https://www.youtube.com/watch?v=xISG4nGTQYE (from 4:30)
Here are some kits to get you started you: https://eater.net/8bit
1000000000x more
transistors
Or with relays
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
49
Let‘s build a larger Adder
First 4-bit number:
Second 4-bit number:
Result:
Carry-out:
A0 … A3
B0 … B3
S0 … S3
C4
https://www.jameco.com/z/74LS83-Major-Brands-4-Bit-Binary-Full-Adder-DIP-16_48063.html
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
50
Illustration: 01102 + 00102
A = 0010
0
0
1
B = 0110
0
1
1
0
0
Test yourselves: What are C1, C2, C3, C4, S0, S1, S2, S3
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
?
51
Let‘s build a LARGER Adder
First 8-bit number:
Second 8-bit number:
Result:
Carry-out:
What’s the physical size of this
circuit (i.e., in cm)?
a0 … a7
b0 … b7
s0 … s7
carry
1x 8-bit adder = 8x 1-bit adders that are connected properly
2x XOR + 2x AND + 1x OR per Full Adder 40 gates in total about 80 transistors in total
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
52
Thank you, Moore!
(in reference to Moore’s law)
This was 22 years ago ;-)
This was 9
years ago ;-)
https://www.latenightim.com/internet-marketing-electrons-and-the-meaning-of-life/
Today’s manufacturing is working with transistors of size
~25nm (incl. pitch). That’s 1/2000th of a human hair.
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
https://cogentlegal.com/2013/11/understand-spatial-relationships/
53
Let‘s build a LARGER Adder
8-bit adder
circuit
1x 8-bit adder = 8x 1-bit adders that are
connected properly
2x XOR + 2x AND + 1x OR per Full Adder 40
gates in total about 80 transistors in total
2000nm if chained in a straight line (+ wiring).
225nm * 225nm if arranged in a 2D grid („rectangle“).
125nm * 125nm * 125nm if arranged in a 3D grid („cube“)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
54
Thank you, Moore!
8-bit adder
circuit
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
55
Another Adder
We’ve been talking about electromechanical (relays) and electronic adders (transistors). Here’s a
mechanical adder (that uses just a small gravity trick ;-))
https://www.youtube.com/watch?time_continue=160&v=mrLxnY8Gtfw (from 0:18)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
56
Our journey so far…
Voilà! This is a Central
Processing Unit, or CPU!
Numbers
School and Assessment Math (Logic, Set Theory)
Information
Text
Context
Memory
Pixels / Colors
Instructions
Binary Digits
Instruction Set
Control Unit
Logic + Arithmetic
Arithmetic/Logic Unit
Electronic Components
(today: Transistors)
Logic Operations
General-Purpose Calculator!
(NOT, NOR, …, NAND)
Function Selector
Logic Gates
Adder Circuits
Circuits for other
arithmetic functions
Any Questions / Comments /
Doubts / Concerns?
Take-Home
- Through (smart) combinations of logic gates, we can build adding
circuits – and you did this!
- Through (smart) combinations of logic gates, we can also build
subtractors, multipliers, etc.
- These can be produced in mass quantities and at extremely
low prices and sizes
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
58
Today’s Menu
Introduction and Administrative
Information Basics: Numbers, Bits, Bytes, Encodings
Computing Basics: Transistors and Circuits
Processor Basics
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
59
From simple Circuits to a simple CPU
Computing is just mathematics (that’s what Leibniz said already!)
Simple idea, in principle…
-
Construct an adding circuit
Construct multiplier/subtractor/divisor/… circuits - let’s say we have 4 such units!
Add memory for inputs and outputs
Wire all these components together…
We also want to select which of these four functions to perform on the input!
Introduce two function select bits
-
00 -> Activate the add unit
01 -> Activate the multiply unit
10 -> Activate the subtract unit
11 -> Activate the divide unit
add
Output / Result
multiply
subtract
divide
Done! Here’s a general-purpose calculator!
“function select” bits = instructions in a “control unit”
Function Select
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
First 4-bit Number
Second 4-bit Number
60
From simple Circuits to a simple CPU
Computing is just mathematics (that’s what Leibniz said already!)
Simple idea, in principle…
-
Construct an adding circuit
Construct multiplier/subtractor/divisor/… circuits - let’s say we have 4 such units!
Add memory for inputs and outputs
Wire all these components together…
We also want to select which of these four functions to perform on the input!
Introduce two function select bits
-
00 -> Activate the add unit
01 -> Activate the multiply unit
10 -> Activate the subtract unit
11 -> Activate the divide unit
Output / Result
In modern processors, we call this an
add
multiply
Arithmetic
/ Logic
Unitsubtract
(ALU) divide
Done! Here’s a general-purpose calculator!
“function select” bits = instructions in a “control unit”
Function Select
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen
| School of Computer Science
First 4-bit Number
Second 4-bit Number
Putting Everything Together
Which instruction is
executed next?
Very small extremely fast memory (e.g.,
16x 64bit general purpose registers)
Persistent Storage
(e.g., SSD)
Data that the ALU can
directly compute on
Memory («RAM»)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
62
https://www.latenightim.com/internet-marketing-electrons-and-the-meaning-of-life/
https://cogentlegal.com/2013/11/understand-spatial-relationships/
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
63
Example: Intel 8008 (1972)
http://www.righto.com/2016/12/die-photos-and-analysis-of_24.html
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
64
Example: Intel Core i7 Haswell Refresh
(2014)
https://www.guru3d.com/articles-pages/core-i7-4790-processor-review,3.html
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
65
Example: Apple M1 System-on-Chip (2021)
CPU – Central
Processing Unit
GPU – Graphics
Processing Unit
NPU – Neural (Network)
Processing Unit
SLC – System-level
Cache
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
66
Let‘s design an extremely simple processor
Let’s examine this in greater detail with a made-up CPU!
Hardware features of our CPU
-
4 Memory Slots of size 4 bits each
2 Registers of size 4 bits each
An ALU that can subtract and add
An instruction set with 5 instructions
-
How many bits do we need to address four memory slots?
How many bits do we need to address two registers?
How many bits do we need to encode five instructions?
Load from memory address x (i.e., M[xx]) into register y (i.e., R[y]): 000 xx y
Store from R[x] into M[yy]:
001 x yy
Add the register contents and store the result in R[x]:
010 x
Subtract the contents of R[0] from R[1] and store the result in R[x]: 011 x
Insert the 4-bit number n into M[xx]
100 nnnn xx
Memory Address
Memory Content
00
Register
Address
Register
Content
01
0
10
1
11
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
67
Let‘s design an extremely simple processor
100 0111 00
Instruction Set
-
Load from M[xx] into R[y]:
Store from R[x] into M[yy]:
Add the register contents and store the result in R[x]:
Subtract the contents of R[0] from R[1] and store the result in R[x]:
Insert the 4-bit number n into M[xx]
000 xx y
001 x yy
010 x
011 x
100 nnnn xx
M[00]
7 = 0*8 + 1*4 + 1*2 + 1*1
// Machine-level Program that adds the numbers 7 and 9
Insert 7 into M[00]:
Insert 9 into M[01]:
Load from M[00] to R[0]:
Load from M[01] to R[1]:
Add registers into R[0]:
Store R[0] into M[10]:
100 0111 00
5-min
teamwork!
Register
Address
Register
Content
Memory Address
Memory Content
00
7
01
0
10
1
11
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
68
Let‘s design an extremely simple processor
Instruction Set
-
Load from M[xx] into R[y]:
Store from R[x] into M[yy]:
Add the register contents and store the result in R[x]:
Subtract the contents of R[0] from R[1] and store the result in R[x]:
Insert the 4-bit number n into M[xx]
// Machine-level Program that adds the numbers 7 and 9
Insert 7 into M[00]:
Insert 9 into M[01]:
Load from M[00] to R[0]:
Load from M[01] to R[1]:
Add registers into R[0]:
Store R[0] into M[10]:
100 0111 00
100 1001 01
000 00 0
000 01 1
010 0
001 0 10
Done!
000 xx y
001 x yy
010 x
011 x
100 nnnn xx
So this means that
programs themselves
are just numbers…
Note that the encoding of this machine-level program is unique!
We may omit the formatting and write the program as:
1000111001001001010000000000110100001010
Memory Address
Memory Content
00
7
Register
Address
Register
Content
01
9
0
7 16
10
16
1
9
11
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
69
A real instruction set
Our instruction set: 5
instructions (Load, Store,
Add, Subtract, Insert)
Intel x86-64 instruction
set: between 981 and 3683
instructions (depending on
what is counted as an
instruction…)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
70
Let‘s design an extremely simple processor
Instruction Set
-
Load from M[xx] into R[y]:
Store from R[x] into M[yy]:
Add the register contents and store the result in R[x]:
Subtract the contents of R[0] from R[1] and store the result in R[x]:
Insert the 4-bit number n into M[xx]
000 xx y
001 x yy
010 x
011 x
100 nnnn xx
What does this program do?
1000010001001000010000000000110110001000
Note that everything here is just binary. So we can use
our digital circuits to actually process all of this!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
71
Let‘s design an extremely simple processor
Instruction Set
-
Load from M[xx] into R[y]:
Store from R[x] into M[yy]:
Add the register contents and store the result in R[x]:
Subtract the contents of R[0] from R[1] and store the result in R[x]:
Insert the 4-bit number n into M[xx]
000 xx y
001 x yy
010 x
011 x
100 nnnn xx
What does this program do?
100001000 100100001 000000 000011 0110 001000
Note that everything here is just binary. So we can use
our digital circuits to actually process all of this!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
72
Let‘s design an extremely simple processor
Instruction Set
-
Load from M[xx] into R[y]:
Store from R[x] into M[yy]:
Add the register contents and store the result in R[x]:
Subtract the contents of R[0] from R[1] and store the result in R[x]:
Insert the 4-bit number n into M[xx]
Insert 2
Insert 8
Load M[00]
000 xx y
001 x yy
010 x
011 x
100 nnnn xx
Load M[01]
Subtract
Store
100001000 100100001 000000 000011 0110 001000
Memory
Address
Register
Address
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
Register
Content
Memory
Content
00
2 6
01
8
0
2 6
10
1
8
11
73
Even better…
Instruction Set
-
Load from M[xx] into R[y]:
Store from R[x] into M[yy]:
Add the register contents and store the result in R[x]:
Subtract the contents of R[0] from R[1] and store the result in R[x]:
Insert the 4-bit number n into M[xx]
000 xx y
001 x yy
010 x
011 x
100 nnnn xx
Could be Load/Store/Add/Subtract.
Let’s see the next bit.
Let’s read this code
one bit at a time…
Demo
0
0
0
0
1
1
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
Could be Load/Store.
Let’s see the next bit.
Ah, this must be Load. Expecting 3 more
bits to complete the instruction.
Aha! This is a Load from M[01] to R[1].
Done. Next Instruction please!
74
Putting Everything Together
How do these components together
“run” a computer program?
Very small extremely fast storage (e.g.,
16x 64bit general purpose registers)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
75
Putting Everything Together
The Memory Unit contains program data and program instructions
Remember that these are
also just Bits + Context!
The Central Processing Unit (CPU) runs in a loop. In each cycle:
1.
2.
3.
4.
5.
The control unit decides which instruction to process next and fetches that instruction from the memory unit
The CPU decodes the instruction to find out what data is required to execute it, and what should be done with
this data (e.g., add two numbers, increment a number, etc.)
The required data is loaded into the CPU registers
The Arithmetic/Logic Unit (ALU) computes the result of the instruction
The result is sent back and stored in the Memory Unit
How often this cycle is executed (per second) is determined by the CPU clock
What are typical CPU clock frequencies?
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
76
Our journey so far…
Voilà! This is a Central
Processing Unit, or CPU!
Numbers
School and Assessment Math (Logic, Set Theory)
Information
Text
Context
Memory
Pixels / Colors
Instructions
Binary Digits
Instruction Set
Control Unit
Logic + Arithmetic
Arithmetic/Logic Unit
Electronic Components
(today: Transistors)
Logic Operations
General-Purpose Calculator!
(NOT, NOR, …, NAND)
Function Selector
Logic Gates
Adder Circuits
Circuits for other
arithmetic functions
Any Questions / Comments /
Doubts / Concerns?
Take-Home
- We previously designed a circuit that can add
- We can selectively switch on that circuit, or other types of circuits
- The instructions to switch them on or off are given in binary as well
- These instructions are, ultimately, what the processor executes
- The processor then is combined with memory that holds instructions and data to work on
- It runs in a cycle to process all instructions it is supplied with
- In the final step, we‘ll how these instructions are generated from the high-level program code that you write
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
78
Putting Everything Together:
From Program to Processor
Interpret the bit string „11111111 00111111 11111111 00111111“ !
(this bit string is 4x8 = 32 bits long)
Demo
- High-level program code to assembly code: https://godbolt.org/
- Assembly code to bits: https://defuse.ca/online-x86-assembler.htm#disassembly
A machine-level instruction in an x86 processor: e.g. addq %rax,%rbx
“Add the two numbers stored at memory locations
%rax and %rbx and store the result in location %rbx.”
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
https://www.exploringbinary.com/twos-complement-converter/
https://www.h-schmidt.net/FloatConverter/IEEE754.html
https://mothereff.in/binary-ascii
79
Putting Everything Together
High-level Programming Language
(e.g., C, C#, Go, etc.)
Assembly Code (ASM)
long doubleMe(long number) {
long doubleNumber = number + number;
return doubleNumber;
}
push %rbp
mov %rbp, %rsp
mov QWORD PTR [%rbp-24], %rdi
mov %rax, QWORD PTR [%rbp-24]
add %rax, QWORD PTR [%rbp-24]
mov QWORD PTR [%rbp-8], %rax
mov %rax, QWORD PTR [%rbp-8]
leave
ret
Executable Machine Code
…represented as hex number.
…represented as
binary number.
0: 55
1: 48 89 e5
4: 48 89 7d e8
8: 48 8b 45 e8
c: 48 01 c0
f: 48 89 45 f8
13: 48 8b 45 f8
17: 5d
18: c3
554889E548897DE8488B45E84801C0488945F8488B45F85DC3
1010101010010001000100111100101010010001000100101111101111010000100100010001011010001011110100001001
000000000011100000001001000100010010100010111111000010010001000101101000101111110000101110111000011
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
80
i.e., each computer program is a binary number,
interpreted as executable instructions!
How many programs are there?
Leibniz would say: «Computer
programs are very large numbers!»
How many problems are there?
This implies that there are (very many) problems for which no algorithm exists to solve them.
Classic example: The “Halting Problem” https://en.wikipedia.org/wiki/Halting_problem
1010101010010001000100111100101010010001000100101111101111010000100100010001011010001011110100001001
000000000011100000001001000100010010100010111111000010010001000101101000101111110000101110111000011
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
81
Demo
-
Compiled languages use a
compiler to translate highlevel code to executable
program code. While
translating, the compiler
also validates the whole
program. Then, the program
code is run on a physical
machine.
High-level program code to assembly code: https://godbolt.org/
Assembly code to bits: https://defuse.ca/online-x86-assembler.htm#disassembly
long doubleMe(long number) {
long doubleNumber = number + number;
return doubleNumber;
}
push %rbp
mov %rbp, %rsp
mov QWORD PTR [%rbp-24], %rdi
mov %rax, QWORD PTR [%rbp-24]
add %rax, QWORD PTR [%rbp-24]
mov QWORD PTR [%rbp-8], %rax
mov %rax, QWORD PTR [%rbp-8]
leave
ret
0: 55
1: 48 89 e5
4: 48 89 7d e8
8: 48 8b 45 e8
c: 48 01 c0
f: 48 89 45 f8
13: 48 8b 45 f8
17: 5d
18: c3
554889E548897DE8488B45E84801C0488945F8488B45F85DC3
1010101010010001000100111100101010010001000100101111101111010000100100010001011010001011110100001001
000000000011100000001001000100010010100010111111000010010001000101101000101111110000101110111000011
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
82
https://medium.com/@astermanuelg/blurred-lines-is-ruby-an-interpreted-language-2d3d6bca3d37
Compiled languages use a
compiler to translate highlevel code to executable
program code. While
translating, the compiler
also validates the whole
program. Then, the program
code is run on a physical
machine.
This interpreter is what you
install when you install «Python»
on your computer!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
In interpreted languages, the high-level
code is executed line-by-line by an interpreter.
The interpreter itself is a compiled program
(often referred to as “Virtual Machine”) that in
turn runs on the physical machine.
https://www.youtube.com/watch?v=jeg1haA3Eis
Python and C++
Our program will calculate PI to a given precision using the Leibniz formula (https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80)
The more terms we use, the higher the precision of our calculation!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
85
Python and C++
Calculate PI to a given precision (terms) using the Leibniz formula
(https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
86
Python and C++ in Education
Jacques Wainer, Eduardo C. Xavier: A Controlled Experiment on Python vs C for an Introductory Programming Course:
Students' Outcomes. ACM Transactions on Computing Education 18 (3): 12:1-12:16 (2018)
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
87
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
88
I’m available for
Your Questions!
Fundamentals and Methods of Computer Science, 3,125 | University of St.Gallen | School of Computer Science
89
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )