0

Is there a way to incorporate a __asm instruction into a C expression?

The idea is to somehow substitute b = expression with __asm(" ":"r" (b)) in

a = b = expression.

Unfortunately __asm is not recognized as expression. Is there a way to mimic __asm as a right-hand expression?

IvanDi
  • 147
  • 2
  • 8
  • By wrapping it into a function or macro `do {} while` block? :-) – oakad Aug 06 '21 at 08:40
  • 2
    I think the inline-assembler notation in C is heavily compiler dependent. What compiler are you using ? – Jakob Sachs Aug 06 '21 at 08:44
  • @JakobSachs, correct, I mean GCC/Clang(LLVM) – IvanDi Aug 06 '21 at 08:57
  • @oakad, unfortunately ```do while``` is not a right-hand expression - ```a = do {} while(0); ``` is not valid – IvanDi Aug 06 '21 at 08:59
  • Does this answer your question? [Does \_\_asm{}; return the value of eax?](https://stackoverflow.com/questions/36802683/does-asm-return-the-value-of-eax) – Jakob Sachs Aug 06 '21 at 09:00
  • @JakobSachs , unfortunately No. ```a = __asm``` is not a valid statement – IvanDi Aug 06 '21 at 09:04
  • @JakobSachs Actually, looking more closely at what I had in mind, the real magic happens because of https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html (https://stackoverflow.com/a/35148076/2702398) – oakad Aug 06 '21 at 09:09
  • Thats because `__asm` doesnt exist in GCC like it does int VC. The first answer on that post details a way how to emulate the same behaviour in GCC. – Jakob Sachs Aug 06 '21 at 09:09
  • Do you need to pass this instruction to other functions? If so, you probably have to create a function for each one and place the asm instruction there and then pass a function pointer to that function. – 12431234123412341234123 Aug 06 '21 at 09:28
  • The other solution would be to create the complete function for each possible asm instruction and call the correct function. Maybe make a function pointer table if you need to store which function you have to use. – 12431234123412341234123 Aug 06 '21 at 09:29
  • I think what you really want is not possible on some architectures and other disable this by default for good reason. You shouldn't change the running code during execution of that code. Disabling this is also a security feature: https://en.wikipedia.org/wiki/W%5EX . If you really need it, you have to use assembler. – 12431234123412341234123 Aug 06 '21 at 09:33

0 Answers0