CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Code generation: "var1=0; var2=0;" versus "va

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

Code generation: "var1=0; var2=0;" versus "va
PostPosted: Fri Aug 30, 2019 10:11 am     Reply with quote

Occasionally I go down a rabbit hole looking at what a compiler does. I have written articles about Arduino code generation on topics of "if/then" versus "switch/case" and what is better/faster/smaller.

Here's a fun one. I don't code this way, but I've seen people batch-set variables like this:

Code:
value1 = value2 = value3 = value4 = value5 = value6 = 0;


Since I am currently looking at CCS code generation, I wondered how it handled it.


Code:
                         ..............................     value1 = value2 = value3 = value4 = value5 = value6 = 0;
00228 810070         MOV     200E,W0        : W0 = [200E]
0022A B3C000         MOV.B   #0,W0L         : W0L = 0 
0022C 890070         MOV     W0,200E        : [200E] = W0
0022E 810070         MOV     200E,W0        : W0 = [200E]
00230 F8200C         PUSH    200C           : PUSH [200C] to TOS
00232 9FFFF0         MOV.B   W0L,[W15-#1]   : [W15+-1] = W0L
00234 F9200C         POP     200C           : POP TOS to [200C]
00236 F8200C         PUSH    200C           : PUSH [200C] to TOS
00238 78004F         MOV     [--W15],W0     : POP TOS to [--W15]
0023A DE0048         LSR     W0,#8,W0       : W0 = W0 >> 8
0023C F8200C         PUSH    200C           : PUSH [200C] to TOS
0023E 9FFFE0         MOV.B   W0L,[W15-#2]   : [W15+-2] = W0L
00240 F9200C         POP     200C           : POP TOS to [200C]
00242 810060         MOV     200C,W0        : W0 = [200C]
00244 F8200A         PUSH    200A           : PUSH [200A] to TOS
00246 9FFFF0         MOV.B   W0L,[W15-#1]   : [W15+-1] = W0L
00248 F9200A         POP     200A           : POP TOS to [200A]
0024A F8200A         PUSH    200A           : PUSH [200A] to TOS
0024C 78004F         MOV     [--W15],W0     : POP TOS to [--W15]
0024E DE0048         LSR     W0,#8,W0       : W0 = W0 >> 8
00250 F82000         PUSH    2000           : PUSH [2000] to TOS
00252 9FFFF0         MOV.B   W0L,[W15-#1]   : [W15+-1] = W0L
00254 F92000         POP     2000           : POP TOS to [2000]
00256 F82000         PUSH    2000           : PUSH [2000] to TOS
00258 78004F         MOV     [--W15],W0     : POP TOS to [--W15]
0025A DE0048         LSR     W0,#8,W0       : W0 = W0 >> 8
0025C F82000         PUSH    2000           : PUSH [2000] to TOS
0025E 9FFFE0         MOV.B   W0L,[W15-#2]   : [W15+-2] = W0L
00260 F92000         POP     2000           : POP TOS to [2000]


29 lines of assembly.

Versus the way I normally do it:

Code:
    value1 = 0;
    value2 = 0;
    value3 = 0;
    value4 = 0;
    value5 = 0;
    value6 = 0;


...which generates:

Code:
                         ..............................     value1 = 0;
00262 810000         MOV     2000,W0        : W0 = [2000]
00264 B3C000         MOV.B   #0,W0L         : W0L = 0 
00266 890000         MOV     W0,2000        : [2000] = W0
                         ..............................     value2 = 0;
00268 810000         MOV     2000,W0        : W0 = [2000]
0026A EF6001         CLR.B   1              : W0H = 0
0026C 890000         MOV     W0,2000        : [2000] = W0
                         ..............................     value3 = 0;
0026E 810050         MOV     200A,W0        : W0 = [200A]
00270 EF6001         CLR.B   1              : W0H = 0
00272 890050         MOV     W0,200A        : [200A] = W0
                         ..............................     value4 = 0;
00274 810060         MOV     200C,W0        : W0 = [200C]
00276 B3C000         MOV.B   #0,W0L         : W0L = 0 
00278 890060         MOV     W0,200C        : [200C] = W0
                         ..............................     value5 = 0;
0027A 810060         MOV     200C,W0        : W0 = [200C]
0027C EF6001         CLR.B   1              : W0H = 0
0027E 890060         MOV     W0,200C        : [200C] = W0
                         ..............................     value6 = 0;
00280 810070         MOV     200E,W0        : W0 = [200E]
00282 B3C000         MOV.B   #0,W0L         : W0L = 0 
00284 890070         MOV     W0,200E        : [200E] = W0


19 lines of assembly.

An aggressive optimizer might generate the same end-result, but you can't rely on that.

Just something fun I wondered about.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 30, 2019 12:01 pm     Reply with quote

18F works better:
Code:

00038:  CLRF   value6
0003A:  MOVFF  value6,value5
0003E:  MOVFF  value5,value4
00042:  MOVFF  value4,value3
00046:  MOVFF  value3,value2
0004A:  MOVFF  value2,value1

Test program:
Code:

#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use i2c(Slave, I2C1)

//======================================
void main()
{         
int8 value1, value2, value3, value4, value5, value6;

value1 = value2 = value3 = value4 = value5 = value6 = 0;

while(TRUE);
}
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Aug 30, 2019 1:06 pm     Reply with quote

It probably all hinges on the 'compiler code cutter'. If he needs 'generic - one code for all' or can he TRULY optimize the code.
Have to remember not ONE guy cut the compiler, so there's probably a few 'rules and regulations' about style and 'how-to-code'. And of course, different PICs may alter the design process...as not 'one shoe fits all'.
There's also the 'time vs size' challenge'. Some code for speed, some space, some mix it up.
Microchip used to have paperback books (yeah, I'm that old...) that had LOTS of neat 'tricks and tips' all Assembler / machine code, but interesting to see how easy it was to maximize the overall performance of a PIC.

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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