this is the actual code that I’m trying to convert to gcc style asm inline assembly code.
#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
}
}
As i was unable to compile the above code with gcc, I tried converting it to some thing that could be compiled successfully by a gcc compiler, but so far I could not manage to produce any meaningful result.
Based on the code in the comment of the OP, here is a modified example that shifts by one bit using inline assembly:
Note that
gccputs the operands into registers if you ask for it using therconstraint so really no reason tomovthem yourself (at least in this small sample).Also, I have used
0in the input constraint to signal that it should be the same register as the output, because that’s howshlworks.Still not sure about the
num == 1condition, but presumably that’s because we don’t know the actual function of the complete function.