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

#define PRECISION   3

double absD(double n)
{
asm(
"_absD: \n"
"pushl %ebp \n"
"fldz \n"
"movl %esp, %ebp \n"
"fldl 8(%ebp) \n"
"fxch %st(1) \n"
"fucomp %st(1) \n"
"fnstsw %ax \n"
"sahf \n"
"jbe L2 \n"
"fchs \n"
"L2: \n"
"popl %ebp \n"
"ret \n"
"fldl %[nIn]\n"
"fabs\n"
"fstpl %[nOut]\n"
: [nOut] "=m" (n)
: [nIn] "m" (n)
);
// do not change anything above this comment

    return n;

// do not change anything below this comment, except for printing out your name
}

int main(int argc, char **argv)
{
    double  n = 0.0;

    printf("CS201 - Assignment 02 - your name\n");
    if (argc > 1)
        n = atof(argv[1]);

    printf("abs(%.*f) = %.*f\n", PRECISION, n, PRECISION, absD(n));

    return 0;
}

I got an error saying parse error before string constant when I tried to compile it. I'm really new to C world . Any help would be appeciated !!

Dani AI

Generated

As suggested, inline assembly is compiler-specific. On Cygwin you’re almost certainly using GCC, and GCC’s extended asm templates treat % specially. Single % before a register (for example %ebp) will be parsed as an asm-placeholder and can trigger “parse error before string constant.” Also note your code uses 32‑bit instructions/registers; if your compiler is targeting x86_64 that will cause additional errors.

Practical fixes:

  • Escape assembler register names by doubling the percent sign inside the template — e.g. use pushl %%ebp, movl %%esp, %%ebp, fnstsw %%ax, fxch %%st(1). Leave the GCC operand references like %[nIn] and %[nOut] as they are.
  • If you really intend to define a separate assembly function (you have a label and a ret), put that asm block at file scope or in a separate .s file. Embedding a full function prologue/epilogue inside inline asm placed inside another C function is dangerous and will confuse the compiler/assembler.
  • If your toolchain is 64‑bit, either compile for 32‑bit with -m32 or rewrite the assembly for x86_64 (different registers/ABI).

Troubleshooting tips: compile with gcc -Wall -Wextra -g and paste the exact gcc command plus the first few error lines if it still fails. A quick sanity check is to try asm("nop"); to confirm asm is accepted. If you post the compiler command and error output, people here can point to the exact line that needs fixing.

Recommended Answers

All 2 Replies

Inline assembly is very compiler dependent. Are you using the same compiler to build this program as your teacher used to write it?

I'm using cygwin to compile it !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.