Conditional Branching with Assembly Language

xor pd
A free video tutorial from xor pd
Low level technology training
4.0 instructor rating • 2 courses • 23,744 students

Lecture description

We introduce the CMP instruction, and the idea of comparing numbers. Then we discuss unsigned and signed comparison, and we introduce specialized instructions for unsigned and signed comparisons.

Learn more from the full course

Assembly Language Adventures: Complete Course

Learn the language of your computer

29:09:32 of on-demand video • Updated November 2019

  • Learn to code on the x86 Architecture using Assembly Language
  • Gain solid understanding about low level concepts.
  • Understand how your computer works
  • Become a tough person
English [Auto] Hi and welcome to another lesson about basic assembly. They were going to speak about some more conditional branching objectives of this lesson we learned about the compelling suction which is useful for number comparison. We will understand how to compare unsigned numbers and signs of using specialized instruction. The compelling stuff or simply we want to be able to compare the we could for example use some instruction and then followed by a J.C jump killing structure for example. This will allow us to compare two and find number let's look at an example code this quote something like. Yes. Yes. And then jumps the sled jumps to my left. The catalog is not set. This part of the code is going to be executed. And eventually in jump will be taken to the outside made. So basically you have two pounds of code. Two parts of composing code. The first part is here which is coming by we are. If x is greater or equal than ECF. The second code we are going to get this part of code is yes X is less than say this. So I can understand how it works. First a sixth. If x is small and x is no problem is going to accrue using the subtraction. So no k is going to happen. The killing is going to stay here. So yes it is six the Kerslake will be zero. And then the jump is not going to be OK. And we are going to behave and in fact we have X going to equal then say in the opposite case well actually see X is going to be bigger than x the current flows will be set after the subtraction. And then we are going to get these 12 called because the John Kerry my lead will jump to my melee that and invoke these. However there is a problem with some instruction some instruction over the lights of compiled file. For example here we just want to compare those two values of x and D-CA. However when using some instruction we override the value of x many times or in many cases we don't want to do. We just want to compare two values and say their favorite is still calling. We don't want to override our actual values if we compare the solution is to compare the we could use the comparing structure instead of the sum of the action the component structure it's just like the subject. However it doesn't throw the subtractions. So that means that the complete instruction is some kind of simulation for what would happen if we didn't subtract instruction affects the legs just like the saw with action. However it does not like any of the. And it does not actually calculate the result. It's is simply saying it's what will happen to the first is still if we use some instruction. So it's a pretty cool stuff. It is very useful to compare the signal to all the arguments of the compelling stockins looks like the way they compare them to listen while we call them here in the bookless taking the confirmed instruction subtracts minus the changes the flex calling. However it does not change any of the as we said earlier it is really useful for numbers comparison. Let's look at an example. This example is just like the NBA example. However this time we use compare instead of sub might not remember the previous example. So let's take a look at. Consensus here we have the Subang Locsin and in the nearest example we use the comparing to. The difference is that this time x is not a lever. This is really useful. For example we could use the value of x in this part of the code or maybe in this part of the code we don't have to recreate it all or maybe keep it somewhere else because content is not going to change the value of it. So it is a very useful instruction. And some in some companies compared several of unsigned numbers in some of this. We have already learned about it in the very first chapters. Well we spoke about the country and there was no sound in sight. Number one well consider an unsigned numbers for example. They say quality is Paul number which is composed of eight. It's is the largest possible unsigned number and obviously it is larger than the number one in some comparison. However when we consider the full complement this number composed of eight hexadecimal smaller than the number one. That is because number one is positive. However this number is negative in the two's complement of presentation. This number actually is a pleasant Smiley swamp and we know that mind is what it is supposed to be smaller than what it's a bit less. We have exactly the same number of our different interpretation of those numbers in different interpretation of those bits we get different results. In the first case we get that this is a presentation or this chunk of this is larger than this one. And the sound of the presentation. We get that one is larger than this number maybe of 8. So how could we know which method to use in each case. Sometimes we should do side comparison and sometimes we should use sun compass. This could be sometimes a bit confusing. That's at the center of it is our responsibility is for Gilmer's know what is the number of the little numbers you can tell her the meaning of unsigned numbers may become something that cannot be signed. Then some compare. So it's been rough for whether it's good as a possibility that you know now science for example maybe you count something that could be signed maybe the number of what said before something the temperature had to be negative. And so we call it in cases like this you should consider using psychopath's luckily only about some specialized instructions which type of comparison. So once you know which kind of comparison you want to have you could just about choosing the right instructions for the machine. Let's begin with sound. Composer. We would like to compare co-incide number by now we all know that we could change that by combining the comparing structure with the jump carrying stuff. Maybe we could even add the jumped to zero instruction if we wanted to check for example. Whoa whoa whoa whoa. Instead of dealing with the carrot and flags our selves. We have some way to use instruction. We had those feelings jump below jump below ecord jump and jump above this destructions are suitable only for unsigned comparison. That means that if you're compelled to number and then one with them by one of these toxins the underlying meaning of it is that you can signal to numbers and some Andrews probably we might do at this point although you probably demanded fuel cells. The report says so doesn't know and doesn't care about the meaning of the number of the screw inside the radius. The i'll only says that the you as a programmer should choose which instructions to choose one of those instructions. The underlying meaning of it is that you say about the novel of compelled as some. How do these instructions well. So we have a simple table that shows the instruction in one side and the condition in check. The other is you should be already familiar with this condition for checking unsigned confers. For example you might have something about them seeing them in one of the exercises. So far the first instruction jump below is just about checking the case checks the Kerslake said they carefully set the jump will be take the next instruction is a bit more complete could just below equal checks to condition at the same time which is very efficient. For example if you had to check those two conditions yourself you could take more than one instruction. However just below equal instruction done checks to condition at the same time. The jumble of equal instruction all jump will be taken if the Catholic is one well is this the sled is what. And it's not very hard quality. If we believe the first one the jump below which happens when the culprit is one then just below equal is just about anything. All the conflict equals one condition to get there. He he quote the instructions and just the opposite of the first two. Thus jumping is about checking that the Catholic is zero and zero is zero. Which is just the opposite of the jump. You know with you know about the more gun laws. Then you should know that the opposite of this statement is exactly the point jump is equal. Just the opposite. Don't know so we just reverse this condition and we get caliphate when they will. You've probably noticed by now that just below is just the decent lane. They jump constuction and jump about equal. It's just a different name for the jump look carries the we are talking about those two stuff in the then kobun of jump below and jump. K is exactly the same because we actually do the same thing. These are just different names for the same code. The same goes for jumpers both equal and jump no kid. Let's look at an example. You probably remember the first example that were more than that change the compelled if you remember there was some action here and now we use the components. We want to go all the making the example even more with the so we can use jump below instead jump here. I remind you from the lies that jump below is just different names. Foljambe OK so it shouldn't really matter. However it does matter for you as a human being. When you say this kind of code jump below you know immediately that what we actually do is comparing two numbers and then checking if X is below. However in this example it could be a bit trickier to understand what happened. We make the change in this example to this one by just changing the jumping stuff that I think love is everything we do. However there is no change in behavior of those friends are exactly the same and they will be according to the same numbers. The Proceso cannot distinguish between those two examples. However this one is more readable to human beings because this instruction says something about the meaning that you give to those two numbers. Say X and C I recommend to this kind of code over this kind of stop sign. Compare the second person could be a bit here. There are more cases you do. I mean by that. That in the Einstein case we just have small numbers and big numbers. However in the sun comparison we should also consider positive numbers and negative numbers which multiplies the number of k we can do. Compare and then check out cells. The sun overflow and 0 for each continuation of those flex will have some meaning regarding the result of the first. If we say for a moment that combination of sine overflow and zero is the co-author of service which is 8 option. This is pretty much in those options should not events point one bit which is about big-O or small. Or maybe a little less. Instead of dealing with all those combinations of less our sets which could be cumbersome and maybe even a small one is not talking about very hard to remember. We can use some of need to use in structure just like the unsigned compilation case we have exactly. Instructions to be with the same person. They haven't given names but they have a different formulation from instructions of Jampolis let me quote jump greater and jump from Clayton. I want to remind you that in the Einstein's Covelli said we with different growth will be low and jump rope and we will jump less and jump. You should remember that jump ball and jump below. He's about Einstein comparison and on the other side jumps less and jumps later. He's about psychopath. This is important to remember. How do these instructions go. This time we have some really strange conditions. Check it for sight you might be surprised looking at those conditions. Take a second to check out the conditions. Check each of those instructions as shown in this tape. One basically we can say that the use of the Zufall is mostly the say in the sun composts on the Einstein you probably can recognize some kind of similarities between this table and the Einstein comparison. We can also see for example that this instruction jumpgate is actually the opposite of this stuff Giampaolo. And we can also see this flattening the condition. This condition is actually the opposite of this condition. You know a bit about the middle of this would be obvious or so that jump like of equal instruction is the opposite of the jump Les's structure. And this is also reflected in the conditions in these condition. This is about equal we can say for example that the only difference between jump greater and jump of equal is that jumpgate has this additional condition to make sure that quirky doesn't help it. He just checked that the leg is not set which means that equality must not happen in difference between those two is that this condition is at he quietly becomes possible because we allow that the Zelalem will be won. This will be enough for this condition to have basically because we can actually understand the relation between all those options. If we managed to understand only one of them we can understand the other. Maybe it's the same if we can pull this one of those walk by the relations that we already understand between those instructions. No you needed only the other instructions. Do you call that maybe the biggest question here is what that sounds like equals whatever floats let me what what actually mean. This is the really today's condition because there is no obvious relation between the some float and the overflow. This is something interesting. There was some sort on this condition.