Hello. This is Dr Miller, and this is Episode 14.9 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 gonna write assembly that reads in a number and then if the number is one will multiply our second input by two.
Otherwise, if it's not one, there were in a multiplier. Second input by eight.
So I've already went ahead and created our project here.
So we're gonna have information inside of our functions that's going to do a couple different things. So we're going to go ahead and print the number,
and so at the top will go ahead and start with some strings. So we got s one,
so we're gonna have to functions. One's gonna be named Fu, and the other one is going to be named bar.
That s one and s two. So this one's gonna be bar,
and they're both just gonna print off their imagers.
We're also gonna have our function pointers. So have ah p one.
Now I'm doing 32 bit assembly. So we're using d D for this
gonna point to bar. So we're gonna have our functions, food and are functions bar.
We're gonna go ahead and will write those functions first. That'll be easiest.
So I got my function. Fu push GDP
and then we'll do movie S p E b p at the end here, Paul E B p
So that's at the end. We're gonna take one argument for our function. And if you remember for arguments, it's going to be e v P plus eight.
Go ahead and move into yea x you
the value of that argument. So e v p
plus eight hadn't moved that into,
um E X. And then we said We're gonna multiply by two so I can just to shift left
the a X ones Little mole by two.
It's quicker and easier to do that.
We're then going to dry and do our prints.
So go ahead and push on
We're then going to move into yea X are string that we went to print
push that onto the stack and then we're gonna call print F.
Normally, I'd correct the stack here, but at the ah epilogue of our function, we already correct a stack,
so we don't actually have to correct it in here. It will get corrected. And I forgot my ea X there.
All right, so we're gonna push 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 death.
So we have, um, the gist of our function here. So I'm actually just gonna copy on all of these lines, and I believe there 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? Why again,
um, that I'm going to go down here and hit p for put.
So that will put our function in here. So now we're gonna create our function called Bar.
So Bar is going to do similar things, except we're gonna shift by three. That's multiplied by eight,
and then we'll use string s too.
So now we've got our two functions,
So now we have to use them, so we're gonna go back into main. Uh, also, we need to make sure you use print death. So
And so now we're gonna use our read int
to read the number from the user bred into a compare of yea ext one
well defined X down here
an actual go ahead and move into E v X. Who,
in here? We're gonna move e
bar into ebx. I'm using EBX because I am going to call Read it again.
So I go and read our second number
and then we'll push it onto the stack is our first argument.
So the results from you, Dent is and yak. So go ahead and push that
well, I'm going to call you B X and then we're going to correct our stacks. Add yes, p
four in order to correct the stack,
if you'd like, you could go through and Princeton prompts off. I grew in a headwind with simple one.
But see here where we are copying the address of the function fu into E v X And here we're copying the address of the function bar into ebx
And so we're gonna go ahead and use those. We could also use P one or P two, so either one of them,
So then we should be able to run our program.
So if I enter 11 it's gonna multiply by two. And if I enter to one,
it's gonna multiply by eight. You know, pick whatever it is. So
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 whichever function we wanted to,
ah 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
I'm toe where it needs to go. And so that's a dead giveaway that we've got, ah, 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 in line assembly
If you have questions, you can email me Miller MJ at you and Kate. I e d u. And you can find me on Twitter at newhouse 30.