Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/31/10 16:33
Read: times


 
Msg Score: +1
 +1 Informative
#174685 - unions and portability
Responding to: ???'s previous message
Andy Neil said:
Things like byte ordering and whether or not padding is added are compiler-specific - so this is not portable!


Indeed. I'm working on a little stepper-motor controller, and I'm using the Silicon Labs app note (AN155) as a reference. The SiLabs code uses a union to store a 16-bit value as either a 16-bit word or a pair of bytes (upper and lower). This union holds a 16-bit value which is used to initialize a timer. So the idea is to add some new offset to the timer value. The timer value is read into the two bytes (two MOVs essentially) and then the offset is added to the 16-bit word, then the two bytes are written back to the timer (two more MOVs).

Keil is big-endian. SDCC is little-endian.

I'm not using the union.

-a


List of 23 messages in thread
TopicAuthorDate
Unions and position of bytes            01/01/70 00:00      
   Syntax and strategy problem.            01/01/70 00:00      
      wow that was quick and excellent thanks            01/01/70 00:00      
      Works a treat            01/01/70 00:00      
   Note that this is heavily compiler-reliant            01/01/70 00:00      
      Code for transparency            01/01/70 00:00      
         Agreed - after bitter experience            01/01/70 00:00      
            Compiler Specific            01/01/70 00:00      
               How many compler brands/versions to test for?            01/01/70 00:00      
                  Create a Compiler header            01/01/70 00:00      
                     Didn't we talk about unions - your examples doesn't...            01/01/70 00:00      
                        Someone already wrote up a good way....            01/01/70 00:00      
                           Incomplete byte order. But pad is still dangerous            01/01/70 00:00      
                              why bother?            01/01/70 00:00      
                                 how would you know?            01/01/70 00:00      
                                    Exactly my point... you don't            01/01/70 00:00      
                                       Never give up on portability - just decide the amount            01/01/70 00:00      
               Yes, but            01/01/70 00:00      
               why bother?            01/01/70 00:00      
      unions and portability            01/01/70 00:00      
         OT: use TR0 = 0; TMR0 -= offset; TR0 = 1;            01/01/70 00:00      
         you're not?            01/01/70 00:00      
            It was just a warning that union type casts are dangerous            01/01/70 00:00      

Back to Subject List