Hi Paul,
Just some more information. Ill try and have a look into where gcc does
all this today.
Are you using optimization? what is your compile line?
Compiling with
msp430-gcc -mmcu=msp430x123 -g -o main main.c --save-temps
then doing a 'msp430-objdump -dS main' I get the following,
a = 0x12345678l;
e0f0: b4 40 78 56 mov #22136, 0(r4) ;#0x5678
e0f4: 00 00
e0f6: b4 40 34 12 mov #4660, 2(r4) ;#0x1234
e0fa: 02 00
b = a >> LVAL;
e0fc: 2e 44 mov @r4, r14 ;
e0fe: 1f 44 02 00 mov 2(r4), r15 ;
e102: 8e 10 swpb r14 ;
e104: 8f 10 swpb r15 ;
e106: 4e ef xor.b r15, r14 ;
e108: 0e ef xor r15, r14 ;
e10a: 8f 11 sxt r15 ;
e10c: 84 4e 04 00 mov r14, 4(r4) ;
e110: 84 4f 06 00 mov r15, 6(r4) ;
and
b = 0x12345678l;
e13e: b4 40 78 56 mov #22136, 4(r4) ;#0x5678
e142: 04 00
e144: b4 40 34 12 mov #4660, 6(r4) ;#0x1234
e148: 06 00
b >>= LVAL;
e14a: 94 10 04 00 swpb 4(r4) ;
e14e: 94 10 06 00 swpb 6(r4) ;
e152: d4 e4 06 00 xor.b 6(r4), 4(r4) ;
e156: 04 00
e158: 94 e4 06 00 xor 6(r4), 4(r4) ;
e15c: 04 00
e15e: 94 11 06 00 sxt 6(r4) ;
for LVAL = 16
a = 0x12345678l;
e0f0: b4 40 78 56 mov #22136, 0(r4) ;#0x5678
e0f4: 00 00
e0f6: b4 40 34 12 mov #4660, 2(r4) ;#0x1234
e0fa: 02 00
b = a >> LVAL;
e0fc: 2e 44 mov @r4, r14 ;
e0fe: 1f 44 02 00 mov 2(r4), r15 ;
e102: 84 43 06 00 mov #0, 6(r4) ;r3 As==00
e106: 84 4f 04 00 mov r15, 4(r4) ;
e10a: 94 44 04 00 mov 4(r4), 6(r4) ;
e10e: 06 00
e110: 94 54 06 00 rla 6(r4) ;
e114: 06 00
e116: 94 74 06 00 subc 6(r4), 6(r4) ;
e11a: 06 00
e11c: b4 e3 06 00 xor #-1, 6(r4) ;r3 As==11
and
b = 0x12345678l;
e14a: b4 40 78 56 mov #22136, 4(r4) ;#0x5678
e14e: 04 00
e150: b4 40 34 12 mov #4660, 6(r4) ;#0x1234
e154: 06 00
b >>= LVAL;
e156: 84 43 06 00 mov #0, 6(r4) ;r3 As==00
e15a: 94 44 06 00 mov 6(r4), 4(r4) ;
e15e: 04 00
e160: 94 44 04 00 mov 4(r4), 6(r4) ;
e164: 06 00
e166: 94 54 06 00 rla 6(r4) ;
e16a: 06 00
e16c: 94 74 06 00 subc 6(r4), 6(r4) ;
e170: 06 00
e172: b4 e3 06 00 xor #-1, 6(r4) ;r3 As==11
There is something very strange going on here. All the other coding go
though r15 and r14 where as this one tries to re-use the same register.
The b >>= c uses a loop and should work fine.
#define LVAL 8
void test() {
long a, b;
int c;
// Works
printf ("TEST1\r\n");
a = 0x12345678l;
b = a >> LVAL;
printf ("TEST: b = a >> %2d ---> 0x%08lx\r\n", LVAL, b);
// Doesn't work for LVAL=8,16+
printf ("TEST2\r\n");
b = 0x12345678l;
b >>= LVAL;
printf ("TEST: b >>= %2d ---> 0x%08lx\r\n", LVAL, b);
// Works
printf ("TEST3\r\n");
b = 0x12345678l;
c = LVAL;
b >>= c;
printf ("TEST: b >>= c ---> 0x%08lx\r\n", b);
}
--
Peter Jansen
STS
Australian Antarctic Division
Channel Highway
Kingston
TAS 7050
AUSTRALIA
Ph (03) 62 323 533
Fax (03) 62 323 351