Searching \ for 'Teaching by Example (was: "Re: PIC Book")' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Teaching by Example (was: "Re: PIC Book")'.

Truncated match.
PICList Thread
'Teaching by Example (was: "Re: PIC Book")'
1997\03\14@044211 by Andrew Warren

face
flavicon
face
Tjaart van der Walt <spam_OUTtjaartTakeThisOuTspamwasp.co.za> wrote:

> I feel that one should suffer a little bit in the beginning to
> develop a way of thinking. Copying other's code teaches nothing.

Tjaart:

I understand your point, but I'm not sure that you can make such a
blanket declaration about copying others' code.

For instance, how long do you suppose it would take a beginner to
come up with the following extremely-useful code fragments without
ever having seen them used in someone else's code?

#1:

   MOVF    SWITCH,W
   XORLW   CASE1           ;IF SWITCH = CASE1, JUMP TO LABEL1.
   BZ      LABEL1          ;
   XORLW   CASE2^CASE1     ;IF SWITCH = CASE2, JUMP TO LABEL2.
   BZ      LABEL2          ;
   XORLW   CASE3^CASE2     ;IF SWITCH = CASE3, JUMP TO LABEL3.
   BZ      LABEL3          ;

#2:

   ADDLW   256-HIVAL       ;THIS IS ERIC SMITH'S CODE TO TEST FOR
   ADDLW   (HIVAL-LOVAL)+1 ;"LOVAL <= W <= HIVAL".
   BC      INRANGE         ;

#3:

   ADDWF   PCL             ;CALCULATE 2^W, WHERE W IS IN THE
   ADDLW   0               ;RANGE [0-7].
   ADDLW   -1              ;
   ADDLW   -3              ;
   ADDLW   -7              ;
   ADDLW   -15             ;
   ADDLW   -31             ;
   ADDLW   -63             ;
   ADDLW   121             ;

#4:

   MOVF    BLO,W           ;DO A 16-BIT SUBTRACTION (A = A - B),
   SUBWF   ALO             ;WITH THE CARRY FLAG PROPERLY SET ON
   MOVF    BHI,W           ;COMPLETION.
   SKPC                    ;
   INCFSZ  BHI,W           ;
   SUBWF   AHI             ;

-Andy

=== Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com
=== Fast Forward Engineering - Vista, California
===
=== Custodian of the PICLIST Fund -- For more info, see:
=== www.geocities.com/SiliconValley/2499/fund.html

1997\03\14@064137 by Andy Kunz

flavicon
face
>I understand your point, but I'm not sure that you can make such a
>blanket declaration about copying others' code.

Very useful fragments, Andy.  Think I'll have to steal some of them!

I agree with you 100%.  Some of the neatest things in my code were thought
up by somebody else!

Andy

==================================================================
Andy Kunz - Montana Design - 409 S 6th St - Phillipsburg, NJ 08865
         Hardware & Software for Industry & R/C Hobbies
       "Go fast, turn right, and keep the wet side down!"
==================================================================

1997\03\14@071158 by tjaart

flavicon
face
Andy Kunz wrote:
>
> >I understand your point, but I'm not sure that you can make such a
> >blanket declaration about copying others' code.
>
> Very useful fragments, Andy.  Think I'll have to steal some of them!
>
> I agree with you 100%.  Some of the neatest things in my code were thought
> up by somebody else!

Sure... But do you reach for the ol' PIC archives if you want to do
some keyboard debouncing, or if you want to drive a LCD? (How many times
have I seen *that* line?)

I concede that one can learn a lot of 'tricks' by looking at what other
folks came up with, but if you are starting out, the hard way makes you
use that grey stuff upstairs.

I may be biased because that was the way I started out, but I feel one
has to adopt an inventive way of thinking when faced with the
restrictions
(ROM, RAM and I/O) of a small micro.

--
Friendly Regards

Tjaart van der Walt
tjaartspamKILLspamwasp.co.za
_____________________________________________________________
| Another sun-deprived R&D Engineer slaving away in a dungeon |
|             WASP International  http://wasp.co.za           |
|             GSM and GPS value-added applications            |
|  Voice : +27-(0)11-622-8686   |   Fax : +27-(0)11-622-8686  |
|_____________________________________________________________|

1997\03\14@100650 by Andrew Warren

face
flavicon
face
Tjaart van der Walt <.....tjaartKILLspamspam.....wasp.co.za> wrote:

> But do you reach for the ol' PIC archives if you want to do some
> keyboard debouncing, or if you want to drive a LCD? (How many times
> have I seen *that* line?)

   Yeah... My favorite posts are the ones that go something like:

       "Does anyone have code to read numbers entered on a keypad,
       perform floating-point calculations on them, display text on
       an LCD, and drive 4 stepper motors according to what was
       entered?

       "I'd write it myself, but I don't want to re-invent the
       wheel...

       "[signed]
       John Doe
       Custom Industrial Controls, Inc."

