.section .data
x: .int 0
value: .int 0
n: .int 0
answer: .int 0
outSpec: .asciz "The value is = %d\n"
outSpec1: .asciz "Enter the value of variable x ==> "
inpSpec: .asciz "%d"
.section .bss #declare raw data section
.lcomm coefficients 100
# will allocate 25 words
.section .text
.globl main
main:
NOP
PUSHL $coefficients # Push the address of coefficients
CALL fillArray # Call fillArray function
MOVL %eax, n # n = number of elements
ADDL $4, %esp # give back stack space
#------------------------- read a value for x ----------------------
PUSHL $outSpec1
CALL printf
ADDL $4, %esp
PUSHL $x
PUSHL $inpSpec
CALL scanf
ADDL $8, %esp
#----------------------------- Display Array ------------------------
MOVL n, %ecx
PUSHL %ecx
PUSHL $coefficients # Push the address of coefficients
CALL printArr
#-------------------------- polynomial evaluation --------------------
## horner`s method
## polyValue = coefficients[0];
## for (j = 1; j < n; j++)
## polyValue = polyValue * x + coefficients[j];
CLRL %eax
CLRL %ecx
LEAL coefficients, %eax
MOVL %eax, value # polyValue = coefficients[0]
CLRL %edx # j
MOVL $1, %edx # set j = 1
for:
CMPL n, %edx # compare j and n
JGE done # if j = n, done
IMULL x, %eax # polyValue = polyValue * x
# find next coeff value
ADDL $4, value
MOVL value, %ebx
ADDL %ebx, %eax # polyValue + coefficients[j]
INCL %edx # j++
JMP for
#--------- end of code for polynomial evaluation goes here ------
done: # or any other label you like
# Assuming that it is stored in %eax
# display the value of the polynomial
PUSHL %eax
PUSHL $outSpec
CALL printf
ADDL $8, %esp
CALL exit
2 Replies - 1156 Views - Last Post: 08 November 2012 - 05:20 PM
#1
how to fix segmentation fault (core dumped) in code that uses an array
Posted 08 November 2012 - 03:46 PM
The problem is with the polynomial evaluation part. I get a segmentation fault(core dumped) error when I run it. I`ve tried everything I can think of to correct it but I can`t figure out what the problem is. I`m sure it`s something simple and I`m completely overlooking it. Any help would be greatly appreciated. Thanks in advance.
Replies To: how to fix segmentation fault (core dumped) in code that uses an array
#2
Re: how to fix segmentation fault (core dumped) in code that uses an array
Posted 08 November 2012 - 04:56 PM
Oh joy, AT&T syntax... gives me a headache. A seg fault is telling you that you are trying to write memory that is NOT yours to write to. You are probably out of your array bounds. That being said, where is your fillArray function?
#3
Re: how to fix segmentation fault (core dumped) in code that uses an array
Posted 08 November 2012 - 05:20 PM
It gives me a headache too! My fillArray is in C. Here it is...
#include <stdio.h>
int fillArray(int a[])
{
int count = 0;
int nextValue;
const int MAXNUM = 25;
printf("Enter next coefficient <999 to exit> => ");
scanf("%d", &nextValue);
printf("\n");
while (nextValue != 999)
{
a[count] = nextValue;
count++;
if (count == MAXNUM) break;
printf("Enter next coefficient <999 to exit> => ");
scanf("%d", &nextValue);
printf("\n");
}
return count;
}
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote







|