CS 101

Introduction to Computers

reading: Syllabus

Plan for Today

  • Course Overview
  • Computer Code
  • Computer Hardware

Course Goals

  • Demystifying Technology
  • Technical Literacy
  • Communication with Software Engineers

Course Overview

  • Computer Hardware (~3 lectures)
  • Introduction to Code (~5 lectures)
  • Artificial Intelligence (~1 lecture)
  • The Internet and the Web(~4 lectures)
  • Human-Computer Interaction (~1 lecture)
  • Security and Privacy (~2 lectures)
  • Big Questions in Computer Science (~1 lecture)

Course Staff

Ashley Taylor
ataylor4@stanford.edu
OH: T/Th 4:20-5:30PM
Gates 323
Shreya Shankar
shreya@cs.stanford.edu
OH: Weds 1-3PM
Basement of Huang

Course Policies: Grade Breakdown

figure

Course Policies: HW

  • Weekly homework, out Tuesday/Thursday, due following Wednesday
  • Combination of written questions and code exercises
  • Two free 24-hour late days (pre-approved extensions)
    • Each late day covers 1 second to 24 hours late
  • Submit on Canvas
  • Short response paper on Artificial Intelligence
    • released week 5, due Friday, November 16

Course Policies: Honor Code

  • Refer to the Stanford Honor Code
  • All work must be in your own words.
  • You may reference outside sources, but include a citation (e.g. URL link) at the end of your answer.
  • The purpose of the homeworks is to assess your understanding of concepts; you must demonstrate that understanding to get credit.

Course Policies: Exams

Midterm

  • Tuesday, October 30 in class
  • Covers first five weeks of lecture
  • Closed note, closed book, provided reference sheet

Final

  • Monday, December 10, 8:30-11:30AM
  • Covers the entire quarter; emphasis on material since the midterm
  • Closed note, closed book, provided reference sheet

No alternate exams will be offered except for OAE and athletic conflicts.

Announcements

  • Office hours will end a little early today (5:15PM); no OH on Thursday
  • I will be at a conference for the rest of this week, so Thursday's lecture will be taught by Shreya
  • Email Shreya with questions if you need a response before Sunday

Introduction to Computers

Abstraction

Applications

  • Any program you run on your computer
    • Microsoft Word
    • Google Chrome
    • Note application
  • .exe (Windows) or .app (Mac) file
  • Can use without knowing how it works behind the scenes (abstraction)
  • Programs are written in code, the next layer of abstraction

Introduction to Code

Strengths of Computers

  • Very fast (billions of operations per second)
  • Very accurate

Weaknesses of Computers

  • Literal
  • Only simple operations
    • Addition
    • Comparison
    • Equality
  • No insight

Introduction to Code:
How Computers Work

figure
  • Code is a series of instructions
  • Each instruction is an operation
  • Computer follows each instruction, one after another
  • Programs are comprised of millions of instructions

Introduction to Code:
The Programmer

figure
  • Computers are hugely useful
    • Messaging
    • Image editing
  • Programmers convert human ingenuity into computer code
  • Develop algorithms (which we'll see later in the course!)

Introduction to Code:
Tips and Previews

  • Code is like Legos - small bricks to big structures
  • Patience :)
  • Computers do exactly what you tell them
    (no interpretive power)
  • We'll be doing green screen soon!

Introduction to Code:
First Code Operation

  • Using Javascript (plus some extensions for 101)
  • Each line of code is small
  • First line: print!

code1-1

 

Introduction to Code: Bugs

Story: named after a moth

Two types: syntax and logic errors


 

Introduction to Code: ASCII Art

Source: Wikipedia

We can use print statements to make art








 

Note: "\" is an escape character, so use "\\" if you want to print a slash

Programming Languages

  • Code is written in a programming language
  • Gives the "rules" (syntax and commands) for the code
  • What programming languages have you heard of?
  • Each has strengths and weaknesses and specializations (just like human languages)
  • High-level (closer to how humans think) vs. low-level (closer to computers, but much faster)

Types of Programming Languages

  • Compiled
    • "Source code" (what the programmer writes) is converted to "object code" (what the computer reads)
    • Compiled once, run many times (like translating a book to another language)
  • Interpreted
    • Machine code instructions generated on the fly (like acting as a verbal translator in a conversation)
    • Slower to run, but no compile step!

Programming Languages: Source Code

  • Source code is editable (what the programmer writes)
  • .exe/.app files are just machine code
  • Can't change a program without the source code!
  • License is to use the software, not change it

Open Source in Theory

  • Idea: give everyone the source code!
  • No more licenses (free to use)
  • Examples:
    • Programming languages
    • Browsers
    • Operating Systems
    • Compilers
  • Championed by Richard Stallman
    • Free Software Foundation
    • Emacs, compiler, and more
    • Led to Linux

Open Source in Practice

  • Give out source code (Github)
  • Users have a right to modify the source code
  • Users are incentivized to contribute back with changes
  • Results:
    • Not a competitive advantage
    • Higher quality
    • Better use of resources

The Operating System

  • Name types of operating systems?
  • Starts running when the computer "boots up"
  • Manager/supervisor
    • Starting/runs/stopping programs
    • Manages all shared resources (like memory)
    • No matter how a program ends, all resources must be released
  • Can run multiple programs at once ("sandboxing")
  • Allows a computer to change over time (updates)
  • Other responsibilities:
    • File system
    • Manages windows
    • Some basic programs

Lifecycle of a program

  • Load the program file from the file system
  • Each program gets its own memory
    • Program's code
    • Data the program manipulates (like file being edited)
  • OS "fetches" an instruction, which is then run (OS alternates instructions between programs)
    • Begins with the first line of code
    • Instructions like "return to step 5" keep the program running
  • At the end of the program, OS needs to stop fetching instructions and ensure program returns memory

Ending programs

  • Normal exit: user quits the program (e.g. by pressing "X")
  • Abnormal exit: Program "not responding"
    • Use Activity Monitor (Mac)/Task Manager (Windows)
  • "Illegal" activity: A program tries to access the memory of another program
  • Out of Memory: A program requests more memory (e.g. you edit a lot of videos at once)

OS: Reboot

  • Why does this fix anything?
  • Sometimes some operating-system managed memory is not quite right
    • a bug in the operating system, or perhaps a hardware error
  • A reboot wipes all the memory
  • Starts up the operating system fresh
  • Theoretically, never necessary (but in practice, a good approach)

Architecture: 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
  • Because of how hardware works (see Thursday's lecture), machine code is written in binary (1s and 0s)
  • Machine-specific

Architecture: Assembly

mov    %esp,%ebp
sub    $0x28,%esp
mov    0x804d300,%eax
add    $0x1,%eax
mov    %eax,0x804d300
mov    0x804d300,%eax
cmp    0x8(%ebp),%eax
  • Example
  • Assembly is converted to machine code
  • %esp, %ebp, etc. are in memory

Conclusion

figure