0

I have the following C code extended with assemble x86 :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// a function to perform a bitwise right rotation
void ror() {

    unsigned int rotatedEdx;

    __asm__(

            "mov $0xabd7e166, %edx;"
            "ror $1, %edx;"
            "mov %%edx, %0;"
            : "=r" (rotatedEdx)

            );

    printf("%ld\n", rotatedEdx);

}

int main(int argc, char *argv[]) {

    ror();

    return 0;
}

Which gcc command should i use to compile my file into an executable one ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Garde Des Ombres
  • 173
  • 1
  • 3
  • 12
  • 3
    That is broken code. As for compiling, you don't need anything special. `gcc foo.c` – Jester Jan 18 '21 at 22:09
  • what do you mean by broken ? – Garde Des Ombres Jan 18 '21 at 22:11
  • 4
    @GardeDesOmbres You need `%%edx`, not `%edx`. You also seem to have two hexadecimal prefixes on that number. You also don't have `edx` in the clobber list, but you overwrite it. You've also used the format sting for printing a `signed long int` when you're trying to print a `unsigned int`. – Thomas Jager Jan 18 '21 at 22:16
  • @ThomasJager how can i add edx into the clobber list ? – Garde Des Ombres Jan 18 '21 at 22:33
  • 2
    @GardeDesOmbres GCC inline assembly isn't the most straightforward thing to work with. I'd strongly suggest reading through all of the related documentation pages. – Thomas Jager Jan 18 '21 at 22:35
  • A better option would be to use an `"=d"` output instead of a `mov` instruction, to just tell the compiler the asm produces an output in EDX. It's pointless to write your own `mov` instruction to the compiler's choice of register; it can do that itself if you tell it where the output is. See https://stackoverflow.com/tags/inline-assembly/info – Peter Cordes Jan 19 '21 at 00:02
  • 1
    But yeah, even if you *do* know exactly how inline asm works, https://gcc.gnu.org/wiki/DontUseInlineAsm. Especially not for rotate. Instead, use C that can compile to a `ror` ([Best practices for circular shift (rotate) operations in C++](https://stackoverflow.com/q/776508)), and look at the compiler's asm output to make sure that happens. ([How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116)). – Peter Cordes Jan 19 '21 at 00:05
  • Why not use builtins? Pete Cordes has a good point - don't EVER use inline assembly, its not portable, and gcc is (usually) smarter than you. – Import Accelerate Jan 19 '21 at 00:52
  • @ImportAccelerate There are valid use cases for inline assembly, but you really need to know what you are doing. And if you can't even find and read the relevant documentation, you definitely don't know what you are doing. – fuz Jan 19 '21 at 14:47
  • I can't think of a use case. Perhaps you could provide one? – Import Accelerate Jan 19 '21 at 14:57

0 Answers0