0

I have the following function :

int __declspec() MyFunc(SOCKET sSocket, const char* sData, int sSize, int sFlag)
{
    pSocket = sSocket;
    return send(sSocket,sData, sSize, sFlag);
}

And here is its assembly code after compiling :

PUSH EBP
MOV EBP,ESP
PUSH DWORD PTR SS:[EBP+14]               // Flags
MOV EAX,DWORD PTR SS:[EBP+8]
PUSH DWORD PTR SS:[EBP+10]               // DataSize
MOV DWORD PTR DS:[pSocket],EAX
PUSH DWORD PTR SS:[EBP+C]                // Data
PUSH EAX                                 // Socket
CALL DWORD PTR DS:[<&WS2_32.#19_send>]   // send
POP EBP
RETN

My questions are:

  1. Whats the difference between RETN, RETN 8 or RETN 10 ?
  2. I have to change the final RETN to RETN 10, what changes should I make to my C++ code ?
perror
  • 7,071
  • 16
  • 58
  • 85
Shahriyar
  • 1,483
  • 4
  • 24
  • 37
  • 2
    May I suggest you get a book on x86 assembly language to learn the difference between retn and retn 10. – Raymond Chen Jul 17 '13 at 06:21
  • I suggest to take a look at similar question http://stackoverflow.com/questions/1396909/ret-retn-retf-how-to-use-them – bkausbk Jul 17 '13 at 06:24
  • Thanks, I got difference between retn and retn 10, what about my second question ? – Shahriyar Jul 17 '13 at 06:27
  • _"what about my second question"_: Make it `stdcall`? (i.e. add an `__attribute__((stdcall))` to the function if you're using GCC) – Michael Jul 17 '13 at 06:32
  • @Michael "Error: identifier "stdcall" is undefined, may i just add `__stdcall` to my function ? – Shahriyar Jul 17 '13 at 06:45
  • @Shahriyar: If you use GCC it would be e.g. `void __attribute__ ((stdcall)) foo(int i) {`. If you use Microsoft's compiler then I think it'd be something like `void __stdcall foo(int i) {`. If you use something else, then I've no idea. – Michael Jul 17 '13 at 06:46
  • @Michael, I tried what you said, Not only it didn't change RETN to RETN 10 Also it removed the whole return :D,But who cares, it completely fixed my problem.Post your answer i will mark it as best – Shahriyar Jul 17 '13 at 06:52

1 Answers1

1

I have to change the final RETN to RETN 10, what changes should i make to my c++ code ?

To get the called function to clean up the stack before returning (using RET(N) imm, or through other means), make it use the stdcall calling convention.

If you're using Microsoft's C/C++ compiler you can achieve this by adding an __stdcall modifier, as in:

void __stdcall foo(int arg1, int arg2) {
  // ...
}

If you're using GCC you can achieve this using the stdcall attribute:

void __attribute__ ((stdcall)) foo(int arg1, int arg2) {
  // ...
}

You could of course #define __stdcall as __attribute__ ((stdcall)) to save yourself some typing, and make the code more portable.

Compiling a function like the above using GCC in Cygwin results in the following assembly:

_foo@8:
push    ebp
mov ebp, esp
... (omitted for brevity)
leave
ret 8
Michael
  • 57,169
  • 9
  • 80
  • 125