c - Extended Inline Assembly GCC- bad register name and junk 'done' after expression error when compiling -
i'm writing program using assembly code write program calculate 1 of quadratic equation roots. i've written of code, have following error:
main.c:37: error: bad register name `%qword' main.c:39: error: junk `done' after expression
how correct error, please?
my codes is:
// function checking assembly code computing correct result double quadraticrootc(double a, double b, double c) { return (-b + sqrt(b * b - 4 * * c)) / (2 * a); } double quadraticroot(double a, double b, double c) { double root; asm( "fld %1 \n" "fadd %%st \n" "fld %1 \n" "fld %3 \n" "fmulp %%st(1) \n" "fadd %%st \n" "fadd %%st \n" "fchs \n" "fld %2 \n" "fld %2 \n" "fmulp %%st(1) \n" "faddp %%st(1) \n" "ftst \n" "fstsw %%1x \n" "sahf \n" "fsqrt \n" "fld %2 \n" "fchs \n" "fdivp %%st(1) \n" "mov %0, %%eax \n" "fstp %%qword, %%eax \n" "mov $1, %%eax \n" "jmp short done \n" "done: \n" :"=g"(root) :"g"(a), "g"(b), "g"(c) :"eax" ); return(root); } int main(int argc, char **argv) { double a, b, c; double root, rootc; if (argc != 4) { printf("need 3 arguments: a, b, c\n"); return -1; } = 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; }
the line error occurs:
"mov %0, %%eax \n" "fstp %%qword, %%eax \n" "mov $1, %%eax \n"
any appreciated.
apart errors listed, there other issues. main 1 being default operand size fld
single precision, while have doubles. need explicitly use fldl
. second, if ever find using g
constraint, think twice, because chances doing wrong. in case, input operands should memory references (m
constraint), , output should left in fpu stack (t
constraint). have removed unused code dealing status word now, suppose placeholder detect taking square root of negative number.
you should comment code, if ask others help. since haven't done that, have no idea wanted eax
@ end. removed too.
here working version:
#include <stdio.h> #include <stdlib.h> #include <math.h> // function checking assembly code computing correct result double quadraticrootc(double a, double b, double c) { return (-b + sqrt(b * b - 4 * * c)) / (2 * a); } double quadraticroot(double a, double b, double c) { double root; asm( "fldl %1 # \n" "fadd %%st # 2a \n" "fldl %1 # 2a \n" "fldl %3 # c 2a \n" "fmulp %%st(1) # ac 2a \n" "fadd %%st # 2ac 2a \n" "fadd %%st # 4ac 2a \n" "fldl %2 # b 4ac 2a \n" "fldl %2 # b b 4ac 2a \n" "fmulp %%st(1) # b^2 4ac 2a \n" "fsubp %%st(1) # b^2-4ac 2a \n" "fsqrt # sqrt(b^2-4ac) 2a \n" "fldl %2 # b sqrt(b^2-4ac) 2a \n" "fchs # -b sqrt(b^2-4ac) 2a \n" "faddp %%st(1) # -b+sqrt(b^2-4ac) 2a \n" "fdivp %%st(1) # -b+sqrt(b^2-4ac)/2a \n" :"=t"(root) :"m"(a), "m"(b), "m"(c) ); return(root); } int main(int argc, char **argv) { double a, b, c; double root, rootc; if (argc != 4) { printf("need 3 arguments: a, b, c\n"); return -1; } = 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; }
Comments
Post a Comment