设为首页 加入收藏

TOP

C语言:编写访问PCI的小工具(一)
2013-11-20 14:17:57 来源: 作者: 【 】 浏览:727
Tags:语言 编写 访问 PCI 工具

  PCI的读写原理我就不罗嗦了,PCI的spec上面写的很清楚,仔细多看几遍就OK了。因为最近公司来了一个新人,要练习写PCI的小工具,试了很久没有搞出来,主要是用vc编译器,写出来的根本无法在DOS下运行,windows下运行需要通过驱动访问底层硬件;用TC编译器,因为是32位的,没法对CFC和CF8两个32的端口访问。所以,唯一的办法就是在C语言中内嵌汇编程序。

  最好的办法就是把读写两个32位端口的动作封装成子程序,这样以后调用就会便利许多。

  1,第一个是读程序:

  [cpp]

  unsigned long ioread(short int port)

  {

  unsigned long valueRet;

  asm mov dx, port;

  asm lea bx, valueRet;

  __emit__(

  0x66,0x50,

  0x66,0xED,

  0x66,0x89,0x07,

  0x66,0x58);

  return valueRet;

  }

  2,第二个是写程序:

  [cpp]

  void iowrite(short int port1, unsigned long value)

  {

  asm mov dx, port1;

  asm lea bx, value;

  __emit__(

  0x66,0x50,

  0x66,0x8B,0x07,

  0x66,0xEF,

  0x66,0x58);

  return;

  }

  注意这两个子程序都用到了_emit这个伪代码,他的具体的用法是这样的:

  The _emit pseudoinstruction defines one byte at the current location in the current text segment. The_emit pseudoinstruction resembles theDB directive of MASM.

  也就是说,它相当于masm中的DB,定义一个byte.

  下面有个例子,来自Microsoft的inline assembler.

  The following fragment places the bytes 0x4A, 0x43, and 0x4B into the code:

  [cpp]

  #define randasm __asm _emit 0x4A __asm _emit 0x43 __asm _emit 0x4B

  .

  .

  .

  __asm {

  randasm

  }

  好了,如果你把这两个小程序搞好之后,那么访问PCI就很简单了。

   

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言中sizeof()求字节数 下一篇c-常用的字符串转换函数

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: