Janosch Hübner
Janosch Hübner

Reputation: 1694

Using ASM command in C

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?

Upvotes: 3

Views: 1720

Answers (3)

TwoCode
TwoCode

Reputation: 127

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).

Upvotes: 0

scott
scott

Reputation: 768

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)();

Upvotes: 0

Étienne
Étienne

Reputation: 4984

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.

Upvotes: 2

Related Questions