video

Lesson video

In progress...

Loading...

Hey there, welcome back to Computer Systems. I'm Mac, your computing teacher for this unit.

I'm super excited for today's lesson, because over the unit, we spent a lot of time looking at the different components that make up a computer system and also how those components work together to execute a programme.

But today, we're going to pull back the hood and actually watch a programme while it's being executed.

For this lesson, you're going to need a notepad and a pen.

So you can take notes and answer questions while you're learning.

And also like you to make sure that you've removed all distractions from around you.

And this includes your mobile phone.

If you'd like to pause the video here and get everything you need, I'll be here when you get back.

In today's lesson, you are going to try to understand that assembly language has a one to one relationship to machine code.

And we're also going to explain basic assembly commands that we're going to use in our programmes.

Right, let's get started.

First, I'd like you to recap a bit of high level programming.

I would like you to design a programme using pseudo code that does the following.

I wanted to input two numbers, add them together, and then output the result.

You can write this out on your notepad in either as I said pseudo code or if you're more comfortable use Python.

If you want to pause the video here, have a go writing this programme that does these three things and then resume when you're done and we'll keep going.

Welcome back.

How did you get on? Should we have a look at a computer programme.

So here is the programme in pseudo code.

We have the first two lines which input two numbers.

So we say num1 and num2, and they're both equal to whatever our user inputs.

We then create a result, which is the two added together.

So num1 plus num2, and then we output that result.

It does all three of those things, I've asked you to do.

Inputs two numbers, add them together, and then outputs the result.

Now we can simplify this programme a little bit by removing the result line and instead just outputting the result of that calculations.

We could change it to look a bit more like this.

So we take two numbers as input, and then we output and add them both together.

Now you'll notice now my lines have got a bit more complicated, especially that last one.

Now you've done a little bit of high level programming.

We're going to turn that into a low level programme.

Here is the programme in Python that just the same as the pseudo code we looked at before, but I've written out as Python statements now, just so we can have a look in a programming language instead of pseudo code.

So you can see that I've got four lines, but only three of them do anything.

What I'd like you to do is have a look at line number one, and try and work out which stages of the computing cycle, which you can see on the right hand side here, right input, process, output and then interacting with storage.

Which of those stage is being accomplished on line number one, if you need to pause the video here and have a think about it, that's absolutely fine, resume when you've gotten an answer and we'll go through it.

So line number one, is both taking user input.

So we've got that input function at the end there, which allows the user to input some data.

So it's doing the input stage, but it's also interacting with storage.

We're saving it as a variable called num1.

So both the input stage and the storage stage are being accomplished in that line.

Now, I'd like you to have a look at line number four and do the same thing.

How many different stages are being accomplished on that line? Again, if you need to pause the video here, that's absolutely fine.

When you have an answer, resume it and we'll go through it.

So line number four, actually, there's three of the stages.

So we have processing, we're adding two numbers together, that's a process our computer needs to complete.

We're interacting with storage because both num1 and num2 are stored.

So we need to access those variables.

And we're outputting it to the user.

So both of these lines are accomplishing multiple things.

And in a high level programming language, each line does accomplish multiple things on multiple stages of that computing cycle.

This is so that high level languages are easy to use, you don't have to think about all the different stages that are going on in the background, you just think about what you want the line to do.

This makes them as I said, easy to use but quite difficult to optimise.

You're stuck with whatever happens inside that print statement, regardless, you can't change the underlying code.

Now low level languages will only accomplish one of those stages on each line.

We need to explicitly tell it to do each part of that process instead of just doing it all for us.

This is an example of abstraction.

High level languages abstract the way the complexities of doing each part of it one by one.

The problem is that we want to see what happens inside a processor when it's executing instructions, so that abstraction is not useful for us.

So instead, we're going to use assembly language, which is a low level language.

And this means that we'll be able to watch each part of the process happening as it's run.

So for this first activity, you're going to play the part of a translator.

Now a translator is a programme that takes a piece of code and goes through it, line by line, and converts it to machine code.

Now, I'm not going to make you write binary.

So instead, we're going to use assembly language, which is just a human friendly way of writing machine code.

Assembly languages are specific to a processor.

So there's only going to be one type of assembly language for one type of processor.

They're going to have a direct link between assembly language and the machine code, which is the instructions as the processor sees it.

So this is where it comes in, we have a one to one relationship.

Every assembly language will only work for one type of machine code, and therefore one type of processor.

And the processor we're going to use is called the Little Man Computer.

You can find it at this link, and it's also in your worksheet.

Don't go there yet, though, we still got a little bit of setup to do.

On your worksheet, I've given you this assembly language toolbox.

So as we go through this translation, I'm going to ask you to pause the video at certain points and fill in this toolbox.

This is so, that you know what's going on and you can remember them for when you have to write your own programmes later.

So you'll see here, I've got each of the commands written out for you.

I've then got a mnemonic, this is a bit of a weird word, but it's pronounced mnemonic.

And basically what that is, is a human readable version of the assembly language commands, they usually be three letters long all capitalised.

The computer doesn't see the mnemonic, this is just for us so that when we're writing the programme, is easier, we'd have to write out binary.

And then we need to put in a description.

What does the command do? What does it cause to happen inside the processor, and each command will also have an opcode.

Now this is the number represented in binary in the processor, but we're going to see it in deanery.

And that the processor sees, so it doesn't care about the mnemonic, that's for us, the opcode is what the processor sees.

Called, again don't do this now, we still got a bit of setup to do, I just wanted to let you know that this is there.

And so when I say go fill in your toolbox, this is the table I mean.

So I've got a programme here, is the programme that we just wrote.

So we got two numbers being input, and then the result of them being added together, being output.

I've also got some of the key registers here for us to watch as we're stepping through this programme and translating it, and also the input and output.

So you can see what's being put in and what is going to be output.

So, this first line here, which says num1 equals input.

And this does two things, remember, so it takes input, and it stores that input.

Now in assembly language, each one of those is going to be a separate command.

So let's have a look what the first one is.

What is the input command, which has the mnemonic I-N-P or INP, it's the first part of input.

As I said, these are going to be three letters, and these are not for the computer, this is for us when we're writing it.

So the input command takes the value entered by the user, and then stores it in the accumulator.

In this case, I've entered 10.

So you can see 10 here on the input and you'll also see 10 in the accumulator.

The next thing we need to do is store that value.

So we've got it in, it's in the accumulator, if we keep going, that's going to be lost, so we need to make sure it's stored.

And to do that, we use another command, which is the store command.

And that has that mnemonic STA.

STA means store.

And then as well, we need to give it something at the end, which is this num1.

Now num1 is a named memory location.

So just like we use variables in high level languages, in low level or in assembly language, particularly, we can use variables similarly.

So we give a named memory location to the store command and it will store whatever values in the accumulator in that position.

So we input a number and then we store it.

What are you to do now is pause the video.

First of all, put the first translation onto your worksheet, you'll see that there's a space for you, this table is there for you as well.

So you can write out yourself.

And I would also like you to fill in the toolbox for the input and store commands, and then have a go using that first part as the example of translating the second line.

So it's num2 equals input this time.

So it's very similar, but there was one key difference.

I'd like you to find out what that is.

Pause the video now, head over, start filling in your toolbox and the translation and then come on back when you've got an answer and we'll Keep going.

Welcome back.

Hopefully you found that okay.

We're going to have lots of chances to go back over these.

So don't worry, if you haven't been able to fill out the whole toolbox or you're a bit lost on the translation, we're going to do lots of practise in today's lesson.

Now, what do you think this second line will translate to? Well, hopefully you've got that, it'll follow the exact same pattern.

We'll take input first, and then we're going to store it.

The key difference I wanted you to find was that the variable name should be different this time.

It should be called num2, we don't want to store it in the same place because we overwrite our first number.

So you can see here, as I said, assembly language will only ever do one thing per line.

So it takes input and then stores it whereas compared to our Python programme, which does both of those things in one line.

So you see here, I've got my two commands there.

From the next time, the next part, we're going to add the two numbers together.

Now have a look at your toolbox.

You can pause the video if you need to, I want you to try and work out what command are we going to use to do that? Resume when you've got an answer and I'll show you.

Right, I hope you got it.

We're going to use the ADD command.

Now, I need to point something out here for you, which is, when we use the ADD command, we give it a named memory locations, we say go find num1 and then add it to whatever is in the accumulator.

So I'm just going to back up and we'll come back and you can see the difference here.

So at the moment, I just have 14 in the accumulator, which is what I put in on my second input.

