From: SourceForge.net <no...@so...> - 2005-03-24 07:19:20
|
Bugs item #1166494, was opened at 2005-03-19 07:28 Message generated for change (Comment added) made by epetrich You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=100599&aid=1166494&group_id=599 Category: msc51(8051) target Group: non bugs Status: Closed Resolution: Rejected Priority: 5 Submitted By: shailesh arya (shailarya) Assigned to: Nobody/Anonymous (nobody) Summary: dont get proper result for while (x > 10) where x is float Initial Comment: i have attached my source file with it T2.c. it compiled properly and generate hex code but it goes in following loop, but it should not go in that way. ---------------------------------------------------------------------- >Comment By: Erik Petrich (epetrich) Date: 2005-03-24 01:19 Message: Logged In: YES user_id=635249 This part of your program: for(;a>1;) { a=a*10; w++; *p++='0'; } overflows your str[16] buffer when a>1, causing other variables to be modified in undefined ways. If I delete the above code, after the program exits I find that str[16] contains "-101199". Is your concern that this is not "-101200"? ---------------------------------------------------------------------- Comment By: shailesh arya (shailarya) Date: 2005-03-23 23:20 Message: Logged In: YES user_id=825443 i have sdcc 2.4.8 977. there is a big problem. still i think it is not correct as compre to normal logic. pls consider small code as follow. still the compre function doe not work as per the logic. for the +/- value of a. try with different value of a less then 1. it give worng result. dont go for sbuf. this will work properly. #include<at89x52.h> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<float.h> float compre (float a); void putchar (char c); unsigned char str[16], k; data unsigned char *p; unsigned char i,w; float a; void main (void) { TH1 = 0xfd ; TL1 = 0xfd ; PCON &= 0x7f ; SCON = 0x50 ; TMOD = 0x25 ; TCON = 0x40 ; TR1=1; p=str; a =-.10120; a = compre(a); for(w=0;w<6;w++) { a=a*10; for(;a>1;) { a=a*10; w++; *p++='0'; } k=a; a=a-k; *p++=k+'0'; } for (i=0;i<16;i++) { SBUF = str[i]; } printf_fast("\n"); } float compre (float a) { if ( a < 0.0) { a = - a; *p++ = '-'; } return a; } ---------------------------------------------------------------------- Comment By: Erik Petrich (epetrich) Date: 2005-03-22 10:55 Message: Logged In: YES user_id=635249 I still don't see a problem. With your new example, I get the output: 3 90 1 Which appears to be correct since (unsigned int)(2000*(2.0/10)) is 0x190. I get the same output when the variables are declared globally or locally. You did not specify the version number of the compiler you are using. My results are with SDCC 2.4.8 #980. ---------------------------------------------------------------------- Comment By: shailesh arya (shailarya) Date: 2005-03-22 02:36 Message: Logged In: YES user_id=825443 epetrich thanks. but even you dont go to full function like initialize and putchar. just simply do as following then also it gives a problem. valuse of a is change. if we remove print_fast then only it goes properly. data unsigned char e[6]; data unsigned char *p; unsigned char c,d; float a; unsigned int i; e[0]=3; p=e; a=2.0; printf_fast ("\n%d",*p); if (a>0.0) { a=a/10; i=(2000*a); c=i; d=(i>>8); printf_fast ("\n%x",c); printf_fast ("\n%x",d); } else { i=-(300*a); c=i; d=(i>>8); printf_fast ("\n%x",c); printf_fast ("\n%x",d); } } ---------------------------------------------------------------------- Comment By: shailesh arya (shailarya) Date: 2005-03-22 02:33 Message: Logged In: YES user_id=825443 epetrich thanks. but even you dont go to full function like initialize and putchar. just simply do as following then also it gives a problem. valuse of a is change. if we remove print_fast then only it goes properly. data unsigned char e[6]; data unsigned char *p; unsigned char c,d; float a; unsigned int i; e[0]=3; p=e; a=2.0; printf_fast ("\n%d",*p); if (a>0.0) { a=a/10; i=(2000*a); c=i; d=(i>>8); printf_fast ("\n%x",c); printf_fast ("\n%x",d); } else { i=-(300*a); c=i; d=(i>>8); printf_fast ("\n%x",c); printf_fast ("\n%x",d); } } ---------------------------------------------------------------------- Comment By: Erik Petrich (epetrich) Date: 2005-03-21 23:53 Message: Logged In: YES user_id=635249 I can't find a problem with the compiler, but I can find several problems with your example code. The first two are the most serious. 1) In the call ftoa(a,str1,5,'f') the pointer str1 has not been assigned a value. Since it is a global variable, it will default to pointing to memory location 0. Writing to this pointer will unexpectedly modify the values in the first register bank, causing very strange behaviour. 2) In the function initialize() you have the statement SP=0x55. This modifies the stack pointer. Thus when the initialize() function ends, it will fetch the return address from this new stack pointer location and likely jump to some random location rather than the expected location in main(). If you must control the stack pointer location manually, use the --stack-loc and ==no-pack-iram command line options. By default, the compiler will automatically initialize the stack pointer to a reasonable location for you. 3) Perhaps this was added just to try debug the problem, but declaring ndiag at 0x50 is generally not a good idea, since this will cause the specified address to be used for this variable even if another variable was allocated there. 4) You are writing to the SBUF sfr without checking to see if any previous character has completed transmission. Thus some characters may be lost if the writes occur too quickly. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=100599&aid=1166494&group_id=599 |