Video Transcription

00:00

Hello. This is Dr Miller in this is episode 14.6 of Assembly.

00:06

Today we're gonna talk about switch statements and see and then the implementation of those using jump tables,

00:14

switch statements and see.

00:17

So a switch statement allows a programmer to easily choose which block of code that they want to run

00:22

and by easier. It's this in taxes, easier to read than a group of If else blocks.

00:29

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.

00:41

Um, and we cannot use things like floats or doubles.

00:45

And then additionally, we have a default case that if none of the other cases match, then we can do that explicit thing.

00:53

So here's an example in C, so we can see that we've allocated

00:57

a energy called X using Malek, and that's on the heap.

01:00

We then do a scan up on that and then

01:03

use that value or the value of X to decide what value be will be,

01:10

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.

01:17

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.

01:26

We can see that if we added a fault case

01:29

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

01:41

be equal to 99.

01:44

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.

01:57

So jump tables

02:00

so the compiler will generate a jump table. Um, if it

02:05

determines that that is the most efficient way to go.

02:08

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,

02:22

and some of the the thoughts that go into it are, how many cases are there? Are those cases close together?

02:28

Um, are there breaks after each case? So do we have a bunch of what we call fall threes?

02:34

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,

02:46

and then doing this is going to be faster than if we have to do a bunch of consecutive

02:51

comparisons.

02:53

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

03:13

jump greater than so it's using all of these in order to make the decision,

03:19

and so we can see. Then each one of these labels L two through L five

03:23

is a different location,

03:25

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.

03:36

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,

03:44

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.

03:53

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

04:06

so moves the correct pointer into Yay X and then calls Jumpy X

04:12

so we can see that the number of instructions that we have in order in the worst case and in the best case is

04:17

it's always better to do this this jump table than it was to do all of our if else's,

04:24

because we could have to go through all of the FL does if else's.

04:28

But we can simply just make a jump here.

04:32

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.

04:48

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

04:56

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.

05:09

So today we talked about switch statements and see. And then we talked about their implementation using jump tables in assembly.

05:16

In future elections, we're going to create an example. Jump table in assembly.

05:21

So here is our quiz. What is a jump table?

05:28

It's an array of pointers to program code with a selection of the pointer is calculated using mathematical functions.

05:35

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

Up Next

Assembly

This course will provide background and information related to programming in assembly. Assembly is the lowest level programming language which is useful in reverse engineering and malware analysis.

Instructed By

Similar Content