The compiler doesn't generate a proper assembly in the following scenario:
- Code: Select all
typedef unsigned long U32;
typedef unsigned char U8;
U8 sum_of_bytes(U32 a) {
U8 s = ((U8*)&a)[0]; // MSB
s += ((U8*)&a)[1];
s += ((U8*)&a)[2];
s += ((U8*)&a)[3]; // LSB
return s;
}
- Code: Select all
_sum_of_bytes:
s --> X+0
a --> X-7
0383: 10 PUSH X (0184) ret
0384: 4F MOV X,SP
0385: 38 01 ADD SP,1
0387: 52 F9 MOV A,[X-7]
0389: 54 00 MOV [X+0],A
038B: 52 06 MOV A,[X+6] <<<< supposed to be [X-6]
038D: 05 00 ADD [X+0],A
038F: 52 05 MOV A,[X+5] <<<< supposed to be [X-5]
0391: 05 00 ADD [X+0],A
0393: 52 04 MOV A,[X+4] <<<< supposed to be [X-4]
0395: 05 00 ADD [X+0],A
0397: 52 00 MOV A,[X+0]
0399: 62 D0 00 MOV REG[208],0
039C: 38 FF ADD SP,255
039E: 20 POP X
039F: 7F RET
The assembly code to get the most significant byte is correct but not for the other bytes.
Cheers,
Zorglub