> I concede that one can learn a lot of 'tricks' by looking at what
> other folks came up with, but if you are starting out, the hard way
> makes you use that grey stuff upstairs.

   You're right; as I said, I can see your point.  I was only saying
   that those "tricks" can open the eyes of beginning programmers
   (and even experienced ones like Andy Kunz) to entirely new
   techniques that they might be unlikely to discover themselves.

   For a COMPLETE beginner, of course, tricks like the examples I
   posted won't help at all... But for a slightly more advanced
   programmer, understanding them is the key to becoming a MUCH
   more advanced programmer.

   As you know, I do a fair amount of "pure" consulting in addition
   to the contract design that occupies most of my time... Most of
   that consulting is for companies with small (one- to
   three-person) software-engineering departments.

   You'd be amazed -- I was, initially -- at the lack of quality in
   the code produced by most of these teams... I usually ask to see
   examples of both their most-recently- and LEAST-recently-written
   code, and there's often no real improvement between the two
   examples, even in cases where the guys have been writing PIC
   code for YEARS.

   It's not their fault, really; it's just that they've usually been
   working in isolation and just don't realize that there ARE better
   ways.  Also, many of the "software engineers" in the
   embedded-control industry have little or no formal software
   training... Lots of them are electrical engineers who just fell
   into writing software out of necessity.

   No matter what the reason for their poor skills, though, most of
   the clients to whom I show those programming "tricks" INSTANTLY
   see the light and are able to make HUGE leaps forward.

   More importantly, though, the realization that "there are more
   things in assembly-language software than are dreamt of in their
   philosophy" seems to rekindle their interest in programming...
   The same guys who were content to let their skills stagnate for
   years suddenly start telephoning to tell me about the exciting
   new programming techniques they've "discovered", and when I make
   a followup visit six months after the initial one, I usually find
   that their newest code is SIGNIFICANTLY improved over the crap
   they used to write.

   So, to get back to my point...

   Programming languages are just like all human languages; it's
   possible (and maybe even desirable) to learn the vocabulary and
   grammar on your own, but in order to become FLUENT, you need to
   have a strong grasp on the language's idioms, dialects, and
   subtle nuances... And to accomplish that, you need to actually
   interact with people who already SPEAK the language.

   Just my opinion; I could be wrong.

   -Andy

=== Andrew Warren - EraseMEfastfwdspam_OUTspamTakeThisOuTix.netcom.com
=== Fast Forward Engineering - Vista, California
===
=== Custodian of the PICLIST Fund -- For more info, see:
=== www.geocities.com/SiliconValley/2499/fund.html

1997\03\14@103445 by John Payson
picon face
> For instance, how long do you suppose it would take a beginner to
> come up with the following extremely-useful code fragments without
> ever having seen them used in someone else's code?

How about this one [figure out what it does yourself :-)]

       movf    Num1,w
       movwf   Result

       movf    Num2,w
       subwf   Result,w
       btfss   C
        subwf  Result,f

       movf    Num3,w
       subwf   Result,w
       btfss   C
        subwf  Result,f

Anyone know of any smaller way?

1997\03\14@114956 by Andrew Warren

face
flavicon
face
John Payson <PICLISTspamspam_OUTMITVMA.MIT.EDU> wrote:

> How about this one [figure out what it does yourself :-)]
>
>         movf    Num1,w
>         movwf   Result
>
>         movf    Num2,w
>         subwf   Result,w
>         btfss   C
>          subwf  Result,f
>
>         movf    Num3,w
>         subwf   Result,w
>         btfss   C
>          subwf  Result,f
>
> Anyone know of any smaller way?

John:

Does this work?

       MOVF    NUM1,W
       SUBWF   NUM2,W

       MOVF    NUM1,W
       SKPC
       MOVF    NUM2,W

       MOVWF   RESULT

       SUBWF   NUM3,W
       SKPC
       ADDWF   RESULT

-Andy

=== Andrew Warren - @spam@fastfwdKILLspamspamix.netcom.com
=== Fast Forward Engineering - Vista, California
===
=== Custodian of the PICLIST Fund -- For more info, see:
=== www.geocities.com/SiliconValley/2499/fund.html

1997\03\14@122420 by Alan G. Smith

flavicon
face
Andy said in response to John:
> Does this work?
>
>         MOVF    NUM1,W
>         SUBWF   NUM2,W
>         MOVF    NUM1,W
...

Perhaps I just don't understand.  But wouldn't the third instruction destroy
all of the work of the first two?

