Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_cdecl calling convention

In an article about the _cdecl calling convention, the writer mentioned:

Release local storage When the function allocates local, temporary space, it does so by decrementing from the stack point the amount of space needed, and this process must be reversed to reclaim that space. It's usually done by adding to the stack pointer the same amount which was subtracted previously, though a series of POP instructions could achieve the same thing.

My question is: could I simply set ESP to the current EBP value instead of "adding to the stack pointer the same amount which was subtracted" or a "series of POP instructions"?

Like:

mov esp, ebp

Seems like a better way to me, because if I later change the number of local variables of this function, I won't have to bother about incrementing the value afterwards.

like image 372
user1091856 Avatar asked Dec 12 '25 22:12

user1091856


1 Answers

Technically this would be an abuse of stack frames, which are there to catch errors arising from stack imbalance, but it would be perfectly legal.

There are a few things to note though, if you have very limited stack space, due to a large alloc or an embedded device, its worth while to clean up the stack after every call. It also makes debugging a bit easier, because then you know something is using the wrong amount of args.

Also if anyone is going to maintain your code, they will find it very confusing.

like image 187
Necrolis Avatar answered Dec 15 '25 21:12

Necrolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!