Hello, this is Dr. Miller and this
is Episode fourteen point nine of
Assembly.
Today, we're going to create and use
a function pointer inside of
assembly.
So our assembly function pointer, so
what's our problem to be solved, so
we're going to write assembly that
reads in a number and then if the
number is one, we'll multiply our
second input by two.
Otherwise, if it's not one
that we're going to multiply our
second input by eight.
So I've already went ahead and
created our project here.
So we're going to have information
inside of our functions that's going
to do a couple of different things.
So we're going to go ahead and print
the number.
And so at the top, we'll go ahead
and start with some strings.
So we got S1.
So we're going to have two
functions, one is going to be named
two and the other one is going to be
named Bar.
Like I S1
and this, too.
And so this one is going to be bar
and they're both just going to print
off their Energous,
we're also going to have our
function pointer. So I'll have P1
now. I'm doing 32 bit assembly.
So we're using DD for this
and P two,
it's going to point to a bar.
So we're going to have our functions
and our functions bar.
So we're going to go ahead and we'll
write those functions first.
That'll be easiest.
So I got my function full
push EDP
move, EVP
E.S.P
set up our function and
then we'll do move E.S.P.
SBP at the end here
pop SBP.
And then return,
so that's at the end, we're going to
take one argument for our function
and if you remember for our
arguments, it's going to be EBP plus
eight.
So, Hayden, move into yak's
you the value
of that argument.
So SBP.
Plus eight, we hadn't
move that into
X, and then we said
we were going to multiply it by two
so I can just to shift left
yak's one Seattle
mall by two.
It's quicker and easier to do that.
We're then going to try and do our
prints.
So go ahead and push on
yak's.
We are then going to
move in to yak's
our string that we want to print,
push that on to the stack and
then we're going to call print F.
Now, normally, I'd crack the stack
here, but at the
epilog of our function, we already
cracked the stack.
So we don't actually have to correct
it in here. It'll get corrected.
I forgot my X there.
Right. So we're going to put you on
our two parameters.
This is our format string.
This is the value that's going to
get substituted in.
And then we go ahead and call print
F.
So we have the gist
of our function here, so I'm
actually just going to copy all
of these lines and I believe that
there are 13 of them.
So I'll go ahead and use the Yank.
So why?
Then type the number of lines you
want to copy and then hate y again,
then I'm going to go down here and
hit P for put,
so that will put our function
in here. So now we're going to
create our function called bar.
So Bar is going to do similar things
except we're going to shift by
three, so that's multiply
by eight
and then we'll use string as to.
So now we've got our two functions,
so now we have to use them,
so we're going to go back into
Maine.
Also, we need to make sure we use
printouts. So
parents, if they're.
And so now we're going to use our
resident.
To read the number from the user,
go ahead and do a compare of
yak's to one
jump zero x
one, define X down here
and Y
and done.
Jump, not zero.
Why
an X will go ahead and move into SBX
to.
Job done
in here. We're going to move
to.
Move Barr into SBX,
I'm using Ebix because I'm going
to call Relient again.
So go ahead and read our second
number.
And then we'll push it on to the
stack as our first argument.
So the result for me tonight is and
yeah, so go ahead and push that
and I'm going to call SBX
and then we're going to correct our
stacks at E.S.P
four. In order to correct the stack,
if you'd like, you could go through
and print some prompts off.
I we went ahead and won the simple
one.
But see here where we are copying
the address of the function FUX into
SBX and here we're copying the
address of the function bar into
SBX.
And so we're going to go ahead and
use those.
We could also use one or
two so either one of them
would work.
So then we should be able to run our
program.
So if I enter one one,
it's going to multiply by two, and
if I enter two one,
it's going to multiply by eight and
I'll pick whatever it is.
So if I have eight,
so eight times two is 16.
Eight times eight is 64, right,
and so we were able to
use our function pointers in
order to call whatever function
we wanted to inside
of our code,
and you can usually tell when it
has a dynamic function, when it says
call and then it has a register
listed, that means it doesn't know
ahead of time what function it needs
to call in order to get
to where it needs to go.
And so that's a dead giveaway that
we've got a function pointer when we
see call and then a register.
So today we created a function
pointer and we use that inside of
assembly and we saw how to
code that in such a way that was
usable.
So looking forward, we're going to
talk about inline assembly
inside of C.
If you have questions, you can email
me, Miller, MJF, you and Kadiatu,
and you can find me on Twitter at
Millhauser 30.