0

I cant convert a c code to assembly code properly

the code is

    #include <stdio.h>      /* Standard Library of Input and Output */
   #include <complex.h>    /* Standard Library of Complex Numbers */


 double complex power(double complex value, int power){
     double complex result=1;
    for (int i=0;i<power;i++){
        result  = result * value;
    }
    return result;
 }

  double complex function(double order,double complex * array,double complex value) // a function that calculates f(x)
  {
    double complex result=0;
     for (int i = order ; i >= 0 ; i--){
      result = result + array[i]*(power(value,i));
  }
   //printf("the value of f(x) is:  = %.10f %+.10fi\n",creal(result), cimag(result));
  return result;
 } 
double complex derivefunction(double order,double complex * array,double complex value,double complex * darray){
  for (int i = order-1 ; i >= 0 ; i--){
       darray[i] = array[i+1]*(i+1);
}
return function(order-1,darray,value);

  }
   double abs(double m){//absolute
    if (m<0) 
        return -m;
    return m;
  }

  int main(void) {


   double tol,order;
   double arrayr[100];//assuming the maximum order is 0
   double arrayi[100];
   double complex array[100];

    printf("Enter the tolarence:\n");
    scanf("%lf", &tol);
    printf("the toleracnce is: %.10f\n",tol);


printf("Enter the order\n");
scanf("%lf", &order);
printf("the order is: %lf\n",order);


for (int i = order ; i >= 0 ; i--){
    printf("Enter the %d coeficience\n",i);
    scanf("%lf", &(arrayr[i]));//taking the real number
    scanf("%lf", &(arrayi[i]));// taking the imaginary number
    array[i] = arrayr[i] + arrayi[i] * I;//summing the complex number
    printf("The %d coeficience is  = %.2f %+.2fi\n",i ,creal(array[i]), cimag(array[i]));
}


double initialr, initiali;
double complex initial;
printf("Enter the initial value of z(the guss) \n");
scanf("%lf", &initialr);//taking the real number
scanf("%lf", &initiali);// taking the imaginary number
initial = initialr + initiali*I;

printf("The initial is  = %.2f %+.2fi\n", creal(initial), cimag(initial));
double complex result1 = function(order,array,initial);



     if (order > 0) {

    double complex darray[sizeof(array)];
    double complex result2 = derivefunction(order,array,initial,darray);



    //now the newton-rashford method
      double complex m = result1/result2;//the initial
    while (abs((double)(m)) >= tol)//if it is in the range
  {

        m = function(order,array,initial) / derivefunction(order,array,initial,darray);//f(x)/f'(x)
        initial = initial - m;
        //printf("The initial is  = %.10f %+.10fi\n", creal(initial), cimag(initial));
  }
  printf("The root is  = %.10f %+.10fi\n", creal(initial), cimag(initial));
 }

return 0;

  }

I used the command gcc -S -O main.c to create an assembly file called main.s but when I try to compile the assembly file I get many errors

    nasm -g -f elf64 -w+all -o main.o main.s
    main.s:1: error: attempt to define a local label before any non-local labels
    main.s:1: error: parser: instruction expected
    main.s:2: error: attempt to define a local label before any non-local labels

and so on.. I think it relates to the fact that I use complex number but I am not sure how to fix this.

  • 1
    AFAIK gcc uses AT&T syntax by default, whereas nasm uses Intel. – Siguza Apr 18 '18 at 19:27
  • Beats me why you would show the original C code rather then the generated assembler for which the error was issued. The assembly code generated by gcc is intended to be assembled using gas rather then nasm perhaps? – Clifford Apr 18 '18 at 19:32
  • Take a look at this: https://stackoverflow.com/questions/199966/how-do-you-use-gcc-to-generate-assembly-code-in-intel-syntax as recommended by @Clifford. – mustachioed Apr 18 '18 at 19:34
  • @MustacheMoses : Don't put words into my mouth. I did not recommend anything. The question is about assembling the generated assembler, the link you have given is about how to generate the assembly code in the first instance - I am assuming he has done that bit, although the evidence is not provided. My only suggestion was that the assembly code be posted not the C. But the answer is I am pretty certain to use gas not nasm. – Clifford Apr 18 '18 at 19:37
  • @Clifford Sorry, I should have worded that differently... – mustachioed Apr 18 '18 at 19:46
  • @MustacheMoses when typing gcc -S -masm=intel main.c it generates an main.s file and doesnt show error but when trying to compile with the command nasm -f elf main.s it generate the same errors. – נירייב שמואל Apr 18 '18 at 19:56
  • help please I cant compile for some reason – נירייב שמואל Apr 19 '18 at 05:38
  • 2
    Your problem is that gcc does not output code intended to be assembled by nasm. It's expecting you to use gas, and there are some differences. I supposed you could try [this](https://stackoverflow.com/a/20743090/2189500), but there might still be tweaks you'd have to do. – David Wohlferd Apr 19 '18 at 06:45
  • the posted C code does not cleanly compile. Strongly suggest fixing the problems in the C code before trying to convert to asm. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) – user3629249 Apr 19 '18 at 14:24

0 Answers0