View previous topic :: View next topic |
Author |
Message |
prondeau
Joined: 26 Mar 2008 Posts: 11
|
Code optimization |
Posted: Wed Dec 12, 2012 2:41 pm |
|
|
I'm revisiting an older project that made use of an 18f45j10. With the changes I'm implementing I'm rapidly approaching the point where I'm going to run out of memory. I've been looking for areas where I can optimize the code to have a smaller footprint. I perform a lot of operations on 32bit integers where I need to access the four bytes individually. I've noticed that the resultant disassembly listing for this operation seems quite a bit larger than if I was to do this in pure assembly. I've tried masking and shifting as well as using pointers. Before I do any more work on this, is there a known method to get the tightest code out of this compiler for the desired operation?
Thanks!
Pete |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
prondeau
Joined: 26 Mar 2008 Posts: 11
|
|
Posted: Wed Dec 12, 2012 3:26 pm |
|
|
fwiw, i'm using 4.076
I sat down to write a small program as requested to demonstrate what I was talking about and discovered the answer on my own.
What I was doing was something like this:
Code: | spi_write((TestVariable >>24) & 0xFF); |
And found that it created 12 lines of assembly code.
I tried to simplify it for the example and just did something like this:
Code: | A= ((TestVariable >>24 )& 0xff); |
I got back 1 line of assembly (as I expected and desired)
so I tried this
Code: | A= ((TestVariable >>24 )& 0xff);
spi_write(A); |
And the result was 5 lines of assembly code.
That was one approach I hadn't tried.. So I guess I don't really need help with this anymore.
Thank you!
Pete |
|
|
prondeau
Joined: 26 Mar 2008 Posts: 11
|
|
Posted: Wed Dec 12, 2012 3:34 pm |
|
|
Just to be thorough:
Code: | #include <18f45j10.H>
#use delay(clock=40M, oscillator=10M)
void main(void){
long long TestVariable;
int A;
TestVariable = 01234567;
spi_write((TestVariable >>24) & 0xff);
A= ((TestVariable >>24) & 0xff);
spi_write(A);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Dec 12, 2012 4:06 pm |
|
|
Even better, use the CCS functions:
Code: |
signed int32 TestVariable; //better to use explicit sizes.
TestVariable = 01234567;
spi_write(make8(TestVariable,3)); //send 3rd byte of TestVariable
|
Less RAM, and similarly short assembler.
Best Wishes |
|
|
prondeau
Joined: 26 Mar 2008 Posts: 11
|
|
Posted: Wed Dec 12, 2012 4:23 pm |
|
|
Oh! excellent!, much less cluttered too. Thanks for this, I had not seen it in the manual. |
|
|
|