CS 40 - Lecture 10

Cay S. Horstmann
How Computers Work
- Everything is a number (in binary)
- Small number of very primitive instructions (move, add, jump) ...
- ...executed very quickly
- That's all
- Computers aren't smart
Everything is a Number
- Letter A = 65, B = 66, C = 67, ... , d = 100, ...
= 38651
- Unicode defines about 100,000 characters (about 80% used for
Chinese/Japanese/Korean)
Everything is a Number
- pixel position = x, y values
- line = collection of pixels

(1,1) (2,1) (3,2) (4,2) (5,3) (6,3) (7,3) (8,4) (9,4) (10,5) (11,5)
Everything is a Number
- Color = 3 numbers (red, green, blue)

- Pink = 255 192 203
Binary Numbers
- Easier to build electric circuits with on/off than with 10 levels of
voltage
- Any number can be expressed in binary
- Instead of ones, tens, hundreds, thousands, etc, use
1, 2, 4, 8, 16, 32 (powers of two)
- Example: 101110 = 1·32 + 0·16
+1·8 +1·4 + 1·2 +
0·1 = 46
Lab: Binary Numbers

Lab: Binary Numbers
- Load Binary.a2w into Alice (link in
schedule)
- What is 111000 in decimal? Ask Cleopatra.
- Through trial and error, find the binary value for 44.
- Answers in ActiveLecture.org
Computer Architecture

Computer Architecture Terms
- CPU = central processing unit. Fetches program instructions, transfers
data, does arithmetic computations
- RAM = random access memory. Stores numbers. (Everything is a
number.)
- These are silicon chips. They stop working/storing when the power is
off.
- Hard disk also stores numbers, using tiny magnets. Storage needs no
power, but retrieval does.
Simplified Architecture

Architecture Details
- CPU has registers. Each register holds a single number.
- Modern CPU have 10 or more registers. Our simple computer has one,
called AX
- Each memory location holds a single number.
- Each memory location has an address (also a number): 0, 1, 2, 3,
...
- Modern computer memory holds a few billion numbers. Our simple computer
holds 100 (addresses 0 - 99).
- The program counter (PC) contains the address of the next
instruction
Architecture Details

Instructions
- Each computer has a set of instructions. Modern computer has hundreds.
Our simple computer has 13.
- Typical instructions:
- Move contents of memory location i to register
- Move register contents to memory location i
- Add contents of memory location i to register
- Jump to instruction at memory location i
- For human convenience, instructions have mnemonics. Ex.
ADC = add with carry
Everything is a Number
- Instructions are numbers

BE the Computer

- Simple computer program (starts at memory location 0)
161 7 45 1 127 2 0 3
BE the Computer
Lab 2: BE the Computer

Lab 2: BE the Computer
- ON PAPER, simulate this program.
- What values are in cells 97, 98, 99 when the program halts? ActiveLecture.org
- What does the program do? (I.e if you put a different number into cell
99 and ran it, what would it compute in general?) ActiveLecture.org
Lab 2: Run the Simulator
- Now run the simple computer simulator (link on schedule)
- Type in the same program
- What does the Single Step button do?
- What result do you get? ActiveLecture.org
Too Boring?

- Write a program that computes powers of two
- Cell 49 contains the number of powers that you should compute
- Put 1 2 4 8 ... into cells 50, 51, 52, 53...,
- If cell 49 contains 10, then you compute up to 210 in cell
59
- Run the simulator.
- Congratulations! You are an assembly programmer!
Food for Thought

- If computers are so dumb, how can they decide whether I get a loan?
- How can you decide anything with just adding, moving, and jumping?
- What else do our brains do?