Crazy Coder
Crazy Coder

Reputation: 53

Linking MASM Objects with C objects gives wrong results

I have implemented a simple macro"intadd" in assembly that adds two integers (QWORDs). The driver code in C also uses QWORDS, which are a typedef for uint32_t from stdint.h. The output is always 7, regardless of the arguments.

asm.asm

intadd PROC x:DWORD, y:DWORD
mov eax, x
add eax, y
ret
intadd ENDP
END

I also tried to move y to ebx and then add eax, ebx but that produces the same results.

C-Snippet

extern DWORD intadd(DWORD x, DWORD y);
printf("%i", intadd(1,1));

Do I need to set a carry flag or something? I link the files with

ml64 asm.asm /c && cl.exe cfile.c /EHsc /c &&
link asm.obj cfile.obj /out:exe.exe

Any help is appreciated.

Upvotes: 0

Views: 101

Answers (1)

rkhb
rkhb

Reputation: 14409

The PROC directive searches the stack for arguments even when using ML64.exe. But the "Microsoft x64 calling convention" passes arguments in registers. You can save the registers in the procedure on the so-called shadow space or - better - work directly with the registers:

intadd PROC
    mov eax, ecx
    add eax, edx
    ret
intadd ENDP

BTW: DWORD is equivalent to unsigned int. So, adapt your format string: printf("%u", intadd(1,1));. Or use the C type int in the C file.

Upvotes: 2

Related Questions