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