News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

32-bit - eax vs edx

Started by BeErikk, April 22, 2024, 10:35:31 PM

Previous topic - Next topic

BeErikk

Does anyone have a clue what the difference is between the two following functions?

Thank you very much in advance!

; void __cdecl sumpair(int32_t* pint1, int32_t* pint2)
sumpair         proc near PRIVATE

pint1           = dword ptr  4
pint2           = dword ptr  8

                mov     eax, [esp+pint2]
                mov     ecx, [eax]
                mov     eax, [esp+pint1]
                add     [eax], ecx
                xor     eax, eax
                retn
sumpair         endp


; void __cdecl sumpair2(int32_t* pint1, int32_t* pint2)
sumpair2        proc near PRIVATE

pint1           = dword ptr  4
pint2           = dword ptr  8

                mov     eax, [esp+pint2]
                mov     ecx, [eax]
                mov     edx, [esp+pint1]
                mov     [edx], ecx
                xor     eax, eax
                retn
sumpair2        endp

jj2007

The first one adds a value to a memory location..
The second one just moves a value between two memory locations.

BeErikk

@jj2007

Thank you very much for the clues! Now I wonder how I can express the code in C/C++. The easy way out would be to just keep the assembly as is. I'll testcompile and dissassemble further.

zedd151

Or you could switch to just using assembly.  :biggrin:
Our specialty.

BeErikk

Quote from: sudoku on April 22, 2024, 11:49:47 PMOr you could switch to just using assembly.  :biggrin:

No, it's an old C++ program where I try to replace some parts. It should look nice, easy and clean.

zedd151

Okay. We have a few members that also work in C/C++ that might be able to assist you futher.   :smiley:

BeErikk

My translations of the two functions to C++ look like this (second function renamed):
static void __cdecl sumpair(int32_t* pint1, int32_t* pint2) noexcept
{
    *pint1 += *pint2;
}

static void __cdecl assignpair(int32_t* pint1, int32_t* pint2) noexcept
{
    *pint1 = *pint2;
}

But the disassembly looks like this:
; void __cdecl sumpair(int32_t* pint1, int32_t* pint2)
sumpair        proc near PRIVATE

pint1          = dword ptr  4
pint2          = dword ptr  8

                mov    eax, [esp+pint2]
                mov    ecx, [eax]
                mov    eax, [esp+pint1]
                add    [eax], ecx
                retn
sumpair        endp


; void __cdecl assignpair(int32_t* pint1, int32_t* pint2)
assignpair        proc near PRIVATE

pint1          = dword ptr  4
pint2          = dword ptr  8

                mov    eax, [esp+pint2]
                mov    ecx, [eax]
                mov    eax, [esp+pint1]
                mov    [eax], ecx
                retn
assignpair        endp


edx is not used. Does it matter?

TimoVJL

Use other C / C++ compiler, it may use edx  :biggrin:
Or change optimization to different.
#include <stdint.h>

void __cdecl sumpair(int32_t* pint1, int32_t* pint2)
{
    *pint1 += *pint2;
}

void __cdecl sumpair2(int32_t* pint1, int32_t* pint2)
{
    *pint1 = *pint2;
}
result with Pelles C
_sumpair:
  [00000000] 8B442404              mov              eax,dword ptr [esp+4]
  [00000004] 8B542408              mov              edx,dword ptr [esp+8]
  [00000008] 8B12                  mov              edx,dword ptr [edx]
  [0000000A] 0110                  add              dword ptr [eax],edx
  [0000000C] C3                    ret             
  [0000000D] 0F1F00                nop              [eax]

_sumpair2:
  [00000010] 8B442404              mov              eax,dword ptr [esp+4]
  [00000014] 8B542408              mov              edx,dword ptr [esp+8]
  [00000018] 8B12                  mov              edx,dword ptr [edx]
  [0000001A] 8910                  mov              dword ptr [eax],edx
  [0000001C] C3                    ret             
I was a bit slow to convert that asm example to C earlier.
May the source be with you

jj2007

Quote from: BeErikk on April 22, 2024, 11:56:28 PMit's an old C++ program where I try to replace some parts

Timo is the expert here. In any case, you won't get more speed for such tiny programs. Your compiler will inline this stuff, which is a lot faster.

BeErikk

@TimoVJL

Very interesting.
This code is implemented in an exe but is executed from a DLL. The DLL is pretty much out of reach for manipulation, so I'm a bit concerned about introducing bugs from my altered code in the exe. These "callback" functions also seem to be stubs with meaningless roles. I guess the safest and most practical thing to do is to keep the code as assembly as I'm not going to alter this code in any way.

Thank you all for input!