Slide 1

Slide 1 text

How do Computers Even Work? Miami Ruby Brigade July 21, 2014

Slide 2

Slide 2 text

Bryce Kerley [email protected] Twitter: @bonzoesc

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

How Do Computers Even Work? • What is Abstraction? • Boolean Logic & Arithmetic • Control Units and Registers • RAM, caches, the memory hierarchy • Machine language, what bytecode is • Assemblers, compilers, interpreters

Slide 5

Slide 5 text

David Wheeler “All problems in computer science can be solved by another level of indirection, except of course for the problem of too many indirections.”

Slide 6

Slide 6 text

This stuff is cool, but you don’t need to think about it every day. Unless you do.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

How Do Computers Even Work? • What is Abstraction? • Boolean Logic & Arithmetic • Control Units and Registers • RAM, caches, the memory hierarchy • Machine language, what bytecode is • Assemblers, compilers, interpreters

Slide 9

Slide 9 text

Boolean Logic Algebra • 0 & 1, false & true, off & on • AND, OR, XOR, NOT • NAND, NOR

Slide 10

Slide 10 text

AND 0 1 0 0 0 1 0 1

Slide 11

Slide 11 text

OR 0 1 0 0 1 1 1 1

Slide 12

Slide 12 text

XOR 0 1 0 0 1 1 1 0

Slide 13

Slide 13 text

NAND 0 1 0 1 1 1 1 0

Slide 14

Slide 14 text

Boolean Logic • Send power to the ECU if the ignition is in “ON” OR “START” • Turn on the stairway lights if the bottom switch XOR the top switch are up • Trigger the hydraulic press if the left hand button AND the right hand button are pressed.

Slide 15

Slide 15 text

Half Adder Output 1 if the left input is 1 XOR the right input is 1 Carry 1 if the left input is 1 AND the right input is 1

Slide 16

Slide 16 text

Half Adder Inputs Outputs A B C S 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0

Slide 17

Slide 17 text

Full Adder

Slide 18

Slide 18 text

Ripple Carry Adder

Slide 19

Slide 19 text

Carry Lookahead Adder

Slide 20

Slide 20 text

64-bit Adder

Slide 21

Slide 21 text

Big Parts Are Made Of Small Parts

Slide 22

Slide 22 text

How Do Computers Even Work? • What is Abstraction? • Boolean Logic & Arithmetic • Logic Units and Registers • RAM, caches, the memory hierarchy • Machine language, what bytecode is • Assemblers, compilers, interpreters

Slide 23

Slide 23 text

Arithmetic & Logic Units • Boolean logic units: AND, OR, XOR, etc. • Integer Arithmetic: addition, multiplication, division • Floating-point Arithmetic: addition, multiplication, division

Slide 24

Slide 24 text

Full Adder

Slide 25

Slide 25 text

Full Adder • Inputs: • Operand (1-bit) • Operand (1-bit) • Carry Operand (1-bit) • Outputs: • Result (1-bit) • Carry Result (1- bit)

Slide 26

Slide 26 text

Arithmetic Logic Unit • Inputs: • Operand (n-bit) • Operand (n-bit) • Operator Picker
 • Outputs: • Result (n-bit)

Slide 27

Slide 27 text

Multiplexer Changes channels

Slide 28

Slide 28 text

Building an ALU • Get adders, multipliers, boolean units • Use a multiplexer to pick different operations

Slide 29

Slide 29 text

Building a Calculator • Buttons to enter operands • Buttons to pick an operation • Button to show results • Memory • Decimal-to-binary circuit

Slide 30

Slide 30 text

SR Latch A bit of memory

Slide 31

Slide 31 text

Memory is just a bunch of single-bit storage

Slide 32

Slide 32 text

Arithmetic Logic Unit • Inputs: • Operand (n-bit) • Operand (n-bit) • Operator Picker
 • Outputs: • Result (n-bit)

Slide 33

Slide 33 text

Operand (8-bit) Operand (8-bit) Operator Picker (8-bit) Destination (8-bit) Operation Operand Operand Destination 8 bits 8 bits 8 bits 8 bits Logic multiplexer Input A Input B Output Multiplexer

Slide 34

Slide 34 text

MIPS Instruction Format Copyright 2009 by Elsevier, Inc., All rights reserved. From Patterson and Hennessy, Computer Organization and Design, 4th ed.

Slide 35

Slide 35 text

It would be cool if we could chain up operations and store results somewhere

Slide 36

Slide 36 text

How Do Computers Even Work? • What is Abstraction? • Boolean Logic & Arithmetic • Logic Units and Registers • RAM, caches, the memory hierarchy • Machine language, what bytecode is • Assemblers, compilers, interpreters

Slide 37

Slide 37 text

Registers • Wired in right next to the logic units • Short wires mean fast • Short wires mean you can’t have very many • This computer has sixteen 64-bit registers

Slide 38

Slide 38 text

Random Access Memory • Ask for an address, get a value • Ask to put a value in to an address • Long wires mean slow and cheap • This computer has 8GB, or 1,073,741,824 registers worth

Slide 39

Slide 39 text

