|
|
View previous topic :: View next topic |
Author |
Message |
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
Code generation issue |
Posted: Wed May 09, 2007 2:19 am |
|
|
I have de following declarations: Code: | struct
{
int8 Dummy;
int8 a[20];
} str;
int8 *x; | To get in x a pointer from one element from a, i could do Code: | void Main()
{
int8 i = 7;
x = &str.a[i];
} | which generates: Code: | ................... int8 i = 7;
0024: MOVLW 07
0026: MOVWF 1D
.................... x = &str.a[i];
0028: CLRF 03
002A: MOVF 1D,W
002C: ADDLW 01
002E: MOVWF 01
0030: MOVLW 00
0032: ADDWFC 03,F
0034: MOVF 01,W
0036: ADDLW 06
0038: MOVWF 1B
003A: MOVLW 00
003C: ADDWFC 03,W
003E: MOVWF 1C |
or: Code: | void Main()
{
int8 i = 7;
x = str.a + i;
} | which generates Code: | .................... int8 i = 7;
0024: MOVLW 07
0026: MOVWF 1D
.................... x = str.a + i;
0028: ADDWF 1D,W
002A: MOVWF 1B
002C: CLRF 1C
002E: BTFSC FD8.0
0030: INCF 1C,F |
Since both C statements are the same, why genereted code is not? |
|
|
Ttelmah Guest
|
|
Posted: Wed May 09, 2007 3:06 am |
|
|
This is very common, and fairly 'classic' with compilers.
You will almost certainly find the effects will change if you change the #opt setting.
Basically, in the second case, you say to take the address 'str.a' (which is 'hardwired' at compile time), and add 'i' to this.
In the first you say to access the array element, and then get the address of this. The arithmetic is done in this case, by working out the offset needed for 'i' in the array, then adding this to the array starting point. Just slightly more complex.
The optimiser, will try to improve this sort of behaviour, but it is the price you 'pay' for the convenience of being able to access elements without having to do the arithmetic yourself...
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|