内联汇编:'out' 的操作数类型不匹配
inline asm: operand type mismatch for 'out'
我对汇编了解不深。以下代码用于写入硬件端口。
编译器在内联汇编的每一行给出操作数类型不匹配错误used.When我编译我得到这些错误:
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, %1\njmp 1f\n1: jmp 1f\n1:" :: "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 位,将选择错误大小的寄存器,生成的程序集将有一个操作数不匹配。
在这种情况下,显然 OP 在创建自己的 uint16_t
定义时不正确地指定了错误的尺寸类型。端口命令(输入、输出等)仅将 DX(16 位)作为寄存器操作数,而不是 EDX 或 RDX 或 DL。如果生成的寄存器不是 DX,则代码将具有 compiled/assembled,并出现 OP 看到的错误。
我对汇编了解不深。以下代码用于写入硬件端口。
编译器在内联汇编的每一行给出操作数类型不匹配错误used.When我编译我得到这些错误:
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, %1\njmp 1f\n1: jmp 1f\n1:" :: "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 位,将选择错误大小的寄存器,生成的程序集将有一个操作数不匹配。
在这种情况下,显然 OP 在创建自己的 uint16_t
定义时不正确地指定了错误的尺寸类型。端口命令(输入、输出等)仅将 DX(16 位)作为寄存器操作数,而不是 EDX 或 RDX 或 DL。如果生成的寄存器不是 DX,则代码将具有 compiled/assembled,并出现 OP 看到的错误。