Assembly

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14

Video Transcription

00:00
Hello. This is Dr Miller in this is episode 14.8 of Assembly.
00:05
Today we're gonna talk about function, pointers,
00:08
function, pointers.
00:10
So function pointer points to a function or a subroutine, as it sometimes is called.
00:15
And what this allows us to do is it allows us to change the functionality of a program at run time and so we can call different functions
00:23
based on that. And there are several different uses. So one is closures
00:28
or, um, curried functions. And so that's one example interrupt handlers or another.
00:34
And then if we have virtual functions inside of C plus plus or doing object or in programming design,
00:42
so in See this Intacs includes the arguments, and it includes the return type and the argument types. So each one of those is listed
00:51
so we can see that in a normal function declaration, we have our return type.
00:55
We then have our arguments. So we have a manager called X a double called B n a float called C.
01:03
And so we create a function pointer. We say the return type. So this returns a character, and then we just give the types of the arguments in here. So we have int double and float and we had to put those inside parentheses. So it knows those air part of that. And then here we give it a name and we say This is a pointer
01:19
to a function that takes an end, a double and afloat and returns a character.
01:25
And so that fund pointer is the name of the pointer that we're creating
01:27
for this function inside of C.
01:32
So once you've created a function pointer, you can use it just like any other function.
01:37
So I can have my fun
01:40
is the actual function returns a character and I can just use function pointer in the same type of Syntex.
01:47
Ah, we can also assign it value. So if we have a function pointer, weaken say are pointer is equal to the address of some function
01:53
that exists,
01:55
um, and function be would have the same signature is the pointer that we defined, right? And so they have to be the same. Otherwise we'll get a compile a warning, saying that this function takes different arguments than what you've given it.
02:07
So let's look at some examples. So we have a couple of different functions here. We got print A and print. Be so they're defined right over here.
02:15
And you notice that print a prince eight times returns eight times a and print be returns eight times eight times a. So we got a squared and cubed.
02:24
And then when it does, the prince often says, Enter one foreign squared, enter to for and cubed And then here we're doing a choice. So here I've declared a function pointer.
02:35
So we got int pointer called print.
02:38
Um, it takes one imager as an argument, and then you can see I just use print as normal.
02:43
But inside of here, it either does choice one. It does print A as the function pointer or the address that it assigns to print and down here, print is then assigned to the address of print to be.
02:58
Then we go ahead and use our function as normal, and we print off the variable for that.
03:02
So here we can see that we have our print
03:06
ah, signing for print Eagle to print A and print B. And down here we can see the pointers are, um, moved into E B P minus 12.
03:15
And so we can see that it puts those offsets in there so it will do the calculation for us on the fly,
03:23
and then we can see at the end when it goes to call that function. So it moves that pointer into yea X and then doesnt call yea X. So that's one way we can tell that this is a function pointer as we're going to see a call to register instead of the normal function that we would normally see.
03:38
And so that's kind of giving us a hint that it's a function point. Or it's something that's dynamic, where the compiler cannot tell what it should use
03:46
for that function. So it's dynamic in nature.
03:51
So here's another example. And so we have a 32 bit function, and so here I just give that gave them into images that so I turned the address of this function into an imager. So I just figured out what that was. And this is 32 bit, and so an imager on 32 bit is the same as a pointer,
04:09
and so we're allowed to do that. And then what we do is we print off when we say Okay, here's the address of A and here's the address of B. We then read in a number from the user, so they're able to type one of their choices.
04:20
And then what we do is we declare a function pointer and we cast it
04:26
to the value that they typed in. So the user can actually type in a number, and that's the function that it's actually going to call.
04:31
And so here you can see a cast, which is wrapped around by parentheses. And then we got our return type, isn't This is gonna be a pointer, and it takes as an argument one imager pointer. So if this is a character than this would be a character right here.
04:46
And so then it gets gonna call that function
04:49
again, we're gonna print off the result to see what it is, and we have A and B again for the squared and cubed.
04:57
So when you run this, it'll basically print off addresses and your machine might give you completely different addresses. But here is that just for a and here is address for B. And so if I type in that number
05:06
and hit, enter it will read that imager and then it'll actually call that function
05:12
the same thing for be right, so B has a different value. I call that I end up with it running function be.
05:19
And so in our compiler, we can see that it's going to go ahead and it's going to, um, load those numbers into the variables we got at 14. And at 16 it's gonna load the addresses of our functions A and functions be
05:32
right. We're gonna go ahead and print those off so that the user knows what those are. So you can see that those get
05:38
push on the stack is arguments.
05:42
We can see the further down
05:44
that e V P minus 28 is gonna be our choice. And so then that
05:48
gets loaded into yea X right. And we then save it back into 20.
05:54
Ah, for our function, pointer. Right. So we're taking the value for choice and saving it into Fu, which is going to be right here, a 20.
06:02
And then when we go farther down, we'll see the call Tofu again. Here it's color coded and we can see it loads value from E V. P. Minus 20 so that variable called Fu gets loaded into yea X. And then we're gonna call the function yaks. So again, we're dynamically calling that function,
06:18
um, using a register because we don't know where it is.
06:24
So today we talked about function pointers in C, and we gave some different examples of how we can declare them and see and then use them within our C program in a valid way. And then we saw how that corresponded with our assembly code.
06:39
So in the next lecture, we'll talk about function pointers and will give an example in assembly and show how that works.
06:46
And then here's our quiz. So given the following sea function signature for Fred,
06:51
um, how would you create a function pointer for it?
06:58
So I gave it the name Ted. But you have character because that's our first type.
07:01
It's got a pointer. So put Prince sees around my pointer here, and then we have double and car. And so we pick all of the types out of it and use that as our function signature
07:13
to create our function Pointer.
07:15
If you have questions, you can email me Miller MJ at you and Kate I e to you. And you can find me on Twitter at Milhouse 30

Up Next

Assembly

This course will provide background and information related to programming in assembly. Assembly is the lowest level programming language which is useful in reverse engineering and malware analysis.

Instructed By

Instructor Profile Image
Matthew Miller
Assistant Professor at the University of Nebraska at Kearney
Instructor