1。我用VC++编写了一个汇编代码生成DLL,控制IO口,无参数传递。编译的时候提示insufficient number of arguments to a function,后来在定义函数的时候特意加上2个参数,可以编译成功。
2。调用这个函数的时候,提示the program has caused a 'privileged instruction' fault at 001B:022611A6
vc++中函数原形int _stdcall Scan_Asm(int x,int y)
{
int lsb=256,msb=16;
int s,c,i=255;
double result[1024];
int base_addr1=0x200;
// ScanInC();
// lsb=i&0x03;
// lsb=_inp(base_addr1+1);
// c=(msb*4)+(lsb/4);
for(i=0;i<1024;i++)
{
_asm
{
/*-------------------------------*/
/*启动DA调谐*/
/*_outp(base_addr1+5,0x25)**********************/
mov dx,205H
mov al,25H
out dx,al
/*msb=i>>2************************************/
mov ecx,i
sar ecx,2
mov msb,ecx
/*lsb=i&0x03************************************/
mov ecx,i
and ecx,3
mov lsb,ecx
/*lsb=lsb<<6;************************************/
sal lsb,6
/*_outp(base_addr1+4,lsb);**********************/
mov dx,204H
mov al,byte ptr[lsb]
out dx,al
/*_outp(base_addr1+5,0xa5);**********************/
mov dx,205H
mov al,10100101B
out dx,al
/*_outp(base_addr1+4,msb);**********************/
mov dx,204H
mov al,byte ptr[msb]
out dx,al
/*_outp(base_addr1+5,0xa5);**********************/
mov dx,205H
mov al,11010101B
out dx,al
/*-------------------------------*/
/*启动DA*/
/*_outp(base_addr1+0,0);**********************///启动12位数据转换
mov dx,200H
mov al,00H
out dx,al
}
/*-------------------------------*/
do
{
s=_inp(base_addr1+2);
s=s&0x01;
}
while(s==0);
_asm
{
/*lsb=_inp(base_addr1+1);lsb=lsb&0xff;**********************///低4位
mov dx,201H
in al,dx
// and al,FFH
mov byte ptr[lsb],al
/*msb=_inp(base_addr1+0);msb=lsb&0xff;**********************///高8位
mov dx,200H
in al,dx
// and al,FFH
mov byte ptr[msb],al
/*-------------------------------*/
}
c=(msb<<4)+(lsb>>4);
result=(double)c*10/4096;
}
return x+y;
}
cvi中调用
int CVICALLBACK ScanAsmCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
typedef int(__stdcall *lpScanFun)(); //定义函数指针
HMODULE hDll; //DLL句柄
lpAddFun ScanFun; //函数指针
int ScanRet;
switch (event)
{
case EVENT_LEFT_CLICK:
hDll = LoadLibrary("dllTest.dll");
if (hDll != NULL)
{
ScanFun = (lpScanFun)GetProcAddress(hDll, "Scan_Asm");
if (ScanFun != NULL)
ScanRet=ScanFun(3,8);
SetCtrlVal(panelHandle,PANEL_NUMERIC_3,ScanRet);
FreeLibrary(hDll);
}
else SetCtrlVal(panelHandle,PANEL_NUMERIC_3,88);
break;
}
return 0;
}