# Instructions & Programs: Crash Course Computer Science #8

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

## 100 thoughts on “Instructions & Programs: Crash Course Computer Science #8”

• ### Coding Grunt Post author

Watch "Team Treehouse Day1" on YouTube
https://youtu.be/gB6-syiaTzg

so cool

• ### brad metcalf Post author

I’m still trying to figure out the abacus from episode 1….

• ### Bruce Williams Post author

Excellent work! I've been a programmer since my dad brought home a TI 99. I loved it and seeing all the assembly language brought back memories. I wish I could have had this course way back when as I would have been a much better programmer; but I got the job done on limited hardware which is what counts! 😉 Thank you again!

• ### M. Shokarim Post author

Watching this and following up with notes from my lectures fully stores the info into my memory. Thanks a lot for the videos.

• ### JuanDVene Post author

Is jump_if_equal kind of like the beg (branch if equal) function in the MIPS assembly language?

• ### Bader Alaraimi Post author

I swear to God since episode 4 I have no idea what you are talking about 😶

• ### Larissa Ford Post author

Is the instruction length approach mentioned the same thing as fixed-length instructions? Thanks for the amazing videos!

• ### Jose Francisco Mejias Post author

You lost me at 5 … I feel stupid, but there is so much I can do with English as a second language

• ### Karan Navani Post author

New drinking game:

Take a shot every time there is a new level of abstraction

• ### Caleb Hubbell Post author

"I'm not going to be teaching any programming languages"
Essentially explains the most important parts of assembly
You crack me up.

• ### Julian Paolo Dayag Post author

Gives me the feels of Deep Look. Good Job!

• ### ColZapp Post author

You talk far too fast, aspies like me need speech to be measured and even

• ### Pay Pul Post author

The complexity is gettin out of hand

• ### Hashem Sahoory Post author

i have a question
the basic instructions (load A ,load B , jump ……. ) are they hardwired in the cpu ???
and do we have one copy of them ??

• ### Niko Fernandez Post author

almost halfway with this series and i can say that i am truly grateful to know all the details about computing. generally it will help me build up my skills on working with computers. thank you so much for this great gift of knowledge CrashCourse.

• ### Aloha Ackbar Post author

Should have touched on hexadecimal… 0010 1110 is hard to say, but 2E is easy 🙂

• ### adityahpatel Post author

the graphics layout pictorials are great. but this is too fast. this is not teaching this is reading like a newsreader. even newsreaders don't read this fast. crazy. please slow down and try to explain not read.

• ### SomeGuy Post author

Just found this channel and these content related to computer science is amazing and well explained. I was reading a book about Assembly, and understanding how processing works at a low level makes it more understandable.
Good content 🙂

• ### Com Dy Post author

thanks so much

• ### Com Dy Post author

Crash course is so amazing I watch every single video in every single playlist.

219594th

• ### Senethys Post author

How can the computer distinguish between instructions and data? Is it the order storing in the RAM?

Screw this.

• ### Ram Kumar Post author

so the 32 and 64 bit corresponds to the instructionlength for a processor, and the 32 & 64bit os is the software that can give 32/64 bit instructions. thats why a 32bit os can run on 64bit processor & vice-versa is not possible. I am crystall clear now!! thanks a lot!, and please correct me if iam wrong

• ### marek karasz Post author

i just getting super nerdy watching these videos 😀

• ### Ismail DUO Post author

Thanks very much !
when we talk about 64bit or 32bit operation system we talk about instruction length or about memory address in episode 6 ?

That's cool

• ### kidstoogesreppin Post author

Hit us with a Discrete Math crash course?

• ### Eric Koontz Post author

60 extra credit if u breadboard the circuits 🙂

• ### Thoriq Thorga Post author

"A New Level of Abstraction" is really a good quote.

• ### Abhinav Post author

Which component carries out the instructions such as load and jump

• ### prozac1127 Post author

