Maker Pro
Maker Pro

Learning to use PICS

S

Spehro Pefhany

Jan 1, 1970
0
I would be interested to see the generated code and to know which compiler
you are using.

I think I have misspoken on the size- the assy file was hiding some of
the instructions for some reason. Here's the code from the program
memory (some manual transcription, so it may not be 100% correct).
It's HitechC for 16F. 27 program words including two bank select
instructions.


0007E5 MOVLW 0x8
0007E6 BCF 0x3, 0x5
0007E7 BCF 0x3, 0x6
0007E8 MOVWF 0x2E
0007E9 MOVF 0x2b,W
0007EA MOVWF 0x2d
0007EB BCF 0x3, 0
0007EC RLF 0x2a, F
0007ED RLF 0x2b, F
0007EE MOVF 0x2c, W
0007EF XORWF 0x2d, W
0007F0 MOVWF 0x40
0007F1 BTFSS 0x40, 0x7
0007F2 GOTO 0x7f7
0007F3 MOVLW CRC_LOW
0007F4 XORWF 0x2a, F
0007F5 MOVLW CRC_HIGH
0007F6 XORWF 0x2b, F
0007F7 BCF 0x3, 0
0007F8 RLF 0x2c,F
0007F9 DECFSZ 0x2e,F
0007FA GOTO 0x7e9
0007FB MOVF 0x2b, W
0007FC MOVWF 0x41
0007FD MOVF 0x2a, W
0007FE MOVWF 0x40
0007FF RETURN


Best regards,
Spehro Pefhany
 
S

Sergio Masci

Jan 1, 1970
0
Spehro Pefhany said:
I think I have misspoken on the size- the assy file was hiding some of
the instructions for some reason. Here's the code from the program
memory (some manual transcription, so it may not be 100% correct).
It's HitechC for 16F. 27 program words including two bank select
instructions.


0007E5 MOVLW 0x8
0007E6 BCF 0x3, 0x5
0007E7 BCF 0x3, 0x6
0007E8 MOVWF 0x2E
0007E9 MOVF 0x2b,W
0007EA MOVWF 0x2d
0007EB BCF 0x3, 0
0007EC RLF 0x2a, F
0007ED RLF 0x2b, F
0007EE MOVF 0x2c, W
0007EF XORWF 0x2d, W
0007F0 MOVWF 0x40
0007F1 BTFSS 0x40, 0x7
0007F2 GOTO 0x7f7
0007F3 MOVLW CRC_LOW
0007F4 XORWF 0x2a, F
0007F5 MOVLW CRC_HIGH
0007F6 XORWF 0x2b, F
0007F7 BCF 0x3, 0
0007F8 RLF 0x2c,F
0007F9 DECFSZ 0x2e,F
0007FA GOTO 0x7e9
0007FB MOVF 0x2b, W
0007FC MOVWF 0x41
0007FD MOVF 0x2a, W
0007FE MOVWF 0x40
0007FF RETURN


Best regards,
Spehro Pefhany

Thank you for that.

It seems that the Hitech C compiler has noticed an optimisation in the "for
loop" that the XCSB compiler missed, essentially converting:

;----------
k = 8
loop_start
if k <= 0 then goto loop_end
...
k = k - 1
goto loop_start
loop_end
;----------

into

;----------
k = 8
loop_start
...
if (k = k - 1) != 0 then goto loop_start
;----------

Even so, it is still reassuring to see how close the output of both
compilers is. 26 machine code instructions for the Hitech C compiler and 31
for the XCSB compiler. If you look back at a previous post in this thread
you will see that I mentioned that the function epilog can actually reduce
the length of the overall code by 4 instructions (depending on optimisation)
So the code generated by the XCSB compiler would be only 27 machine code
instructions.

I will have to put this "for loop" optimisation on my todo list :)

Regards
Sergio Masci

http://www.xcprod.com/titan/XCSB - optimising PIC compiler
FREE for personal non-commercial use.
 
B

Bill Bowden

Jan 1, 1970
0
<snipped 33 assembler instructions>
Hi Bill,

The following CRC function is a standard XCSB library function. It is
written in XCSB (XC Structured BASIC) and compiles to 31 machine code
instructions.

Yes, that's amazing a compiler can be so efficient to
produce a assembly code of similar size to a manually
written assembly routine. The crc routine I wrote
was developed from a drawing of the bit movements
of the two CRC bytes as they are exored against
sequential bits of the data byte. I don't know why the
various bits were chosen, but probably because they yield
the most unique crc.

The flow is something like this:

Basically, the MSB bit of the data is XORed against bit 15 of
the 16 bit crc. That result is XORed with bit 11 of the crc and
the result is pushed up the stack underneath bit 12 (15 disappears).
The first result is then XORed with bit 4 of the crc and pushed
on the stack under bit 5 (old bit 11 is pushed off). The last
step is to XOR the first result against 0 and the result pushed
on the bottom of the stack (old bit 4 disappears). Here's a crude
diagram:

DATA IN (MSB)---> XOR <------- 15 CRC (MSB)
| 14
| 13
| 12
|----XOR-->
| 11 11
| 10
| 9
| 8
| 7
| 6
| 5
|----XOR-->
| 4 4
| 3
| 2
| 1
| 0
|----XOR-->
0

-Bill
 
Top