How #define can be used to 32 bit register?

Sampath
2007-07-09
2013-03-12
  • Sampath
    Sampath
    2007-07-09

    Hi ,

    I am trying to pass an argument to a 32 bit register by using the #define.
    How can I do it by using a define .
    Is there any way to make the define to 32 bit value.
    .. for example.

    #define field1 0x5F   // 8 bit field
    #define field2 0x7    // 3 bit field
    #define field3 0x1F   // 5 bit field
    #define field5 0x5F   // 8 bit field
    #define field5 0x5F   // 8 bit field
    #define My_arg ((field5<<24 | field4<< 16 | field3<< 11 | field2 <<8 | field1))

    ..........................
    ..........................

    In my function

    My_func(My_arg
            ,...);

    If I compile, as the #define will take only 16 bit,
    I get the warning that
    test_1.c:45: warning 116: left shifting more than size of object changed to zero

    and in the asm file created shows msgs like changed mov to clr,removed redundant mov etc..

        Peephole 101    removed redundant mov
        movx    @dptr,a
        inc    dptr
    ;    Peephole 181    changed mov to clr
        clr    a
        movx    @dptr,a
        inc    dptr
    ;    Peephole 101    removed redundant mov
        movx    @dptr,a
    ;    test_1.c:47: *((volatile unsigned long xdata *) (0x1008)) = ORIG_TD0_TD01;
    ;    genAssign
    ;    Peephole 182.b    used 16 bit load of dptr
        mov    dptr,#0x1008
    ;    genPointerSet
    ;     genFarPointerSet
        mov    a,#0x65
        movx    @dptr,a
        inc    dptr
    ;    Peephole 181    changed mov to clr
    ;    Peephole 101    removed redundant mov
    ;    Peephole 181    changed mov to clr

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    but if I pass the argument hard coded with 32 bit value (without any

    #define CONFIG0_TD01 0x12345678
    main(void)

    {

    *((volatile unsigned long xdata *) (0x1000)) = CONFIG0_TD01;
    }

    It produces the correct output as such

        mov    dptr,#0x1000
    ;    genPointerSet
    ;     genFarPointerSet
        mov    a,#0x78
        movx    @dptr,a
        inc    dptr
        mov    a,#0x56
        movx    @dptr,a
        inc    dptr
        mov    a,#0x34
        movx    @dptr,a
        inc    dptr
        mov    a,#0x12
        movx    @dptr,a

    Sampath

     
    • wek
      wek
      2007-07-09

      Typecast explicitly to unsigned long before shift, i.e.
      #define My_arg (((unsigned long)field5<<24 | (unsigned long)field4<< 16 etc.

      JW

       
    • Sampath
      Sampath
      2007-07-09

      Yes it has worked.
      Thanks for the help.

      Sampath

       
    • Maarten Brock
      Maarten Brock
      2007-07-10

      Or append L or UL to either side of the literals. This is all basic C.

      #define My_arg ((field5<<24UL | field4<<16UL etc.

      or

      #define field1 0x5FUL // 8 bit field
      #define field2 0x7UL  // 3 bit field 

      Maarten