|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Setting W register as a function variable |
Posted: Thu Jan 13, 2005 2:51 pm |
|
|
Hi,
I'm trying to optimize my code and I would like to know if there's any clean way to do this.
Let's assume that I'm using a function
void TRANSMIT(int8 data)
If I call it like this:
TRANSMIT(0x88);
I'll always get:
;----------------- TRANSMIT(0x88)
MOVLW 88
MOVWF 71 <---This is not necessary
CALL 108
Is there a way to force the variable to be W so there would not be a MOVWF everytime the function is called. (The MOVWF would be inside the function)??
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 14, 2005 2:35 pm |
|
|
I tryed using #inline and it didn't help with the problem.
You could use a macro. If you use a macro, then it
doesn't make a function call. It just replicates the code
in each instance. I don't know if that's going to help your
efficiency problem, though.
The program below creates this ASM code:
Code: | 0000 00296 ....................
0000 00297 .................... TRANSMIT(0x55);
0012 3055 00298 MOVLW 55
0013 1E0C 00299 BTFSS 0C.4
0014 2813 00300 GOTO 013
0015 0099 00301 MOVWF 19
0000 00302 .................... TRANSMIT(0xAA);
0016 30AA 00303 MOVLW AA
0017 1E0C 00304 BTFSS 0C.4
0018 2817 00305 GOTO 017
0019 0099 00306 MOVWF 19 |
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
/*
void TRANSMIT(int8 data)
{
putc(data);
}
*/
#define TRANSMIT(data) putc(data)
//============================
void main()
{
TRANSMIT(0x55);
TRANSMIT(0xAA);
while(1);
} |
|
|
|
dperron
Joined: 13 Jan 2005 Posts: 1
|
|
Posted: Fri Jan 14, 2005 3:56 pm |
|
|
The problem with #define is that it's limited to only 1 line, you cannot do a multiline macro with it.
I really wished there were some way to set W a the variable...
So it's not so trivial after all... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 14, 2005 4:20 pm |
|
|
Quote: | The problem with #define is that it's limited to only 1 line, you cannot do a multiline macro with it. |
Actually you can, if you have a modern version of the compiler.
You can use the backlash symbol to indicate line continuation. Example:
Code: |
// This macro transmits the data byte 4 times.
#define TRANSMIT(data) putc(data); \
putc(data); \
putc(data); \
putc(data) |
|
|
|
Ttelmah Guest
|
|
Posted: Sat Jan 15, 2005 11:25 am |
|
|
dperron wrote: | The problem with #define is that it's limited to only 1 line, you cannot do a multiline macro with it.
I really wished there were some way to set W a the variable...
So it's not so trivial after all... |
As PCM programmer has pointed out, you can have multi-line macros. You can also go further, and use a very rarely used feature of C, and have a multi-line macro, returning a value. If you generate a macro like:
Code: |
#define from_locn(x) (temp=x,\
x=(x+1)%maxval,\
buffer[temp])
|
The code will save 'x', increment it, and generate the remainder after division my the 'maxval' value (ideal for incrementing a buffer pointer), then return the contents of 'buffer[temp]', as the macro return value!.
The 'comma' seperator, as opposed to the normal semi-colon seprator, in C, allows multiple statements to be executed, and only returns the value of the last statement.
The macro language in C, can do a lot more than is often realised...
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
|