Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these registers pushed to stack?

Tags:

assembly

push    %ebp
push    %esp, %ebp
push    edi
push    esi
push    ebx

(x86 32bit Linux)

Why are these registers pushed to stack?
Migrated for some reason...?
But, why only 'edi' 'esi' 'ebx' are pushed?

like image 623
ru10 Avatar asked Mar 21 '12 01:03

ru10


1 Answers

It is a gcc implementation detail for the x86 code generator. Surprisingly hard to find good docs for it, I did find this page which is pretty accurate. Key part:

after ret instruction:

%eip contains return address
%esp points at arguments pushed by caller
called function may have trashed arguments
%eax contains return value (or trash if function is void)
%ecx, %edx may be trashed
%ebp, %ebx, %esi, %edi must contain contents from time of call 

The "must contain content from time of call" phrase explains why their are pushed in the function prologue and popped again in the epilogue.

like image 161
Hans Passant Avatar answered Sep 28 '22 08:09

Hans Passant