Memory Hierarchy • Register: keeping a number in your head while you do math on paper • RAM: keeping a number in a file cabinet in the other room while you do math on paper

Slide 40

Slide 40 text

Cache • Faster than memory, bigger than registers • Multiple levels, L1, L2, … • Lower levels faster but smaller • Cache invalidation

Slide 41

Slide 41 text

Memory Hierarchy • Register: keeping a number in your head • L1 Cache: jotting a number down on paper • L2 Cache: putting that paper in a pile • L3 Cache: putting that pile in a folder • RAM: a file cabinet in the other room

Slide 42

Slide 42 text

Virtual Memory Automatically moving file cabinets from the other room to a storage facility on the Moon (disk is slow, and huge)

Slide 43

Slide 43 text

How Do Computers Even Work? • What is Abstraction? • Boolean Logic & Arithmetic • Logic Units and Registers • RAM, caches, the memory hierarchy • Machine language, what bytecode is • Assemblers, compilers, interpreters

Slide 44

Slide 44 text

MIPS Instruction Format Copyright 2009 by Elsevier, Inc., All rights reserved. From Patterson and Hennessy, Computer Organization and Design, 4th ed.

Slide 45

Slide 45 text

Bytecode Bytes that are Code

Slide 46

Slide 46 text

> otool -t /bin/ls /bin/ls: (__TEXT,__text) section 00000001000010f8 55 48 89 e5 48 83 c7 68 48 83 c6 68 5d e9 50 35 0000000100001108 00 00 55 48 89 e5 48 8d 46 68 48 8d 77 68 48 89 0000000100001118 c7 5d e9 3b 35 00 00 55 48 89 e5 48 8b 4f 60 4c 0000000100001128 8b 41 30 48 8b 56 60 b8 01 00 00 00 4c 39 42 30 0000000100001138 7f 1a 7d 07 b8 ff ff ff ff eb 11 48 8b 49 38 48 0000000100001148 39 4a 38 7f 07 b8 ff ff ff ff 7d 02 5d c3 48 83 0000000100001158 c7 68 48 83 c6 68 5d e9 f6 34 00 00 55 48 89 e5 0000000100001168 48 89 f9 48 8b 51 60 4c 8b 42 30 48 8b 7e 60 b8 0000000100001178 ff ff ff ff 4c 39 47 30 7f 1a 7d 07 b8 01 00 00 0000000100001188 00 eb 11 48 8b 52 38 48 39 57 38 7f 07 b8 01 00 0000000100001198 00 00 7d 02 5d c3 48 83 c6 68 48 83 c1 68 48 89 00000001000011a8 f7 48 89 ce 5d e9 a8 34 00 00 55 48 89 e5 48 8b 00000001000011b8 4f 60 4c 8b 41 20 48 8b 56 60 b8 01 00 00 00 4c 00000001000011c8 39 42 20 7f 1a 7d 07 b8 ff ff ff ff eb 11 48 8b

Slide 47

Slide 47 text

"Altair 8800 at the Computer History Museum, cropped" by Todd Dailey - File:Altair 8800 at the Computer History Museum.jpg (cropped; curves; contrast). Licensed under Creative Commons Attribution-Share Alike 2.0 via Wikimedia Commons - http:/ /commons.wikimedia.org/wiki/ File:Altair_8800_at_the_Computer_History_Museum,_cropped.jpg#media viewer/File:Altair_8800_at_the_Computer_History_Museum,_cropped.jpg

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

> otool -tV /bin/ls /bin/ls: (__TEXT,__text) section 00000001000010f8 pushq %rbp 00000001000010f9 movq %rsp, %rbp 00000001000010fc addq $0x68, %rdi 0000000100001100 addq $0x68, %rsi 0000000100001104 popq %rbp 0000000100001105 jmpq 0x10000465a 000000010000110a pushq %rbp 000000010000110b movq %rsp, %rbp 000000010000110e leaq 0x68(%rsi), %rax 0000000100001112 leaq 0x68(%rdi), %rsi 0000000100001116 movq %rax, %rdi 0000000100001119 popq %rbp 000000010000111a jmpq 0x10000465a 000000010000111f pushq %rbp 0000000100001120 movq %rsp, %rbp

Slide 51

Slide 51 text

How Do Computers Even Work? • What is Abstraction? • Boolean Logic & Arithmetic • Logic Units and Registers • RAM, caches, the memory hierarchy • Machine language, what bytecode is • Assemblers, compilers, interpreters

Slide 52

Slide 52 text

