将 MASM 对象与 C 对象链接会给出错误的结果
Linking MASM Objects with C objects gives wrong results
我在程序集中实现了一个简单的宏"intadd",它添加了两个整数 (QWORD)。 C 中的驱动程序代码也使用 QWORDS,它是 stdint.h 中 uint32_t 的类型定义。无论参数如何,输出始终为 7。
asm.asm
intadd PROC x:DWORD, y:DWORD
mov eax, x
add eax, y
ret
intadd ENDP
END
我也尝试将 y 移动到 ebx,然后添加 eax、ebx,但结果相同。
C 代码段
extern DWORD intadd(DWORD x, DWORD y);
printf("%i", intadd(1,1));
我需要设置进位标志还是什么?我 link 文件
ml64 asm.asm /c && cl.exe cfile.c /EHsc /c &&
link asm.obj cfile.obj /out:exe.exe
感谢任何帮助。
即使在使用 ML64.exe 时,PROC
指令也会在堆栈中搜索参数。但是 "Microsoft x64 calling convention" 在寄存器中传递参数。您可以将寄存器保存在所谓的影子 space 过程中,或者 - 更好 - 直接使用寄存器:
intadd PROC
mov eax, ecx
add eax, edx
ret
intadd ENDP
顺便说一句:DWORD
等同于 unsigned int
。因此,调整您的格式字符串:printf("%u", intadd(1,1));
。或者在C文件中使用C类型int
.
我在程序集中实现了一个简单的宏"intadd",它添加了两个整数 (QWORD)。 C 中的驱动程序代码也使用 QWORDS,它是 stdint.h 中 uint32_t 的类型定义。无论参数如何,输出始终为 7。
asm.asm
intadd PROC x:DWORD, y:DWORD
mov eax, x
add eax, y
ret
intadd ENDP
END
我也尝试将 y 移动到 ebx,然后添加 eax、ebx,但结果相同。
C 代码段
extern DWORD intadd(DWORD x, DWORD y);
printf("%i", intadd(1,1));
我需要设置进位标志还是什么?我 link 文件
ml64 asm.asm /c && cl.exe cfile.c /EHsc /c &&
link asm.obj cfile.obj /out:exe.exe
感谢任何帮助。
即使在使用 ML64.exe 时,PROC
指令也会在堆栈中搜索参数。但是 "Microsoft x64 calling convention" 在寄存器中传递参数。您可以将寄存器保存在所谓的影子 space 过程中,或者 - 更好 - 直接使用寄存器:
intadd PROC
mov eax, ecx
add eax, edx
ret
intadd ENDP
顺便说一句:DWORD
等同于 unsigned int
。因此,调整您的格式字符串:printf("%u", intadd(1,1));
。或者在C文件中使用C类型int
.