lesson 2 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/lecture02.pdf · never do...
TRANSCRIPT
CS102:Introduction to Computer Science
LESSON 2:
© 2016 Sean CusackAll rights reserved.
SSH / SCP from Home
● As an aside... in order to ssh or scp when you'renot on the cooper LAN (or in the dorm), youneed to set up public/private keys
● NEVER DO THIS ON A COOPER COMPUTER● If you have a PC laptop:
http://the.earth.li/~sgtatham/putty/0.53b/htmldoc/Chapter8.html
– If you don't understand it, ask for help in thecomputer center, or from Prof Lent, or KeithNg
● If you have a Mac laptop...
● NEVER DO THIS ON A COOPER COMPUTER
● On your laptop's Terminal:– mkdir ~/.ssh – (if this fails because it already exists, skip to the cd command below)– chmod 0755 ~/.ssh– cd ~/.ssh– ssh-keygen -d rsa– (hit enter for the passphrase)– (this creates an id_pub.rsa file)
● NEVER DO THIS ON A COOPER COMPUTER
● NEVER DO THIS ON A COOPER COMPUTER
SSH / SCP from Mac
● NEVER DO THIS ON A COOPER COMPUTER
● You have to be on the Cooper LAN for this part:– scp id_pub.rsa [email protected]:~/.ssh/key– ssh -l username students.cooper.edu– cat ~/.ssh/key >> ~/.ssh/authorized_keys– chmod 0700 ~/.ssh/authorized_keys
● NEVER DO THIS ON A COOPER COMPUTER
● If you can't bring it to cooper (like if it's not a laptop), you can:– cp id_pub.rsa key– Then email laptopkey to yourself and retrieve it at Cooper– And push laptopkey to your students account from there, and then log on and do
the cat command
● NEVER DO THIS ON A COOPER COMPUTER
SSH / SCP from Mac
Today's Lecture
● Today, we will depart from purely navigating UNIX● Then delve a bit into several topics lightly, each of
which has its own full class here at Cooper, all aresuggested:
– Digital Logic Design (*especially recommended course)
– Computer Architecture
– Compilers
– Operating Systems
● History of Computers● Then write our own first program
Back to the Hardware
● There are several reasons why we're bouncing backand forth between UNIX and hardware
– UNIX is our intended work environment– UNIX is itself a kind of program, as we will learn later – Hardware (i.e. Physically how the computer functions) is
the underlying workings behind programming– The UNIX environment actually gives us a clearer view
of how things work than Windows– Slowly, we'll close the gap between them and meet in the
center
What is in a computer?
● What makes up a computer?● How do you “use a computer”?● How does it know exactly what to do?
Wired Yet?
● Computers are composed of wires and circuits.● Wires either have electricity going through them
or else they don't. ● This is called binary (bi means two).● When we count in the normal system we're used
to (called “decimal”, as deci means ten), weneed a new digit after 9.
● Thus, wires can count to 1. Zero, then one.
Seeing Double
● The digits in a decimal system “roll over” like an odometer in a car.Much like this, but with a limit of 1 for each digit, binary counts asfollows:0000 10000001 10010010 10100011 10110100 11000101 11010110 11100111 1111
Can I have just a nibble?
● Each “1 or 0” represents one “bit” (b) of information.
● 4 bits make up a “nybble”.
● Which is not quite as much as a “byte” (B), 8 bits.
● A kilobyte (KB) is either 1000 bytes or 1024 bytes.Why 1024? Why the discrepancy? What's yourguess?
● Likewise, a megabyte (MB) is 1000000 bytes or1048576 bytes.
Wire Cross Section
● ON OFF ON OFFOFF ON OFF OFF
● 1 0 1 0 0 1 0 0 = 128 + 32 + 4 = 160
Memory Games
● Great, a computer can send numbers, but what good does itdo if it can't remember one while it's working withanother? Imagine a calculator that could only deal withone number at a time. “2+”, “3x”, “5-”... Not very useful.
● Thus we have memory to keep this information safe for thetime being
● “Memory” in a computer is something that can rememberthe bits (the on and off pattern in a bunch of wires) andreplicate the pattern later.
Many Gates
● "NOR" gates in a "latch":
http://en.wikipedia.org/wiki/Flip-flop_(electronics)● Flood gates in a canal:
FULL CONTAINER EMPTY CONTAINER
CLOSED PIPELINE
Copy a "1"
1 0
What happens if we let water flow?
1 0
OFF OFF
OFF OFF
OFF
ON
Whoops, 1/2 isn't binary...
Copy a "1" (try again)
1 0
Need to turn on a "source"
1 0
11
Now the 1 was copied
Copy a "0"
0 1
Need to turn on a "sink"
0 1
00
Now the 0 was copied
Copy a "101"
Copy a "1" to multiple places
11
1
1
Copy a "1" to multiple places
1
0
1
1
1
1
Buses or Subways?
● The way that the computer gets thesebits from place to place is on a bus.Not a school bus, but a set ofparallel wires that carry the bits,that are shared by a lot ofcomponents in the computer.
● Think of these as canals with lots offloodgates along the path. Each isfull or empty.
● When the computer wants to move datafrom one place to another, it opensthe flood gates at the source anddestination, the canals fill upappropriately from the source, andreach the destination.
● When the flood gates are shut off, thedestination now has a copy of thememory.
Memory Copy Phase 1
● Here, note that thenumber 1 is storedin memory already.
Memory Copy Phase 2
● Here, the correctfloodgates areopened.
Memory Copy Phase 3
● Wires can only holdone value, on or off.
● So the bus takes onthe value of thesource.
● And thus, since it'sconnected, thedestination takes onthe value of the bus.
Memory Copy Phase 4
● When the flood gatesare shut off, wenow have a copy ofthe originalmemory.
More than just Transportation
● If that were all a computer could do, it would be useless. Ithas to be able to manipulate data, too.
● There has to be a way to add numbers, compare them, etc.
● That kind of operation is done by something called an ALU,the Arithmetic Logic Unit. It understands how to take thebinary representations of two numbers (as signals leadinginto it-- it's a computer chip itself), and put the result of anoperation on it out different leads.
Memory Copy Phase 5
● As we said earlier,you need twovalues to doanything useful, solet's make anothercopy of “01”...
● Open the gates...
Memory Copy Phase 6
● Let the water(current) flow...
Memory Copy Phase 7
● Finally, two numbersready and inposition...
Addition Phase 1
● And in one fellswoop, we haveadded one plus one.
● Was all that so hard?
Vicious Cycles
● All that took 8 tiny operations, called “clock cycles”
● Memory moving was in this case 3 steps, and arithmetic 1, but it candiffer from machine to machine. “Moving memory”, “adding”, andother kinds of things like that, which take just a few cycles each, arecalled “instructions”.
● So where's the clock and what's cycling?
● The clock is a chip that turns on and off, on and off, really fast. Whenyou say you have a “2 Giga-Hertz Intel Pentium 4”, you're sayingthat your clock turns on and off 2 billion times per second.
● Each clock “tick” tells the CPU (Central Processing Unit) to performthe next one of those tiny steps that's next in its list of things to do.The CPU is connected to the ALU, all the little flood gates, and ahundred other things you need not get into unless you want to takethe Computer Architecture course here.
CPU
CPU
Seeing the Steps
● Where does the CPU get its “next step” from?
● There's a list of instructions somewhere:
● The arrow is called the “program counter”, it means“this is the next thing to do”
● When the CPU is done, it moves it along to the nextthing and does that. If it can perform this operation100 million times per second, then the computer isconsidered to be able to handle 100 MIPS, or Millionsof Instructions Per Second
010000010100001010110110
Program Counter
CPU
1 1
1 0
0 1
Learning the Steps
● Let's say that the steps can be interpreted as follows:– The first 2 bytes are the instruction:
● 0100 means “move”● 1011 means “add and place result in memory location 3”
– The second two bytes are “arguments”, like in UNIX,except that they mean memory locations here:
● 00 and 01 in the context of “move” means “move thecontents of memory location 00 to the location 01”
● 01 and 10 in the context of “add” means “add the contentsof memory location 01 and the contents of location 10”
Dancing the Steps
● So the list we had before was the same as the “add inmemory demo” a few slides back:
● Let's say that we have exactly what we started withfrom the above, then:
● That seems like a lot of work to add 2 and 2...
010000010100001010110110
010011010100111010110110
One Guess
● The computer isn't smart enough to make this stuffup itself
● Once told to move memory around, it can manage● So who's the genius that needs to tell it this stuff?● One guess :)
Ancient Computers(before keyboards or screens)
● MITS ALTAIR 8800 COMPUTER
Ancient Computers(before screens)
● Teletype ASR33
Monkey See, Monkey Do
● When screens as we know them arrived, theprograms still emulated teletype machines
● Programs like ed and ex● One line at a time● And the cat program was like printing a file to
the printer (or teletype) machine
A new dimension
● It was a jump of logic to think of moving up anddown a page of information
● Bill Joy wrote “vi” in 1976● And we still use it today● But also pico and winword
History of Computers
● Initially "computer" was a job title● Computers predate electricity● Programming and technology have been an equal
endeavor of the sexes
Ada Lovelace
http://en.wikipedia.org/wiki/Ada_Lovelace● First programmer● In 1842● Worked with Charles Babbage● Documented work that he
never did● Programmed his machine,
proven correct a only acentury later
Charles Babbage
http://en.wikipedia.org/wiki/Charles_Babbage● Physical computer● The "difference engine"
https://www.youtube.com/watch?feature=player_detailpage&v=BlbQsKpq3Ak#t=927
Alan Turing
http://en.wikipedia.org/wiki/Alan_Turing● Cracked Nazi Enigma
encryption code● Father of computers today● Turing test / complete
Jerry Lawson
http://www.engadget.com/2015/02/20/jerry-lawson-game-pioneer/
● Mid 1970's: first programmable ROM
● Founder of Videosoft, which created cartridge-based video games (i.e. Atari 2600)
● Also designed a very early game himself:Demolition Derby
Women in Tech
● Harassment Policy
● Issues in the field
● https://www.usenix.org/blog/my-daughters-high-school-programming-teacher
● Imposter Syndrome
Onwards to Programming
● You're all going to be programmers now, and that's what programmersdo. They tell computer chips to turn on and off circuits.
● Back in the “good old days”, in order to tell the computer to add 1 and1, you had to tell it bit by bit. We're not in the dark ages anymore,and few can still read or write binary.
● Instead, there's a “language” that looks a little more familiar, it uses theEnglish alphabet instead of 10110001's. It's called “Assembler”.
● Assembler looks like a bunch of lines like:move a badd a b c
● Each line is one instruction, as previously defined, and each “a” or “b”is the name of a memory location.
● And there's a program called “assembler” that takes this stuff and turnsit into the binary that the computer can read.
Assembler Example
● So the “floodgates” example we had before nowbecomes something more readable:
● Is that easier to read?
010000010100001010110110010011010100111010110110
move location-1 ← location-0 move location-2 ← location-0
add location-3 ← location-1, location-2 move location-1 ← location-3move location-2 ← location-3
add location-3 ← location-1, location-2
● You may copy this from my directory to yours (for example, your lab-2directory):cp /home/c/cusack/prog.s ./cp /home/c/cusack/prog.sparc.s ./
● You could use cat or pico to look at it, but there's stuff in there we don'tneed to focus on, so for now, only stuff we want to see:
● pico ./prog.sparc.s...
mov 1,%o0mov %o0,%o1mov %o0,%o2add %o1,%o2,%o3
● Here's how we “assemble” it:gcc ./prog.s -o ./prog
● Here's how we “execute” it:./prog
Assembling a Program
Translation needed
● Computers can't read assembler
● They can only read binary
mov a badd a b c
010011010101100000001111
Assembler = Translator
● So you write in a language that's easier for you
● And tell a translator program to read that, andwrite a new file that's the binary equivalent
mov a badd a b c
010011010101100000001111
mov a badd a b c gcc
Retranslation
● Every time you make a change...
● You need to re-translate it, or the binary is nolonger a representation of your new program:
mov a badd a b csub x y z
010011010101100010101010
gcc
mov a badd a b csub x y z
Grace Hopper
http://en.wikipedia.org/wiki/Grace_Hopper● Wrote first compiler/assembler● Coined "bug"
● Copy this from my directory to yours (for example, your lab-2 directory):cp /home/c/cusack/hello.s ./
● Assemble it into a executable program and run it, it should print:helloworldi am alive!
● Now, edit the assembler file "hello.s" in pico such that when you assemble it(make the output file of gcc called "hello") and run it, it prints:<your name here>CS102 Fall 20XXSep XX, 20XXWhere <your name here> should be your name, etc. (Duh :-)
● You "assemble" the code "hello.s" into the executable "hello" just like Iturned "prog.s" into "prog"
Assembler Lab