Hello,

I am trying a peice of code in VC++ which always worked in my embeded
compiler.
The code works fine when you pass it an address as we are suppossed to since
it takes a pointer. The code however did fail when its parameters come
directly from a structure. Now I would like to try this in VC++ and see if it
still fails to calculate.

However for me carry out the exact same test in VC++ 2003, I need to declare
8 bit type variables! I tried to use byte and small. I found the 8 bit type
coded this way:

__int8 or __int8 nsmall

However the calculation does not get carried out right!

Here is the code:

==========================================
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>

struct xxx
{
__int8 a;
__int8 b;
__int8 c;
}d[]={0,0,1};

void INC_ADDR(
__int8 INCBY,
__int8 *A2,
__int8 *A1,
__int8 *A0)
{
short carry;

carry = (*A0 > 255 - INCBY); //If the calculation is true then carry = true
(*A0) += INCBY;

if (carry) {
++(*A1);
if ((*A1) == 0)
{
(*A2)++; }}
}

int main(void)
{
INC_ADDR( 176,
&(d[0].c),
&(d[0].b),
&(d[0].a));

INC_ADDR( 176,
&(d[0].c),
&(d[0].b),
&(d[0].a));

return 0;
}
=================================================

All help appreciated!

--
Best regards
Robert

Re: C Question! How to declare 8 bit types? by Alan

Alan
Sat Jan 05 22:43:06 CST 2008

"Robby" <Robby@discussions.microsoft.com> wrote in message
news:0296A082-13AF-4082-A212-5E08B3931BF3@microsoft.com...
> Hello,
>
> I am trying a peice of code in VC++ which always worked in my embeded
> compiler.
> The code works fine when you pass it an address as we are suppossed to
> since
> it takes a pointer. The code however did fail when its parameters come
> directly from a structure. Now I would like to try this in VC++ and see if
> it
> still fails to calculate.
>
> However for me carry out the exact same test in VC++ 2003, I need to
> declare
> 8 bit type variables! I tried to use byte and small. I found the 8 bit
> type
> coded this way:
>
> __int8 or __int8 nsmall
>
> However the calculation does not get carried out right!
>
> Here is the code:
>
> ==========================================
> #include <stdio.h>
> #include <string.h>
> #include <conio.h>
> #include <stdlib.h>
> #include <malloc.h>
>
> struct xxx
> {
> __int8 a;
> __int8 b;
> __int8 c;
> }d[]={0,0,1};
>
> void INC_ADDR(
> __int8 INCBY,
> __int8 *A2,
> __int8 *A1,
> __int8 *A0)
> {
> short carry;
>
> carry = (*A0 > 255 - INCBY); //If the calculation is true then carry =
> true
> (*A0) += INCBY;
>
> if (carry) {
> ++(*A1);
> if ((*A1) == 0)
> {
> (*A2)++; }}
> }
>
> int main(void)
> {
> INC_ADDR( 176,
> &(d[0].c),
> &(d[0].b),
> &(d[0].a));
>
> INC_ADDR( 176,
> &(d[0].c),
> &(d[0].b),
> &(d[0].a));
>
> return 0;
> }
> =================================================

Carry is never true because you are using a signed type.

Try this instead:

struct xxx
{
unsigned __int8 a;
unsigned __int8 b;
unsigned __int8 c;
}d[]={0,0,1};

void INC_ADDR(
unsigned __int8 INCBY,
unsigned __int8 *A2,
unsigned __int8 *A1,
unsigned __int8 *A0)
{
short carry;

carry = (*A0 > 255 - INCBY); //If the calculation is true then carry = true
(*A0) += INCBY;

if (carry) {
++(*A1);
if ((*A1) == 0)
{
(*A2)++; }}
}


int _tmain (int /*argc*/, _TCHAR* /*argv[]*/)
{
INC_ADDR( 176,
&(d[0].c),
&(d[0].b),
&(d[0].a));

INC_ADDR( 176,
&(d[0].c),
&(d[0].b),
&(d[0].a));


return 0;
}



Re: C Question! How to declare 8 bit types? by Robby

