CS 101
Hardware
Lecture by Shreya Shankar, TA
Reminder from last time
What happens when we press run?
Reminder: Machine Code
Architecture refers to the instructions sent by software that hardware can understand.
- Reminder: computer runs code instructions
- Each line of machine code is one operation
- Javascript is higher-level language
- Have to translate Javascript to machine code
- Computer runs the machine code (binary -- more on this today)
- Machine-specific
Reminder: Assembly
mov %esp,%ebp
sub $0x28,%esp
mov 0x804d300,%eax
add $0x1,%eax
mov %eax,0x804d300
mov 0x804d300,%eax
cmp 0x8(%ebp),%eax
- Assembly is converted to machine code
- %esp, %ebp, etc. are in temporary storage
Programs and Applications
- Millions of lines of machine code per program
- Firefox.exe is about 20 million machine code instructions
- All the machine code is in the .exe (Windows) or .app (Mac) file
Machine code
- What is this machine code we keep speaking of?
- Humans write programs that are human-readable. A machine needs code that is machine-readable (aka binary)
- Our code, like Javascript, is compiled and interpreted to binary (1s and 0s) instructions that machines can understand
- Something in the computer must take and understand these instructions...what component is it?
CPU
- "Brain" of a computer
- Receives input data and performs the binary instructions given to it
- Communicates with I/O (input-output) devices, such as:
- Contains registers that house information, and performs operations (i.e. assembly instructions) on values in these registers
Transistors
- Companies thrive off selling CPUs, which consist of billions of tiny transistors on a computer chip
- Transistors are tiny switches that are triggered by electrical signals. Like a switch, but instead of connecting or disconnecting, transistors conduct or insulate.
- Transistors make arithmetic calculations on the chip possible
- These days, companies are trying to make them smaller and smaller (more profit)
- Moore's law: the number of transistors on a chip will double every two years -- has been true from 1965 to 2015
- Popular CPU makers: Intel, AMD, Apple, NVIDIA, Qualcomm
- Is having a fast CPU enough to have a fast computer?
Memory
- CPUs must get their instructions and data from somewhere
- Memory: computer hardware components that store data
- Two types of memory:
- RAM (random access memory)
- Hard drive (also known as disk)
- Any program you are currently running (in foreground or background) has memory stored in RAM. CPU gets data directly from RAM.
RAM
- Anything you are running is stored in RAM
- Very easily accessible (can find something in RAM quickly)
- When computer is turned off (or power cut), everything in RAM is wiped
- (This is why you should save documents before shutting down!)
Hard Drive
- Slow to access memory in hard drive
- When you save a file, it is saved to "disk" or stored in the hard drive
- Typically lots of storage (256GB to 1TB) compared to RAM (4 GB to 16 GB)
What about "flash storage?"
- Flash storage is high-speed memory that doesn't require power like RAM (also known as SSD, or solid state drive)
- Fast compared to traditional hard drives, which rely on spinning disks, motors, and magnetism
- Drawbacks of flash storage: very pricey, rewrite limitations
Circuits
- Basically, hardware is circuits
- Circuits are composed many digital logic gates
- Digital logic gates can take in multiple inputs and redirect to one output
- Logic gates are made up of transistors
AND Gate
- Takes in 2 inputs, each either 0 or 1
- Outputs 1 only if both inputs are 1
OR Gate
- Takes in 2 inputs, each either 0 or 1
- Outputs 1 only if at least input is 1
XOR Gate
- Takes in 2 inputs, each either 0 or 1
- Outputs 1 only if both inputs are 1 OR both inputs are 0
- Stands for "exclusive or"
NOT Gate
- Takes in 1 input, each either 0 or 1
- Outputs the "flipped" input
- ~1 is 0; ~0 is 1
The Operating System: Full Picture
- Name types of operating systems?
- Starts running when the computer "boots up"
- Manager/supervisor
- Starting/stopping programs
- Manages RAM, persistent storage, and other shared resources between programs
- Can run multiple programs at once ("sandboxing")
- Allows a computer to change over time (updates)
- Other responsibilities:
- File system
- Manages windows
- Some basic programs
OS: Starting a Program: Full Picture
- Load the program file from the file system (persistent storage)
- Each program gets its own RAM
- Program's machine code
- Data the program manipulates (like file being edited)
- Copy machine code from persistent storage to RAM
OS: Running programs: Full Picture
- CPU "fetch/execute cycle"
- Fetch one instruction
- Execute (run) that instruction
- Repeat
- Machine code is loaded into RAM
- CPU begins at the top
- Instructions like "return to step 5" keep the program running
OS: Program runs and exits normally: Full Picture
- Operating system starts and stops programs
- Each program has its own separate area in RAM: its instructions + data
- CPU "round robin"
- Persistent storage is organized as a file system, programs can read and write data here
- User quits the program
- Operating system reclaims shared resources
OS: Abnormal Exit: Full Picture
- Program "not responding"
- The operating system stops running that program - involuntary vs. normal-exit
- The operating system reclaims the program's area of RAM
- Use Activity Monitor (Mac)/Task Manager (Windows)
OS: Running Out Of Memory: Full Picture
- A program requests more RAM from the operating system
e.g. to hold an image, but there's not enough RAM available
- The operating system refuses the request, the program gives an error message
OS: Memory Access Error: Full Picture
- A program tries to access the memory of another program
- Maybe because of a bug (common)
- Maybe on purpose because it is malware
- The operating system blocks the access (ideally)
- Maybe kills the offending program too
OS: Reboot: Full Picture
- Why does this fix anything?
- Sometimes some operating-system managed bytes in RAM is not quite right
- a bug in the operating system, or perhaps a hardware error
- A reboot wipes all the data from RAM
- Starts up the operating system fresh
- This should never be necessary