Now when we call the ADD command, watch this accumulator because something's going to happen to it, it's going to go up by whatever value we stored in num1.

So now my accumulator is 24.

It was 14, and now it's 24 because num1 was 10.

Remember, I input 10 on the first instance.

So when we call the ADD command, it adds the value of a named memory location to whatever is stored in the accumulator.

Finally, we're going to use the OUT command.

Because the OUT command takes whatever is in the accumulator, and it will output it to our users.

So we've added the value in, we've created that result, we've added the two numbers together and now we need to show it to our users.

So the OUT command is for output, use the mnemonic OUT, O-U-T, and then a while put whatever's in the accumulator to show our users.

So even though the Python is finished now, we still have to do a couple things for our assembly language programme to work.

Firstly, we need to tell the programme to stop.

We need to say halt, this is the end of the programme.

And to do that, we use another command called the halt command which has the pneumonic H-L-T.

This just tells our assembly language, our processor, what is going through there, it's reached the end of a programme.

When we serve variables in Python is all done in one line, we give it a name, and then we assign it a value.

And that is all done for us.

It finds a memory location, it calls that memory location, the variable name, and then it stores wherever we want in that memory location.

By an assembly language, we need to explicitly tell it, which memory locations are num1 and num2.

And we do that using another command, which is DAT.

So this is a data location.

So we say num1, and then we use the mnemonic DAT, which tells it to set the next memory location as num1.

So when I first command here, when I say store in num1, it knows that the memory location at the end of my programme is called num1.

And then we need to do the same thing for num2.

So we also say, that there's a num2 data location.

So when I gave my second input, that's where we store it.

I'd like you to pause the video again here, I'd like you'd go into your worksheet and we need to fill out this translation that we've just done together, so there you have it on there.

And I'd also like you to try to add some of these things to your toolbox.

Add a description, the mnemonic.

We haven't really looked at opcodes yet, so don't worry about that, we'll see that in a minute.

But I'd like you to fill out the parts of your worksheet that you can.

Resume the video when you're done and we will carry on.

Welcome back.

Let's keep going.

So now we're going to have a look at tracing our programme while it's run on the LMC.

So you've already seen the LMC before, but we're going to use a slightly different simulator this time.

This is because this one lets you see most of the important components of the CPU so that you can follow what is happening.

This is on your worksheet, so don't worry.

I'll zoom my page and a little bit so you can see.

So that's why my video is so big, but it's okay, because you can see my smiling face.

This is the Little Man Computer.

This is the balance by a guy called Peter Higginson.

And it's completely free to use, and it allows you to watch what's going on inside of both the processor and RAM while you run a programme.

I'm just going to give you a brief tour of the site first, and then I'll show you how to input a programme.

So first of all can see, you've got your buttons down here, this allows you to either edit your programme, step through it, run the thing as a whole, you can reset it if you want to start it again, and you can also choose a few programmes if you want.

We're not going to do that, we're going to input our own.

But you can do that if you going to have a play around with it afterwards, you can choose some other programmes to have a look.

You've then got the CPU, which is this box right here, you've got our programme counter, the instruction register, the address register, and the accumulator.

And that is at the bottom you can see linked out here is the arithmetic logic unit.

So you'll see all the bits go in here, when some maths operations or logic operations need to be done.

We've got an input box on the bottom here, which allow you to put input, and an output box, which will show you anything that our programme prints out.

Finally, you've got your RAM array here.

So it goes from zero all the way up to 99, just like the other LMC that you saw a few lessons ago.

So when we load our programme, you'll see it'll occupy these memory locations in RAM.

And this is also what we reserve when we tell it that we're going to reserve memory location, it'll pick one of these, the one at the end of our programme, and it will give it a name.

So hopefully that is a nice tour.

So you've gotten to grips with the LMC.

Now I'm going to type out my programme just in here.

So we had input, this is all the stuff from our translation, if you want to copy and paste it and just paste it in here, that's absolutely fine.

I'm typing out just so you can see how the Little Man Computer works.

Now, it's given me an error, saying it has an unknown address line one.

And that's because I accidentally put an exclamation mark in there instead of the number one.

So you need to be very careful that wherever you put in the, when you use a variable name it's the same as the name you use down here.

You can see here, that I've got some memory locations.

