Em 14/02/2014 00:23, William "Chops" Westfield escreveu: > On Feb 11, 2014, at 1:24 AM, alan.b.pearce@stfc.ac.uk wrote: > >> I believe the free mode [of XC8] now does a minimal optimisation so that= any code generated will at least work in the way the code writer intended. > I dunno. I downloaded the latest, in hopes that this would be true. But= it's still producing rather weird code: > > >From one of their "blinky" examples: > > void delay() { > int counter =3D 0; > for (counter =3D 0; counter<10000; counter++) { > ; > } > } > > 7FD8 0E00 delay MOVLW 0x0 ;; 16 bits of 0 to counter. > 7FDA 6E02 MOVWF 0x2, ACCESS > 7FDC 0E00 MOVLW 0x0 > 7FDE 6E01 MOVWF counter, ACCESS > 7FE0 0E00 MOVLW 0x0 ;; and again (the source > 7FE2 6E02 MOVWF 0x2, ACCESS ;; does it twice, too, so > 7FE4 0E00 MOVLW 0x0 ;; I guess that's OK.) > 7FE6 6E01 MOVWF counter, ACCESS It is doing exactly what was requested. > 7FE8 5002 lp: MOVF 0x2, W, ACCESS > 7FEA 0A80 XORLW 0x80 ;; Strange compare to 10000 > 7FEC 0F59 ADDLW 0x59 ;; I guess it's jumping through hoops= to wind up with a sequence > 7FEE 0E10 MOVLW 0x10 ;; that skips appropriately at the e= nd, rather than resolving > 7FF0 B4D8 BTFSC STATUS, 2, ACCESS ;; the branch in the middle. But= =85 Eww! > 7FF2 5C01 SUBWF counter, W, ACCESS > 7FF4 B0D8 BTFSC STATUS, 0, ACCESS > 7FF6 D003 BRA 0x7FFE ;; (exit) end of strange compare It is calculating ( counter - 0x8000 + 0x5900 - 0x0010 ) that is exactly th= e same as ( counter - 10000 ) 1) Subtract (XORLW) 0x80 from the high byte. Note that the low byte isn't d= isturbed. 2) Add 0x59 to the high byte. and test for zero. If the result is zero, cou= nter is between 0x2700 (9984) and 0x27ff (10239). If the Z bit is clear and= the C bit is set then counter is above 10239 and it takes the BRA. 3) If the Z bit is set, counter is in the range between 9984 and 10239, it = simply subtracts 0x10 (16) from the low byte. If the C bit gets set it is b= ecause counter is above 9999 (0x270f) and it takes the BRA. > 7FF8 4A01 INFSNZ counter, F, ACCESS > 7FFA 2A02 INCF 0x2, F, ACCESS ;; Pretty nice 16bit increment > 7FFC D7F5 BRA 0x7FE8 ;; (lp) > 7FFE 0012 exit: RETURN 0 > --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .