Using ASM command in C

有些话、适合烂在心里 提交于 2020-01-02 08:46:28

问题


I have a small question about using ASM in c. I want to execute the instruction:

LDR PC,=0x123456

This gives me the error "unexpected token in operand".

asm("LDR PC,=0x123456");

This gives "invalid constraint".

asm("LDR PC," : "m" (0x123456));

What's the right way to do this?


回答1:


You are using this:

asm("LDR PC,=0x123456");

This is not a standard ARM assembly instruction, but a pseudo-instruction provided as a compiler extension. This pseudo-instruction is converted to other assembly instructions when you compile it. It seems clang doesn't support this compiler extension (see this thread). You should do the conversion to assembly instructions yourself, see the ARM documentation for how the LDR pseudo-instruction is converted.




回答2:


You can probably achieve the effect you want in plain C:

((void (*)(void))0x123456)();

or if you prefer more verbose:

typedef void FN(void);
((FN*)0x123456)();



回答3:


I agree with @Étienne. I tried you code with mi Google toolchain. It's working fine.

I think you should read the manual how the compiler changes the directive to instructions (normally two mov instructions).



来源:https://stackoverflow.com/questions/19007207/using-asm-command-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!