+---------------
| Alan G. Smith
| KILLspamagsKILLspamspampoboxes.com
| http://www.innovatus.com/ags

1997\03\14@134347 by Andrew Warren

face
flavicon
face
Alan G. Smith <RemoveMEPICLISTTakeThisOuTspamMITVMA.MIT.EDU> wrote:

> Andy said in response to John:
> > Does this work?
> >
> >         MOVF    NUM1,W
> >         SUBWF   NUM2,W
> >         MOVF    NUM1,W
> ...
>
> Perhaps I just don't understand.  But wouldn't the third instruction
> destroy all of the work of the first two?

Alan:

Glad you asked.  Here... I'll explain.

First, though, a warning:

   IF YOU WANT TO FIGURE OUT ON YOUR OWN WHAT JOHN PAYSON'S CODE
   DOES, PLEASE READ NO FURTHER.

The rest of you can scroll down.

1997\03\16@235716 by tjaart

flavicon
face
Andrew Warren wrote:
{Quote hidden}

Touche! I visited a major local company some time ago and discovered
that they use a compiler that I personally found *many* bugs in.
(Even more than MPLABC <G>)

{Quote hidden}

I agree - as long as you know the basics before you start making
speeches. Otherwise you'd be asking people for a pre-written speech
because you don't want to reinvent the wheel.... Geez, how *that* one
really grates me!

--
Friendly Regards

Tjaart van der Walt
TakeThisOuTtjaartEraseMEspamspam_OUTwasp.co.za
_____________________________________________________________
| Another sun-deprived R&D Engineer slaving away in a dungeon |
|             WASP International  http://wasp.co.za           |
|             GSM and GPS value-added applications            |
|  Voice : +27-(0)11-622-8686   |   Fax : +27-(0)11-622-8686  |
|_____________________________________________________________|

1997\03\17@214545 by Steve Hardy

flavicon
face
I have added comments to each line...


> From: Andrew Warren <RemoveMEfastfwdspamTakeThisOuTIX.NETCOM.COM>
>
> John Payson <PICLISTEraseMEspam.....MITVMA.MIT.EDU> wrote:
>
> > How about this one [figure out what it does yourself :-)]
> >
> >         movf    Num1,w      w = n1
> >         movwf   Result      r = n1
> >
> >         movf    Num2,w      w = n2
> >         subwf   Result,w    w = n1 - n2
> >         btfss   C
> >          subwf  Result,f    if (n2 > n1) r = n1 - (n1 - n2) = n2
> >
> >         movf    Num3,w      w = n3
> >         subwf   Result,w    r = r - n3 = max(n1,n2) - n3
> >         btfss   C
> >          subwf  Result,f    if (n3 > max(n1,n2)) r = n3

thus result = max((unsigned chars) n1, n2, n3)

{Quote hidden}

                                 r = min(n2,n1) + n3 - min(n2,n1) = n3

thus result = min(n1, n2, n3).  Not exactly the same, Andy, but we
assume you meant SKPNC instead of SKPC.  I know I forget this
wierd Microchip convention all the time -- I'm so used to carry
meaning the answer was negative after a subtraction.

This is reminiscent of the XOR technique of swapping 2 or more bytes.
It basically works because there are operations (op) such that given
Z = X op Y, then if you know any two of X, Y or Z, then the third
value can be reconstructed.  ADD, SUB and XOR are such op's.  AND
and OR, on the other hand, fundamentally destroy information.

Regards,
SJH
Canberra, Australia

PS: I missed your subsequent post with 'the answer' so forgive me
if I'm correcting an already corrected oversight.

1997\03\19@020028 by Andrew Warren

face
flavicon
face
Steve Hardy <EraseMEPICLISTspamMITVMA.MIT.EDU> wrote:

> [John Payson's aesthetically pleasing code produces]
> max((unsigned chars) n1, n2, n3)
> ....

> [and Andy Warren's ugly-yet-shorter code produces] min(n1, n2, n3).
> Not exactly the same, Andy, but we assume you meant SKPNC instead
> of SKPC.

   No, I meant SKPC... I must have misread John's code; I thought
   it found the minimum.

   My apologies to anyone who was misled by my message; as Steve
   points out, you can make my code find the maximum, or make
   John's code find the minimum, by simply changing his BTFSSs to
   BTFSC or my SKPCs to SKPNCs.

   -Andy

=== Andrew Warren - RemoveMEfastfwdEraseMEspamEraseMEix.netcom.com
=== Fast Forward Engineering - Vista, California
===
=== Custodian of the PICLIST Fund -- For more info, see:
=== www.geocities.com/SiliconValley/2499/fund.html

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...