Home
Service
Ideas & Tricks
Contact Us
 


This page is a random collection of items from different sources. If you wish to add something, please email us

One trick for swapping variables does not appear all that necessary in 'C', but can be very useful in limited memory cases:
In 'C':    
     int a,b;
     a = 5;
     b = 22;
     //to swap
     a^=b^=a^=b;
     //now a=22 and b=5

The same trick in asm may look like:
     mov a,5
     mov b,22
     ;now swap
     xor a,b
     xor b,a
     xor a,b
     ;now a=22 and b=5

---------------------------------------------------------------------

Decimal Number to BCD:

In this example, the firmware revision number is in decimal, but the USB revision number must be provided in BCD with switched bytes:

#define FW_REV_MSB        6       
#define FW_REV_LSB         42  //REV 6.42

//Assuming (int) is a 16-bit value.
#define USB_REV   (int)( ((((int)FW_REV_MSB / 10 ) * 16 ) & 0xF0 )      | \
                       ( (int)FW_REV_MSB - (( (int)FW_REV_MSB /10) *10) ) | \
                             (((( (int)FW_REV_LSB /10 ) * 16 ) & 0xF0 ) << 8)  | \
                 (( (int)FW_REV_LSB - (( (int)FW_REV_LSB /10) *10))  << 8 ) \
                                  )  


// Afterwards, USB_REV equal 0x4206

   
Top