title: Converting C function to assembly
body: <p>Hi i am trying to convert this C function that I wrote to assembly code(SPARC arc)but keep getting a segmentation fault(core dumped) error. The function is supposed to convert a string to a long value. I know I did the C function correctly as I have tested it and it passed all the tests. Here is the C function I am trying to convert.</p>
  
  <pre><code>long strToLong(char *str, int base )
  {
    char *endptr;
    errno = 0;
    long num;
    num = strtol(str, &amp;endptr, base);
    char holder[BUFSIZ];
  
    /*if *endptr isn't '\0' then strtol didn't get to the end of the string
    *so it wasn't a valid integer*/
    if(*endptr != '\0')
    {
      (void)fprintf(stderr, "\n\t""'%s'" "  is not an integer\n", str);
       return 0;
    }
    /*if errno isn't 0 then an error occured so print out an error message*/
    else if(errno != 0)
    { 
      (void)snprintf(holder, BUFSIZ,"\n\tConverting \"%s\" base \"%d\"\n caused an         ", str, base);
      perror(holder);
      return 0;
    }
  
    /*if no errors, then it was a valid integer so return the integer*/
    return num;
  }
  </code></pre>
  
  <p>Here is the assembly code I wrote.</p>
  
  <pre><code>.global strToULong
  
  .section ".data"    
  
  fmt1:    .asciz    "%s  is not an integer\n"    !ascii format
  
  fmt2:
      .asciz    "Converting %s base %d\n"    !ascii format
  
  .section ".text"
  
  strToULong:
      save    %sp, -( 92 + 1028) &amp; -8, %sp
      add    %fp, -4, %o1
      mov    %i0, %o0
      mov    %i2, %o2
      call    strtol
      nop
      mov    %o0, %l0
      ld    [%fp - 4], %l1
      ldub    [%l1], %l1
      cmp    %l1, %g0
      be    errno
      nop
  
      set    stdError, %o0
      ld    [%o0], %o0
      set    fmt1, %o1
      mov    %i0, %o2
      call    fprintf, 3
      nop
  
  errno:
      mov    %l0, %i0
      set    errno, %l2
      ld    [%l2], %l2
      cmp    %l2, %g0
      be    return
      nop
  
      add    %fp, -1028, %o0
      mov    1024, %o1
      set    fmt2, %o2
      mov    %i0, %o3
      mov    %i1, %o4
      call    snprintf
      nop
      add    %fp, -1028, %o0
      call    perror
      nop
      mov    0, %i0
  
  return:
      ret
      restore
  </code></pre>
  
  <p>I debugged the program and the seg fault comes when I call the strtol function. Im not really sure what im doing wrong, I think I am passing the parameters correctly but I still get that error. Oh and in my main i have something declared like FILE* StdError = stderr to pass stderr as a parameter to fprintf.</p>
  
  <p>Any help would be appriciated.</p>