Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/30/04 09:43
Read: times


 
#84091 - C Code t o Assembly
Hello

I have some C code which is taking to much space in my 8052 memory. I would like to convert it in 8052 Assembly. Is there any tool or site which can help me convert the C code in Assembly or atleast optamize it so that it takes less space. I don't have much experience with Assembly

Following is the C code. May be it is helpfull for other as well. it converts seconds to date and visa virsa.

Shahzad Aslam








void Sec2Date(unsigned long s)
{
unsigned long ElapsedDays;
unsigned long ElapsedHours;
unsigned long ElapsedMins;
unsigned long ElapsedSecs;
unsigned long RemDays;
unsigned long YearCtr;
unsigned long YearLen;
unsigned long DaysInPriorMonths[12];
unsigned long MonCtr;

ElapsedMins=s/60;
ElapsedHours=ElapsedMins/60;
ElapsedDays=ElapsedHours/24;

ElapsedSecs=s-(ElapsedMins*60);
ElapsedMins-=ElapsedHours*60;
ElapsedHours-=ElapsedDays*24;

// Find the current year by stepping through the century one year at a time
// until we find the year ElapsedDays falls in
RemDays=ElapsedDays;
for (YearCtr=0; YearCtr<100; YearCtr++)
{
YearLen=365;
if (IsLeapYear(YearCtr))
YearLen=366;
if (RemDays<YearLen) break;
RemDays-=YearLen;
}

// Build this year's month table
DaysInPriorMonths[0]=0;
DaysInPriorMonths[1]=DaysInPriorMonths[0]+31;
DaysInPriorMonths[2]=DaysInPriorMonths[1]+28;
if (YearLen==366) DaysInPriorMonths[2]+=1;
DaysInPriorMonths[3]=DaysInPriorMonths[2]+31;
DaysInPriorMonths[4]=DaysInPriorMonths[3]+30;
DaysInPriorMonths[5]=DaysInPriorMonths[4]+31;
DaysInPriorMonths[6]=DaysInPriorMonths[5]+30;
DaysInPriorMonths[7]=DaysInPriorMonths[6]+31;
DaysInPriorMonths[8]=DaysInPriorMonths[7]+31;
DaysInPriorMonths[9]=DaysInPriorMonths[8]+30;
DaysInPriorMonths[10]=DaysInPriorMonths[9]+31;
DaysInPriorMonths[11]=DaysInPriorMonths[10]+30;

//printf("Feb:%ld n",DaysInPriorMonths[2]);

// Find which month we're in
for (MonCtr=1; MonCtr<12; MonCtr++)
{
if (RemDays < DaysInPriorMonths[MonCtr])
break;
}

//printf("%ld/%ld/%ld %ld:%ld:%ldn" ,2000+YearCtr, MonCtr, 1+RemDays-DaysInPriorMonths[MonCtr-1], ElapsedHours, ElapsedMins, ElapsedSecs);
}


unsigned IsLeapYear( unsigned y )
{
if( ((y % 4) == 0) && ((y % 100) != 0) || ((y % 400) == 0) )
return( 1 );
else
return( 0 );
}


// buf is expected to point to a string with the format:
// DD:MM:YYYY HH:MM:SS (19 bytes)
long DateToSeconds(char *buf)
{
int Months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long Result = 0;
unsigned long Day, Month, Year, Hour, Minute, Second;
int t;

Day = ASCIIToInt( buf, 2 );
Month = ASCIIToInt( (buf + 3), 2 );
Year = ASCIIToInt( (buf + 6), 4 );
Hour = ASCIIToInt( (buf + 11), 2 );
Minute = ASCIIToInt( (buf + 14), 2 );
Second = ASCIIToInt( (buf + 17), 2 );
for( t=2000 ; t<Year ; t++ )
Result += (long)(365 + IsLeapYear(t)) * 86400;
for( t = 0; t < Month - 1; t++ ) {
Result += Months[t] * 86400;
if( (t == 1) && (IsLeapYear(Year) == 1) )
Result += 86400;
}
Result += (Day - 1) * 86400;
Result += Hour * 3600;
Result += Minute * 60;
Result += Second;
return( Result );
}


unsigned ASCIIToInt(char *Number, unsigned Digits)
{
unsigned Result = 0, r;

if( (Digits < 1) || (Digits > 4) )
return( 0 );

Number += (Digits - 1);
r = 1;

while( Digits-- > 0 )
{
Result += (unsigned)(((unsigned)(*Number--) - 48) * r);
r *= 10;
}

return( Result );
}


List of 22 messages in thread
TopicAuthorDate
C Code t o Assembly            01/01/70 00:00      
   C to Asm            01/01/70 00:00      
   C Code t o Assembly            01/01/70 00:00      
      only one table            01/01/70 00:00      
   Need some more details            01/01/70 00:00      
      Not even true !            01/01/70 00:00      
      Answer is Wrong!!            01/01/70 00:00      
   Re : previous 3 replies and Paul            01/01/70 00:00      
      Previously on 8052.com            01/01/70 00:00      
      Overflow            01/01/70 00:00      
      just one array            01/01/70 00:00      
         too limited, Paul            01/01/70 00:00      
         Nothing to do with scope            01/01/70 00:00      
            Agreed            01/01/70 00:00      
   No success !            01/01/70 00:00      
   Re: Peter            01/01/70 00:00      
   to assembly            01/01/70 00:00      
   Example code            01/01/70 00:00      
   Any unused functions in your program?            01/01/70 00:00      
   query            01/01/70 00:00      
      Off-Topic - start a new thread            01/01/70 00:00      
         ... and use a _descriptive_ subject!            01/01/70 00:00      

Back to Subject List