Robby
Mon Jan 07 20:00:01 CST 2008

Thanks Alan!

--
Best regards
Robert


"Alan Carre" wrote:

> "Robby" <Robby@discussions.microsoft.com> wrote in message
> news:0296A082-13AF-4082-A212-5E08B3931BF3@microsoft.com...
> > Hello,
> >
> > I am trying a peice of code in VC++ which always worked in my embeded
> > compiler.
> > The code works fine when you pass it an address as we are suppossed to
> > since
> > it takes a pointer. The code however did fail when its parameters come
> > directly from a structure. Now I would like to try this in VC++ and see if
> > it
> > still fails to calculate.
> >
> > However for me carry out the exact same test in VC++ 2003, I need to
> > declare
> > 8 bit type variables! I tried to use byte and small. I found the 8 bit
> > type
> > coded this way:
> >
> > __int8 or __int8 nsmall
> >
> > However the calculation does not get carried out right!
> >
> > Here is the code:
> >
> > ==========================================
> > #include <stdio.h>
> > #include <string.h>
> > #include <conio.h>
> > #include <stdlib.h>
> > #include <malloc.h>
> >
> > struct xxx
> > {
> > __int8 a;
> > __int8 b;
> > __int8 c;
> > }d[]={0,0,1};
> >
> > void INC_ADDR(
> > __int8 INCBY,
> > __int8 *A2,
> > __int8 *A1,
> > __int8 *A0)
> > {
> > short carry;
> >
> > carry = (*A0 > 255 - INCBY); //If the calculation is true then carry =
> > true
> > (*A0) += INCBY;
> >
> > if (carry) {
> > ++(*A1);
> > if ((*A1) == 0)
> > {
> > (*A2)++; }}
> > }
> >
> > int main(void)
> > {
> > INC_ADDR( 176,
> > &(d[0].c),
> > &(d[0].b),
> > &(d[0].a));
> >
> > INC_ADDR( 176,
> > &(d[0].c),
> > &(d[0].b),
> > &(d[0].a));
> >
> > return 0;
> > }
> > =================================================
>
> Carry is never true because you are using a signed type.
>
> Try this instead:
>
> struct xxx
> {
> unsigned __int8 a;
> unsigned __int8 b;
> unsigned __int8 c;
> }d[]={0,0,1};
>
> void INC_ADDR(
> unsigned __int8 INCBY,
> unsigned __int8 *A2,
> unsigned __int8 *A1,
> unsigned __int8 *A0)
> {
> short carry;
>
> carry = (*A0 > 255 - INCBY); //If the calculation is true then carry = true
> (*A0) += INCBY;
>
> if (carry) {
> ++(*A1);
> if ((*A1) == 0)
> {
> (*A2)++; }}
> }
>
>
> int _tmain (int /*argc*/, _TCHAR* /*argv[]*/)
> {
> INC_ADDR( 176,
> &(d[0].c),
> &(d[0].b),
> &(d[0].a));
>
> INC_ADDR( 176,
> &(d[0].c),
> &(d[0].b),
> &(d[0].a));
>
>
> return 0;
> }
>
>
>

Re: C Question! How to declare 8 bit types? by Ben

Ben
Tue Jan 08 09:17:44 CST 2008


"Robby" <Robby@discussions.microsoft.com> wrote in message
news:0296A082-13AF-4082-A212-5E08B3931BF3@microsoft.com...
> Hello,
>
> I am trying a peice of code in VC++ which always worked in my embeded
> compiler.
> The code works fine when you pass it an address as we are suppossed to
> since
> it takes a pointer. The code however did fail when its parameters come
> directly from a structure. Now I would like to try this in VC++ and see if
> it
> still fails to calculate.
>
> However for me carry out the exact same test in VC++ 2003, I need to
> declare
> 8 bit type variables! I tried to use byte and small. I found the 8 bit
> type
> coded this way:
>
> __int8 or __int8 nsmall
>
> However the calculation does not get carried out right!

Robby,

Are these the variables that were "int" in the CCS compiler? If it defines
int not only as the system word size (8-bit) but also as unsigned then you
really need to get a different compiler. "int" has to be signed.