crashcourse cs, 6 years of graduate course in 6 hours

• ### John Elam Post author

Curious, Will you cover Block-Chain Tech and Crypto-Currencies? I would love to see how CC explains it!

• ### Xavier Egdom Post author

its still a shame a woman is talking about computers.

• ### AmandaOlson77 Post author

Is this the numberphlie guy's sister or something?

• ### duo317 Post author

Are you guys using the old marvel vs capcom 1 stage select noise for your transition noises?

• ### Owen Linsey Post author

check out the channel in my bio for good luck!

fAOUSSens

• ### KT Mansfield Post author

I love these videos, and I definitely know more than I did at the start, but I feel stupid because her clock goes so fast and my processing core can't handle it. Also, I'm still at a loss at how to calculate binary numbers… my poor brain.

• ### Peter Edmonds Post author

If your RAM is storing 16 8-bit numbers, and the first four bits are reserved for an OPCODE won't you only be able to express 0-15 instead of 0-255, greatly reducing potential of the computer? On a similar note, how does a computer differentiate between actual values and instructions. Wouldn't any value starting with 0010, 0001, 0100, etc be treated as an instruction followed by an address. What if I wanted to express 00101110 as '46' and not 'LOAD_A 14'. Thanks for all of this!

• ### Behaarte Glatze Post author

forever…ever..ever..ever xD

Awesome Video ^^ Thanks a lot !

• ### Sher Khan Post author

So what we basically made is 8085 microprocessor. Am I correct?

😍😍😍

• ### Chanwoo Post author

What programming language is this?

YES

• ### Yuxin Wang Post author

I'm really afraid that I will forget some basic things after watching those complicated ones…

• ### Ribbitt III Post author

100:1 like to dislike ratio

• ### azem ahmetxhekaj Post author

your voice is simply unbearable. otherwise very good.

• ### john guzman Post author

Holy fick! You just made C program so much more understandable!!!!! I was so lost when trying to allocate memory, and you just drew up a map. AWESOME SAUCE!!

• ### tevo Post author

I LOVE YOU! This helps me understand assembler a little better <3

• ### avi12 Post author

Did you consider teaching computer science at a university? If not, you definitely should
Now I visually see the difference between binary and Assembly

• ### DarkChocolate33 Post author

Why am I not getting this?!?!

• ### Julia Suarez Post author

You are absolutely awesome! thank you very much!

Wow

• ### GuruNandan Post author

She is soooo lovely

• ### FujoshiPeanut Post author

This series has helped me look at computers in a completely new light and has helped demystify them for me. Great video series!

• ### Olivier Bégassat Post author

Great series with great explanations : )

• ### Fire Nation Files Post author

Hello. Enjoy learning about computer science!

• ### DIYAZ - Gaming Post author

I want to be a computer science/engineer when I grow up and I hope this series will help me

11 mod 5 !!!

• ### Naif Alkhunaizi Post author

You are an amazing teacher !

• ### lpfcdd Post author

I am catching up this series more than year later. And its AWESOME !!!!!! I am thinking how many 0's and 1's i have written to make this appear on screen …. and its mind-blowing !!!!!!!! Thanks a lot for this awesome series !!!!!!!!!!! (too many 10001000100 ) 😀

• ### RJ Bush Post author

By reducing the speed of this one and #7 to 0.75 it made far more sense. Slow down Carrie-Anne.

• ### Tixo Moments Post author

I love it! however when the register goes negative wouldn't it loop back to 252 instead of -4?

• ### Fuzzy Lumpkin Post author

Cuz of all of you on this channel I’m gonna build my first gaming pc

• ### Edson Fernandes Post author

A new level of abstraction !!!

• ### Patryk Z Post author