So seven, which is this one here is going to be num1, and eight, which is this one here is going to be num2.

Hopefully, that's got you all set up ready to run this, you can do that by clicking the run down here.

But I'm not going to show you that part, 'cause I want you to see it for yourself.

I'm just going to head back over to my slides, and I'll see you there in a sec.

Now your setup on the LMC.

I'd like you to both run the programme and then step through it, using those running step buttons on the bottom, and make notes on what happens in the different parts of the processor.

Now the notes section is on your translator, you should see that there's a section for you to write notes next to the translation.

I'd also like you to fill in the appropriate opcodes for each command by examining the RAM.

If you want to pause the video here, head over, go run through it and step through it and see what happens in the different parts of the processor.

Resume when you're done and we will keep going.

Hopefully you are able to step through the programme.

But I'm just going to do it with you now.

So I can point out some important parts you should have noticed.

So let's just click Run.

So you can see here, the blobs show where different data is travelling.

These are the buses, that you learned about as a part of the processor.

And you can see the data travels across them to get both from the memory and within the CPU as well.

So now it's asking me for some input, so I'm going to input the number 10 in.

Now the number 10 has gone into the accumulator.

So that's what happens on the input.

My programme counter has gone up by one and should have noticed that go down to the arithmetic and logic unit to get that one added to it.

Now hopefully you are able to point out all the parts of the process are working together to complete this programme.

Now, I've got a slightly more complex one for us to take a look at.

Next, we're going to analyse some assembly language.

We're not going to be doing a translating, I'm just going to give you an assembly language programme, we're going to do some analysis on it.

So here we go.

We have another programme.

This one's a bit longer, but it uses a lot of the commands that you've seen already, plus a few new ones.

This programme is also on your worksheet.

What I'd like you to do is have a read through it and make some predictions about what you think it's going to do.

So don't worry if you can't get it exactly right.

I just want you to make a best guess by reading through it and try it and trying work out what you think some of the new commands will do.

It's easy to think about what's happening in the accumulator and across the processor while you're reading it, and it should give you some inkling into what it is doing.

Resume when you've got some predictions, and we'll go through them.

Okay, what do you think it's going to do? As a little clue, here's the same programme but in Python.

So you can see again, we're taking two numbers as inputs.

But this time, we have an if statement.

Now, if statements in assembly language are handled using things called branches.

And branches just allow your programme to jump from one part to another.

If we were to run this Python programme, if this condition was not true, we would skip this next line, we jump to the next one.

And that happens because of branches.

So have a look at the assembly code again.

And like you just see if you can spot where the branches are.

Welcome back to the LMC.

Let's have a look at what happens in this programme or when we run it on this simulator.

So the first thing I'd like to point out is I've just copied the code from my slides and pasted it in here, you can do the same thing using the code on your worksheet or you can type it out if you want to, but absolutely fine if you just copy and paste it.

It's about analysing not about your typing skills right now.

So one thing I'd like to point out is you can see that these two new commands are things you hadn't seen before, have been pushed to the edge here.

So we have bigger and end.

Now those are going to be important because the things on the left hand side here are markers, they're names, same way num2 was a name, num1 was a name, they're in the same place.

So let's run the programme and see what happens.

So the first couple stages are going to be grabbing the input and storing it.

So we can watch the process or sort of go through this.

In fact, I might actually just skip the video here until we get to the point where we're on to new commands.

So I'm just going to input two numbers, I'm going to do 10 and 14, and then I'll see you one move on to the next bit.

So I've just entered my second number, and it's just going through the process of storing it in my name data location.

So you can see here that my RAM and 12 has 10 and 13 has 14.

So next we are on to this SUB command.

Now this works very similarly to the ADD command, where it's going to subtract whatever is in the data location, which is just going to grab now, which is 10, it's going to subtract that from the accumulator.

Now my accumulator is holding four, and I'm going to fetch the next instruction.

Which is this BRP bigger.

Now this means a branch if positive, bigger.

So if the value in my accumulator is positive, I'm going to branch to the market bigger, which is down here.

So I've subtracted num1, and then I'm going to check what's in the accumulator and if it is positive, I'm going to skip to the bigger marker, which is just here.

And what that does, is it loads num2, and then outputs it.

And if you remember, in the Python code, what this programme does, is that it takes two numbers for input, it checks which one is bigger and then outputs whichever one it is.

