0

How do I assign a simple constant like "10" to a register in inline assembly and move it to an output variable in gcc c++?

int eax;
asm("label1:"
"mov $100000000, %%ecx"
"dec %%ecx"
"cmp $0, %%ecx"
"jnz label1"
"mov %%ecx, %%eax"
: "=a"(eax)
: : );

This is the error I get: "Error: bad register name `%ecxmov $0'"

Edit: I fixed it in a way that the program runs by adding "\n\t" to each line. I also added ecx as a clobber. But Now I get an endless loop even though it should break when ecx is 0.

int eax;
asm("label1:\n\t"
"mov $100000000, %%ecx\n\t"
"dec %%ecx\n\t"
"cmp $0, %%ecx\n\t"
"jnz label1\n\t"
"mov %%ecx, %%eax\n\t"
: "=a"(eax)
: : "ecx");
  • 2
    One big issue is that you don't have anything to denote the end of line so your string is one large string with no breaks. At the end of each instruction put a `;` or `\n\t` . Modifying registers (like ECX) without telling the compiler can lead to potential bugs. – Michael Petch Oct 03 '20 at 18:15
  • 1
    You forgot `\n` at the end of each line. Remember that C string literals separated only by whitespace get concatenated into one long string literal. Once you fix that, then you're missing a clobber on `"ecx"`. – Peter Cordes Oct 03 '20 at 18:16
  • My program is now starting but it is looping endlessly which is weird since it should break when ecx is 0 – abcdefghi019283 Oct 03 '20 at 18:37
  • How many times do you move 100000000 to ecx? If you do that at the start of each loop, it will never go to zero. – David Wohlferd Oct 03 '20 at 19:31
  • oh god that is embarrassing; yeah you are right.. – abcdefghi019283 Oct 03 '20 at 19:34

0 Answers0