im studying first year cyber security, and we having 3 modules that are similar to each other and so many times i was confused in the lecture coming back home with a lot of doubts, and then ive found that gem, that miracle, that light in the tunel, that wooden raft in the middle of the sea, THIS CRASH COURSE. Thank you so much from bottom of my heart, i appreciate you a lot guys and Carrie Anne, you did great job and whenever i feel confused i just run those videos again and again until i understand, honestly my uni should run this on lectures , i even start to ignore lecturers sometimes because i know that i will understand it on Crash Course much better. I wish you good luck all that helped to make this happen! future is ours!

• ### ARCHIBALD BARCKLOW Post author

hello im default Dan

tired

• ### osayamen omigie Post author

wow!!!… how can you teach me so much is 10 mins… something is wrong with our school education system.

• ### Yeppy McYepFace Post author

Such a wonderful series!!

• ### Gareth Williams Post author

Any idea would be a follow up on MIC-1 to 4 designs

• ### KEBI Photography Post author

If you want to find pure magic in this world, go look at a car. You press a pedal and suddenly you love!

• ### Jordan Howlett Post author

Marry me carrie please

• ### mehmet2247 Post author

What the CrashCourse has done here is providing a fast and solid introduction to assembly language who I am a newbie at. By having seen your video, I progressed much more than the other sources I did with. Thank you very much and greetings.

• ### Mike DeOliveira Post author

Love this Channel sooo much knowledge. 😁💻💾

• ### bleuflamenco Post author

"There is no instruction with the opcode of zero, so the computer would just crash". I don't know about other CPUs, but the 6510 has an opcode that's zero, and it happens to be the BRK instruction.

• ### Amruta Krishna R Post author

Program counter is missing which keeps track of the RAM address.

• ### Carlos Eduardo Post author

I'm studying Computer Science and practically thanks to this series of videos I approved the course of Computer Architecture haha 🙂

• ### Unidorsal Icosahedron Post author

You must hate poetry.

• ### Emma Kronberg Post author

Thank you Carry! So much I understand now! Carry on!

• ### sicence account Post author

Oh so is software the greates level of abstraction or there is more in newx videos ?

• ### blessed7fold Post author

My brain is about to explode watching this! It's very complicated and hard to follow.

• ### cytroyd Post author

thank you carrie anne, crash course, and zachtronics games. perfect combo for getting me started into the depth of my beloved computer science.

• ### Ome Mishra Post author

in one cycle how many instructions can be execute?

• ### Tyler Roundy Post author

00:10 "…to make a basic but functional sexual processing unit." Did I miss an episode?

• ### r a j a Post author

'Buses' topic is remaining maybe, i want to know how these different bus architectures(PCI, ISA,PCI-ex,SATA,PATA etc.)fits n what changes each has, how they evolved

• ### andarted Post author

Is there a small mistake at ~ 6:12 ?
The CPU we build was only able to store values from 0 (00000000) to 255 (1111 1111). But the value in Register A is -4. That't not in the realm between 0 and 255.
Am I wrong or is this a tiny slip?

• ### DirtCube Post author

see the creeper in the baccground?

• ### Nikhilesh K Post author

Carrie.. please marry me 🙁

• ### Jake Turner Post author

9:16, AMD SQUAD!!!

• ### bill Niko Post author

So is the program a re discrete combinations of old representations?

• ### Saad Saiyed Post author

I've a doubt at 4:12 there we process the code for Add B A.
So that means addition value should be stored in B right? And not in A.
Please correct me if I am wrong.
Thank you.

• ### Garrison Pledger Post author

Dude this woman is smart. I also agree computers are like 10% less magic now lol

thank you

• ### Roger Hinds Post author

When does the IAR increment – if it happens at end of the Execute cycle then it would contain 3 not 2 …

• ### Ashish Kumar Post author

This is what real Computer Science is…..it would've been amazing if we were taught this in schools. Love your work CC and everyone else associated.

• ### Vallerious Post author

If you find this interesting you should try TIS-100 game. trust me

• ### Harshavardhan Gupta Post author

superb teaching