Hello, this is Dr. Miller and this
is Episode fourteen point six of
Assembly.
Today, we're going to 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 buy easier.
It's the syntax is easier
to read than a group
of Ifill's blocks.
The examples of inputs
that can be used are based on
discrete cases.
So, for example, characters and
integers where we don't
have any rounding errors that can
hinder our choice
and we cannot use things like
float's or double's.
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 and see so
we can see that we've allocated
a integer called X using
Malac, and that's on the heap.
We then do a scan F on that and
then use that value
or the value of X to decide
what value B will be.
In this case, the value of X and the
value of B are the same.
But the point is to show you that
this is how you would issue a
switch.
Otherwise you'd have to do a series
of three if else.
And Elspeth's in order to get
the same functionality in C.
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 one, two or three, we
would get the default case in that
case had set B equal
to ninety nine.
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 if
it determines that
that is the most efficient way to
go.
So we have different
metrics on how a particular
set of if Elsas would work.
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?
Are there breaks after each case?
Do we have a bunch of what we call
falsus?
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
comparisons.
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 compared jump equal compare,
jump greater than compare, jump
equal, compare, jump
greater then.
So it's using all of these
in order to make the decision.
And so we can see then each one
of these labels, L2
through 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 of one, the move
of two and the move of three and the
move of four.
But what happens is if we add one
more case, so we had five
cases, so we got cases one through
five, again, these are all
consecutive.
And so we can see that instead of
all those if Elsas that
there's a couple compares.
So there's a compare 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 X times for.
So we know that each one of these
longus is going to be four bytes.
And so then it does the
multiplication in order to decide
which one of these it does.
So it moves the correct pointer into
X and then calls jump 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 elsass, because
we could have to go through all of
the EFAs forces.
But we can simply just make a jump
here.
And then we can see that if we had
the default case down here
and we're using characters again,
we can see that multiplication by
four for our offset.
So we're loading our offset into
yak's and then copying the
pointer into X and then
doing a jump.
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 ninety nine.
And so you can see that that again
is our default case that gets
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's 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 KSTU
and you can find me on Twitter at
Milhouse 30.