title: Assembly Code in a C Function
body: <p>I'm trying to write a quadratic root function in assembly code in a .c file, and I've been having trouble getting the syntax in the AT&amp;T format correctly. The errors I'm getting are: "in function quadratic root, undefined named operand EAX, undefined named operand 1, unknown register name ST1 in asm". Any help is appreciated. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  // function for checking that assembly code is computing the correct result
  double quadraticRootC(double a, double b, double c)
  {
  return (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
  }
  
  double quadraticRoot(double a, double b, double c)
  {
  // assembly code to calculate the quadratic root
      double root1;
  asm( 
      //"quadraticRoot:                      "
               //"enter  0,0               "
               "fld    %[a]               \n" // a
               "fadd   %%ST               \n" // 2a
               "fld    %[a]               \n" // a, 2a
               "fld    %[c]               \n" // c,a,2a
               "fmulp  %%ST(1)            \n" // ac, 2a
               "fadd   %%ST               \n" // 2ac, 2a
               "fadd   %%ST               \n" // 4ac, 2a
               "fchs                      \n" // -4ac, 2a
               "fld                       \n" // b, -4ac, 2a
               "fld    %[b]               \n" // b, b, -4ac, 2a
               "fmulp  %%ST(1)            \n" // b*b, -4ac, 2a
               "faddp  %%ST(1)            \n" // b*b-4ac, 2a
               "ftst                      \n" // compare (b*b-4ac) with 0
               "fstsw  %%AX               \n" // store status word in AX
               "sahf                      \n"
               "jb     no_real_roots      \n"
               "fsqrt                     \n" // sqrt(b*b-4ac), 2a
               "fld    %[b]               \n" // b, sqrt(b*b-4ac), 2a
               "fchs                      \n" // -b, sqrt(b*b-4ac), 2a
               "fadd   %%ST(1)            \n" // -b+sqrt(b*b-4ac), sqrt(b*b-4ac), 2a
               "fdiv   %%ST(2)            \n" // -b+sqrt(b*b-4ac)/2a, sqrt(b*b-4ac), 2a
               "mov    %%EAX, %[root1]    \n"
               "fstp   %%qword, %[EAX]    \n" // store root1                    
               //"fchs                    \n" // -sqrt(b*b-4ac), 2a
               //"fld    $b               \n" // b, sqrt(b*b-4ac), 2a
               //"fsubp  %ST1             \n" // -b-sqrt(b*b-4ac), 2a
               //"fdivrp %ST1             \n" // -b-sqrt(b*b-4ac)/2a
               //"mov    %EAX, (root2)    \n"         
               //"fstp   %qword[EAX]      \n" // store root2
               "mov      %%EAX, %[1]      \n" // real roots exist
               "jmp      short done       \n"
     "no_real_roots:                    \n"
               "sub      %%EAX, %%EAX     \n" // EAX = 0 (no real roots)
               "done:                     \n"
               :"=m" (root1)
               :[a] "m" (a), [b] "m" (b), [c] "m" (c), [root1] "m" (root1)
               :"ST(1)"
  
     //return(root1);                 
     // done:
           //"leave\n"
           //"ret\n" 
  );
  
  return(root1);
  }
  
  // main
  int main (int argc, char **argv)
  {
  double  a, b, c;
  double  root, rootC;
  
  printf("*\n");  
  if (argc != 4) {
      printf("need 3 arguments: a, b, c\n");
      return -1;
      }
  a = atof(argv[1]);
  b = atof(argv[2]);
  c = atof(argv[3]);
  root = quadraticRoot(a, b, c);
  rootC = quadraticRootC(a, b, c);
  
  printf("quadraticRoot(%.3f, %.3f, %.3f) = %.3f, %.3f\n", a, b, c, root, rootC);
  
  return 0;
  }
  </code></pre>