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

Arithmetic on numbers larger than 32 bits

I need to add a 32-bit number to a 48-bit number. What I want to do is perform the first 32 bit arithmetic in C as follows:
Wv.A += Wv.B ;
And to propagate the carry to the most significant word as follows:
__asm { ADDC WORD Wv+10,#0 }
Unfortunately I get on compilation the following error:
error C195: inline-asm: Invalid instruction operand(s)
Apparently the identifier Wv is not known by the compiler though it is known in C expressions and similar assembler statements are generated by the compiler when I select a SRC output of the compiler. Is someone familiar with inline assembly? Thanks

Parents Reply Children
  • You can do this all in C by checking to see if the low word rolls over, and if so incrementing the high word. Something like:

    // access 64-bit word in various sized pieces
    typedef union
        {
        U64 u64;
        U8  array[8];
        U16 warray[4];
        struct { MultiByte32 ms; MultiByte32 ls; } longs;
        } MultiByte64;
    
    void U64AddEqU32 (U64* u64, U32 u32)
    	{
    
    	if ((MaxU32 - ((MultiByte64*)u64)->longs.ls.u32) <= u32)
    		{ // addition will roll over
    		((MultiByte64*)u64)->longs.ms.u32++;
    		}
    
    	((MultiByte64*)u64)->longs.ls.u32 += u32;
    
    
    	} // U64AddEqU32
    
    

    ("MultiByte64" here is a typedef for a union that allows access to bytes in a word by various means.)