Long division Modulus Errors

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Long division Modulus Errors

跳至解决方案
1,966 次查看
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?
标签 (1)
标记 (1)
0 项奖励
回复
1 解答
638 次查看
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 项奖励
回复
2 回复数
639 次查看
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 项奖励
回复
638 次查看
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 项奖励
回复