> otool -t /bin/ls /bin/ls: (__TEXT,__text) section 00000001000010f8 55 48 89 e5 48 83 c7 68 48 83 c6 68 5d e9 50 35 0000000100001108 00 00 55 48 89 e5 48 8d 46 68 48 8d 77 68 48 89 0000000100001118 c7 5d e9 3b 35 00 00 55 48 89 e5 48 8b 4f 60 4c 0000000100001128 8b 41 30 48 8b 56 60 b8 01 00 00 00 4c 39 42 30 0000000100001138 7f 1a 7d 07 b8 ff ff ff ff eb 11 48 8b 49 38 48 0000000100001148 39 4a 38 7f 07 b8 ff ff ff ff 7d 02 5d c3 48 83 0000000100001158 c7 68 48 83 c6 68 5d e9 f6 34 00 00 55 48 89 e5 0000000100001168 48 89 f9 48 8b 51 60 4c 8b 42 30 48 8b 7e 60 b8 0000000100001178 ff ff ff ff 4c 39 47 30 7f 1a 7d 07 b8 01 00 00 0000000100001188 00 eb 11 48 8b 52 38 48 39 57 38 7f 07 b8 01 00 0000000100001198 00 00 7d 02 5d c3 48 83 c6 68 48 83 c1 68 48 89 00000001000011a8 f7 48 89 ce 5d e9 a8 34 00 00 55 48 89 e5 48 8b 00000001000011b8 4f 60 4c 8b 41 20 48 8b 56 60 b8 01 00 00 00 4c 00000001000011c8 39 42 20 7f 1a 7d 07 b8 ff ff ff ff eb 11 48 8b

Slide 53

Slide 53 text

> otool -tV /bin/ls /bin/ls: (__TEXT,__text) section 00000001000010f8 pushq %rbp 00000001000010f9 movq %rsp, %rbp 00000001000010fc addq $0x68, %rdi 0000000100001100 addq $0x68, %rsi 0000000100001104 popq %rbp 0000000100001105 jmpq 0x10000465a 000000010000110a pushq %rbp 000000010000110b movq %rsp, %rbp 000000010000110e leaq 0x68(%rsi), %rax 0000000100001112 leaq 0x68(%rdi), %rsi 0000000100001116 movq %rax, %rdi 0000000100001119 popq %rbp 000000010000111a jmpq 0x10000465a 000000010000111f pushq %rbp 0000000100001120 movq %rsp, %rbp

Slide 54

Slide 54 text

• Tapping instructions into memory from switches • Writing bytecode to tape or disk, and loading it • Writing machine code, and assembling it • Programming in higher-level languages

Slide 55

Slide 55 text

(0) INPUT INVENTORY FILE-A PRICE FILE-B ; OUTPUT PRICED-INV FILE-C UNPRICED-INV FILE-D ; HSP D . (1) COMPARE PRODUCT-NO (A) WITH PRODUCT-NO (B) ; IF GREATER GO TO OPERATION 10 ; IF EQUAL GO TO OPERATION 5 ; OTHERWISE GO TO OPERATION 2 . (2) TRANSFER A TO D . (3) WRITE-ITEM D . (4) JUMP TO OPERATION 8 . (5) TRANSFER A TO C . (6) MOVE UNIT-PRICE (B) TO UNIT-PRICE (C) . (7) WRITE-ITEM C . (8) READ-ITEM A ; IF END OF DATA GO TO OPERATION 14 . (9) JUMP TO OPERATION 1 . (10) READ ITEM B ; IF END OF DATA GO TO OPERATION 12 . (11) JUMP TO OPERATION 1 . (12) SET OPERATION 9 TO GO TO OPERATION 2 . (13) JUMP TO OPERATION 2 . (14) TEST PRODUCT-NO (B) AGAINST ZZZZZZZZZZZZ ; IF EQUAL GO TO OPERATION 16 ; OTHERWISE GO TO OPERATION 15 . (15) REWIND B . (16) CLOSE-OUT FILES C ; D . (17) STOP . (END)

Slide 56

Slide 56 text

static void __init setup_command_line(char *command_line) { saved_command_line = memblock_virt_alloc(strlen(boot_command_line) + 1, 0); initcall_command_line = memblock_virt_alloc(strlen(boot_command_line) + 1, 0); static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0); strcpy (saved_command_line, boot_command_line); strcpy (static_command_line, command_line); }

Slide 57

Slide 57 text

(defun ruby-electric--block-beg-keyword-at-point-p () "Test if there is a block beginning keyword at point." (and (looking-at ruby-block-beg-symbol-re) (if (string= (match-string 1) "do") (looking-back "\\s-") (not (looking-back “\\."))) ))

Slide 58

Slide 58 text

Language A Language B Compiler

Slide 59

Slide 59 text

• Compilers: translate code between languages • Assemblers: translate assembly language into bytecode • Interpreters: run code piece by piece

Slide 60

Slide 60 text

Why Compile • Source might be valuable • Parsing can be expensive • Optimizing is hard for humans

Slide 61

Slide 61 text

Why Interpret • Source might not be valuable • Speed might not be a big deal • Optimizing might be data-dependent

Slide 62

Slide 62 text

How Do Computers Even Work? • What is Abstraction? • Boolean Logic & Arithmetic • Logic Units and Registers • RAM, caches, the memory hierarchy • Machine language, what bytecode is • Assemblers, compilers, interpreters

Slide 63

Slide 63 text

Computers are cool

Slide 64

Slide 64 text

Hide Complexity

Slide 65

Slide 65 text

Some More Reading • Take a computer architecture class • “Inside the Machine” by Jon Stokes • “Computer Organization and Design” by Patterson & Hennessy • Wikipedia • http:/ /j.mp/howdocomputers

Slide 66

Slide 66 text

Thanks