CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

INT32 Math

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
andy e2l
Guest







INT32 Math
PostPosted: Fri Feb 13, 2004 10:14 am     Reply with quote

I am having problems using Int32 type variables, see code segment below


static int32 indexSectorNo; // 32 bit address of scratch index sector

int16 bytesPerSector;
int16 reservedSectors;
int fatCount;
int16 rootEntCount;

int16 sectorsPerFat;
int16 rootDirSectors;

// this 16 bit operation works without problem
rootDirSectors = ((rootEntCount * 32) + (bytesPerSector - 1)) / bytesPerSector;


// but this code produces erroneous results
indexSectorNo = reservedSectors + (fatCount * sectorsPerFat) + rootDirSectors;

I've tried makeing everything 32 bit

indexSectorNo = (int32)reservedSectors + ((int32)fatCount * (int32)sectorsPerFat) + (int32)rootDirSectors;

but this does work either.

I've tried
indexSectorNo = 0; // works
but
indexSectorNo++; // increments the wrong byte

I would be grateful if anyone could help.

I'm using a 16F877 part

Exclamation
Pete Smith



Joined: 17 Sep 2003
Posts: 55
Location: Chester, UK

View user's profile Send private message Visit poster's website MSN Messenger ICQ Number

Re: INT32 Math
PostPosted: Fri Feb 13, 2004 11:13 am     Reply with quote

andy e2l wrote:
I am having problems using Int32 type variables, see code segment below


static int32 indexSectorNo; // 32 bit address of scratch index sector

int16 bytesPerSector;
int16 reservedSectors;
int fatCount;
int16 rootEntCount;

int16 sectorsPerFat;
int16 rootDirSectors;

// this 16 bit operation works without problem
rootDirSectors = ((rootEntCount * 32) + (bytesPerSector - 1)) / bytesPerSector;


// but this code produces erroneous results
indexSectorNo = reservedSectors + (fatCount * sectorsPerFat) + rootDirSectors;

I've tried makeing everything 32 bit

indexSectorNo = (int32)reservedSectors + ((int32)fatCount * (int32)sectorsPerFat) + (int32)rootDirSectors;

but this does work either.

I've tried
indexSectorNo = 0; // works
but
indexSectorNo++; // increments the wrong byte

I would be grateful if anyone could help.

I'm using a 16F877 part

Exclamation


You could try

indexSectorNo = (int32)reservedSectors + (int32)((int32)fatCount * (int32)sectorsPerFat) + (int32)rootDirSectors;

Try breaking the line of code into smaller parts as well

indexSectorNo = (int32)reservedSectors;
indexSectorNo += (int32)rootDirSectors;
indexSectorNo + ((int32)fatCount * (int32)sectorsPerFat);

FWIW, my code that does this reads..

fat_address=partition_start;
fat_address+=(int32)reserved_sectors*bytes_per_sector;
fat_backup_address=fat_address+(int32)sectors_per_fat*bytes_per_sector;
root_dir_address=fat_address+(int32)sectors_per_fat*bytes_per_sector*2;
data_area=root_dir_address+((int32)root_dir_entries*32);
bytes_per_cluster=sectors_per_cluster*bytes_per_sector;

If I ever find things like this don't work, I split them up, and then add debugging between each line, and trace through the maths.

HTH

Pete.
andy e2l
Guest







PostPosted: Fri Feb 13, 2004 11:44 am     Reply with quote

I tried simplifying the expression as suggested

indexSectorNo = (int32)reservedSectors;
indexSectorNo += (int32)rootDirSectors;
indexSectorNo + ((int32)fatCount * (int32)sectorsPerFat);

The statement
indexSectorNo = (int32)reservedSectors;

yielded indexSectorNo = 256 when reservedSectors has a value of 1.

I'm on compiler version 3.168
Andy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 13, 2004 12:28 pm     Reply with quote

Quote:
The statement
indexSectorNo = (int32)reservedSectors;

yielded indexSectorNo = 256 when reservedSectors has a value of 1.

I installed PCM vs. 3.168, and ran the test program below.
It worked fine. Here are the results displayed in the terminal window:

indexSectorNo = 00000001 hex
indexSectorNo = 1 decimal

Here is the test program:

#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

static int32 indexSectorNo;

//========================================
main()
{
int16 reservedSectors;

reservedSectors = 1;
indexSectorNo = (int32)reservedSectors;

printf("indexSectorNo = %lx hex\n\r", indexSectorNo);
printf("indexSectorNo = %lu decimal\n\r", indexSectorNo);

while(1);
}
dazza
Guest







!
PostPosted: Sat Feb 14, 2004 3:33 am     Reply with quote

Hi

Had a similar problem in the past where an expression read something like this:

j = i + (b * 10) + (c * 33)

but it didn't produce the correct answer which is disappointing on behalf of the compiler

so i did

j = i
j = j + (b * 10)
j = j + (c * 33)

which worked

honestly it's not as if it's complicated math either

some things really bug me (pardon the pun) about this compiler, other times i think it's great

regards,
darren
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group