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