与C内联编写的汇编代码不起作用

时间:2022-03-04 15:13:31

I'm following a tutorial on youtube and he said that it's needed to write some asm code in C (i'm not very good in assembly) so i just copyed the entire code:

我正在关注youtube上的一个教程,他说需要在C中编写一些asm代码(我在汇编方面不是很好)所以我只是复制了整个代码:

unsigned char inPortB (unsigned int _port) {
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}

and

void outPortB (unsigned int _port, unsigned char _data) {
    __asm__ __volatile__ ("outb %1, %0" : :  "dN" (_port), "a" (_data));
}

but when i compile i get this error:

但是当我编译我得到这个错误:

operand type mismatch for 'in'
operand type mismatch for 'out'

how can i fix it?

我该怎么办呢?

1 个解决方案

#1


2  

The port number needs to be in dx which is a 16 bit register. To make gcc generate a reference to dx instead of edx as it does with your code, you need to give _port a 16 bit type, e.g. unsigned short:

端口号需要是dx,这是一个16位寄存器。要使gcc生成对dx而不是edx的引用,就像对代码一样,你需要给_port一个16位类型,例如未签名的短片:

unsigned char inPortB (unsigned short _port) {
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}

This should fix your issues.

这应该可以解决您的问题。

#1


2  

The port number needs to be in dx which is a 16 bit register. To make gcc generate a reference to dx instead of edx as it does with your code, you need to give _port a 16 bit type, e.g. unsigned short:

端口号需要是dx,这是一个16位寄存器。要使gcc生成对dx而不是edx的引用,就像对代码一样,你需要给_port一个16位类型,例如未签名的短片:

unsigned char inPortB (unsigned short _port) {
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}

This should fix your issues.

这应该可以解决您的问题。