__stdcall alternative

Discussion in 'C Programming' started by Bartc, Apr 24, 2008.

  1. Bartc

    Bartc Guest

    I'm mixing C and ASM, and for calling ASM from C, I just happened to use the
    __stdcall convention (using C convention is not practical for other
    reasons).

    But, __stdcall seems to assume that machine registers are saved by the
    called routine (as far as I can gather after a few hours messing with some
    strange behaviours especially with optimised C code).

    Is there any other call convention I can use likely to be commonly
    available?

    Or is there a way of specifying that an external function does not save
    registers or is badly behaved?
     
    Bartc, Apr 24, 2008
    #1
    1. Advertisements

  2. Bartc

    Ian Collins Guest

    What is __stdcall? It's not C, so it must be platform specific, try a
    group for your platform, you'll get more help there.
     
    Ian Collins, Apr 24, 2008
    #2
    1. Advertisements

  3. Bartc

    jacob navia Guest

    OBVIOUSLY you should save the registers ytour compiler thinks are permanent

    Under windows (where stdcall is commonly used) you should save
    (I am assuming 64 bit CPU)

    rsi
    rdi
    rbx
    r12-r15
    rbp

    And at the end you should issue a
    ret $8

    when you receive 8 bytes of arguments for instance.
    you could just as well use the C calling convention. The registers to save
    are THE SAME but you should just issue

    ret
    and the called procedure adjusts the stack.

    There is NO way to do that. YOU HAVE TO SAVE THOSE if not
    you will just have a crash.
     
    jacob navia, Apr 24, 2008
    #3
  4. Bartc

    jacob navia Guest

    What's the point of showing your ignorance as it was a quality?

    You do not know the subject matter?

    Just stay silent! Is that too complicated?

    Of course it is. It is a common extension.
    no, it isn't
     
    jacob navia, Apr 24, 2008
    #4
  5. It sounded to me like the external function isn't one he's written, and
    he can't control its behavior. If that's the case, there's nothing for
    it except to work out what calling convention the function expects, and
    use that.
     
    Antoninus Twink, Apr 24, 2008
    #5
  6. "_stdcall" isn't platform-specific? Seriously?
     
    Keith Thompson, Apr 24, 2008
    #6
  7. Bartc

    jacob navia Guest

    If you use gcc you should
    #ifndef STDCALL
    #define STDCALL __attribute__((stdcall))
    #endif
     
    jacob navia, Apr 24, 2008
    #7
  8. Bartc

    jacob navia Guest

    #ifndef STDCALL
    #define STDCALL __attribute__((stdcall))
    #endif
     
    jacob navia, Apr 24, 2008
    #8
  9. What does defining "STDCALL" have to do with "__stdcall"? They're two
    different identifiers, you know.

    I suppose your point is that gcc supports a feature that's similar to
    "__stdcall", but uses the syntax "__attribute__((stdcall))". So two
    different platforms support a similar feature using radically
    different syntax. If I were sufficiently motivated, I'm sure I could
    find another platform that either supports it with yet another
    different syntax, or doesn't support it at all.

    "__stdcall" is platform-specific.
     
    Keith Thompson, Apr 24, 2008
    #9
  10. Bartc

    Ian Collins Guest

    Fool, if you read the bit of my post you snipped - "try a group for your
    platform, you'll get more help there." - you would see I was attempting
    to help the OP.
    Not on any platform I use, and that's quite a few.
    Eric's already shown you that is is.
     
    Ian Collins, Apr 24, 2008
    #10
  11. Bartc

    jacob navia Guest

    Go to the kindergarten then. There you will find all answers
     
    jacob navia, Apr 24, 2008
    #11
  12. Bartc

    jacob navia Guest

    What piss me off of this regulars is that when they are proved
    wrong they insist
     
    jacob navia, Apr 24, 2008
    #12
  13. Bartc

    jacob navia Guest

    And I showed Eric and you and Thompson that is supported
    on gcc/MSVC/Lcc-win/Watcom/Borland and what have you

    what piss me off of this regulars is that when proved wrong
    they insist
     
    jacob navia, Apr 24, 2008
    #13
  14. Bartc

    Ian Collins Guest

    Which are specific platforms, so?
    On what?
     
    Ian Collins, Apr 24, 2008
    #14
  15. Bartc

    jacob navia Guest

    Well, You are a genius...
    You were attempting to help the original poster by
    showing your ignorance.

    Yes of course it helps him when you say you do not know the
    subject matter.

    Obviously, this proves that you are a genius.
    Who cares?

    It is supported by gcc and all the platforms gcc runs on,
    supported by MSVC, supported by Watcom, Borland, lcc-win,
    and what have you.

    That is almost 100% of the market. Obviously you use
    something else and never look into mainstream computing like
    many regulars here.

    Ignorance is a bliss!

    I told you, you are a genius.
     
    jacob navia, Apr 24, 2008
    #15
  16. Bartc

    Bartc Guest

    OK, for x86-32 then I have to worry about esi,edi,ebx.
    The ASM is emitted by a compiler of mine from years back; I'm not going to
    change it! I'll just stick in some bits of inline asm for now.

    This is just a temporary measure until my project is 100% C. Or it will have
    some ASM calling C which is a bit easier.
    (That always seemed a crazy way to do things; one extra instruction /per
    call/).
     
    Bartc, Apr 24, 2008
    #16
  17. Either I'm missing something, or you haven't done anything that
    resembles proving me wrong.

    I say __stdcall is platform-specific. You say it isn't. Fine, then
    convince me that you're right. If you do, then I'll gladly admit it
    (I admit to my mistakes here on a fairly regular basis).

    What do you mean by "platform-specific", and how does that concept
    *not* apply to __stdcall? Is it supported on all platforms?

    I asked about "__stdcall"; you replied with macro named "STDCALL".
    Are they intended to be the same thing?

    Don't waste your time and ours by telling us that you're pissed off
    because somebody disagrees with you. If you're right, demonstrate it.

    (And by the way, this isn't about you; I'd say the same thing if
    anyone else made the same claim.)
     
    Keith Thompson, Apr 24, 2008
    #17
  18. Bartc

    jacob navia Guest

    AND EBP!!!
    If your compiler was able to interface with outside code, it surely
    saved those. It is very simple

    push esi
    push ebx
    push edi

    ....

    pop edi
    pop ebx
    pop esi

    and you are all set

    It saves 5% code space, it is very efficient.
     
    jacob navia, Apr 24, 2008
    #18
  19. That sounds a bit platform-specific! But luckily I do use gcc.
    I get:

    foo.c:5: warning: 'stdcall' attribute directive ignored

    Presumably this is an x86-only declaration.

    -- Richard
     
    Richard Tobin, Apr 24, 2008
    #19
  20. Bartc

    Flash Gordon Guest

    Keith Thompson wrote, On 24/04/08 22:51:
    markg@hal02 ~ $ cat t.c
    #ifndef STDCALL
    #define STDCALL __attribute__((stdcall))
    #endif
    extern void STDCALL foo(void);
    markg@hal02 ~ $ gcc -c t.c
    t.c:4: warning: `stdcall' attribute directive ignored
    markg@hal02 ~ $

    Admittedly that is not an x86 based system. So just in case it is
    specific to the processor type lets try on one of my x86 based systems...

    On another system:
    -bash-2.05b$ cc t.c
    UX:i386acomp: ERROR: "t.c", line 4: error: Syntax error before or at: (
    UX:i386acomp: ERROR: "t.c", line 4: error: cannot recover from previous
    errors
    -bash-2.05b$ cat tt.c
    #ifndef STDCALL
    #define STDCALL __stdcall
    #endif
    extern void STDCALL foo(void);
    -bash-2.05b$ cc tt.c
    UX:i386acomp: ERROR: "tt.c", line 4: error: syntax error, probably
    missing ",", ";" or "="
    UX:i386acomp: ERROR: "tt.c", line 4: error: Syntax error before or at: foo
    UX:i386acomp: WARNING: "tt.c", line 4: warning: declaration missing
    specifiers: assuming "int"
    -bash-2.05b$


    Nope, doesn't work on all x86 based systems either.

    So Keith and Ian are correct to say it is platform specific.
     
    Flash Gordon, Apr 24, 2008
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.