1

I know how to do it in C,
and I know how to do it in assembly.
But I never did inline assembly before.
How could I do it in inline assembly?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
李維仁
  • 11
  • 2
  • If you know how to do this in assembly then you know how to do it in inline assembly; the only difference is that the latter appears within a program written in some higher-level language. I presume that is C in your case? What have you tried? What's going wrong? – Joe Farrell Sep 02 '18 at 15:12
  • 1
    With most compilers, you *shouldn't* use inline asm. Use an intrinsic instead so the compiler can do constant-propagation. Also, so it can choose between `bswap reg` vs. `movbe reg, [mem]` vs. `movbe [mem], reg`, if we're talking about x86 where there are multiple efficient ways. – Peter Cordes Sep 02 '18 at 15:16
  • Anyway, which compiler are you using, and which architecture? There are two major flavours of inline-asm syntax: MSVC (where single instructions are impossible to do efficiently) vs. GNU (where you need to accurately describe the asm to the compiler by telling it which registers and memory locations are inputs / outputs / clobbers.) https://stackoverflow.com/tags/inline-assembly/info – Peter Cordes Sep 02 '18 at 15:27
  • I'm using GCC on x86. I haven't used inline assembly before, so I don't know how to convert my assembly to inline assembly. – 李維仁 Sep 02 '18 at 16:26
  • 2
    What's wrong with gcc's [__builtin_bswap32](https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fbswap32)? As a general rule, I'd recommend [not using inline asm](https://gcc.gnu.org/wiki/DontUseInlineAsm). But if you must, have you read gcc's [docs](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html) for inline asm? – David Wohlferd Sep 02 '18 at 19:16
  • Welcome to stackoverflow. Please take the [tour] of the site to understand how stackoverflow works and how to ask good questions. Then come back and [edit] your question. Include the code of what you've tried as a [mcve], plus any error messages. Ask specific questions. Most people are happy to help, but don't want to do your work for you. Questions like "how do I do x?" that don't show any research effort on your end are likely to get ignored. – Robert Sep 02 '18 at 21:13
  • I improved the question format and some grammar. – zx485 Sep 02 '18 at 23:12
  • Almost a duplicate of [How to make GCC generate bswap instruction for big endian store without builtins?](https://stackoverflow.com/q/36497605), but the inline-asm answer there is not good. [How to BSWAP the lower 32-bit of 64-bit register?](https://stackoverflow.com/q/176966) has the answer to this as part of the question. – Peter Cordes Sep 02 '18 at 23:23
  • Possible duplicate of [How to BSWAP the lower 32-bit of 64-bit register?](https://stackoverflow.com/questions/176966/how-to-bswap-the-lower-32-bit-of-64-bit-register) – Peter Cordes Sep 02 '18 at 23:23

0 Answers0