View previous topic :: View next topic |
Author |
Message |
andy e2l Guest
|
INT32 Math |
Posted: Fri Feb 13, 2004 10:14 am |
|
|
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](images/smiles/icon_exclaim.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Pete Smith
Joined: 17 Sep 2003 Posts: 55 Location: Chester, UK
|
Re: INT32 Math |
Posted: Fri Feb 13, 2004 11:13 am |
|
|
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](images/smiles/icon_exclaim.gif) |
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. |
|
![](templates/subSilver/images/spacer.gif) |
andy e2l Guest
|
|
Posted: Fri Feb 13, 2004 11:44 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 13, 2004 12:28 pm |
|
|
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);
} |
|
![](templates/subSilver/images/spacer.gif) |
dazza Guest
|
! |
Posted: Sat Feb 14, 2004 3:33 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|