MASM 和 Turbo C 2.0 混合编程

件 :masm 5 /Turboc 2.0 /Windows 7 ultimate SP1 32bit
件: Intel Core T450

c程序调用masm汇编程序的函数int sum(int ,int),代码如下

/*   test.c   */
#include <stdio.h>
extern int sum(int a,int b);
int main()
{
	int a = 1,b = 15;
	printf("a + b = %d\n",sum(a,b));
return 0;}

;the function prototype in c language is :  int  sum(int a,int b);
.model small
_TEXT segment word public 'CODE'
assume cs:_TEXT
public _sum
_sum proc far
push bp
mov bp,sp
push bx
mov ax,[bp+6]    ;int a
mov bx,[bp+8]  ;int b;
add ax,bx
pop bx
pop bp
ret 
_sum endp
_TEXT ends
end 

函数_sum名称必须加下划线。segment必须是_TEXT ,这样才能正确连接.
将test.c和sum.asm编译成test.obj和sum.obj
在终端下执行

>TLINK lib/C0S.obj test.obj sum.obj,out , ,lib/CS
编译出 out.exe,在终端执行它
>out.exe
弹出对话框提示illegal instruction








你可能感兴趣的:(ASM)