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

remainer of division

C has one shortcoming that keeps bothering me. When converting a number from binary to decimal, we typically do something like :
int n,d;
for (i=0; i<5; ++i)
{

d=n%10;

n/=10;

display_area[4-i]=d;
} When doing firmware, we are always concerned about code space, and sometimes speed too. In either case, doing the division twice is a big waste of time and space.
Some compilers will allow something like :
int32 l;
int16 n;
int8 c;
for (i=0; i<5; ++i)
{

l/=10;

d=_REM4_; // where _REM4_ is the last remainder of a 32 bit division
or

n/=10;

d=_REM2_; // where _REM2_ is the last remainder of a 16 bit division
or

c/=10;

d=_REM1_; // where _REM1_ is the last remainder of a 8 bit division display_area[4-i]=d;
}

On the Keil compiler, the most efficient way I found to do it is to use assembly, while knowing the name that the variable where I want to put the remainder will have after the compile step. This is far from being user friendly.
Also, I haven't found a way to get the .lst file to list the compile code together with the source code when using the assembly code directive. Well, that creates a big mess just to save a few bytes...

Is there a better way ?

0