Hello, this is Dr. Miller, and this
is absurd. Fourteen point seven of
assembly.
Today, we're going to create a jump
table in assembly,
so assembly jump table,
so one of the things that we're
going to do is we're actually going
to define data in the code section
and I'll show you how to do that.
And then we're going to write a
program that calculates
whose turn it is based on
a schedule based on the day of the
month.
So we're going to alternate
between the three persons, A person,
A, B and C, so if
the modulus is zero, it's going to
go to the first person.
If it's one, it goes to the second
person. And if it's two, it goes to
the third person.
So I've already went ahead and
created our project here,
and so at the top, we're going to
create a couple of different prompts
that we're going to have.
So the first one
enter the
day of the month.
Make sure we know terminate it and
then we have one for each of our
persons.
I'll put a nice new line after
it.
Do some copy pasting here so
that we don't have to type the same
thing over and over again.
We got A B and we got C,
and then we're going to have one
more.
And we'll put none.
And we'll do a sanity check on that
one.
All right. So then we're going to go
into our code section here and
then we're going to do a
move, our first prompt in there and
then print it. So move the X
text,
all print,
string,
call, read.
And so when we call, read it,
we know that Yak's then
has our value.
And so what we're going to use here
then is we're going to do some
division on that.
So movie six
three. So that's going to be our
modulus
operator that we're going to run on
move MDX zero,
got a zero that out and then we'll
go and divide what's in italics
by SBX.
And then if you remember, Ed
has our modulus,
so we'll just copy that.
Into DCX.
And so we might want to do a
little comparison in here later,
making sure that our mojos
is correct, but here we'll just
so we'll jump greater than or equal
to. So we shouldn't get anything
greater than that.
But just to make sure
and I'll just call it defaulter.
So down here, we're going to create
our default label.
So we'll use the other value
in that, and so then with all
of these, when we are
done,
we're going to move,
we'll call print string.
To print off the label to get set.
So in each of our cases, we're going
to set a label.
No, go ahead and call print string
and call print
new line at the very end
so I could put a jump done here.
But it's just going to fall through,
so there's no use doing that.
So now we're assuming that we've
gotten a number.
That is what we want.
And so now it's time to
we'll build our table and then we'll
do our comparisons on that.
So in here,
I'm going to
just call a table.
You can call it whatever you want,
but we can actually define things.
So I'm going to say X,
Y, and we have this.
And so they look nice
and you can pick whatever names you
want here. But these are also going
to be label names.
And so it's going to have those
addresses inside of there.
So I say X
and I'm going to say move X
person one
jump done.
Right. So we have our done label so
that it will go ahead and do our
print.
So now I'll define why
x p to
be done and
Z
AXP three
done.
So we have our labels here.
And so when we define this table,
this is going to be each one of
these is a pointer to one of these
labels inside of there.
So we will do our calculation.
We will then jump to the location,
point in to buy this and then these
each point to a very specific
memory location
where we'll start executing code.
So now it's time to figure out our
table offset.
So first we're going to move into
UTX,
the offset of the table, so
we know where that is.
I'm going to move into to the number
four. So this is how big each entry
in the table is.
So this is very specific to thirty
two bit.
If it was sixty four bit we would be
doing eight, but a thirty two
pointer is going to be four bytes.
And so that's why that's coded in
there.
And then we're going to multiply
by X.
So X should have our
number, it should be zero one
or two.
And so when we multiply that by
four, we'll get an offset of either
zero, four or eight
for our offset.
So then we can add that
yak's
to SBX.
So we'll just use Ebix
because that has the offset of our
table and then we're getting the
offset of what entry
do we want inside of there.
So we're going to add X
and then we'll go ahead and jump to
SBX. So we could have done it either
way.
It really doesn't matter.
Could have taken the offset and
added the table or vice versa
either way would have worked.
But what we're doing here is we're
going to jump to Ebix, but we want
to jump to the location pointed to
by SBX.
So we had our address of our table.
So we'll then get the data, either
having the pointer for X, the
pointer for Y or the pointer for
Z.
And so that is how we would
implement that.
So now we should be done.
So if it's the first of the month,
it's person B, if it's the second in
a month, if it's the third of the
month, depending on
your definition of when a month
starts. So most people would use the
numbers one, two or three
for the days of the month, not zero
through the number, minus one.
And so then we can see that if we
pick a higher number, so let's say
15, well, that
made three is zero.
So that would be personne or if we
did 17,
that would be Mod Mod three would
give us two.
And so that would be person C.
So today, we created a jump table
in assembly and we defined our
pointers inside the data section.
We could have put inside the code
section, we could have put them in
the data section.
But it makes more sense and it
aligns with how we
typically would do it
when you would see compiler
generated code.
So looking forward, we're going to
talk about function pointers.
If you have questions, you can email
me Miller, MJ at you in
Keda, Edu, and you can find me on
Twitter at Nilus 30.