具体过程:

用visual studio2022新建一个x86汇编项目

二 c语言调用x86汇编函数的代码部分

c_call_x86.asm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
;c_call_x86.asm
.model flat,c
.code

; extern "C" int CalcSum_(int a, int b, int c)
;
; Description: This function demonstrates passing arguments between
; a C++ function and an assembly language function.
;
; Returns: a + b + c

CalcSum_ proc

; Initialize a stack frame pointer
push ebp
mov ebp,esp

; Load the argument values
mov eax,[ebp+8] ; eax = 'a'
mov ecx,[ebp+12] ; ecx = 'b'
mov edx,[ebp+16] ; edx = 'c'

; Calculate the sum
add eax,ecx ; eax = 'a' + 'b'
add eax,edx ; eax = 'a' + 'b' + 'c'

; Restore the caller's stack frame pointer
pop ebp
ret

CalcSum_ endp
end

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <tchar.h>
extern "C" int CalcSum_(int a, int b, int c);

int _tmain(int argc, _TCHAR* argv[])
{
int a = 17, b = 11, c = 14;
int sum = CalcSum_(a, b, c);

printf(" a: %d\n", a);
printf(" b: %d\n", b);
printf(" c: %d\n", c);
printf(" sum: %d\n", sum);
return 0;
}