main.c
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
// Declare the assembly function
extern int add_func(int a, int b);
#ifdef __cplusplus
}
#endif
int main() {
printf("Starting the assembly function...\n");
int result = add_func(2, 3);
printf("Result: %d\n", result);
return 0;
}
Include irvine32.inc
.model flat, STDCALL
.data
result DWORD ?
.code
add_func PROC
; Save the base pointer
push ebp
mov ebp, esp
; Get the arguments
mov eax, [ebp+8] ; a
mov ecx, [ebp+12] ; b
; Add a and b
add eax, ecx
; Store the result in a variable
mov result, eax
call WriteInt
exit_function:
; Restore the base pointer
pop ebp
; Return the result in EAX
mov eax, result
ret 8 ; Clean up the stack (2 arguments * 4 bytes each)
add_func ENDP
END
我想使用程序集写入控制台或执行其他标准调用。我的环境已正确设置以进行常规组装;它可以使用 write int 轻松打印到控制台。但是,我需要确保该设置也适用于其他标准调用。
感恩节迈克尔·佩奇
Include irvine32.inc
; irvine32.inc already uses this directive for us:
; .model flat, STDCALL
.data
result DWORD ?
.code
add_func PROC C
; Save the base pointer
push ebp
mov ebp, esp
; Get the arguments
mov eax, [ebp+8] ; a
mov ecx, [ebp+12] ; b
; Add a and b
add eax, ecx
; Store the result in a variable
mov result, eax
call WriteInt
exit_function:
; Restore the base pointer
pop ebp
; Return the result in EAX
mov eax, result
ret
add_func ENDP
END