Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode seven of Assembly.
Today we're going to talk about calling conventions
So calling conventions are the assumption that the software makes about how a collar and a collie work.
So a collar is a function is going to call some function. Akali is the function that is being called and entered.
And so we have several different calling mentions that you will need to be aware of.
We have C Dekel standard call this call
and the different versions of fast calls. So 32 bit fast call in 64 bit fast call
32 big conventions.
So the C declaration is the sea calling conventions. This was created by the people who created UNIX, and so they made a way of declaring function calls
that the function that calls the Cali
that the caller function will clean up the stack. So this is faster than standard call.
And then parameters are pushed on the stack, as we've done in right toe left order.
So, for example, at the end of the function,
if you see a pop of EVP and then a return, then this is probably Ah, see Dekel call.
So here's an example. So we have this procedure called 4019 a four services out of reverse engineering.
We can see that we pushed on two parameters, and then we just popped those off into E. C X.
And then, as we're going to the function, we'll see our our entries of pushy BP movie B P E S p.
You see a poppy BP and then a return. And so we don't see any additional return on their
now standard call. The Cali cleans up the stack, and this is going to be easier to use except for when you have variable arguments. So an a easy example to think about for variable arguments is the printout function.
So print after you can give it one argument. 23456 as many as you want, as long as you have four matters in there.
And so in that case, it's very difficult for it to clean up the stack and an appropriate way.
And then in ST Standard call, we have registers like yea XTC X and E d X. Our caller saved, which means that if you care about the results of those registers of the contents and you're worried about them being blown away.
Then you need to save them.
And then the Cali has to save any other registers that it wouldn't modify.
And so this standard call is the default for C and C plus programming on Marcus Off Win 32 a p I.
So if you do any work with Windows 32 you might have to deal with the fact that
you're gonna have a different calling convention.
And so at the end, instead of just saying return, we would say red 12. And that would mean subtract 12 bytes off top of the stack.
So here, Now here's an example. So if you notice here, it says standard call. So the reverse engineering software can tell that this is a standard coal, and we see at the end we have returned n which mean end is how many bites are gonna pop off the stack.
So it does its prologue, does part of the epilogue and then does this return.
And so then you know that these bites aren't going to be saved on the stack in the end, and so you have to take that into account.
There's also this call, so in C plus Plus, we have this notion that there is a this variable.
So this points to the current object, and it has a set of attributes that are linked on the stack as well as functions that are linked onto the stack.
And so all of this is pointed to by the EEC X Register. So this register will point to those variables and those attributes. And then if you C E c X being used as a pointer in some notion that you're what you're probably looking at is C plus plus code.
And so here's an example.
So it thinks that this is a this call,
and when we get in here, we see that
and no place up here. E c X has a value set, but E. C X is getting moved into Yes, I and then that is being de referenced.
And so that means that this is a C plus plus function where the assumption is easy. X points to basically a function or a set of attributes, and so then that's being referenced, Right? So here it's moving some data into that location. But that was only initialized from EEC X, which wasn't set in the program.
And then you can see that it also might return right return four off the top of the stack, so that would be of this call.
And then we have fast calls. So part of the problem with the standard way that we pass parameters to functions is the fact that arguments have to be put on the stack and the stacks is a much slower
piece of memory, then registers. And so if you're only using maybe one or two registers instead of throwing them onto the stack and then doing your function and then popping them back off or subtracting from the stack, they just put them into registers.
And so, for the 32 bit Microsoft Fast Coal, it uses the registers of E, C X and E D X, and then the rest of the parameters of person with stack. So if you only have two arguments, then it's going to run faster because they'll be in registers is instead of on the stack
64 bit conventions.
So Microsoft has to find a 64 bit convention
and with 64 bit. We have instead of just e c X. We have RC X. So all of the easily replaced by our
and then there's additional registers that were beyond the normal ones.
And so we can use our age and are nine as our 1st 4 inner jer pointers. Or
or put imagers in that order
and then for floating point. So the XMM registers are the floating point registers that you can use in a program for doing floating point
arithmetic that came with some later architectures.
And so those 1st 40 through three are used for floating points,
and then the color also has to allocate 32 bytes of shadow space so we can save These registers are C X, already X R eight and are nine.
And then all of these registers are considered volatile, so the color has to save them,
and then the rest of them are non volatile. Macaulay has to save them,
and so these helped to make it so that function calls can run faster and not have to access the stack.
And with 64 bit, we end up with more registers so you can see that we have registers like are eight or nine or 10 all the way up to our 15.
And so these additional registers allow us to access mawr memory easier in 64 bit. And that's one of the reasons why 64 bit can be faster because it has more registers and the registers air faster than Ram.
And then we have so for arm, we have the system 5 64 bit A. B I or application binary interface. And this defines that. Registers like already IRC, um, are what the pointers can be put into. So it just has a different set of registers that we can use,
and then floating point registers zero through seven or eight of them can be used, right? And so that's the 64 bit,
um, application binary interface that could be used,
with the A m d 64 bit
So today we talked about some of the different calling conventions So standard. See, Dekel this and then a couple of different variants of fast coal
Looking forward, we're gonna look at local variables, so local variables are a place where we contemporary store data
inside of our functions, and we'll look at how we can save data there or
read data from local variables.
So which registers are used for the 1st 2 parameters on Microsoft? 32 bit fast Call
E C X and E D. X.
If you have questions, you can contact me at Miller MJ at you and Katie to you, and you can find me on Twitter at Milhouse 30.