Long division Modulus Errors

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Long division Modulus Errors

Jump to solution
1,984 Views
Emac
Contributor III
Hello all,
 
I have recently seen a problem where global variables have been clobbered when I get down to a "Long MODulo Unsigned" operation.  I have 5.7.0 CW IDE for HCS12 and have debugged down to the C implementation of
dword n
 
 n%10
 
the code works OK but TOTALLY CLOBBERS GLOBAL variables.
 
actual code
 
Code:
/**This routine converts numbers of type word to a string format of 0xXXXX===============================================================================**DESCRIPTION:***This routine converts numbers of type word to a string format of 0xXXXX in *decimal notation.*CALLING SEQUENCE:   wordToDecStr(char *s, word n)**@param *s (character pointer of size + 1  or greater).@param n  (16 bit word to be converted).@param size  (char variable indicating number of leading zeros required).*\todo- List of things to do- Remove commented code*/void wordToDecStr(unsigned char *s, dword n,char size){ unsigned char i, j; dword temp; //return in error case if((size==0)) {   return; }    //fill with character zeros for (j=0;j<size;j++) {  s[j] = '0'; } //modulus your way from the back to the front i=0; while ((n > 0)&&(size>i))  {    temp=(n % 10) + '0'; ////<<<<ERROR OCCURS HERE  s[size-i-1] =(char)temp;    n /= 10;  i++; }//end while s[size]='\0';//string terminator}//end wordToDecStr()

 
 
Does anybody have any experience with this?
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
656 Views
CompilerGuru
NXP Employee
NXP Employee
Where is the stack pointer SP when that code gets executed?

The description sounds as if the application would use more stack space that which was allocated for it.

Daniel

View solution in original post

0 Kudos
Reply
2 Replies
657 Views
CompilerGuru
NXP Employee
NXP Employee
Where is the stack pointer SP when that code gets executed?

The description sounds as if the application would use more stack space that which was allocated for it.

Daniel
0 Kudos
Reply
656 Views
Emac
Contributor III
Thanks,
 
You were absolutely right, the symptoms were that the stack was clobering global variables.  I finally found the CPU build options for stack size and saw that it was originally set to 80!  I increased it and works fine now.  Thanks. 
 
Let this be a warning to those who unit test code and find problems durring integration, stack size and subroutine nesting can bite you if you don't know what to look for.
 
Daniel you are awesome!
0 Kudos
Reply