Why does IA-32 have a non-intuitive caller and callee register saving convention?

The common calling conventions for IA-32 say:

• Callee-save registers
%ebx, %esi, %edi, %ebp, %esp
Callee must not change these.  (Or restore the caller's values before returning.)

• Caller-save registers
%eax, %edx, %ecx, condition flags
Caller saves these if it wants to preserve them.  Callee can freely clobber.

Why does this strange convention exist? Why not save all the registers before calling another function? Or have the callee save and restore everything with pusha/popa?

Answers


Why would you want to write code to save registers in every function that you might not need? That would add extra code and extra memory writes to every single function call. It may not seem significant now, but back in the 80's when this convention was created it probably did matter.

And note that ia-32 doesn't have a fixed calling convention - what you list is only an external convention - ia-32 doesn't enforce it. If you're writing your own code you use the registers however you wish.

Also see the discussion History of Calling Conventions at the Old New Thing Blog.

When deciding which registers should be preserved by a calling convention, you need to balance the needs of the caller against the needs of the callee. The caller would prefer that all registers be preserved, since that removes the need for the caller to worry about saving/restoring the value across a call. The callee would prefer that no registers be preserved, since that removes the need to save the value on entry and restore it on exit.

If you require too few registers to be preserved, then callers become filled with register save/restore code. But if you require too many registers to be preserved, then callees become obligated to save and restore registers that the caller might not have really cared about. This is particularly important for leaf functions (functions that do not call any other functions).


Need Your Help

PHP-MySQL-How to safely increment MySQL integer field?

php mysql transactions counter

I want to increment a field value safely using php and mysql.

Ordering the items in the array

java android arrays

Hi am trying to draw polygons on the map depending on the shortest distance.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.