内联 ASM:'out'的操作数类型不匹配
inline asm: operand type mismatch for 'out'
我不知道汇编深度。以下代码是为硬件端口写作。
编译器在使用内联ASM的每一行中都会给操作数类型的不匹配错误。当我编译时,我会得到这些错误:
port.cpp: Assembler messages: port.cpp:27: Error: operand type mismatch for 'out' port.cpp:34: Error: operand type mismatch for 'in' port.cpp:51: Error: operand type mismatch for 'out' port.cpp:69: Error: operand type mismatch for 'out' port.cpp:75: Error: operand type mismatch for 'in' port.cpp:94: Error: operand type mismatch for 'out' port.cpp:100: Error: operand type mismatch for 'in'
port.h
#ifndef __PORT_H
#define __PORT_H
#include "types.h"
class Port
{
protected:
Port(uint16_t portnumber);
~Port();
uint16_t portnumber;
};
class Port8Bit : public Port
{
public:
Port8Bit(uint16_t portnumber);
~Port8Bit();
virtual uint8_t Read();
virtual void Write(uint8_t data);
};
class Port8BitSlow : public Port8Bit
{
public:
Port8BitSlow(uint16_t portnumber);
~Port8BitSlow();
virtual void Write(uint8_t data);
};
class Port16Bit : public Port
{
public:
Port16Bit(uint16_t portnumber);
~Port16Bit();
virtual uint16_t Read();
virtual void Write(uint16_t data);
};
class Port32Bit : public Port
{
public:
Port32Bit(uint16_t portnumber);
~Port32Bit();
virtual uint32_t Read();
virtual void Write(uint32_t data);
};
#endif
port.cpp
#include "port.h"
Port::Port(uint16_t portnumber)
{
this->portnumber = portnumber;
}
Port::~Port()
{
}
Port8Bit::Port8Bit(uint16_t portnumber)
: Port(portnumber)
{
}
Port8Bit::~Port8Bit()
{
}
void Port8Bit::Write(uint8_t data)
{
__asm__ volatile("outb %0, %1" :: "a" (data), "Nd" (portnumber));
}
uint8_t Port8Bit::Read()
{
uint8_t result;
__asm__ volatile("inb %1, %0" : "=a" (result) : "Nd" (portnumber));
return result;
}
Port8BitSlow::Port8BitSlow(uint16_t portnumber)
: Port8Bit(portnumber)
{
}
Port8BitSlow::~Port8BitSlow()
{
}
void Port8BitSlow::Write(uint8_t data)
{
__asm__ volatile("outb %0, %1njmp 1fn1: jmp 1fn1:" :: "a" (data), "Nd" (portnumber));
}
Port16Bit::Port16Bit(uint16_t portnumber)
: Port(portnumber)
{
}
Port16Bit::~Port16Bit()
{
}
void Port16Bit::Write(uint16_t data)
{
__asm__ volatile("outw %0, %1" :: "a" (data), "Nd" (portnumber));
}
uint16_t Port16Bit::Read()
{
uint16_t result;
__asm__ volatile("inw %1, %0" : "=a" (result) : "Nd" (portnumber));
return result;
}
Port32Bit::Port32Bit(uint16_t portnumber)
: Port(portnumber)
{
}
Port32Bit::~Port32Bit()
{
}
void Port32Bit::Write(uint32_t data)
{
__asm__ volatile("outl %0, %1" :: "a" (data), "Nd" (portnumber));
}
uint32_t Port32Bit::Read()
{
uint32_t result;
__asm__ volatile("inl %1, %0" : "=a" (result) : "Nd" (portnumber));
return result;
}
我该如何纠正?程序结构有什么问题吗?
OP从未显示types.h
,但是后续评论仅表明存在一种实际可能性,而uint16_t
并未定义为16位类型。在x86/x86-64上,可以通过以下方式定义这样的类型。
typedef unsigned short int uint16_t;
错误之所以出现,是因为为了选择一个扩展的内线装配模板GCC的寄存器使用约束中传递的类型的大小,以确定所选寄存器是否应为16/32/64位寄存器。如果大小不是16位,则将选择错误的尺寸寄存器,并且生成的组件将具有操作数不匹配。
在这种情况下,显然,当他创建自己的uint16_t
定义时,OP不正确地指定了错误的尺寸类型。端口命令(IN,OUT等)仅将 dx (16位)作为寄存器操作数,而不是 edx 或 rdx 或 rdx 或DL 。如果生成的寄存器不是 dx ,则代码将与OP所看到的错误一起编译/组装。
相关文章:
- 与'operator='不匹配(操作数类型'String'且"void")
- 不正确的操作数类型 MSVC
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- c++运算符重载-我实际返回的操作数类型是什么
- 没有运算符"=="与这些操作数匹配。操作数类型为:int == type_name'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- 没有运算符匹配这些操作数;操作数类型为: std::istream >> const char [5]
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 重载运算符'<<'的使用不明确(操作数类型'ostream'(也称为"basic_ostream<char>")和"Person")
- 错误:与"运算符[]"不匹配(操作数类型为"QVector<int>"和"QCharRef")
- 错误:如果(object1 == object2),“操作器==”无匹配(操作数类型为“ const class”和“
- 没有运算符"="匹配这些操作数操作数操作数类型为 std::basic_ostream<char、std::char_traits<char>> = int