WEBVTT
Kind: captions
Language: en

1
00:00:00.120 --> 00:00:03.199
Every modern computer, when you get
right down to the bare metal,

2
00:00:03.199 --> 00:00:06.010
is doing basically the same sort of thing.

3
00:00:06.010 --> 00:00:09.210
I’ve said before that computers are just
overgrown calculators,

4
00:00:09.210 --> 00:00:13.710
but how do you go from a simple calculator
to playing video games,

5
00:00:13.710 --> 00:00:14.839
sending stuff over the internet,

6
00:00:14.839 --> 00:00:18.650
or even decompressing and displaying
the millions of pixels in this video?

7
00:00:18.650 --> 00:00:23.020
In short, what’s your computer
actually doing?

8
00:00:25.460 --> 00:00:28.110
Behind me is a scaled-up version of a computer,

9
00:00:28.110 --> 00:00:30.420
but we’re going to go much, much simpler.

10
00:00:30.420 --> 00:00:33.039
If you take apart your phone or PC,

11
00:00:33.039 --> 00:00:37.000
somewhere in the heart of it will a
Central Processing Unit, or CPU,

12
00:00:37.000 --> 00:00:39.600
connected to all the other devices
that make it work.

13
00:00:39.600 --> 00:00:43.300
Now, to show a really basic example,
we’re not going to use all those devices.

14
00:00:43.300 --> 00:00:45.909
The first one we are going to use
is the clock.

15
00:00:45.909 --> 00:00:46.909
With every tick of the clock,

16
00:00:46.909 --> 00:00:51.070
our CPU goes through a step in what’s called
the “Fetch-Execute” cycle,

17
00:00:51.070 --> 00:00:52.989
or “Fetch-Decode-Execute”.

18
00:00:52.989 --> 00:00:54.100
This clock is slightly magic,

19
00:00:54.100 --> 00:00:57.540
in that it ticks (click) every time (click)
I click my fingers. (click)

20
00:00:57.980 --> 00:00:58.660
(click)

21
00:00:58.660 --> 00:01:01.100
In the CPU I’m going to have three registers.

22
00:01:01.100 --> 00:01:05.540
These are bits of fast storage where the CPU
holds values that it’s working on right now.

23
00:01:05.540 --> 00:01:08.250
These are: a register that keeps track of
our instruction cycle,

24
00:01:08.250 --> 00:01:10.180
another that loads our instructions from memory,

25
00:01:10.180 --> 00:01:12.250
and an Accumulator.

26
00:01:12.250 --> 00:01:16.010
The final thing we need in our simplified
computer is somewhere to keep the instructions

27
00:01:16.010 --> 00:01:18.140
and any values that we end up calculating.

28
00:01:18.140 --> 00:01:20.620
That is RAM, Random Access Memory.

29
00:01:20.620 --> 00:01:24.540
We call it Random Access because it
doesn't matter when or in what order

30
00:01:24.540 --> 00:01:27.210
the information is read or written.

31
00:01:27.210 --> 00:01:29.370
So: that is our computer.

32
00:01:29.370 --> 00:01:31.130
Let’s run a simple program.

33
00:01:31.130 --> 00:01:33.100
All it’s going to do is count up.

34
00:01:33.100 --> 00:01:35.470
The processor has three steps:
Fetch, Decode, Execute.

35
00:01:35.470 --> 00:01:37.360
It will just repeat those on a loop,

36
00:01:37.360 --> 00:01:40.180
that’s the one thing that’s
actually built into it.

37
00:01:40.180 --> 00:01:42.960
So we need some instructions,
actually in memory,

38
00:01:42.970 --> 00:01:45.090
so let’s load our program into RAM.

39
00:01:45.090 --> 00:01:48.390
The RAM is also used to store our answers, 
our outputs.

40
00:01:48.390 --> 00:01:50.950
In the real world,
these would all be stored in binary,

41
00:01:50.950 --> 00:01:55.660
but let’s not overcomplicate things right now,
let's keep them human-readable.

42
00:01:55.660 --> 00:01:57.380
An instruction has two parts.

43
00:01:57.380 --> 00:01:59.060
The first part is the instruction itself.

44
00:01:59.060 --> 00:02:01.560
And the second part is usually
a memory address.

45
00:02:01.560 --> 00:02:04.660
On each clock tick, the CPU will do
one of three things:

46
00:02:04.660 --> 00:02:07.200
It will fetch an instruction from
a memory address.

47
00:02:07.200 --> 00:02:08.780
It will decode that instruction.

48
00:02:08.780 --> 00:02:10.380
And it will execute the instruction.

49
00:02:10.390 --> 00:02:12.900
Round and round in a loop.
So it’s going to count up.

50
00:02:12.900 --> 00:02:14.230
We're going to begin with a number,

51
00:02:14.230 --> 00:02:16.810
and add one to it, over and over again.

106
00:02:16.810 --> 00:02:18.460
(click) Fetch.

53
00:02:18.460 --> 00:02:21.010
One clock tick.
The Program Counter is set to 0,

54
00:02:21.010 --> 00:02:24.300
so the CPU fetches the instruction at address
0 in the memory

55
00:02:24.300 --> 00:02:26.700
and puts it into the instruction register.

107
00:02:26.700 --> 00:02:28.540
(click) Decode.

71
00:02:28.540 --> 00:02:30.519
The CPU decodes the instruction.

58
00:02:30.519 --> 00:02:31.930
The first part is the instruction,

59
00:02:31.930 --> 00:02:33.340
and the second part is a location.

60
00:02:33.340 --> 00:02:36.459
In our case, the instruction is LOAD
and the address is 6.

61
00:02:36.459 --> 00:02:40.659
So we will be loading the value in
address 6 into the accumulator.

104
00:02:40.659 --> 00:02:42.250
(click) Execute.

63
00:02:42.250 --> 00:02:44.810
The CPU executes this instruction.

64
00:02:44.810 --> 00:02:46.299
It takes the value at address 6,

65
00:02:46.299 --> 00:02:47.540
and loads it into the accumulator.

66
00:02:47.540 --> 00:02:16.810
In this case the value is 1.

106
00:02:16.810 --> 00:02:18.460
(click) Fetch.

68
00:02:51.549 --> 00:02:52.659
The program counter is incremented,

69
00:02:52.659 --> 00:02:26.700
and the CPU fetches the next instruction
in the next bit of the memory.

107
00:02:26.700 --> 00:02:28.540
(click) Decode.

71
00:02:28.540 --> 00:02:30.519
The CPU decodes the instruction.

72
00:02:59.920 --> 00:03:02.219
This time, it’s ADD,
and the address is 7.

73
00:03:02.220 --> 00:02:40.659
So we’ll be adding what’s at address 7
into what is already in the accumulator.

104
00:02:40.659 --> 00:02:42.250
(click) Execute.

75
00:03:07.560 --> 00:03:11.580
The CPU executes the instruction.
We add the value at address 7.

76
00:03:11.580 --> 00:03:13.020
In this case,
it's the value 1.

77
00:03:13.020 --> 00:02:16.810
1 + 1 is 2.

106
00:02:16.810 --> 00:02:18.460
(click) Fetch.

79
00:03:16.549 --> 00:02:26.700
From the next memory location, number 2.

107
00:02:26.700 --> 00:02:28.540
(click) Decode.

81
00:03:20.150 --> 00:02:40.659
An instruction to STORE the value in the accumulator
into RAM, at address 6.

104
00:02:40.659 --> 00:02:42.250
(click) Execute.

83
00:03:26.370 --> 00:03:29.620
Now, notice that we are overwriting what’s
already there,

84
00:03:29.620 --> 00:02:16.810
so address 6 now has 2 in it,
instead of 1.

106
00:02:16.810 --> 00:02:18.460
(click) Fetch.

86
00:03:34.120 --> 00:03:36.079
A new instruction: JUMP.

87
00:03:36.079 --> 00:03:37.079
With a jump,

88
00:03:37.079 --> 00:02:26.700
the next address we fetch from is the one
in this instruction.

107
00:02:26.700 --> 00:02:28.540
(click) Decode.

90
00:03:41.670 --> 00:02:40.659
So we’re going to jump to
address number 1.

104
00:02:40.659 --> 00:02:42.250
(click) Execute.

92
00:03:45.739 --> 00:03:48.560
The Program Counter is now back at 1.

93
00:03:48.560 --> 00:03:53.290
The ability to jump, to loop, and to build
instructions recursively is one of the foundations

94
00:03:53.290 --> 00:03:55.620
of computer science.
So: we're back up there.

95
00:03:55.620 --> 00:02:26.700
(click) Fetch from location 1.

107
00:02:26.700 --> 00:02:28.540
(click) Decode.

97
00:03:59.290 --> 00:02:40.659
It's the ADD instruction again.

104
00:02:40.659 --> 00:02:42.250
(click) Execute.

99
00:04:02.230 --> 00:04:04.900
Our accumulator still contains the values
from before,

100
00:04:04.900 --> 00:02:16.810
so: 2 + 1 = 3.

106
00:02:16.810 --> 00:02:18.460
(click) Fetch.

107
00:02:26.700 --> 00:02:28.540
(click) Decode.

103
00:04:10.750 --> 00:02:40.659
STORE again.

104
00:02:40.659 --> 00:02:42.250
(click) Execute.

105
00:04:13.450 --> 00:02:16.810
Storing it into location number 6.

106
00:02:16.810 --> 00:02:18.460
(click) Fetch.

107
00:02:26.700 --> 00:02:28.540
(click) Decode.

108
00:04:18.660 --> 00:04:19.930
And we jump again.

109
00:04:19.930 --> 00:04:25.930
(click) Fetch. (click) Decode. (click) Execute.
(click) Fetch. (click) Decode. (click) Execute.

110
00:04:25.930 --> 00:04:28.420
(continues clicking) We're in a loop,
and we’re counting upwards

111
00:04:28.420 --> 00:04:31.669
by one on every sixth clock cycle.
(stops clicking)

112
00:04:31.669 --> 00:04:36.300
Our program, with these simple instructions,
doesn’t have a halt command,

113
00:04:36.300 --> 00:04:37.840
or any way to interrupt it,

114
00:04:37.840 --> 00:04:42.060
so it will just keep incrementing
that value by one (many fast clicks)

115
00:04:42.060 --> 00:04:46.090
until the number becomes so large it can
no longer be held by the memory address.

116
00:04:46.090 --> 00:04:49.840
How it breaks then… well, that’s a whole
other video. (stops clicking)

117
00:04:49.840 --> 00:04:51.650
And my fingers are tired.

118
00:04:51.650 --> 00:04:54.259
This is a very fiddly way to program a computer.

119
00:04:54.259 --> 00:04:56.479
In theory, it can be 
but at this level,

120
00:04:56.479 --> 00:04:59.020
these instructions are just encoded
in raw binary data,

121
00:04:59.020 --> 00:05:01.220
which is basically unreadable for humans.

122
00:05:01.220 --> 00:05:04.110
So we can convert that base 2 binary
to base 16, hexadecimal,

123
00:05:04.110 --> 00:05:06.590
at that level we call it machine code.

124
00:05:06.590 --> 00:05:11.300
The next step up from that is a symbolic language
called Assembly, which is a bit more readable,

125
00:05:11.300 --> 00:05:14.130
but it’s still close to working at that
bare metal.

126
00:05:14.130 --> 00:05:18.490
The original "Prince of Persia" game was completely
programmed in assembly.

127
00:05:18.490 --> 00:05:20.810
That is almost unbelievable to me:

128
00:05:20.810 --> 00:05:25.979
painstakingly figuring out each pixel of animation
and encoding it into something that the computer

129
00:05:25.980 --> 00:05:28.340
almost understands directly.

130
00:05:28.340 --> 00:05:30.560
Programming like that is complex, and hard,

131
00:05:30.560 --> 00:05:34.479
and prone to the sort of human error that
introduces massive security problems.

132
00:05:34.479 --> 00:05:37.889
It is difficult to code and difficult to debug.

133
00:05:37.889 --> 00:05:40.320
So rather than dealing with the messiness,

134
00:05:40.320 --> 00:05:43.220
or, well, the pristine logic of machine code,

135
00:05:43.220 --> 00:05:46.680
higher-level languages were developed as an
intermediary step.

136
00:05:46.680 --> 00:05:49.890
Those languages handle all of that memory
reading and writing for us,

137
00:05:49.890 --> 00:05:54.060
so all we need to focus on is
what we want the computer to do.

138
00:05:54.060 --> 00:05:56.260
So, here’s my code:

139
00:05:56.260 --> 00:06:00.220
just the same instructions,
phrased a little bit differently,

140
00:06:00.220 --> 00:06:01.879
phrased for humans to be able
to read.

141
00:06:01.879 --> 00:06:05.949
I specify a variable, X. I then write a function
that loops forever,

142
00:06:05.949 --> 00:06:09.939
and each passing through that loop
I increment X by 1.

143
00:06:09.939 --> 00:06:12.539
Once I've written that code, I then pass it
to a compiler,

144
00:06:12.539 --> 00:06:15.069
which turns it into that
original machine code for me.

145
00:06:15.069 --> 00:06:16.259
So when I run the program,

146
00:06:16.259 --> 00:06:19.130
it’s loaded into the computer's memory,
and executed.

147
00:06:19.130 --> 00:06:23.560
If I want to run it on a completely different
type of computer, a Mac instead of a PC,

148
00:06:23.560 --> 00:06:26.640
I can compile it for that CPU instead.

