2 Replies - 3030 Views - Last Post: 08 November 2012 - 05:20 PM

#1 bnmiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 12

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.

.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




Is This A Good Question/Topic? 0
  • +

Replies To: how to fix segmentation fault (core dumped) in code that uses an array

#2 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

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?
Was This Post Helpful? 0
  • +
  • -

#3 bnmiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 12

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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1