__asm__ and return value problem?

This is a discussion on __asm__ and return value problem? within the Linux Programming forums, part of the Platform Specific Boards category; The next function should return the stack pointer's value. I think I'm losing my mind, but how??? Am I missing ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Question __asm__ and return value problem?

    The next function should return the stack pointer's value.
    I think I'm losing my mind, but how??? Am I missing something?
    I don't see a return... how could it return a value then?
    Code:
    unsigned long get_sp(void)
    {
            __asm__("movl %esp, %eax");
    }

    Thank you.
    Last edited by Devil Panther; 04-12-2004 at 05:41 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Returning values is completely platform/compiler independent. On most systems (x86 and stuff, you know), returning integer values (anything under sizeof int bytes), you return the value in eax. For floating points values, you return it on st0 (top of the stack). So it would be like this in VC++.
    Code:
    __declspec(naked) unsigned int sp() {
       __asm mov eax, esp;
    }
    The naked specifier tells the compiler not to place any supplemental code to the function. I guess you can call it inline but I bet VC++ will inline it in the Release build with certain options on. And btw, I haven't tested/compiled that.

    EDIT: Oh this is Linux. I bet its the same anyways haha! But eax is still the return value. Just look up your compiler's documentation for naked fuctions and inline assembly.
    Last edited by Speedy5; 04-12-2004 at 09:06 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You figure out the asm, but like this
    Code:
    unsigned long get_sp(void)
    {
            unsigned long result;
            /* mov %esp result */
            __asm__("movl %esp, %eax");
            return result;
    }
    Then you only have to worry about getting the esp value, and not how the result is returned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Arrow

    It funny since I don't recall reading it in a book, any book.

    So AX is always used to return a value from a function.

    Just one last thing...
    Code:
    movl %esp, %eax
    why is %eax (the source parameter) on the right, and not on the left?



    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Quote Originally Posted by Devil Panther
    It funny since I don't recall reading it in a book, any book.

    So AX is always used to return a value from a function.

    Just one last thing...
    Code:
    movl %esp, %eax
    why is %eax (the source parameter) on the right, and not on the left?



    Thank you.
    It's AT&T syntax. For some reason they do it the other way around to the intel syntax...

    Just to make life that little bit more complex for you

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    damn at&t... if it weren't for C and unix, i would have burned them long time ago
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    We are usingi the AT&T syntax in my ASM class right now and
    Code:
    movl %esp, %eax
    is copying the stack pointer into eax, not the other way around.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21