07 Switch Statements


[MUSIC]. Hello again. Now that, that we know how to implement conditionals and loops in assembly. Let's see how we implement a little more complex control structure. we talk, we're going to talk about switch statements. And the way switch statements work in C is as follows. You have an example here. If I say switch x, what I'm saying is now there's going to be a bunch of options that depending on the value of x, I'm going to execute a different piece of code. Okay? So here in our example if x happens to be set to 1, we execute this piece of code here. And then this break statement just says that this piece of code is going to be executed. And then we're going to stop and continue executing past the end of the switch statements. There are some other cases called fall through cases that do not have a break statement. For example, if x is happened to be set to 2. This piece of code is going to be executed and is going to continue executing through the next block, until it see's the next break statement. Then [INAUDIBLE] jumps it out of the switch and continues executing. Okay? And now so if you have any missing values in a set cases, for example if x happens to be set to 4, since there's no value, no case for 4 here. What gets executed is the default value. So the default block of codes. So, the way to think about the default set is there, if the value that you're switching on is not in any of the case. You have to view the default case. And why could implement this with branch instructions that we say earlier. It gets really complicated quickly, so another way of oftentimes and this is using what we call a jump table. And a jump table works as follows. Say here we have our example we have an example of a switch statement. Now we going to have a table that has roughly as many entries as the number of cases in our, in our switch statement. The number of cases now in the switch statement. It's, now in each one of these entries the table points to the address of a code block. Which is course, corresponds to the code block that has to be executed, based on the case value. So, for example, if our x happens to be val 1. we're going to jump, we're going to look at the jump table, get the corresponding address. And select the tag1 and that's the code that we're going to execute. We're going to execute this piece of code here. now what we are going to think about this is the following. You could have a variable called target which holds the target address for a given option. We're going to look up our jump table, which is just an array of instruction addresses. We're going to index that array with x and we're going to get the value storing target and we're going to jump on it. We're going to do a Go To and use that as [UNKNOWN] Go To and to jump to that address, go to that address. So, these are the way of looking at this more visually. I have another switch statements in fourteen other cases I have the corresponding vision of memory. Just in the color matching there and then our jump table is going to map back to the corresponding To the corresponding memory region that implements that block of code corresponding to the, to the case statement. So, now 1 is green, so that means that the [INAUDIBLE] 1 here has to point to green. That's this one. And same, same thing for orange and so on. Okay? Now notice an interesting case here, which is the default case. The default case, as I said before, gets executed if none of the value if, if the value of x doesn't happen to, happens to be none of the values explicitly defined by cases, okay? So, here it happens to be 0, right? We're going to jump to the default case, and also happens to be four. We're going to jump to the default case. Okay? And we can use a jump table as long as the value is lower than six. For any other value that's greater than six, we go to execute the default code. The default block. for this jump table, just a list of addresses. And he's how we get setup. in assembly, you would see a declaration of long words that point, that happen to be the same value as the label that maps to the address of the code block. So now this, for example, if we take .L61, that's where the default code block starts. So, the corresponding structured address is going to be stored here, and same thing for the other case values. So, let's see how we use this table in assembly now. so here we have the jump table. And what we're going to do, is we're going to, depending on the value of x, which is loaded in edx here. We're going to find the corresponding entry. And the way this is done, this is done using a indirect jump. The indirect jump says that, instead of jumping to specific address, it jumps to an address containing an address. So, the way this works is this .L62 here, is the base address of our, jump table. Now the value of edx gets multiplied by four and [UNKNOWN] because a long word is four bites. So, if the x is say four, we're going to add zero, we're going to add four, one, two, three, four. And that's the value that's going to be, jumped to. 'Kay? So, another thing to note, here, is that we are comparing whether x happens to be greater than six. And if it greater than six, it jumps straight to default. So, now lets see how the code blocks are implemented. So here, suppose that we have we're interested in case two here. And in case two happens to be mapped to label .L57. It means that this is where, the assembly codes that implements the block for case two is located. So, we have a division here, so that's why we must have a division instruction. And note that this a fall through case, so it's going to be in case x is 2, is going to be executing. And it's going to continue executing through the next case block, until the break statement. And the break statement here is implemented by essentially just finishing the execution. Returning from the function in which the switch block is inserted. And note here that the default case also, after this there is a return. So, if you look at here in label .L61 you're going to see here. That after, right after being done with the corresponding code block, we also leave and return from the function. So, here's the, the rest of the code block for our example. again you see, the important thing to notice here is that, you have returns after every single block that had breaks. And for both case five and, and case six, they're both mapped to case L60, which is the label right here. Now let's look at how these labels are defined now. If you look at the object code, they, you see that they become an instruction [INAUDIBLE] before as chosen by the linker, okay? And there'll be one of those for each one of the the case values in our switch statement. So, if you go look at entire table you see there will be one address for case statement. And those have two as we saw before put points at a code block. So now we can look at each one of those you see mapped to a block of codes corresponding to the code inserted in the, in the case block for that value. so here's our, our, our list of instruction addresses for the jump table, and each one of them points to a block of code. And notice that we have some repeated ones. Remember that some, some of the cases has, for example, follow through or cases that are not defined. So, in this case here it's, it's the default value. For value four, if you remember it has to go through the default case, so it points there. So the same thing for value zero. Now let me ask you a question. Would you implement this one with a jump table? Well, look at the size of this value here. Probably not. This table would be very, very, very large. Okay? So, jump jump tables are, are, are used normally when you have few case values and you can build a small table for it. In this case here, it would be much more efficient to just implement this with branch instructions. So, now we conclude our exodus six assembly programming section. And I'll see you next time.

Wyszukiwarka

Podobne podstrony:
overview sirius position switches 07 en
12 control statements
07 Charakteryzowanie budowy pojazdów samochodowych
9 01 07 drzewa binarne
02 07
str 04 07 maruszewski
07 GIMP od podstaw, cz 4 Przekształcenia
07 Komórki abortowanych dzieci w Pepsi
07 Badanie „Polacy o ADHD”
CKE 07 Oryginalny arkusz maturalny PR Fizyka
07 Wszyscy jesteśmy obserwowani
R 05 07
07 kaertchen wortstellung hs

więcej podobnych podstron