Function Pointers Example

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:00
Hello. This is Dr Miller, and this is Episode 14.9 of Assembly.
00:07
Today, we're going to create and use a function pointer inside of assembly.
00:11
So our assembly function pointer. So what's our problem to be solved.
00:15
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.
00:21
Otherwise, if it's not one, there were in a multiplier. Second input by eight.
00:28
So I've already went ahead and created our project here.
00:32
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,
00:39
and so at the top will go ahead and start with some strings. So we got s one,
00:52
so we're gonna have to functions. One's gonna be named Fu, and the other one is going to be named bar.
01:00
That s one and s two. So this one's gonna be bar,
01:06
and they're both just gonna print off their imagers.
01:10
We're also gonna have our function pointers. So have ah p one.
01:15
Now I'm doing 32 bit assembly. So we're using d D for this
01:19
and P two
01:23
gonna point to bar. So we're gonna have our functions, food and are functions bar.
01:26
We're gonna go ahead and will write those functions first. That'll be easiest.
01:30
So I got my function. Fu push GDP
01:34
Movie VP Yes,
01:37
GSP
01:40
set up our function
01:42
and then we'll do movie S p E b p at the end here, Paul E B p
01:49
and then return.
01:52
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.
01:59
Go ahead and move into yea x you
02:01
the value of that argument. So e v p
02:07
plus eight hadn't moved that into,
02:12
um E X. And then we said We're gonna multiply by two so I can just to shift left
02:17
the a X ones Little mole by two.
02:23
It's quicker and easier to do that.
02:24
We're then going to dry and do our prints.
02:28
So go ahead and push on
02:30
the X.
02:32
We're then going to move into yea X are string that we went to print
02:39
push that onto the stack and then we're gonna call print F.
02:44
Normally, I'd correct the stack here, but at the ah epilogue of our function, we already correct a stack,
02:51
so we don't actually have to correct it in here. It will get corrected. And I forgot my ea X there.
02:57
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.
03:05
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,
03:15
so I'll go ahead and use the Yank. So why
03:19
then type the number of lines you want to copy and then hate? Why again,
03:23
um, that I'm going to go down here and hit p for put.
03:28
So that will put our function in here. So now we're gonna create our function called Bar.
03:32
So Bar is going to do similar things, except we're gonna shift by three. That's multiplied by eight,
03:42
and then we'll use string s too.
03:47
So now we've got our two functions,
03:50
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
03:59
print if there.
04:03
And so now we're gonna use our read int
04:08
to read the number from the user bred into a compare of yea ext one
04:15
jump zero x
04:16
well defined X down here
04:19
and why
04:23
and done
04:27
jump, not zero.
04:29
Why
04:31
an actual go ahead and move into E v X. Who,
04:35
um, done
04:38
in here? We're gonna move e
04:42
bar into ebx. I'm using EBX because I am going to call Read it again.
04:53
So I go and read our second number
04:58
and then we'll push it onto the stack is our first argument.
05:00
So the results from you, Dent is and yak. So go ahead and push that
05:04
well, I'm going to call you B X and then we're going to correct our stacks. Add yes, p
05:11
four in order to correct the stack,
05:14
if you'd like, you could go through and Princeton prompts off. I grew in a headwind with simple one.
05:18
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
05:28
And so we're gonna go ahead and use those. We could also use P one or P two, so either one of them,
05:34
um, would work.
05:39
So then we should be able to run our program.
05:47
So if I enter 11 it's gonna multiply by two. And if I enter to one,
05:55
it's gonna multiply by eight. You know, pick whatever it is. So
05:58
if I have eight,
06:00
so eight times two is 16
06:04
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,
06:15
um, inside of our
06:16
ah code. And you can usually tell when it has a dynamic function when it says call and then it has a register listed.
06:24
That means it doesn't know ahead of time what function it needs to call in order to get
06:29
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.
06:38
So today we created a function pointer and we use that inside of assembly, and we saw how to
06:44
code that in such a way that was usable.
06:47
So looking forward, we're going to talk about in line assembly
06:50
inside of C.
06:53
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.
Up Next