开发者

gcc inline assembler shift left problem

I'm having trouble compiling the code below. It may also have logical errors, please help. thanks,

#include <iostream>

using namespace std;

int main()
{
    int shifted_value;
    int value = 2;

    __开发者_运维技巧asm__("shll %%eax,%1;" : "=a" (shifted_value): "a" (value));

    cout<<shifted_value<<endl;

    return 0 ;
}

The error is:

Error: suffix or operands invalid for `shl'


It should look like

__asm__(("shll %%cl, %%eax;"
            : "=a" (shifted_value)
            : "a" (shifted_value), "c" (value)
           );
Credit for correct code goes to the other answer for pointing out that the operands were in the incorrect order. You dont need to specify eax as clobbered because eax is an output register.


it can work with shll also, shll being the GNU at&t mnemonic for "shift left longword". However, the invalid operand error is due to the operand needing to come first! I found this out when I googled up these sources: http://meplayer.googlecode.com/svn-history/r23/trunk/meplayer/src/filters/transform/mpcvideodec/ffmpeg/libavcodec/cabac.h. I also used the excellent http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html#s6

Here is one way that works, taking into account Jesus Ramos's observation that shifted_value needs to be initialized:

jcomeau@intrepid:/tmp$ cat test.cpp; make test; ./test
#include <iostream>

using namespace std;

int main()
{
    int shifted_value = 1;
    char value = 2;

    __asm__("shll %%cl, %%eax;"
            : "=a" (shifted_value)
            : "a" (shifted_value), "c" (value)
           );

    cout<<shifted_value<<endl;

    return 0 ;
}

g++     test.cpp   -o test
4
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