读书人

请问一个C嵌汇编的有关问题

发布时间: 2012-03-21 13:33:14 作者: rapoo

请教一个C嵌汇编的问题
小弟最近在写一个系统,主要是C的代码,少量涉及到底层的部分用汇编来

实现。我看到一个参考资料上有这样函数片段,代码如下:

unsigned char inportb (unsigned short _port)

{

unsigned char rv;

__asm__ __volatile__ ( "inb %1, %0 " : "=a " (rv) : "dN " (_port));

return rv;

}

我虽然能用这个函数实现我想要的功能,但是对其中的

( "inb %1, %0 " : "=a " (rv) : "dN " (_port))

这句汇编代码,是在是不明白啊,我的猜测是从I/O端口读入数据。可我在

《Intel 64 and IA32 Architectures Software Development 's Manual》

中并没有查到有INB这条指令啊,倒是有IN这条指令,描述如下:

IN AL,DX ;Input byte from I/O port in DX into AL

还请知道的兄弟给指点一下啊。小弟先谢谢了!

[解决办法]
outb (u_char value, u_long port):将value送入端口port。
inb (u_long port) 从端口port读入数据。
__asm__ 表示行内汇编
__volatile__ 表示某个属性,特别是适合某些经常变化的端口社定的变量是不允许被优化的.


其中: 包含文件 #include "system.h "
OUTx 函数:
sends a byte (or word or dword) on a I/O location. Traditionnal names are outb, outw and outl respectively. the "a " modifier enforce value to be placed in eax register before the asm command is issued and "Nd " allows for one-byte constant values to be assembled as constants and use edx register for other cases.


static __inline__ void outportb(uint16_t port,uint8_t value)
{
__asm__ volatile ( "outb %0,%1 "
:: "a " ((char) value), "d "((uint16_t) port));
}



INx 函数:
receives a byte (or word or dword) from an I/O location. Traditionnal names are inb, inw and inl respectively

static __inline__ uint8_t inportb(uint16_t port)
{
uint8_t _v;
__asm__ volatile ( "inb %1,%0 "
: "=a " (_v): "d "((uint16_t) port));
return _v;
}



[解决办法]
接个是 at&t 风格的汇编, 不是 intel 风格的汇编代码, 就等于 in al , dx ....

读书人网 >C语言

热点推荐