149
00:06:26.640 --> 00:06:29.278
But this still doesn’t answer the question
of how the computer is

150
00:06:29.278 --> 00:06:33.970
doing something as complex as
decompressing and displaying this video.

151
00:06:33.970 --> 00:06:35.220
The answer to that is:

152
00:06:35.220 --> 00:06:36.220
speed.

153
00:06:36.220 --> 00:06:37.910
At the speed I was clicking,
at the end there,

154
00:06:37.910 --> 00:06:42.590
we were executing one instruction every couple
of seconds on one thread, one bit of the system.

155
00:06:42.590 --> 00:06:48.120
A modern CPU executes billions of instructions
per second – gigahertz – on multiple threads.

156
00:06:48.120 --> 00:06:50.000
But at the heart of your PC, or your phone,

157
00:06:50.000 --> 00:06:55.640
there is still just a ticking clock and 
a fetch-execute cycle.

158
00:06:55.640 --> 00:06:57.380
I’ve used a password manager for years,

159
00:06:57.380 --> 00:07:00.920
and if you’re techie enough to reach the
end of this video, you should too.

160
00:07:00.920 --> 00:07:05.600
And I’m not just saying that because this video is
sponsored by Dashlane, a password manager,

161
00:07:05.600 --> 00:07:11.160
and if you go to dashlane.com/tomscott, you can get
a free 30-day trial of their premium version.

162
00:07:11.160 --> 00:07:16.580
Why should you use Dashlane? Well, first,
reusing passwords is a terrible idea.

163
00:07:16.580 --> 00:07:18.180
If you’re like me a few years back,

164
00:07:18.180 --> 00:07:22.300
then you’re reusing very similar passwords
that have a few letters changed,

165
00:07:22.300 --> 00:07:24.580
or have the site’s name stuck somewhere in them.

166
00:07:24.580 --> 00:07:26.360
It’s not great.

167
00:07:26.360 --> 00:07:30.340
One data breach at one of those sites, and it’d be time to start worrying.

168
00:07:30.340 --> 00:07:33.760
These days, Dashlane sits in my browser,
and when I want to log in somewhere,

169
00:07:33.760 --> 00:07:36.300
I unlock Dashlane with the single
password that I have to remember,

170
00:07:36.300 --> 00:07:38.740
and it autofills it for me.

171
00:07:38.740 --> 00:07:43.480
Dashlane also stores and autofills credit cards and
address information across your devices,

172
00:07:43.480 --> 00:07:47.700
and it’s got a VPN to encrypt your traffic on public wifi networks if you want to.

173
00:07:47.700 --> 00:07:49.820
If you are the kind of person who can remember

174
00:07:49.820 --> 00:07:56.620
20-character unique symbol-filled passwords
for dozens of different sites, congratulations.

175
00:07:56.620 --> 00:08:00.060
But if you’re not superhuman,
then your choice is basically:

176
00:08:00.060 --> 00:08:03.800
use insecure passwords or
use a password manager.

177
00:08:03.800 --> 00:08:07.120
Now, you could use post-it notes on your
monitor as a password manager…

178
00:08:07.120 --> 00:08:08.720
or you could use Dashlane,

179
00:08:08.720 --> 00:08:13.820
which generates, stores, and autofills
long, secure, different passwords for every site.

180
00:08:13.820 --> 00:08:16.600
I don’t have to try and type in a 20-character password

181
00:08:16.600 --> 00:08:18.940
filled with symbols on my phone any more.

182
00:08:18.940 --> 00:08:20.240
I pull up Dashlane instead.

183
00:08:20.240 --> 00:08:23.480
Everything just works, synchronised on all my devices,

184
00:08:23.480 --> 00:08:26.880
across Windows, Mac, iPhone, Android,
all automatically.

185
00:08:26.880 --> 00:08:31.100
It plugs into Chrome, Firefox, Edge,
Safari, Opera, even Internet Explorer.

186
00:08:31.100 --> 00:08:33.940
And despite the fact that it’s all
synchronised in the cloud,

187
00:08:33.940 --> 00:08:36.980
Dashlane themselves don’t know what
those passwords are

188
00:08:36.980 --> 00:08:40.620
and based on their security architecture,
can never find them out.

189
00:08:40.620 --> 00:08:42.600
I’ll explain that next time.

190
00:08:42.600 --> 00:08:47.740
So: dashlane.com/tomscott for a 30-day
free trial of Dashlane Premium,

191
00:08:47.740 --> 00:08:50.580
which includes unlimited password storage and sync.

192
00:08:50.580 --> 00:08:53.560
And if you like it, you can use the code
“tomscott” for 10% off.

