Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/13/07 15:20
Modified:
  06/13/07 17:11

Read: times


 
#140680 - Post Mortem #1
Responding to: ???'s previous message
Well, well. Since we've been stalled for a day or so now with Chris's 34-byte program as the shortest one, maybe it would be interesting to continue what Michael started and see how the code generated by the compiler related to the source code. Like Michael, I used the Keil compiler with its optimization level set at 8.

Here are the programs:
1.  c=c/2&85|c*2&170;c=c/4&51|c*4&204;RT c>>4|c<<4;       // (47 bytes)
2.  UC r,i=8;WH(i--){r>>=1;r|=c&128;c*=2;}RT r;           // (43 bytes)
3.  UC r,i=8;WH(i--){r*=2;r|=c&1;c/=2;}RT r;              // (40 bytes)
4.  UC r,i=8;WH(i--){r=r*2|c&1;c/=2;}RT r;                // (38 bytes)
5.  UC r,i=8;WH(i--)r=r*2|c&1,c/=2;RT r;                  // (36 bytes)
6.  UC r,i=1;WH(r+=c&i?r++:r,i+=i);RT r;                  // (36 bytes)
7.  IN r=1;WH(r<256)r=r*2|c&1,c/=2;RT r;                  // (36 bytes)
8.  UC r,i=1;WH(r+=r+c/i%2,i+=i);RT r;                    // (34 bytes)
And here is a summary of the results
Program   Length of generated code

   1               38
   2               26
   3               26
   4               26
   5               26
   6               25
   7               35
   8               22
As you might expect, the shorter C programs generally produced less generated code. Program #7 is a glaring exception. Its problem is that it uses a 16-bit variable, which is obviously an expensive proposition on an 8-bit machine.

Here's the generated code for #8:
;     UC r,i=1;WH(r+=r+c/i%2,i+=i);RT r;
;---- Variable 'c' assigned to Register 'R7' ----
;---- Variable 'i' assigned to Register 'R6' ----

0000 7E01              MOV     R6,#01H
0002         ?C0028:
0002 EF                MOV     A,R7
0003 8EF0              MOV     B,R6
0005 84                DIV     AB
0006 5401              ANL     A,#01H
0008 2500        R     ADD     A,r
000A 2500        R     ADD     A,r
000C F500        R     MOV     r,A
000E EE                MOV     A,R6
000F 2E                ADD     A,R6
0010 FE                MOV     R6,A
0011 70EF              JNZ     ?C0028
0013 AF00        R     MOV     R7,r
0015 22                RET     
A (nearly?) optimized (for size) ASM solution is about half as big.
0047           Flip5:
0047 C8                xch     a,r0
0048 7480              mov     a,#80h  ; Done when this bit pops out
004A           Floop5:
004A C8                xch     a,r0    ; Shift MSB out of source byte ...
004B 33                rlc     a
004C C8                xch     a,r0    ; ... and into destination
004D 13                rrc     a
004E 50FA              jnc     Floop5  ; Done when initial bit pops out
0050 22                ret
That 2-to-1 size reduction by going to ASM is pretty impressive, but not something you would expect to see with every problem. For example, the code generated by a compiler for the straightforward initialization of a bunch of 8051 peripherals, where all you're doing is setting a bunch of constant values into a bunch of SFRs, would probably be identical to what an ASM programmer would write.

-- Russ


