13 hours 15 minutes
Hello. This is Dr Miller in this is episode 14.6 of Assembly.
Today we're gonna talk about switch statements and see and then the implementation of those using jump tables,
switch statements and see.
So a switch statement allows a programmer to easily choose which block of code that they want to run
and by easier. It's this in taxes, easier to read than a group of If else blocks.
Um, the examples of inputs that can be used are based on discrete cases. So, for example, characters and managers where we don't have any rounding errors, that can hinder our choice.
Um, and we cannot use things like floats or doubles.
And then additionally, we have a default case that if none of the other cases match, then we can do that explicit thing.
So here's an example in C, so we can see that we've allocated
a energy called X using Malek, and that's on the heap.
We then do a scan up on that and then
use that value or the value of X to decide what value be will be,
in this case, the value of the X in the value of be of the same. But the point is to show you that this is how you would issue a switch.
I'm Otherwise you'd have to do a series of three if else and else ifs in order to get the same functionality and see.
We can see that if we added a fault case
that if it doesn't match any of those So, for example, if X was zero or four or any other number besides 12 or three, we would get the default case in that case had set
be equal to 99.
And then we also said we can use characters. So here's an example where we use characters so we have the characters A, B and C, and again we can choose any of those and set our value for B. Or we can have a default case.
So jump tables
so the compiler will generate a jump table. Um, if it
determines that that is the most efficient way to go.
So we have different metrics on how ah, particular set of if else is woodwork. And so the compiler makes a choice in to either generate a jump table or not,
and some of the the thoughts that go into it are, how many cases are there? Are those cases close together?
Um, are there breaks after each case? So do we have a bunch of what we call fall threes?
So what a jump table is on the back end is just an array of pointers to some program code, and then the selection of that pointer is calculated using mathematical functions. So multiplication or addition,
and then doing this is going to be faster than if we have to do a bunch of consecutive
So, for example, I put here together a switch statement like we had before, where we have our cases and we can see that if we have four cases, we can see that it's doing a bunch of compares and jumps. So compare jump equal compare jumped greater than compared jump equal compare
jump greater than so it's using all of these in order to make the decision,
and so we can see. Then each one of these labels L two through L five
is a different location,
and it's going to just do the jump to that and then execute that code so we can see the move off one the move of two in the move of three in the move before.
But what happens is if we add one more case. So he had five cases. So we got cases one through five again these air all consecutive,
and so we can see that instead of all those, if else, is that there's a couple compares. So there's a compared to the number five to see if it's greater than that.
And then it basically goes in and does a multiplication. So it takes E X times for so we know that each one of these Longs is going to be four bites. And so then it does the multiplication in order to decide which one of these it does
so moves the correct pointer into Yay X and then calls Jumpy X
so we can see that the number of instructions that we have in order in the worst case and in the best case is
it's always better to do this this jump table than it was to do all of our if else's,
because we could have to go through all of the FL does if else's.
But we can simply just make a jump here.
And then we can see, though, if we had the default case down here and we're using characters again, we can see that multiplication by four, for our offsets were loading are offset into yea X and then copying the pointer into yea X and then doing a jump.
Um, and then we can see Here are different pointers that point into the different spots, and we can see our default case down here
again easily. So when you reverse engineer the code, you can definitely see it says B is equal to 99 so you can see that that again is our default case that get set.
So today we talked about switch statements and see. And then we talked about their implementation using jump tables in assembly.
In future elections, we're going to create an example. Jump table in assembly.
So here is our quiz. What is a jump table?
It's an array of pointers to program code with a selection of the pointer is calculated using mathematical functions.
If you have questions, you can email me Miller, MJ at you and Keita et you, and you can find me on Twitter at Milhouse 30
How to Use GDB (BSWJ)
In this part of the Breaking Stuff with Joe (BSWJ) series, Joe Perry demonstrates how ...
Certificate of Completion Offered
How to Use IDA (BSWJ)
The IDA is used throughout the IT and cybersecurity industries by exploit developers, vulnerability analysts, ...
Certificate of Completion Offered