Thread: C and Assembly (error: expected identifier or '(' before '.' token)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    C and Assembly (error: expected identifier or '(' before '.' token)

    So I'm having trouble linking my assembly code into my C code. Simply, I have one program, but I'm using two different source codes to implement this one program.

    For the assembly code, I simply converted from C to assembly using the linux command line "gcc -S..". I ran my program with an equivalent C code and it worked perfectly. But when I try to use assembly with it..it just doesn't work. So I don't think it has anything to do with the code. I think I may be linking it incorrectly. Do you guys have more knowledge about this stuff? D:

    (I'll post all of my codes so you guys can see what I'm working with and the errors I'm getting)

    "formula.c"
    C source code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include "nCr.s"
    
    int isNumeric(char *num);
    int form(int power);
    
    int main(int argc, char *argv[]){
    
        clock_t start, end;
        double time;
        int num;
        char value[50];
    
        //error checking
        if(argc<2){
            printf("There are no arguments\n");
        }else if(argc>2){
            printf("There are too many arguments\n");
        }else{
            strcpy(value,argv[1]);
    
            if(value=="-h"){
                printf("Usage: formula<positive integer\n");
            }else{
                if(isNumeric(value)==0){
                    printf("This must be a number or type -h for help\n");
                    return;
            {     }
    
                num = atoi(value);
                if(num<0){
                    printf("Number must have a positive value\n");
                }else{
                    start = clock(); //start of time
                    form(num);
                    end = clock(); //end of time
                    time = ((double)(end-start)/CLOCKS_PER_SEC)*1000000;
                    printf("\nTime Required = %.0f microsecond\n", time);
                }
            }
        }
    
        return 0;
    }
    
    int isNumeric(char *num){
        char *val; //returns 0 if number and 1 if it's not..
    
        if (num == NULL || *num == '\0' || isspace(*num)){
          return 0;
        }
    
        strtod(num, &val);
        return *val == '\0';
    }
    
    int form(int power){
        int powerInd;
        int index = 0;
        int val;
    
        printf("(1 + x)^%d = ",power);
    
        if(power==0){
            printf("1");
            return;
        }
    
        powerInd=power;
        while(powerInd>0){
            if(index==0){
                printf("1 ");
                index++;
            }else{
                val = nCr(power,index);
                printf(" + %d*x^%d",val,index);
                index++;
                powerInd--;
            }
        }
    }

    "nCr.s"
    ASSEMBLY source code:
    Code:
    .globl nCr
        .type    nCr, @function
    nCr:
    .LFB0:
        .cfi_startproc
        pushq    %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        subq    $32, %rsp
        movl    %edi, -20(%rbp)
        movl    %esi, -24(%rbp)
        movl    -24(%rbp), %eax
        movl    -20(%rbp), %edx
        movl    %edx, %ecx
        subl    %eax, %ecx
        movl    %ecx, %eax
        movl    %eax, -12(%rbp)
        movl    -12(%rbp), %eax
        movl    %eax, %edi
        call    Factorial
        movl    %eax, -12(%rbp)
        movl    -20(%rbp), %eax
        movl    %eax, %edi
        call    Factorial
        movl    %eax, -20(%rbp)
        movl    -24(%rbp), %eax
        movl    %eax, %edi
        call    Factorial
        movl    %eax, -24(%rbp)
        movl    -24(%rbp), %eax
        imull    -12(%rbp), %eax
        movl    %eax, -4(%rbp)
        movl    -20(%rbp), %eax
        movl    %eax, %edx
        sarl    $31, %edx
        idivl    -4(%rbp)
        movl    %eax, -8(%rbp)
        movl    -8(%rbp), %eax
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
    .LFE0:
        .size    nCr, .-nCr
        
        .globl Factorial
        .type    Factorial, @function
    Factorial:
    .LFB1:
        .cfi_startproc
        pushq    %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl    %edi, -20(%rbp)
        movl    $1, -4(%rbp)
        jmp    .L4
    .L5:
        movl    -4(%rbp), %eax
        imull    -20(%rbp), %eax
        movl    %eax, -4(%rbp)
        subl    $1, -20(%rbp)
    .L4:
        cmpl    $0, -20(%rbp)
        jg    .L5
        movl    -4(%rbp), %eax
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc

    "nCr.h"
    ASSEMBLY header file
    Code:
    #ifndef _NCR_H_
    #define _NCR_H_
    
    extern int nCr(int n, int r);
    extern int Factorial(int n);
    
    #endif /* _NCR_H_ */
    MY ERROR!!!:
    Code:
    nCr.s:1: error: expected identifier or '(' before '.' token
    nCr.s:2: error: stray '@' in program
    nCr.s:51: error: stray '@' in program
    make: *** [formula.o] Error 1

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why do you think this line is valid in C.

    Code:
    #include "nCr.s"
    I see no reason to believe your assembly code is valid C code as you included it.

    You may have meant to do this
    Code:
    #include "nCr.h"
    Tim S.
    Last edited by stahta01; 04-13-2013 at 10:27 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Sorry, I forgot to mention..when I ran my program with full C code, it obviously didn't have that "include nCr.s" line. It just had the regular "include nCr.c". But when I incorporated the assembly code, I changed it..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do what stahta01 suggests, then compile with

    gcc nCr.s formula.c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Okay, I'll try it. I'll let you know in a minute!

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    It worked! Thank you! Now, I have a makefile that I use to run my programs. How would I incorporate nCr.s into my makefile?

    Here's my makefile in case..
    Code:
    COMPILER=gcc
    CFLAGS = -ansi -pedantic -Wall
    all: formula
    
    debug:
        make DEBUG=TRUE
    
    formula: formula.o
        $(COMPILER) $(CCFLAGS) -o formula formula.o
    formula.o: formula.o
        $(COMPILER) $(CCFLAGS) -c formula.c
    
    ifeq ($(DEBUG), TRUE)
     CCFLAGS += -g
    endif
    
    clean:
        rm -f formula
        rm -f *.o

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > formula.o: formula.o
    Surely some mistake - how can a file depend on itself?

    > How would I incorporate nCr.s into my makefile?
    In the same way you have a .c to .o rule, you would have a .s to .o rule as well.

    Then the executable depends on all the .o file(s)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Oops. I didn't even notice that. Thank you for noticing.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Thanks. I have my makefile working now. Though, is there a way to make the executable not "a.out", but a variable called "formula"..so that I can call the program like "./formula blahblah"

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just rename the file, or specify its name when compiling, e.g., with the -o option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    oh got it! I didn't really understand makefiles that well until I read a bit more into it and it's not as bad as I thought it'd be. I realized that I was making my makefile longer than it should have been, but it works better now.

    Here's what I got in case anyone was wondering
    Code:
    COMPILER=gcc
    CFLAGS = -ansi -pedantic -Wall
    all: formula
    
    debug:
        make DEBUG=TRUE
    
    formula: nCr.s formula.c
        $(COMPILER) $(CCFLAGS) nCr.s formula.c -o formula
    
    ifeq ($(DEBUG), TRUE)
     CCFLAGS += -g
    endif
    
    clean:
        rm -f formula
        rm -f *.o


  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I realized that I was making my makefile longer than it should have been, but it works better now.
    You've made the makefile shorter, but you've increased the amount of work that needs to be done each time you build the program.

    You're recompiling everything, even when only one thing has changed. When you have projects with 1000's of files, having efficient makefiles is a must.

    Code:
    formula: nCr.o formula.o
        $(COMPILER) $(CCFLAGS) nCr.o formula.o -o formula
    
    nCr.o : nCr.s
        $(COMPILER) $(CCFLAGS) -c nCr.s
    
    formula.o : formula.c
        $(COMPILER) $(CCFLAGS) -c formula.c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected ')' before '*' token error?
    By Draymire in forum C Programming
    Replies: 2
    Last Post: 11-05-2012, 11:28 PM
  2. Error expected '=', ',' ,'asm', 'or' before token '{'
    By tmac619619 in forum C Programming
    Replies: 2
    Last Post: 10-13-2012, 02:33 PM
  3. Replies: 3
    Last Post: 08-21-2012, 11:50 PM
  4. Error: expected identifier or ‘(’ before ‘{’ token
    By jpcanaverde in forum C Programming
    Replies: 66
    Last Post: 06-08-2010, 12:53 PM
  5. error: expected ‘;’ before ‘:’ token
    By kris.c in forum C Programming
    Replies: 5
    Last Post: 02-10-2008, 10:26 PM

Tags for this Thread