例10.14 使用C语言调用汇编语言子过程dif.asm计算二数之差并在C语言中显示其结果。程序如图10.8所示,其中(1)为C语言程序,(2)为汇编语言子过程。经QC编译器对C语言程序编译,MASM对汇编语言子过程汇编后,把两个目标模块相连接而得到可执行文件。程序运行时屏幕上将显示:
(1)
The difference is equal to 8275
extern unsigned long dif(int,int);
main()
{
printf("The difference is equal to %u",dif(8400,125));
}
(2)
.model small
.code
public _dif
;this procedure gets two words from the stack
;and subtracts them.
;At the end DX:AX has the difference
_dif proc near
push bp ;save BP
mov bp,sp ;use it as SP
sub dx,dx ;clear DX
mov ax,[bp+4] ;load the minuend
sub ax,[bp+6] ;minus subtrahend
sbb dx,0 ;
pop bp ;restore BP
ret ;go back to C
_dif endp
end
图10.8 在MASM6版本下的dif.asm 程序
