This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

can not perform recursion function correctly

hi: I want to use recursion function to transform a integer from binary form to ascii form. But the result is wrong.I don't why ,please help to analyse.

soure code:

void
binary_to_ascii(unsigned int value)
{

   unsigned int quotient;
   quotient = value/10;
   if(quotient !=0 )
       binary_to_ascii(quotient);
   putchar(value%10+'0');

}


int
main()
{
 while(1){
    binary_to_ascii(4267);
  }
}


I want to produce characters '4','2','6','7' in sequence. but the result is '4','4','4'. I don't know what is wrong with my code.

Parents
  • It also requires some way to pass parameters & maintain local variables so that each "recursion" doesn't corrupt the data & parameters of previous recursions; ie, it requires that the functions are reentrant.

    Most 'C' compilers achieve this by using the stack, so that their functions are inherently reentrant - Keil C51 does not do this and its functions are inherently not reentrant.

    This is the same reason why Keil C51 has issues with function pointers - as previously explained: http://www.keil.com/forum/17469

    Again, this is down to the specific features of the 8051 architecture and, again, you must not just assume that the 8051 is "just another processor" - it isn't!

Reply
  • It also requires some way to pass parameters & maintain local variables so that each "recursion" doesn't corrupt the data & parameters of previous recursions; ie, it requires that the functions are reentrant.

    Most 'C' compilers achieve this by using the stack, so that their functions are inherently reentrant - Keil C51 does not do this and its functions are inherently not reentrant.

    This is the same reason why Keil C51 has issues with function pointers - as previously explained: http://www.keil.com/forum/17469

    Again, this is down to the specific features of the 8051 architecture and, again, you must not just assume that the 8051 is "just another processor" - it isn't!

Children