List of 87 messages in thread
TopicAuthorDate
Sunday Quiz - Bit Flipper in C            01/01/70 00:00      
   Here's My 41 Bytes            01/01/70 00:00      
      Very nice, but call it 43 (explained herein)            01/01/70 00:00      
   Sunday Quiz Update            01/01/70 00:00      
      OK Then 39 Bytes...            01/01/70 00:00      
         Oh my goodness            01/01/70 00:00      
            If you want performance...            01/01/70 00:00      
         More bytes, but ...            01/01/70 00:00      
   OK Then 38 Bytes            01/01/70 00:00      
      Comma            01/01/70 00:00      
         36! Holy cow! Very nice!            01/01/70 00:00      
            my 36            01/01/70 00:00      
               Non-conforming            01/01/70 00:00      
                  Wow! 34! |<3WL! :-)            01/01/70 00:00      
                     ???            01/01/70 00:00      
                        KEWL = cool in l33t :-) (worse than SMS...)            01/01/70 00:00      
                  Wow is right!!! plus another 36            01/01/70 00:00      
         Broken            01/01/70 00:00      
            C doesn't try to save you ...            01/01/70 00:00      
            Not Broken            01/01/70 00:00      
   look for samples of FFT code ...            01/01/70 00:00      
   this is competing to make the worst possible            01/01/70 00:00      
      it's certainly not the solution...            01/01/70 00:00      
         correction            01/01/70 00:00      
            Unless you're "porting" ...            01/01/70 00:00      
               there are situations...            01/01/70 00:00      
                  au contraire            01/01/70 00:00      
                     overlayed variables            01/01/70 00:00      
                        non-religious reasons and debunking some            01/01/70 00:00      
                           I doubt that            01/01/70 00:00      
                              now try            01/01/70 00:00      
                                 with the error or without?            01/01/70 00:00      
                                    the 49 is a compare the 48 is coding time            01/01/70 00:00      
                                       47, 48, 49            01/01/70 00:00      
                                          for such a cause as            01/01/70 00:00      
                                          Still more            01/01/70 00:00      
                                             one more point for Pascal            01/01/70 00:00      
                                                Language and vocabulary contributions            01/01/70 00:00      
                                                   Language and vocabulary contributions            01/01/70 00:00      
                                                Yes, it's much more "self-documenting"            01/01/70 00:00      
                                    Singular?            01/01/70 00:00      
                                 well... around 5 minutes            01/01/70 00:00      
                              It's not funny            01/01/70 00:00      
                                 this is why I don't like the "modern" over-windowe            01/01/70 00:00      
         Tastes            01/01/70 00:00      
      Skill in reading            01/01/70 00:00      
         in other words: because there are burglars detecti            01/01/70 00:00      
            That's why one should use ASM and not 'C'            01/01/70 00:00      
               nope            01/01/70 00:00      
                  are you perfect?            01/01/70 00:00      
                  That's why the documentation is necessary            01/01/70 00:00      
                     the old argument from C haters            01/01/70 00:00      
                     One page of comments per statement?            01/01/70 00:00      
                        If it's to be understood later on ...            01/01/70 00:00      
                           if I have to explain my choice of syntax in C, the            01/01/70 00:00      
                              The point is to show why, and not why-not            01/01/70 00:00      
                                 I post, you 'reply'            01/01/70 00:00      
                           Sorry, I still need more help on this            01/01/70 00:00      
                              Never had to do that ... and for good reason.            01/01/70 00:00      
                                 Maybe just one unclear point now            01/01/70 00:00      
                                    Well, if it were up to me ...            01/01/70 00:00      
                                       Thanks            01/01/70 00:00      
                                          I hope you're not missing my point ...            01/01/70 00:00      
                                             You explained yourself clearly            01/01/70 00:00      
                                 Upside down            01/01/70 00:00      
                                    Just look at the body of work ...            01/01/70 00:00      
                                       Nature of commercial software            01/01/70 00:00      
                                          Maybe it's more like a 747 vs. a bicycle            01/01/70 00:00      
                                    on driving and "coding"            01/01/70 00:00      
         even Mr. K agrees            01/01/70 00:00      
   Post Mortem #1            01/01/70 00:00      
      I object            01/01/70 00:00      
      Post Mortem #1.1            01/01/70 00:00      
         both translations are "strange"            01/01/70 00:00      
            Uninitialised?            01/01/70 00:00      
      20 bytes.            01/01/70 00:00      
         teaching the compiler...            01/01/70 00:00      
            Maybe for HLLs, but not for C ...            01/01/70 00:00      
               I know this is the praxis....            01/01/70 00:00      
         incorrect/incomplete statement            01/01/70 00:00      
   Post Mortem #2            01/01/70 00:00      
      Vote: 1. Yes 2. Dont care            01/01/70 00:00      
      modify it!!!!            01/01/70 00:00      
      vote            01/01/70 00:00      
         methink            01/01/70 00:00      
   its interesting that C            01/01/70 00:00      
      I like your proposal            01/01/70 00:00      

Back to Subject List