C++ produced ASM code

Discussion in 'C++' started by Joris, Aug 9, 2005.

  1. Joris

    Joris Guest

    Hi

    I'm not sure if this is the correct newsgroup for this question
    as it involves both ASM and c++, but here goes.

    I have written a function in C++ that has to replace one of
    a process in memory (to change functionality).
    The original function's address is retrieved from 1 offset.
    When the application starts I write the address of my new
    function to that offset. So far so good. I found out the original
    function has 1 argument (4-byte) and returns a 4-byte value
    (1 or 0, BOOL). This fails. I took a look at the code c++
    had generated, in a debugger. I saw c++ ends the new
    function with "retn". When I changed this to "retn 4"
    the process ran without any problems.

    Why does "retn 4" work and "retn" not? Why does
    Visual C++ compile "retn" and not "retn 4"?

    Thanks
    Joris
    Joris, Aug 9, 2005
    #1
    1. Advertising

  2. * Joris:
    > I'm not sure if this is the correct newsgroup for this question
    > as it involves both ASM and c++, but here goes.
    >
    > I have written a function in C++ that has to replace one of
    > a process in memory (to change functionality).
    > The original function's address is retrieved from 1 offset.
    > When the application starts I write the address of my new
    > function to that offset. So far so good. I found out the original
    > function has 1 argument (4-byte) and returns a 4-byte value
    > (1 or 0, BOOL). This fails. I took a look at the code c++
    > had generated, in a debugger. I saw c++ ends the new
    > function with "retn". When I changed this to "retn 4"
    > the process ran without any problems.


    The problem is C-style machine code calling convention versus Pascal-like
    style machine code calling convention.

    Standard C++ does not help out with machine code calling conventions.

    The closest you get in standard C++ is 'extern "C"' versus 'extern "C++"',
    but for your problem, consult the compiler's documentation for language
    extensions (OT: for MSVC the language extension is 'declspec').


    > Why does "retn 4" work and "retn" not?


    See above.


    > Why does Visual C++ compile "retn" and not "retn 4"?


    See above.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 9, 2005
    #2
    1. Advertising

  3. Joris wrote:
    > I'm not sure if this is the correct newsgroup for this question
    > as it involves both ASM and c++, but here goes.


    It's not. Your question is compiler-specific. It should be asked
    in the newsgroup for that compiler. 'microsoft.public.vc.language'
    is probably the one you need.

    > I have written a function in C++ that has to replace one of
    > a process in memory (to change functionality).
    > The original function's address is retrieved from 1 offset.
    > When the application starts I write the address of my new
    > function to that offset. So far so good. I found out the original
    > function has 1 argument (4-byte) and returns a 4-byte value
    > (1 or 0, BOOL). This fails. I took a look at the code c++
    > had generated, in a debugger. I saw c++ ends the new
    > function with "retn". When I changed this to "retn 4"
    > the process ran without any problems.
    >
    > Why does "retn 4" work and "retn" not? Why does
    > Visual C++ compile "retn" and not "retn 4"?


    <offtopic>
    You probably didn't declare your function appropriately. There exist
    several modifiers in Visual C++ (they call them "calling conventions")
    that can change how the function is called, how the arguments are passed,
    and how the function returns. See 'CALLBACK', 'WINAPI', '__stdcall',
    etc.
    </offtopic>

    V
    Victor Bazarov, Aug 9, 2005
    #3
  4. Joris

    Joris Guest

    Thank you.


    "Alf P. Steinbach" <> schreef in bericht
    news:...
    * Joris:
    > I'm not sure if this is the correct newsgroup for this question
    > as it involves both ASM and c++, but here goes.
    >
    > I have written a function in C++ that has to replace one of
    > a process in memory (to change functionality).
    > The original function's address is retrieved from 1 offset.
    > When the application starts I write the address of my new
    > function to that offset. So far so good. I found out the original
    > function has 1 argument (4-byte) and returns a 4-byte value
    > (1 or 0, BOOL). This fails. I took a look at the code c++
    > had generated, in a debugger. I saw c++ ends the new
    > function with "retn". When I changed this to "retn 4"
    > the process ran without any problems.


    The problem is C-style machine code calling convention versus Pascal-like
    style machine code calling convention.

    Standard C++ does not help out with machine code calling conventions.

    The closest you get in standard C++ is 'extern "C"' versus 'extern "C++"',
    but for your problem, consult the compiler's documentation for language
    extensions (OT: for MSVC the language extension is 'declspec').


    > Why does "retn 4" work and "retn" not?


    See above.


    > Why does Visual C++ compile "retn" and not "retn 4"?


    See above.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Joris, Aug 9, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio D'Ottavio

    URL inside a mail produced with aspx

    Antonio D'Ottavio, Aug 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    575
  2. Replies:
    0
    Views:
    729
  3. LordBlue

    Asm code to C code

    LordBlue, Aug 15, 2003, in forum: C Programming
    Replies:
    2
    Views:
    495
    Pieter Droogendijk
    Aug 15, 2003
  4. Gernot Frisch

    asm code for ARM (very simple)

    Gernot Frisch, Sep 4, 2008, in forum: C++
    Replies:
    7
    Views:
    528
    Jorgen Grahn
    Sep 16, 2008
  5. MarlonBrando

    ASM method attribute and code order

    MarlonBrando, Oct 15, 2009, in forum: Java
    Replies:
    7
    Views:
    438
    Marcin RzeĀ¼nicki
    Oct 15, 2009
Loading...

Share This Page