This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Popping registers


> -----Original Message-----
> From: gcc-owner On Behalf Of Richard Henderson
> Sent: 21 December 2004 00:52
> On Mon, Dec 20, 2004 at 09:23:44PM +0100, Sam Lauber wrote:
> > I was wondering, how do you pop a register (like EAX) into 
> a C variable?
> 
> You don't.  Messing with the stack like that behind the compiler's
> back is strictly verboten.
> 
> 
> r~


  Oh, come on, it's Christmas!  Let's show him how to do it: use an inline asm
to pop into a pre-reserved asm register variable like this:-

-----------------<!snip!>-----------------
Admin@ubik ~/test> cat foo.c

#include <stdlib.h>

int main (int argc, const char **argv)
{
register int Eax asm ("%eax");

        asm volatile ("pop  %0" : "=r" (Eax) :  );
        printf ("Eax was $%08x\n", Eax);
        return -1;
}
-----------------<!snip!>-----------------

  As you'll see, it compiles without problems and produces exactly the code Sam
wants:

-----------------<!snip!>-----------------
Admin@ubik ~/test> gcc -O3 foo.c --save-temps -o foo
Admin@ubik ~/test> cat foo.s
        .file   "foo.c"
        .def    ___main;        .scl    2;      .type   32;     .endef
        .section .rdata,"dr"
LC0:
        .ascii "Eax was $%08x\12\0"
        .text
        .p2align 4,,15
.globl _main
        .def    _main;  .scl    2;      .type   32;     .endef
_main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        xorl    %eax, %eax
        call    __alloca
        call    ___main
/APP
        pop  %eax
/NO_APP
        movl    %eax, 4(%esp)
        movl    $LC0, (%esp)
        call    _printf
        movl    $-1, %eax
        movl    %ebp, %esp
        popl    %ebp
        ret
        .def    _printf;        .scl    2;      .type   32;     .endef
DKAdmin@ubik ~/test> ./foo.exe
Eax was $6101c900
DKAdmin@ubik ~/test>
-----------------<!snip!>-----------------

  It even works perfectly!  That's just the sort of value you would expect to
see on the stack of a cygwin application.  I mean, what more could you want? ;)

    cheers, 
      DaveK
-- 
Can't think of a witty .sigline today....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]