使用 GCC 编译内联程序集时出错,"shl"
error compiling inline assembly with gcc, "shl"
这是我尝试转换为gcc风格的asm内联汇编代码的实际代码。
#include<iostream>
using namespace std;
int reverse(int num);
int main(){
int num;
cout << "enter number: ";
cin >> num;
cout << endl;
cout << reverse(num);
return 0;
}
int reverse(int num){
if(num == 0 || num == 1){
return num;
}
__asm
{
xor eax, eax
xor ecx, ecx
mov ebx, num
clc ; clear carry
not_found:
inc ecx
shl ebx, 1
jnc not_found
rcr eax, 1
mov edx, ecx
again:
shl ebx, 1
rcr eax, 1
inc ecx
cmp ecx, 32
jne again
dec edx
again2:
shr eax, 1
dec edx
cmp edx, 0
jne again2
}
}
由于我无法使用 gcc 编译上述代码,我尝试将其转换为一些可以通过 gcc 编译器成功编译的东西,但到目前为止我无法产生任何有意义的结果。
根据 OP 注释中的代码,下面是一个修改后的示例,它使用内联程序集移动了一位:
#include<iostream>
using namespace std;
int reverse(int num);
int main()
{
int num;
cout << "enter number: ";
cin >> num;
cout << endl;
cout << reverse(num) << endl;
return 0;
}
int reverse(int num)
{
if (num == 0 || num == 1)
{
return num;
}
int temp = 0;
asm( "shll $1, %0 nt" : "=r"(temp) : "0"(num));
return temp;
}
请注意,如果您使用 r
约束要求操作数,gcc
将操作数放入寄存器中,因此实际上没有理由自己mov
它们(至少在这个小示例中(。此外,我在输入约束中使用了0
来指示它应该与输出相同的寄存器,因为这就是shl
的工作方式。
仍然不确定num == 1
条件,但大概这是因为我们不知道完整函数的实际功能。
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- 导入/导出变量时出错
- 使用 GCC 编译内联程序集时出错,"shl"