All right. So it's such a beautiful spring
day, here, on campus. I'm gonna record
outside, here at Stamford campus. Just
kind of sunny, park like. So where college
students just come [inaudible]. So the
topic for this section is: What is
software? What is code? How is it that
something runs on a computer. So when we
talked about hardware in the computer we
talked about the cpu, that's the brains of
the thing. So the cpu's what actually does
running, and the cpu implements what is
called? Machine code instructions'. And
machine code instructions are extremely
simple. One machine code instruction might
add two numbers, another instruction might
compare two numbers to see which one is
bigger. So when. For code that we've seen,
so something like pixel set red dot ten.
That's much more complicated, than an
individual machine code instruction. So
the way that's going to work, is that
pixel set red dot ten. Will ultimately
expand to a series of maybe five or ten of
these very simple machine code
instructions. Such that, when those ten
instructions are run, one after the other,
it sorta has the effect of setting the red
value of the pixel to ten. Alright, and
I'll get into more detail later on about,
how that, how that expansion happens. So,
I think a good first question for running
a program is, what is a program? So I'll
look at the, the right hand side of this
diagram. So a program, and I'll, I'll sort
of use this as my running example,
Firefox. So a program is made actually of
just an enormous sequence of these very
simple machine code instructions. And so,
when Firefox is, is running on your
computer, that means somewhere in RAM,
there's a block of these instructions.
Such that running them, has, you know,
does the things that Firefox does. So it
blinks the cursor, it takes url, draws,
gets web pages and all that sort of stuff.
So. The individual instructions are really
trivial, right. Just add two numbers. So
how does it get from that to like blinking
the cursor? And the best I can say is it's
maybe sort of like the relationship
between sand and sculpture. That each
instruction is like a grain of sand. Like
by itself it's meaningless and it kind of
looks like all the others. But if you put
them together in just the right way, you
can build this complicated overall
structure. And so that is the way that
Firefox is built out of these, this these
simple instructions. So the way it works
is the CPU, over here on the left, runs
what is called a fetch execute cycle. And
all that means is that, the CPU will start
off, let's say here, with Instruction one,
and it'll load that instruction and run
it. Or sometimes we'll say it executes
that instruction. So, it, it adds the two
numbers, or it does whatever the
instruction says. And when it, it's done
with Instruction one, it just goes down
the list. It goes to Instruction two, and
it does that one. Instructions for you, it
does that one, it's only just ru-, it just
runs through the sequence. So, when we say
that a CPU operates at two gigahertz, two
billion operations per second, it's these
little instructions that, that refers to.
So there are, there's obviously a, a big
variety of instructions. But I'm just
gonna point out a couple special types. So
one type of instruction has the effect of
changing the order that the instruction.
Normally the CPU just runs down the list
and does them in order. But let?s say
instruction four maybe says, oh jump back
and start executing again at instruction
one. And so think about what the cpu's
gonna do. So it's gonna do instructions...
One, two, three, four. And then when it
gets to four, it'll sorta jump back, and
do instructions one, two, three again. And
then one, two, three again, and again. You
can sorta see that's how loops are
implemented. Just arranging the
instructions so that they'res some piece
of code we wanna do 500,000 times. Well,
you can set up an instruction to just loop
back and so, do those instructions again
and again. Another sort of instruction.
Our [inaudible] idea is an instruction,
well, the same instruction two here, which
tests some condition and if the condition
is true maybe it skips ahead where
instruction five would be here. So. That's
how if statements are implemented. You
have an instruction that's gonna look at
some condition, and if it's, if the
condition is true, it's gonna sort of tell
the CPU to go over here. And if it's
false, it'll go over to some other place.
So by arranging the instructions just so,
you can get the effect of, something that
we, that, in our code, looks like an if
statement. [sound]. Alrighty. So Well so
how, how does a program get running and
how do we get to, how do we get to this
thing. So I would imagine you've got,
Firefox on your flash drive, you know, the
file. So on your flash drive, or on your
hard drive. So here, I've stored, long
term, you know, persistent storage down
here. And here's RAM and here's the CPU.
So when you've got Firefox on your hard
drive. It's, it's a file, basically, and
here I, it's called Firefox exe, that's
just a windows convention, for, how to
name a file, which is a program, but it,
it helps keep things clearer so I'll
follow that. So firefox.exe, that file, it
has a lot of bytes in it, and for the most
part, those bytes, are just the
instructions, that make up the program,
plus some icons. [inaudible] and other
stuff. So what I want to think about is
well, what happens when you double click
Firefox.exc? How does it start running?
And basically what happens is there's two
steps. The first thing that happens is the
bytes for these instructions, at least
enough of the instructions to get started,
get copied up to RAM. So it just copies,
puts the instructions in RAM where the CPU
can get at them. And then step two, just
tell the CPU, 'Okay, well, here's
instruction one. Start executing here.'
And so then the CPU just starts ripping
down the series of instructions and now
it's running Firefox. So I think this
leads to the question of like, well who do
this right? How did, how did, who handles
the double click? Who gets Firefox
running? And so that's what an operating
system is. So the operating system is a
set of kind of supervisory and
administrative programs that sort of you
know, organize, organize the whole system.
So, in particular, the operating system
manages multiple programs, and starting
and, ending programs. So a modern computer
can, run multiple programs at the same
time and so the operating system sorta
keeps things organized. So it, it does the
initial startup of a program, giving it
some RAM to use, and maybe giving it a
window to draw in. Also, operating systems
try to keep programs isolated from each
other. So each program has its RAM, but it
can't necessarily just go mess with the
RAM of some other program. So that helps
if a program has bugs. Hopefully, it'll
just mess up that program, but not mess up
some other program that you're running. Or
maliciously, if I program was trying to do
something bad to some other program that,
the operating system tries to keep them
separate. So, the operating system is
really the first thing that runs when your
computer starts up. So that's what's gonna
put up those first windows, and maybe show
you what's on the hard drive. And then,
from thereafter, you can double click, or
do other things, and the operating system
will start those programs for you. So
that's really the, the thing you're
seeing, when you start up your laptop.
Digital camera, it, you don't think of it
as a computer, necessarily, but it's
really the same. When the digital camera
first starts up, there's probably a couple
programs. There's maybe the program that
takes pictures, and the program that lets
you look through the pictures you've
already taken. And so, when the camera
first starts out, there's a little bit of
administrative, sorta get, get the, the
programs running to sort of show you the
interface, and then it just lets you go.
Am. So just as a, a last thing, there's
sort of the, the, these, this terminology
boot and reboot, I always thought that was
kinda funny. Like, where does that come
from? And I-, it refers to the sort of
chicken egg problem of, like, well, when
the computer first turns on, when it first
starts up, how does it. How does it get
itself organized? How does it start
running a program? And this terminology
actually comes from sort of an old joke,
about if you want to get over a fence,
what you could just do is reach down to
your bootstraps, and pull up, and just
like lift yourself over the fence that
way. And it's obviously kind of absurd.
But it, it has the same quality of like,
well wait, what program does the computer
run to enable the computer to run
programs. So it sort of a chicken and egg
problem. So the way it works is that the
hardware, the, the CPU, has, when it
recognizes that it has, it was turned off,
and that it has, it has just started up.
And so there's a special tiny program that
is hardwired into the computer to run, at
that moment. And that program can maybe
check, check what's hardware area, you
know, do some basic early start-up stuff.
And then pretty much it looks around for a
hard disk or a flash drive or something
that contains an operating system on it.
And then it can start that operating
system and then, then the computer can
start up in that way. So that's why it's
called boot up. It refers to this old boot
strap idea. And then that's what rebooting
is. So reboot means just to sort of, we
want to get a clean slate so it's going to
shut down and start up fresh. All right.
So now you know.