So in this case number two was bigger.

So we're going to hopefully see here, num2 has been output.

That's because we've skipped this bit, where it's going to output num1, and just on straight to this bigger part here.

Now the only other command I want to point out is this branch always.

So this doesn't take into account what's in the accumulator instead, it just always branches to whichever marker I pass it, in this case end, which just takes it to my halt command.

I'd like you to pause the video again here and just run it by putting a bunch of different numbers in to see what results you get.

And to watch it execute the different branches.

Resume when you're done.

And we'll head back and check your toolbox.

Welcome back, guys.

Now we're just going to do one final activity and that is to check what you've got in your toolbox.

And this also might help you if you missed any of the opcodes or the descriptions.

Like I said, this is your first time having a go at assembly language.

So I just want to make sure that you've got all the correct information going forward.

So what I'm going to do is, I'm going to talk through each of these and give you a little bit more detail about them, feel free to pause the video at any point on this slide.

And just fill in your toolbox for any bits that you've missed.

So first, we have the input command, which we spoke about.

We get user input, and we load into the accumulator.

And that has the opcode 901.

We then have the store command, which stores the current value in the accumulator to the memory address provided.

And you'll see that this has the opcode 3XX, that's because the XX is replaced with whatever memory location I passed to it.

So if I had num1, which in our first programme was in 12, that that command would be 312.

Load is very similar, it loads into the accumulator, whatever is in data address supplied, and this has the opcode 5XX.

So the XX again will be replaced with a memory location.

We then have the OUT command, which outputs the contents of the accumulator and has the opcode 902.

If you haven't already, just pause the video here and make sure you've got all of these down just as I've got it here and then resuming it and we'll do the next part.

Right, should we keep going.

We then had the branch if positive command.

Which branches or jumps to the memory address if the accumulator value is zero or positive.

Now the memory address we supplied was that bigger flag or end depending on it, and wherever we put it, it just takes that memory location as the memory location needs to jump to.

So we just put it on the left hand side of the line and then it knows to jump there if the value in the accumulator is zero or positive.

And that has the opcode 8XX.

Again, the XX is going to be replaced with whatever memory location that flag is in.

We then had two others, which are branch is zero.

Now this command you won't have seen, so it's a bit unfair of me ready to put it on there.

But I thought I would tell you about it just so you are fully prepared, and you knew what each of them was.

This will branch to the memory address provided if the accumulator is zero.

The usefulness of this is, this is a way to check if two variables are equal.

'Cause if we take one away from the other and our result is exactly zero, it means that they are equal.

And the opcode for that is 7XX, memory location will replace that XX.

And then had branch always which does the same thing, but just doesn't check the accumulator, it jumps to the memory address provided always, every time.

And this is useful for things like else statements or skipping over a bit of a branch that you don't want to use.

That has the opcode 6XX.

We then had the ADD command, which adds the contents of a memory location to the accumulator.

And that has the opcode 1XX.

Again, I'm going to stop saying after this one I promise, but that XX will be replaced with memory location.

You want to pause the video here and just add in, especially that branch if zero one because that wasn't in the programme.

Just make sure you've got everything here and then we'll resume and I'll do the next part.

There's only a couple of commands left.

Right, let's just see the last few commands before the end of this lesson.

So we then had the subtract command.

So subtract command, will subtract the contents of a memory location that's provided from the accumulator.

So it has the opcode 2XX, and the mnemonic S-U-B or SUB.

We then have the Halt command, which has the mnemonic H-L-T.

And that stops the programme, it tells the processor to stop.

And that has the opcode 000, so it's nothing.

But it's three zeros instead of the usual two that is stored in RAM.

And then the last one was that DAT command.

So we use that to assign a memory address to a label.

You can use this to create variables in your assembly language programmes.

And these just have an opcode XX because the memory location won't be the only opcode there.

Pause the video here, last time.

Just make sure that you've got all this in your toolbox, on your worksheet, and then we can go on and we'll finish up this lesson.

Great work everybody.

Honestly, you've done so well to get through that first lesson on assembly language.

It's a completely different way of thinking about programming and really well done for persevering.

The last thing I'd like to ask you is to share your work at Oak National.

If you'd like to, of course, please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak.

That's all from me today.

Thank you again.

I hope you enjoyed this lesson and I will see you in the next one.

Happy learning.