Popping registers

Peter Barada peter@the-baradas.com
Tue Dec 21 05:47:00 GMT 2004

>I didn't know that registers are on a stack.
>>> What is a stack? What does the compiler use it for? Why would messing
>>> with it be a bad idea? And no, I do not know assembly, and I haven't
>>> had computer science class yet.
>> Well if you don't know what a stack is, then why are you asking how to
>> pop from it? :)
>> The stack is where the program allocates local storage and
>> pushes parameters and save/restores registers used by called functions
>> as well as save the return address when it calls another function so
>> the called function knows where to access the parameters and how to return to
>> the caller function.

I didn't say that registers are "on a stack".

Registers are registers and accessed as such.  When a function is
called, all but a few of the registers are required to be saved onto
the stack since the calling function expects that those registers will
retain the values that they had when when the called function

As a simplified example, the ColdFire architecture has 16 registers, 8
data registers(%d0 - %d7) and 8 address registers(%a1 - %a7).  Any
function that is called is free to modify the contents of data
register 0 and 1(%d0, %d1), as well as address registers 0 and 1(%a0,
%a1) since the calling convention allows this, but any other registers
that a function modifies need to be saved when the function is first
entered, and then restored when the function returns.  As a result, if
a function needs more than the registers %d0, %d1, %a0, %a1, then it
has to save those registers(to the stack) before it modifies them, and
then before the return, restore those saved registers(from the stack).  

Peter Barada

More information about the Gcc mailing list