Instructions & Programs: Crash Course Computer Science #8 — Transcript
Full transcript
- 0:03Hi, I’m Carrie Anne and this is Crash Course Computer Science!
- 0:06Last episode, we combined an ALU, control unit, some memory, and a clock together to
- 0:10make a basic, but functional Central Processing Unit – or CPU – the beating, ticking heart
- 0:14of a computer.
- 0:15We’ve done all the hard work of building many of these components from the electronic
- 0:19circuits up, and now it’s time to give our CPU some actual instructions to process!
- 0:23The thing that makes a CPU powerful is the fact that it is programmable – if you write
- 0:28a different sequence of instructions, then the CPU will perform a different task.
- 0:31So the CPU is a piece of hardware which is controlled by easy-to-modify software!
- 0:36INTRO
- 0:44Let’s quickly revisit the simple program that we stepped through last episode.
- 0:48The computer memory looked like this.
- 0:50Each address contained 8 bits of data.
- 0:52For our hypothetical CPU, the first four bits specified the operation code, or opcode, and
- 0:57the second set of four bits specified an address or registers.
- 1:00In memory address zero we have 0010 1110.
- 1:04Again, those first four bits are our opcode which corresponds to a “LOAD_A” instruction.
- 1:10This instruction reads data from a location of memory specified in those last four bits
- 1:14of the instruction and saves it into Register A. In this case, 1110, or 14 in decimal.
- 1:21So let’s not think of this of memory address 0 as “0010 1110”, but rather as the instruction
- 1:27“LOAD_A 14”.
- 1:28That’s much easier to read and understand!
- 1:30And for me to say!
- 1:31And we can do the same thing for the rest of the data in memory.
- 1:35In this case, our program is just four instructions long, and we’ve put some numbers into memory
- 1:39too, 3 and 14.
- 1:41So now let’s step through this program:
- 1:42First is LOAD_A 14, which takes the value in address 14, which is the number 3, and
- 1:47stores it into Register A.
- 1:49Then we have a “LOAD_B 15” instruction, which takes the value in memory location 15,
- 1:54which is the number 14, and saves it into Register B.
- 1:56Okay.
- 1:57Easy enough.
- 1:58But now we have an “ADD” instruction.
- 2:00This tells the processor to use the ALU to add two registers together, in this case,
- 2:04B and A are specified.
- 2:06The ordering is important, because the resulting sum is saved into the second register that’s specified.
- 2:11So in this case, the resulting sum is saved into Register A.
- 2:15And finally, our last instruction is “STORE_A 13”, which instructs the CPU to write whatever
- 2:19value is in Register A into memory location 13.
- 2:23Yesss!
- 2:24Our program adds two numbers together.
- 2:26That’s about as exciting as it gets when we only have four instructions to play with.
- 2:30So let’s add some more!
- 2:31Now we’ve got a subtract function, which like ADD, specifies two registers to operate on.
- 2:35We’ve also got a fancy new instruction called JUMP.
- 2:38As the name implies, this causes the program to “jump” to a new location.
- 2:42This is useful if we want to change the order of instructions, or choose to skip some instructions.
- 2:45For example, a JUMP 0, would cause the program to go back to the beginning.
- 2:48At a low level, this is done by writing the value specified in the last four bits into
- 2:53the instruction address register, overwriting the current value.
- 2:56We’ve also added a special version of JUMP called JUMP_NEGATIVE.
- 3:00This only jumps the program if the ALU’s negative flag is set to true.
- 3:04As we talked about in Episode 5, the negative flag is only set when the result of an arithmetic
- 3:08operation is negative.
- 3:10If the result of the arithmetic was zero or positive, the negative flag would not be set.
- 3:14So the JUMP NEGATIVE won’t jump anywhere, and the CPU will just continue on to the next instruction.
- 3:19And finally, computers need to be told when to stop processing, so we need a HALT instruction.
- 3:24Our previous program really should have looked like this to be correct, otherwise the CPU
- 3:28would have just continued on after the STORE instruction, processing all those 0’s.
- 3:32But there is no instruction with an opcode of 0, and so the computer would have crashed!
- 3:36It’s important to point out here that we’re storing both instructions and data in the
- 3:40same memory.
- 3:41There is no difference fundamentally -- it’s all just binary numbers.
- 3:43So the HALT instruction is really important because it allows us to separate the two.
- 3:47Okay, so let’s make our program a bit more interesting, by adding a JUMP.
- 3:51We’ll also modify our two starting values in memory to 1 and 1.
- 3:55Lets step through this program just as our CPU would.
- 3:58First, LOAD_A 14 loads the value 1 into Register A.
- 4:01Next, LOAD_B 15 loads the value 1 into Register B.
- 4:05As before, we ADD registers B and A together, with the sum going into Register A. 1+1 = 2,
- 4:11so now Register A has the value 2 in it (stored in binary of course)
- 4:15Then the STORE instruction saves that into memory location 13.
- 4:18Now we hit a “JUMP 2” instruction.
- 4:20This causes the processor to overwrite the value in the instruction address register,
- 4:24which is currently 4, with the new value, 2.
- 4:27Now, on the processor’s next fetch cycle, we don’t fetch HALT, instead we fetch the
- 4:31instruction at memory location 2, which is ADD B A.
- 4:34We’ve jumped!
- 4:35Register A contains the value 2, and register B contains the value 1.
- 4:38So 1+2 = 3, so now Register A has the value 3.
- 4:42We store that into memory.
- 4:44And we’ve hit the JUMP again, back to ADD B A.
- 4:471+3 = 4.
- 4:49So now register A has the value 4.
- 4:51See what's happening here?
- 4:52Every loop, we’re adding one.
- 4:53Its counting up!
- 4:54Cooooool.
- 4:55But notice there’s no way to ever escape.
- 4:57We’re never.. ever.. going to get to that halt instruction, because we’re always going
- 5:01to hit that JUMP.
- 5:02This is called an infinite loop – a program that runs forever… ever… ever… ever…
- 5:07ever
- 5:08To break the loop, we need a conditional jump.
- 5:10A jump that only happens if a certain condition is met.
- 5:13Our JUMP_NEGATIVE is one example of a conditional jump, but computers have other types too - like
- 5:18JUMP IF EQUAL and JUMP IF GREATER.
- 5:20So let’s make our code a little fancier and step through it.
- 5:24Just like before, the program starts by loading values from memory into registers A and B.
- 5:28In this example, the number 11 gets loaded into Register A, and 5 gets loaded into Register B.
- 5:34Now we subtract register B from register A. That’s 11 minus 5, which is 6, and so 6
- 5:39gets saved into Register A.
- 5:40Now we hit our JUMP NEGATIVE.
- 5:42The last ALU result was 6.
- 5:44That’s a positive number, so the the negative flag is false.
- 5:47That means the processor does not jump.
- 5:49So we continue on to the next instruction...
- 5:51...which is a JUMP 2.
- 5:52No conditional on this one, so we jump to instruction 2 no matter what.
- 5:56Ok, so we’re back at our SUBTRACT Register B from Register A. 6 minus 5 equals 1.
- 6:01So 1 gets saved into register A.
- 6:03Next instruction.
- 6:04We’re back again at our JUMP NEGATIVE.
- 6:061 is also a positive number, so the CPU continues on to the JUMP 2, looping back around again
- 6:11to the SUBTRACT instruction.
- 6:13This time is different though.
- 6:141 minus 5 is negative 4.
- 6:17And so the ALU sets its negative flag to true for the first time.
- 6:20Now, when we advance to the next instruction,
- 6:23JUMP_NEGATIVE 5, the CPU executes the jump to memory location 5.
- 6:27We’re out of the infinite loop!
- 6:29Now we have a ADD B to A. Negative 4 plus 5, is positive 1, and we save that into Register A.
- 6:35Next we have a STORE instruction that saves Register A into memory address 13.
- 6:39Lastly, we hit our HALT instruction and the computer rests.
- 6:43So even though this program is only 7 instructions long, the CPU ended up executing 13 instructions,
- 6:49and that's because it looped twice internally.
- 6:52This code calculated the remainder if we divide 5 into 11, which is one.
- 6:56With a few extra lines of code, we could also keep track of how many loops we did, the count
- 7:00of which would be how many times 5 went into 11… we did two loops, so that means 5 goes
- 7:05into 11 two times... with a remainder of 1.
- 7:08And of course this code could work for any two numbers, which we can just change in memory
- 7:12to whatever we want: 7 and 81, 18 and 54, it doesn’t matter -- that’s the power
- 7:17of software!
- 7:18Software also allowed us to do something our hardware could not.
- 7:21Remember, our ALU didn’t have the functionality to divide two numbers, instead it’s the
- 7:26program we made that gave us that functionality.
- 7:28And then other programs can use our divide program to do even fancier things.
- 7:32And you know what that means.
- 7:34New levels of abstraction!
- 7:41So, our hypothetical CPU is very basic – all of its instructions are 8 bits long, with
- 7:46the opcode occupying only the first four bits.
- 7:49So even if we used every combination of 4 bits, our CPU would only be able to support
- 7:54a maximum of 16 different instructions.
- 7:56On top of that, several of our instructions used the last 4 bits to specify a memory location.
- 8:01But again, 4 bits can only encode 16 different values, meaning we can address a maximum of
- 8:0616 memory locations - that’s not a lot to work with.
- 8:10For example, we couldn’t even JUMP to location 17, because we literally can’t fit the number
- 8:1517 into 4 bits.
- 8:16For this reason, real, modern CPUs use two strategies.
- 8:19The most straightforward approach is just to have bigger instructions, with more bits,
- 8:23like 32 or 64 bits.
- 8:25This is called the instruction length.
- 8:28Unsurprisingly.
- 8:29The second approach is to use variable length instructions.
- 8:32For example, imagine a CPU that uses 8 bit opcodes.
- 8:35When the CPU sees an instruction that needs no extra values, like the HALT instruction,
- 8:40it can just execute it immediately.
- 8:41However, if it sees something like a JUMP instruction, it knows it must also fetch
- 8:45the address to jump to, which is saved immediately behind the JUMP instruction in memory.
- 8:50This is called, logically enough, an Immediate Value.
- 8:52In such processor designs, instructions can be any number of bytes long, which makes the
- 8:56fetch cycle of the CPU a tad more complicated.
- 8:59Now, our example CPU and instruction set is hypothetical, designed to illustrate key working
- 9:04principles.
- 9:05So I want to leave you with a real CPU example.
- 9:07In 1971, Intel released the 4004 processor.
- 9:11It was the first CPU put all into a single chip and paved the path to the intel processors
- 9:15we know and love today.
- 9:17It supported 46 instructions, shown here.
- 9:19Which was enough to build an entire working computer.
- 9:22And it used many of the instructions we’ve talked about like JUMP ADD SUBTRACT and LOAD.
- 9:26It also uses 8-bit immediate values, like we just talked about, for things like JUMPs,
- 9:31in order to address more memory.
- 9:33And processors have come a long way since 1971.
- 9:36A modern computer processor, like an Intel Core i7, has thousands of different instructions
- 9:41and instruction variants, ranging from one to fifteen bytes long.
- 9:44For example, there’s over a dozens different opcodes just for variants of ADD!
- 9:48And this huge growth in instruction set size is due in large part to extra bells and whistles
- 9:52that have been added to processor designs overtime, which we’ll talk about next episode.
- 9:57See you next week!
About this transcript
This page contains the full transcript of Instructions & Programs: Crash Course Computer Science #8 by CrashCourse, generated from the public captions YouTube serves with the video. The transcript has 1,952 words across 181 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.