Placeholder Image

Subtitles section Play video

  • hello.

  • Whilst chatting with a Web developer friend the other day, he asked the question What?

  • His assembly language And this took me back a little bit.

  • So I thought, You know your program.

  • But surely you know what's assembly languages?

  • Then it occurred to me.

  • Actually, there's probably many programming professions where people will never have any exposure or or reason to use assembly language.

  • Such obvious Web development database back ends that user interface designers, script writers.

  • And so I thought it might be worth to put together a little video on what is assembly language.

  • When I was working in academia, I used to work in a laboratory that designed processes right through from the conceptual stage to actually fabricating them on silicon.

  • When you develop your own processes, you also need to create your own framework and tools.

  • So we had to design assembly language, correct to design an instruction set, then build compilers, Andy booking tools.

  • Now, the thing with assembly languages there are many, many different flavors.

  • In fact, you could argue every different type of process architecture out there.

  • There is a unique assembly language for it.

  • I'm not going to go into the specifics for this video today.

  • This is really a word.

  • Is assembly language fit in the scheme of programming?

  • To understand the role of assembly language, we're going to need to look at the fundamentals of computer architecture.

  • Let's look at CP years and memory and how they talk to each other.

  • But before we get started at the end of this video, there's a couple of my thoughts about the recent events that have happened in Manchester.

  • I'd appreciate it if you stuck around to watch those two.

  • So let's get started.

  • When trying to classify assembly language, it's useful to look at the hierarchy of other languages.

  • No, I've chosen some naming see, which probably aren't conventional, but I think you'll see the gist of what I'm trying to get across.

  • So for declarative structures, these air the really high level things like, I consider that to be things like html Jason essentially languages, which don't really have an executed ble order but still nonetheless defying useful things.

  • These very high level language is that usually used to define things in scripting language is as well as other things, so scripting language.

  • I would consider things like python on JavaScript on oddball ones like Luis, I like Lou.

  • These languages usually execute line by line.

  • There are some fancy compiling techniques to make things run a bit quicker, but ultimately they are interpreted as and when they are required on.

  • Therefore, they run in something called a virtual machine, which is basically a software equivalent to a computer.

  • Now these virtual machines will be written in a more sophisticated language, usually along the lines off something like c++ Debatable E something like C.

  • Sharp.

  • I'll await some of the hate in the comments.

  • There, on the whole load of, ah, older language is the list of those is endless.

  • And at the very bottom we have assembly language, which is the language that the CPU speaks.

  • The problem we're trying to define a hierarchy of languages is some of them share bits and pieces with others.

  • So, for example, some could argue that C sharp here is actually a scripting language.

  • Because it's interpreted, it produces a bite code, which requires a virtual machine.

  • Ultimately, it's not black and white on The point I'm trying to get across is at the very top.

  • We usually have things that define stuff like what a website looks like.

  • And at the very bottom.

  • We have the instructions that tell the computer hardware how to display on the screen what that website should look like.

  • But this video is about assembly language, so we need to have a look at computer systems architecture.

  • Fundamentally, a computer consists off a central processing unit on Dhe RAM random access memory.

  • On between the two, the central processing unit will outputs and address on it can read or write to a certain location in the memory.

  • Let's start with a very simple one.

  • Ram is effectively an array to the code is out there.

  • There is usually on index on dhe.

  • There will be some sort of data.

  • Now.

  • The width of the data depends on the architecture.

  • We commonly here 32 bits, perhaps could be 128 bit could be 256 but it really does depend on the system.

  • This video is not going to go for any particular architectural.

  • We're just going to keep everything very, very high level.

  • Where is Ram is used to store data.

  • The CPU is used to manipulate data, and it does this by using several internal units, so I'll have a look at what these are.

  • The first thing is CPU usually contains registers, which are basically its own internal ram, and it'll only have a small number of these.

  • So in my simplified example, here we have four registers are a R, B, R, C and R D on the's will just store numeric values as we'll see later.

  • Some CPUs have specific registers for specific functions.

  • A CPU isn't very useful unless it has an arithmetic logic unit, otherwise known as an A L.

  • U V ale.

  • You is responsible for performing well.

  • Addition, subtraction on logic, ANS and oars on other sort of computations.

  • CPS will also contain a status flag register, which is a collection of bits, which tells us about the state of the CPU on theory Thematic logic Unit.

  • The program counter is used to store the address of where we're up to in our program.

  • So as the program is executed sequentially, the program counter increases the program.

  • Counter value can be set depending on the result of something in the status flags register, so we can use this to branch and we'll see an example of that later on on finally usually have a section of important output or communication.

  • So this is how we get data into an out of the CPU.

  • The purpose and sophistication of a CPU depends on what part has got inside.

  • It's that some see pews will have more registers, and some CPUs will have bigger and more complex arithmetic logic units.

  • These are known as extensions on on desktop computer architectures were very reliant on extensions for the processes to do lots of the wonderful things you expect them to do these days.

  • Now, for the purposes of today's video, we're going to assume we have one more module and you can't go into a shop and buy this on.

  • It's going to be program memory, so I'm going to keep this separate from random access memory.

  • On the program, memory is accessed when the CPU outputs, it's ah program counter and in return receives the next instruction that it needs to execute.

  • This configuration is not how it works on your desktop and in fact, that the random access memory is used to store the program as well.

  • However, on some embedded systems, the program memories frequently kept separate from the working random access memory.

  • I've decided to keep the two separate just for this video.

  • It clears things up through the worked example.

  • Later on, if you're familiar with basic programming, you'll be familiar with the function and they function takes arguments.

  • Well, an assembly language can be considered to be the same thing.

  • But if we use the correct parlance for assembly language, the function is not normally called on op code.

  • On each argument will be called on up around to demonstrate how assembly language works.

  • We're going to design a very simple process of just a handful of instructions.

  • Now, operations can be either registers as we saw before.

  • Registers A, B, C and D.

  • They can be memory addresses, location somewhere in memory, and they can be Constance numeric values.

  • How a simple process was only going to have four instructions, So the first is going to be moved.

  • Which moves is going to take to our friends on it.

  • These are going to be the destination on the source on the point of the move.

  • Command is it moves contents from the source location to the destination location Well, let's have some very simple mathematics, so we'll have, add and subtract.

  • These are up codes on Dhe, the first opera, and is going to be the Register, which were targeting with the addition on the second, our friend is going to be a value or register, and the fourth instruction is going to be a jump and the first opera and is going to be the condition on which we jump and second off brand will be the location of where we're going to jump to.

  • This will become a bit more clear when we start writing some code later on.

  • RCP could only move stuff at stuffs, obstruct stuff on job to a different location.

  • Is it very useful?

  • Well, probably not, but it is enough to do some more advanced functions.

  • So how can we multiply a couple of numbers together?

  • For example, if we had three times 10 well, we know that if we sit in a loop, we can simply add 10 to itself three times.

  • So let's have a look at the program for this.

  • Here's a little example program I've written using our new language to multiply two numbers together, but I'll need to explain some of the syntax to you first.

  • Clearly, here we have the op code and we have our two operations.

  • So the first opera and here is our A, which we saw in our CPU means register a second opera I've put in square brackets and this means we want the source of the move to be from memory address number three.

  • Unlike this one here, where I have not used the square brackets are what I'm implying by this thing.

  • This up around is that we want to move the value zero into register, see, so we can load in a constant value or we can read from memory.

  • Looking back at our ram, I'm going to assume that our memory has now been populated with some values.

  • Eso we can execute our program in this case, we want to multiply 10 by three.

  • So these air already stored somewhere in memory.

  • Now we're going to do something very old fashioned and execute this program by hand.

  • So we know the first thing here we're going to do is read from Location three, which has the value 10.

  • What's that puts into r A.

  • The value 10 on the next situation we want to read in from memory into registered Be, which we know is going to be three.

  • This time.

  • The next instruction was simply setting a counter to zero.

  • This is going to store our result.

  • The ad instruction here will take our A and add it to what's already in our C.

  • So in this case, we've got our C equals R C plus R A, which in this instance is, of course the same as saying zero plus 10.

  • So are sea.

  • After this instruction equals 10.

  • Likewise, the subtraction instruction here sub subtracts the value from the current register.

  • So here we've got a B equals B minus one.

  • But it's a value this time.

  • We're not using another register that equals three minus one equals do.

  • Now with the jump instruction, we're going to see something interesting.

  • Let's look at the aromatic logic unit in a bit more detail.

  • Simply put, it takes two values and an instruction on performs the operation.

  • So in this case it could be our registers or Constance.

  • The instruction is the add or subtract, and it gives us a result.

  • But it also updates the status register the state is registered contains some interesting things based on the result of the calculation.

  • It's just performed.

  • So if I do a little bit of pseudo code here, we could say if results is equal to zero, then set zero bit else don't.

  • In.

  • Our simple process in this state is registered only contains this one bit whether the results of the previous computation was equal to zero enough All the bits that are commonly found in stages registers include overflows carry bits past the result of the computation gone out of bounds of the number of bits allowed to start the result.

  • You can also include errors in the safest register.

  • So just divide by zeros going back to our program.

  • We can now try and understand the jump statement a bit more.

  • In this case, the condition is looking for not zero.

  • So remember the zero bit was set by the result of the calculation we can see in this case it was too.

  • So it is not zero.

  • So the jump condition is true on we tell it to jump back to address 13.

  • So this is going to loop background appear r C is now our C equals 10 plus 10 and I'll be is now to take warm.

  • But we can see this is still not a zero.

  • So again we jump background this situation weaken CRC equals 20 plus 10 which equals 30.

  • So we know we're getting close to a result.

  • Now on on the next line down I'll be is now one take one which is equal to zero.

  • And that's important because now on our jump instruction, the result waas zero.

  • But we're checking for not being zero.

  • So in this case we don't jump.

  • So on the final instruction we just carry on with the next one, which is finally a move instruction which says whatever is in register see here we're going to store in memory address five register See already contains our 30 which is the result on we're going to store that in memory address five somewhere in Ram.

  • So when the writer cares 30 the result is written to that address location.

  • So we've done a successful multiply on this program demonstrates what is typically assumed by most people don't really know assembly line, which is that they think it is hard must because we didn't have a multiply instruction to help us out.

  • So we have to do it the long way around.

  • If our little CPU did have a multiply instruction, we'd only need to call it the once.

  • We wouldn't need to set up a loop.

  • We wouldn't need to manage the registers as much.

  • Let's now consider a slightly more real world example.

  • So I'm using of the visual studio environment here to look at some X 86 compiled assembly language.

  • This isn't going to go into the very minutia of the language or what's going on, but it will show you how you can actually use the environment to study assembly language.

  • First we want to do is run it in debug mode.

  • So I would like to highlight the line and press control in F 10 on.

  • What I've set up here in the debug window is if I get it to show first of all the disassembly, it goes a bit small, and I can't zoom in unfortunately, visual studio.

  • So you'll have to bear with me on that on in the registers.

  • I've got the well, I've got the register window viewing at the bottom here so we can see that in the X 86.

  • And it's most simple level.

  • We have some registers.

  • E x p b x e c x e d x.

  • These are like a R a, R, B, r, C and R D that we had before, and we've also got some flags.

  • Well, let's have a look at the code that it produced so we can see from my line of C code here the end A equals 10.

  • We stored the value 10 in the address pointed to buy a using a move command just as we did before.

  • So it's obviously in Hexi decimal here.

  • The same goes for B was storing the value three at the location pointed to buy the variable B.

  • Let's have a look at how it handles the instruction, so the first thing is doing is loading from the memory were variable a start.

  • It's loading it into E a X, and we can see here down here in the register window.

  • That X has now been set to 10 in hex, of course, which is a The X 86 does have a multiply instruction, and we can see it uses the E X Register, which is preloaded with the value 10 on its using directly for memory, the value stored wherever the variable B is.

  • So the disassembly window here doesn't show you all of the memory addresses of these things because it would be inhumane, quite frankly, to debug these things.

  • So it does actually use the symbol that I provided in the code was very handy.

  • And so it will do.

  • The result on the result is gone straight into E X.

  • And so one e is in fact, the hex decimal 4 30 on we store now, just as we did before.

  • With the move instruction again, we store back into wherever location C is in the memory.

  • We store the value of Register T a X, which is the result of the equation.

  • So this program does exactly the same as the little toy program we created earlier.

  • Let's have a look at some differences between the CPU architecture, So if I choose to operate in the floating point domain now instead of the interview, how does it resolve this?

  • Let's take a look, and when we look at this, we start to see why Assembly language has a bit of a reputation for being tough.

  • What is going on here?

  • Well, let's just have a look at the commands that are being used first.

  • So the first thing we could see there is a move command.

  • It is a slightly different move command to before, but it's still the same things.

  • The principle is the same, and we could see.

  • Actually, there is a multiply commands down here, but everything else looks a lot more complicated.

  • Why is this?

  • The X 86 processor has some extension specifically for dealing with floating point on.

  • The compilers recognized that our code could benefit on be faster from using these instead.

  • In fact, the extension it's used here is the SSC extension, so we can see that the Register xmm zero is on here.

  • So this is an extra set of registers just on the CPU, specifically for handling floating point data.

  • Let's step through and see what happens.

  • This time I've expanded the registers window to the side, makes things a bit clearer on we can see appear in the These are the roar register values.

  • But the de Booger also provides the floating point equivalent, so that will make things a bit easier for us to follow What's going on?

  • So the first instruction was load A with 10 on because we need to use a specific register.

  • The first thing we have to do is load our data into the into the process.

  • Er so we've loaded in now are valued 10 here, which, of course, is all in now.

  • Exponential notation We can load in our value.

  • Three.

  • In this case, we're loading into floating point register xmm zero and then was storing the contents of that registered back into memory.

  • So here we've got the actual the guts of the calculation.

  • The first thing he's done is load the memory now pointed to buy a into x mm zero, which is 10.

  • We can see here in exponential notation that it's done it and then it multiplies directly the register with itself on the value from beast.

  • This is three operations going on here, not just two and straight away.

  • We've now got the result 30 it stores that result back out in memory.

  • Even though this looks horrific, we can disable some of these compile a settings so we can use simpler.

  • And although extensions of the CPU.

  • So if I go into the project properties for this program and go to the code generation, I can specify which sets of extensions are accused for floating point calculations, so I'm going to disable them entirely.

  • Go say you can't use any of the more advanced features.

  • Let's see how it compiles the code this time, the first thing we can see it.

  • It doesn't use all of these complicated register, so I'm going to get rid of these from the view.

  • In fact, what it does uses the really old school Intel X 86 Floating point registers Andi.

  • This uses a slightly different opera and on up code architecture, So the first instruction was a floating point load F L D.

  • We're loaded in the numeric value.

  • It then goes and stores that value in memory.

  • Again, we sleep see exactly the same notation used for memory.

  • Then we're going to do exactly the same for three.

  • We do not tender enough three and then we'll have a look at the calculation.

  • It works a little bit like a stack in this case that it now is going to read from the memory address pointed to buy variable a with the f l D floating point load.

  • You can see here that the register on this ah s t zero I'm not quite sure the S t stands for but it makes sense to think of it in a snack.

  • In this sense.

  • So we've loaded in Ah, the value 10 in exponential notation on Now we're going to multiply directly by the value Store the memory three, which gives us our result 30 and then we store that back out in SI We've now looked at two different ways of compiling the same program.

  • Just using different compile flex.

  • So we're choosing which bits of the CPU we want to perform computations.

  • What's really sure is that yes, Even though the notation has changed slightly, there's still a common thread.

  • There's still some familiarity between each of the extension, so we can we can work out what's going on.

  • We can follow.

  • The code is being executed.

  • My system level architecture diagram here is a little oversimplified.

  • For example, if the process that can only talk to the memory, how does it do anything useful?

  • It's just doing calculations and storing it back in memory well, on quite a lot of architectures.

  • One thing to do is to have memory addresses, which have specific functionality.

  • Let's consider a piece of hardware.

  • There's monitoring a temperature sensor, for example.

  • The temperature sensor is filling an area of this memory on our central processing unit is using its boss to read from it.

  • Likewise, let's say we wanted to set the brightness of an led at one way to do it is the CPU will write to a known memory location, the value that it wants and some some extra hardware will go away and deal with the brightness, knowing that you've got certain parts of your memory allocated.

  • Doing hardware stuff does make using memory quite tedious and tricky in Windows and other operating systems.

  • What kind of used to just letting the operating system handle that force?

  • We just give us a memory, and it goes, thank you very much.

  • Here you are, but on an embedded platform, it's very important that you know what these locations are.

  • We do occasionally still see this in windows.

  • For example, if you install more hard work, you will see the amount of overall round decreasing.

  • Somebody put a big graphics card in with the gig around.

  • It's not uncommon to see a system around decrease by gig.

  • Because it's Matt.

  • That's address space over to the graphics count.

  • I will emphasize that what we're seeing today is a very simplified view of assembly language, although it should give you a taste of how useful it could be, you might think, Why would I ever need to program like this?

  • Well, on new systems systems that haven't got any software written for them already?

  • If you want to build drivers, for example, for hardware, somebody still has to do this level of coding.

  • The compiler simply doesn't exist.

  • They used to be an argument that you could write much quicker on more optimized code using assembly language.

  • Although this is debatable now, the power of today's compilers is is quite surprising on often will create more optimized code than the program of thought that they could do in the first place.

  • Even though there are professional program is out there that will never need to see a Sindhi language.

  • It's good to have an appreciation just how much work the process of is doing to do what they consider to be a simple thing.

  • Having appreciation about just how many clock cycles are required to execute your pretty logo on the screen could only be a good thing.

  • I quite like assembly language, and I think I'm gonna do more.

  • Videos on this topic in particular like to create a virtual machine that executes are very simplified assembly language used in the example.

  • Potentially, we could take that even further.

  • We could implement it in V HDL and release on F PGA, so it's no longer a software machine.

  • It's the genuine physical processes running our own language.

  • I don't know.

  • We'll see where it goes.

  • One thing I'm sure of when we start talking about a semi languages, some people can get very excited about it.

  • So if you if I've said something that upset you or if you agree with something, please say so in the comments.

  • If you found this video useful, give me a thumbs up.

  • Subscribe.

  • We'll see you next time.

  • Thanks for continuing to watch.

  • This isn't code related, and I'm not going to get into the habit of wearing my personal views but the recent events in Manchester have hit a little too close to home for me to ignore.

  • Until recently, I lived and worked in Manchester City Centre for 12 years and still have many close connections to the city.

  • Clearly, the cowardly act of terrorism was abhorrent, was rightfully been condemned by the international community.

  • The thing with Manchester, those perhaps its citizens are the embodiment off cultural acceptance.

  • Just days after the attack, Manchester hosted the Manchester Marathon.

  • What crowds of hundreds of thousands of different races colors, creed, sexual orientations, religious persuasions got together to celebrate charity.

  • I can't think of a better way to say up yours to those who wants us to live our lives according to the bizarre, myopic and hate fueled doctrines.

  • Well, don't Manchester?

hello.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it