Searching \ for '[PIC:] Lookup table modifies PCLATH: How to restor' 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/memory.htm?key=table
Search entire site for: 'Lookup table modifies PCLATH: How to restor'.

Exact match. Not showing close matches.
PICList Thread
'[PIC:] Lookup table modifies PCLATH: How to restor'
2003\08\15@114224 by Ed Sutton

flavicon
face
I am a PIC newbie struggling with lookup tables and PCLATH on a 16f877.

My lookup table returns the expected result when the table crosses page
bounderies.  My question is, what approach can I use to restore the original
value of PCLATH before the next CALL takes off into hyperspace?

I have been reading the following.

www.piclist.com/techref/microchip/pages.htm
http://www.piclist.com/techref/microchip/tables.htm

Here are my code fragments:

;************
;My main loop
;************

SUBI16  int16FeedbackTemp, MAX_SETPT_A2DCOUNTS
movf    int16FeedbackTemp+1,W
call    ACountsToDegC

;************
;My lookup table
;************
ACountsToDegC:
               addlw   LOW aCountsTodegC
               movwf   temp
               movlw   HIGH aCountsTodegC
               skpnc
               addlw   1
               movwf   PCLATH
               movf    temp,W
               movwf   PCL

;                       ----------        -----------
;                       TABLE OUTPUT      TABLE INPUT
;                       Temp DegC         A2D Count
;                       Lookup            Index
;                       ----------        -----------
               radix dec
aCountsTodegC:  retlw   255             ; 000   0x00
               retlw   255             ; 001   0x01
               retlw   251             ; 002   0x02
               retlw   250             ; 003   0x03


Thanks in advance for any tips or suggestions,

-Ed

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@121542 by Olin Lathrop

face picon face
> My lookup table returns the expected result when the table crosses page
> bounderies.  My question is, what approach can I use to restore the
> original value of PCLATH before the next CALL takes off into hyperspace?

The whole code including the lookup table indexing should be treated like
any other subroutine in terms of PCLATH.  You do have to take special care
since subroutine is returning a value in W, but that's not a paging issue.
You don't need to set/restore PCLATH around the call if it's in the same
module (same code page), but you should if it's in a different module
(might not be same code page).

See my GCALL (global call) macro in STD.INS.ASPIC at
http://www.embedinc.com/pic.  It does the set/restore PCLATH (and a few
other things) around a call.  However, this macro trashes W from the
subroutine.  See the GCALLR and GCALLWR macros for examples of how to get
the W return value and still deal with paging issues.

> ACountsToDegC:
>                 addlw   LOW aCountsTodegC

Add to what?  W hasn't been set to anything yet.

>                 movwf   temp
>                 movlw   HIGH aCountsTodegC
>                 skpnc
>                 addlw   1
>                 movwf   PCLATH
>                 movf    temp,W
>                 movwf   PCL

You need to comment your code.  This is atrocious.

Here's how I do this kind of table lookup on a PIC16:

    movlw   high table     ;get table start address high byte
    movwf   pclath         ;init jump address high byte
    movlw   low table      ;get table start address low byte
    addwf   tindex, w      ;make jump address low byte
    skip_ncarr             ;no carry into high byte ?
    incf    pclath         ;propagate the carry
    movwf   pcl            ;jump to the selected table entry


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@124501 by Ed Sutton

flavicon
face
Olin,

Thank you for your reply.

> You don't need to set/restore PCLATH around the
> call if it's in the same module (same code page), but you should if
> it's in a different module (might not be same code page).

I think I do not yet understand enough to ask the proper question.  Here's
what I am seeing in the simulator:

0 - Before lookup call, PCLATH=0x07
1 - Then I call a lookup table subroutine with index in W.  The call is in
the same code page. The table spans a code page.
2 - The table spans the 0x0800 page boundary. After the retlw, PCLATH=0x08
3 - Execute instruction following call to lookup table subroutine ,
PCLATH=0x08
4 - The next call encountered executes code that does not exist in the
0x0800 page

I assumed the next call went nuts because the lookup routine returned with
a modified PCLATH.  Does this make sense?

> See my GCALL (global call) macro in STD.INS.ASPIC at

I will take a look up at your GCALL macro.  Maybe I will see the light then.

Thanks again for your help.

-Ed

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@125751 by Olin Lathrop

face picon face
> 0 - Before lookup call, PCLATH=0x07
> 1 - Then I call a lookup table subroutine with index in W.  The call is
> in the same code page. The table spans a code page.

There's the problem.  Why does it span a code page boundary?  You were
using a single byte table index, implying that the table only has at most
256 entries.  Why does that need to span a code page?  It sounds like a
bug in the linker control file, which may cause additional problems if
other things unexpectedly cross page boundaries too.

If you download my PIC development environment at
http://www.embedinc.com/pic, you'll get a bunch of linker control files
that don't let individual modules cross page boundaries.

In either case, you still need to set/restore PCLATH around external
calls, whether they perform a table lookup or not.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@150727 by Andrew Warren

flavicon
face
Olin Lathrop <spam_OUTPICLISTTakeThisOuTspammitvma.mit.edu> wrote, quoting Ed Sutton:

> > ACountsToDegC:
> >     addlw   LOW aCountsTodegC
>
> Add to what?  W hasn't been set to anything yet.

   Olin, please.

   Have you used PIC lookup-table subroutines before?  Are you
   claiming not to recognize that the table offset is being passed
   to the subroutine in the W register?

> >    movwf   temp
> >    movlw   HIGH aCountsTodegC
> >    skpnc
> >    addlw   1
> >    movwf   PCLATH
> >    movf    temp,W
> >    movwf   PCL
>
> You need to comment your code.  This is atrocious.

   It is not.

   As you know perfectly well, the code Ed shows is a very standard
   "16-bit plus 8-bit" addition routine. Anyone capable of helping
   Ed with his question about restoring PCLATH would also recognize
   and understand the addition code at a glance; I don't see how
   commenting each line of it would be either necessary or useful.

   -Andy

=== Andrew Warren -- .....aiwKILLspamspam@spam@cypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@194348 by Olin Lathrop

face picon face
>     Have you used PIC lookup-table subroutines before?  Are you
>     claiming not to recognize that the table offset is being passed
>     to the subroutine in the W register?

Actually I hadn't noticed that.  I just figured it was a bug.  It makes
sense now that you mention it, but this just underscores the importance of
documenting a subroutine's interface.

>     As you know perfectly well, the code Ed shows is a very standard
>     "16-bit plus 8-bit" addition routine.

Well he was having problems, so my assumption was specifically that it was
NOT the correct code that is known to work until inspected and found to be
so.

>     Anyone capable of helping
>     Ed with his question about restoring PCLATH would also recognize
>     and understand the addition code at a glance;

Yes, Andy, I am quite capable of figuring out what each instruction is
for.  The point is that I nor anyone else should have to figure it out.
It also helps others help him if the intent is known.  When I write code,
I put comments in there just as the example snippet I posted.  This is
something I do whether I'm posting to the list or writing something for
myself that I don't think anyone else will ever see.  Anything less would
be irresponsible and unwise.  Teaching this to the original poster is far
more important than showing him the correct table lookup procedure,
although I attempted to do both.

> I don't see how
> commenting each line of it would be either necessary or useful.

I frankly can't believe I'm hearing this from a computing professional.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@211350 by Jim Tellier

picon face
Ok, I can't help but throw my $0.02 into this... aside from the actual code
in question, this is more to the issue of commenting code.   (BTW, I speak
with 32 years of SW Engr experience in myriad assembler and HLL contexts).
The Importance of commented code *cannot* be disputed.  Period.  HOWEVER, I
*always* take issue with the concept of "commenting as you write it"!
IMHO, it is an order of magnitude more useful to comment it *after* it has
been debugged and Proven.   When someone (e.g. neophyte, expert, whatever)
writes & comments a piece of code, discovers that it doesn't work, then asks
buddy/coworker/newsgroup/FAE/PICLIST/his mama or whomever, to "please take a
look at this and see if you can give me a clue as to why it doesn't do what
I expect".... I've seen many times that the comments (describing the INTENT
of the programmer) simply obfuscate the actual problem manifested in the
CODE: i.e., the comments tell you what the code is *supposed* to do, thus
adding a  certain "credibility factor" to the actual code statements before
the reviewer.   I've seen this phenomenon manifest itself in countless code
reviews, at all levels, to the point where if I'm conducting a code review
(of all but the most mundane HLL stuff), I ask for code without comments.
The time to comment code, I contend, is after it's been completed and
tested.
Hmm... didn't mean for this to be a diatribe of some sort! :^)  Perhaps it
should have been OT!
Jim
{Original Message removed}

2003\08\15@214318 by Mike Singer

picon face
Andrew Warren wrote:
> > >    movwf   temp
> > >    movlw   HIGH aCountsTodegC
> > >    skpnc
> > >    addlw   1
> > >    movwf   PCLATH
> > >    movf    temp,W
> > >    movwf   PCL
> >
> > You need to comment your code.  This is atrocious.
>
>     It is not.
>
>     As you know perfectly well, the code Ed shows is a very standard
>     "16-bit plus 8-bit" addition routine. Anyone capable of helping
>     Ed with his question about restoring PCLATH would also recognize
>     and understand the addition code at a glance; I don't see how
>     commenting each line of it would be either necessary or useful.


I see how commenting each line of it is necessary and useful:

Most of Piclisters don't know that "the code Ed shows is a very
standard "16-bit plus 8-bit" addition routine.
But they will know if the code would be well commented. It's a
matter of ethics to let all the members understand what's going on.

(not to say about the necessity to follow some standard rules
in coding, in engineering, in anything without permanently wasting
time on discussion are they really necessary and useful)


Regards,

Mike.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@224201 by Jinx

face picon face
> *always* take issue with the concept of "commenting as you
> write it"! IMHO, it is an order of magnitude more useful to
> comment it *after* it has been debugged and Proven

This is very useful both at the time of completion (for one's
own satisfaction that there are no loose ends) and for
future reference. In 6 months time, code with no or just
general commenting has to be thought through all over
again just to get up to speed reminding yourself why it
was written that way

> I've seen many times that the comments (describing the
> INTENT of the programmer) simply obfuscate the actual
> problem manifested in the CODE

When you're carried away in the moment, fuzzy thinking that
ends up in the code will also appear in any specific comments
written at the time. The error of assuming that such commented
code "works" and doesn't need reviewing can lead to much
frustration and fruitless debugging time. As code errors can,
and often do, appear in the last place you'd think to look, it's
sensible practice to periodically take a break from making
new code and examine and justify every instruction (and
comment) of what's already been done. It may seem a waste
of time but reviewing your own work is perilous at the best of
times and you really need to focus

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@230702 by Dave Tweed

face
flavicon
face
Jim Tellier <jimtellierspamKILLspamCOX.NET> wrote:
> The Importance of commented code *cannot* be disputed. Period.

I absolutely agree.

> HOWEVER, I *always* take issue with the concept of "commenting as you
> write it"! IMHO, it is an order of magnitude more useful to comment it
> *after* it has been debugged and Proven.

I actually disagree here.

I tend to do top-down design, bottom-up implementation, so I actually
write most of the comments *first*, as I develop the module structure
and interfaces I want to use in the project. If this is done rigorously
enough, writing the actual code ends up being very straightforward.

And I absolutely abhor line-by-line comments, in assembly or any other
language. More often than not, they just restate what the line of code
says without adding any higher-level insight as to what the overall
intention or functionality is.

A block comment at the top of each basic block is much more readable
and tends to force a certain amount of discipline on the writer.
It also falls out that way naturally if you're basically converting
pseudocode into real code.

-- Dave Tweed

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\15@233347 by Jinx

face picon face
> And I absolutely abhor line-by-line comments, in assembly
> or any other language. More often than not, they just restate
> what the line of code says without adding any higher-level
> insight as to what the overall intention or functionality is

Could I suggest that this is the author's fault, as a comment
in itself can only be how it's written. I agree that verbose text
(waffle) can make code harder to follow, and prefer a fullish
subroutine title + short paragraph and special commenting
which doesn't, as you say, just put the mnemonics into plain
English. The choice of names for labels, macros and so on
is commenting too and helps make code self-explanatory

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\08\16@083319 by Olin Lathrop

face picon face
Jim Tellier wrote:
> The Importance of commented code *cannot* be disputed.  Period.
> HOWEVER, I
> *always* take issue with the concept of "commenting as you write it"!
> IMHO, it is an order of magnitude more useful to comment it *after* it
> has been debugged and Proven.

I strongly disagree with this.  Show me someone who comments code "later",
and I'll show you lots of code that hasn't been touched in a long time
still waiting for "later".  I also suspect that much of this code will
have hidden bugs.

To me, comments are in integral part of the development process.  My
overall productivity would be a lot less if I was not allowed to comment
code until after the project is working, and I'd be making stupid mistakes
along the way.

How many times have you had a case where there could be several ways to
express the same value in a global variable?  Without documenting the
format where it is defined, you could easily assume a different format
when you're writing a module that uses the variable a few days after
writing the module that sets it.  The same principle holds for any
interface between two separate sections of code, like how a subroutine is
called.  Do you really want to have to look thru the executable code of a
subroutine to figure out exactly how a parameter is passed to it and what
the format needs to be?

Think of it another way.  As you are writing the executable code you are
as close to the details as you're going to get for that code.  There are
tradeoffs and other low level decisions you are juggling in your mind as
you write that code.  That's the time to write them down, if for no other
reason so that you don't have to try to remember them as you go to the
next chunk of code or module.  It's like keeping a design notebook, except
that you don't write it in a separate place.  That's much better since it
will more likely be kept up to date and will always be available along
with the designed product itself.

> When someone (e.g. neophyte, expert,
> whatever) writes & comments a piece of code, discovers that it doesn't
> work, then asks buddy/coworker/newsgroup/FAE/PICLIST/his mama or
> whomever, to "please take a look at this and see if you can give me a
> clue as to why it doesn't do what I expect".... I've seen many times
> that the comments (describing the INTENT of the programmer) simply
> obfuscate the actual problem manifested in the CODE: i.e., the comments
> tell you what the code is *supposed* to do, thus adding a  certain
> "credibility factor" to the actual code statements before the reviewer.

You are making the mistake I see too often, which is thinking of comments
as something "extra" that can be added to code like a little extra salt to
soup to make it just right.  Comments are an integral and essential part
of the code.  Any review process therefore needs to review the comments as
much as the executable part of the code.  I suppose sloppy reviewers could
check the comments more than the executable part of the code, but that
only shows that reviewing code is a skill, and there are bad reviewers
just like there are bad programmers.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@085230 by Olin Lathrop

face picon face
> And I absolutely abhor line-by-line comments, in assembly or any other
> language. More often than not, they just restate what the line of code
> says without adding any higher-level insight as to what the overall
> intention or functionality is.

I agree that every line shouldn't be commented just 'cause (Andy made it
sound like I said this, but I hadn't).  However, end of line comments have
their own particular purpose and are very useful if done properly.  I
certainly wouldn't want an absolute rule about how many lines should have
end of line comments, but if less then half your code lines have them, you
probably aren't using them effectively.

I think the reason end of line comments sometimes have a bad reputation is
that there are so many bad end of line comments out there.  Good
commenting of code is an important but separate skill from thinking out
the executable part of the code.  Unfortunately this skill is all too
often neglected when programming is taught.

For example:

    incf   cnt     ;add 1 to CNT

is a useless comment.  Comments are way to important to waste duplicating
the assembler manual.  You should assume anyone reading your code is
familiar with the language, and therefore already knows that this
instruction will add 1 to CNT.  The end of line comment gives you an
opportunity to explain the next level higher intent of the instruction.
For example:

    incf   cnt     ;count one more button press

is a good comment.  It tells you something you couldn't otherwise learn
from just this instruction.  If you follow this concept, you will find
useful things to say about most lines of code.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@093319 by Mike Singer

picon face
Olin Lathrop  wrote:
+++++++++++++++++++++++++++++++
For example:

    incf   cnt     ;add 1 to CNT

is a useless comment.  Comments are way to important to waste
duplicating the assembler manual.  You should assume anyone reading your
code is familiar with the language, and therefore already knows that
this instruction will add 1 to CNT.  The end of line comment gives you
an opportunity to explain the next level higher intent of the
instruction. For example:

    incf   cnt     ;count one more button press

is a good comment.  It tells you something you couldn't otherwise learn
from just this instruction.  If you follow this concept, you will find
useful things to say about most lines of code.
+++++++++++++++++++++++++++++++

Really good example, thanks.

Mike.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@104751 by Sergio Masci

picon face
Jim Tellier wrote:

> Ok, I can't help but throw my $0.02 into this... aside from the actual code
> in question, this is more to the issue of commenting code.   (BTW, I speak
> with 32 years of SW Engr experience in myriad assembler and HLL contexts).
> The Importance of commented code *cannot* be disputed.  Period.

>HOWEVER, I
> *always* take issue with the concept of "commenting as you write it"!

I completely agree with you. It is obvious that this code

   rlf    tmp_acc,w
   rlf    acc
   clrf    tmp_acc
   movlw    1
   andwf    acc

does a 15 bit unsigned shift right of an 8 bit variable that has been
temporarily promoted to 16 bits

and it is obvious that the author of

   movf    indx,w
   addwf    base,w
   movwf    fsr
   movf    indf,w

actually ment to write

   movf    indx,w
   addlw    base
   movwf    fsr
   movf    indf,w

It is also obvious that IRP is valid and should not be touched and that adding
indx to base will never produce a carry.

> IMHO, it is an order of magnitude more useful to comment it *after* it has
> been debugged and Proven.

why bother, you know it works.

Regards
Sergio Masci

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@121135 by Joe Farr

flavicon
face
Well said,

Commenting after the event should be a criminal offence. It is on
projects I run.

As a system architect in charge of the development team, uncommented
code goes stright in the bin. The number of mistakes that I've found
over the years because the comments and the code don't seem to mean the
same thing. You have to read them both. You often see comments that the
developer wrote, almost as if they were thinking and typing. Then you
see the code and quickly discover some minor logic problem.

Comments don't have to be 'war and peace', just factual and to the
point.
If a block of code caused you to have to think harder when writing it,
comment it all the more. Show your thought process. It's not like it
uses and code space these days.
And it's also no good saying things like ' Set Accumilator = 13 - what
use is that - I can read the code. ' Set Accumilator to an ASCII TAB is
a whole lot better. Ah... a mistake. Should it be TAB or CR as the code
and the comment don't match. That would be lost when you go back and add
your comments. So now, your code is wrong and the comments supports it.
It could take a month of Sundays for somebody to find this. I accept
that it's more than possible to enter the code and the comment and still
make a mistake, but your more likely to create a duff comment when you
go back and do it later.

On virtually all projects I've worked on, cost is paramount. The
customer will never give you enough time to go back and comment the
code. Why does he care. He's expecting a system that works and is
maintainable.

Also, what happens if you get hit by a bus just before 'code commenting
day'.

I do sometimes go back over my code and add additional comments here and
there. It's also a great way of checking your own code for bugs. It's
amazing how quickly the original ideas behind even a small block of code
can be forgotten.


> {Original Message removed}

2003\08\16@122348 by Jim Tellier

picon face
> Dave Tweed wrote:
> A block comment at the top of each basic block is much more readable
> and tends to force a certain amount of discipline on the writer.
> It also falls out that way naturally if you're basically converting
> pseudocode into real code.
   In fact, I should have been more specific, distinguishing "Design
Comments" (those block comments that begin a method or major block of code)
from "line end comments".   I *do*, in fact, write my Design Intentions in
the form of a block comment, but I only go back to add line-end comments
after functionality has been proven (except in a few cases where the code is
*so* obtuse that I might not remember (as Olin, I think, pointed out)
something like how a particular global is being used in this particular
case, or to be careful of some instructions' side-effects, etc.  In general,
(as I think Jinx pointed out) going back over the code at such later time
provides the oppty to do one's own review (which one might be less inclined
to do if it's all neatly commented from the start).
This approach works for me...

Also...
> Olin Lathrop  wrote:
> +++++++++++++++++++++++++++++++
> For example:
>
>      incf   cnt     ;add 1 to CNT
>
> etc. etc.

  Good example.  I cringe when I see (or write :^) comments that are
redundant to the code itself!

Jim



> http://www.piclist.com hint: The list server can filter out subtopics
> (like ads or off topics) for you. See http://www.piclist.com/#topics

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@141451 by Bob Axtell

face picon face
Olin, this is the best explanation I've ever seen on the importance of
commenting code. Well done!

--Bob


At 08:31 AM 8/16/2003 -0400, you wrote:
{Quote hidden}

--------------
Bob Axtell
PIC Hardware & Firmware Dev
Tucson, AZ
1-512-219-2363

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@142332 by Dave Tweed

face
flavicon
face
Olin Lathrop <.....olin_piclistKILLspamspam.....EMBEDINC.COM> wrote:
> I wrote:
> > And I absolutely abhor line-by-line comments, in assembly or any other
> > language. More often than not, they just restate what the line of code
> > says without adding any higher-level insight as to what the overall
> > intention or functionality is.
>
> For example:
>
>      incf   cnt     ;count one more button press
>
> is a good comment. It tells you something you couldn't otherwise learn
> from just this instruction.

I think we're basically in agreement, but I would argue that even this
comment is completely redundant if the 'cnt' variable has a sufficiently
descriptive name and is properly documented where it is declared/defined.

> If you follow this concept, you will find useful things to say about
> most lines of code.

The other objection that I have with end-of-line comments is that they
really get in the way when subsequently modifying the code. They require
constant fiddling and reformatting, and this tends to interrupt the train
of thought regarding the actual code. Even in those rare situations where
every line requires a comment, I firmly believe that the comments and code
should be on separate lines.

-- Dave Tweed

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@144018 by Olin Lathrop

face picon face
Dave Tweed wrote:
> The other objection that I have with end-of-line comments is that they
> really get in the way when subsequently modifying the code. They require
> constant fiddling and reformatting, and this tends to interrupt the train
> of thought regarding the actual code.

With good comments, you only need to "fiddle" with those attached to
whatever you are changing.  In that case it is a Very Good Thing that you
also need to adjust the comments.  That gives you two chances to catch an
incompatibility in the modification, one from the executable code and one
from the comments.

> Even in those rare situations where
> every line requires a comment, I firmly believe that the comments and code
> should be on separate lines.

I guess we'll just have to disagree on this one.  You're one of the few
people on this list that has had to make substantial modifications to PIC
code I wrote years earlier.  Did you feel the extensive end of line comments
were more of an annoyance or a help in understanding the code in the first
place?


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@152528 by Dave Tweed

face
flavicon
face
Olin Lathrop <EraseMEolin_piclistspam_OUTspamTakeThisOuTEMBEDINC.COM> wrote:
> Dave Tweed wrote:
> > Even in those rare situations where every line requires a comment, I
> > firmly believe that the comments and code should be on separate lines.
>
> I guess we'll just have to disagree on this one.  You're one of the few
> people on this list that has had to make substantial modifications to
> PIC code I wrote years earlier.  Did you feel the extensive end of line
> comments were more of an annoyance or a help in understanding the code
> in the first place?

Content: somewhat helpful, somewhat redundant
Placement: annoyance

In the code I modified, however, I tried to maintain the existing style,
especially if the changes were limited to a few instructions here and
there.

But where I wrote significant blocks of new code, you'll find that there
are few (if any) end-of-line comments.

-- Dave Tweed

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@155733 by Mike Singer

picon face
Olin Lathrop wrote:
> Even in those rare situations where every line requires a comment,
> I firmly believe that the comments and code should be on separate
> lines.
>
> I guess we'll just have to disagree on this one...
> Did you feel the extensive end of line comments were more of an
> annoyance or a help in understanding the code in the first place?


With usual 1280 X 1024 (768 X 1024) screen resolution
"end of line commenting" is fitted much better on the screen:
less scrolling etc.

Anyway, the less number of lines - the more readable code is.

(Just my 2 local monetary units)

Best Regards.

Mike.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\16@180549 by Olin Lathrop

face picon face
> Olin Lathrop wrote:

No, I didn't.  This was written by Dave Tweed:

>> Even in those rare situations where every line requires a comment,
>> I firmly believe that the comments and code should be on separate
>> lines.

This part I did write:

>> I guess we'll just have to disagree on this one...
>> Did you feel the extensive end of line comments were more of an
>> annoyance or a help in understanding the code in the first place?


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\08\17@004314 by Mike Singer

picon face
Sorry, I incorrectly cut and pasted text.

Mike.

Olin Lathrop wrote:

{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

2003\08\18@193647 by Andrew Warren

flavicon
face
Olin Lathrop <PICLISTspamspam_OUTmitvma.mit.edu> wrote, quoting me:

> > I don't see how commenting each line of [an 8-bit + 16-bit
> > addition] would be either necessary or useful.
>
> I frankly can't believe I'm hearing this from a computing
> professional.

Olin:

I posted this to the piclist about 5 years ago:

   http://tinyurl.com/ketv

and you've seen hundreds of lines of my (commented) code here on the
list, too.  Here's a random example from a year ago:

   http://tinyurl.com/ketr

It should be obvious that I don't advocate the writing of comment-
free code in general; what I wrote was directed at this specific
case.

-Andy

P.S.  The TinyURL links point to http://www.geocities.com/
     SiliconValley/2499/essays.html#COMMENTS and
     www.piclist.com/techref/postbot.asp?by=time&
     id=piclist/2002/08/22/194000a&tgt=post

=== Andrew Warren -- @spam@aiwKILLspamspamcypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email KILLspamlistservKILLspamspammitvma.mit.edu with SET PICList DIGEST in the body

2003\08\18@214001 by Sabachka

flavicon
face
I'm not sure if I am the only one, but the page this leads to displays,
as far as I can tell, incorrectly. I'm guessing from a quick skim of
the html, that the problem lies in the use of tables for the rendering
of the pages layout. Not sure, as I didn't spend much time fiddling with
it.

It would, however, be good regardless to try switching over to CSS. A
very large number of browsers support it and those that do not, if the
page is laid out correctly, render the text quite legibly (for those
with console only browsers, etc).

I loved the posts regarding Brent and his new car. :)

Sabachka

On Mon, Aug 18, 2003 at 04:38:41PM -0700, Andrew Warren wrote:
...
> I posted this to the piclist about 5 years ago:
>     http://tinyurl.com/ketv
> and you've seen hundreds of lines of my (commented) code here on the
...

--
RemoveMEsabachka-piclistTakeThisOuTspamoddmagic.net

King's Law of Clues :  Common sense is inversely proportional to
the academic intelligence of the person concerned.

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spamBeGonelistservspamBeGonespammitvma.mit.edu with SET PICList DIGEST in the body

2003\08\18@221658 by Andrew Warren

flavicon
face
Sabachka <TakeThisOuTPICLISTEraseMEspamspam_OUTmitvma.mit.edu> wrote:

> I'm not sure if I am the only one, but the page this leads to
> displays, as far as I can tell, incorrectly. .... It would,
> however, be good regardless to try switching over to CSS.

   I apologize, Sabachka, and I appreciate the advice.  However,
   that page was last updated over four years ago, and it's not
   going to be changed.  In fact, I'm surprised every time I point
   my web browser to it and see that it's still up.

   I couldn't find the original post in the piclist archive; if I'd
   been able to, I would have just linked to it instead.

> I loved the posts regarding Brent and his new car. :)

   Yeah, Brent actually turned out to be a good kid; he was
   just a little excitable back then.

   -Andy

=== Andrew Warren -- RemoveMEaiwspamTakeThisOuTcypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservEraseMEspam.....mitvma.mit.edu with SET PICList DIGEST in the body

2003\08\19@005517 by Sabachka

flavicon
face
On Mon, Aug 18, 2003 at 07:18:30PM -0700, Andrew Warren wrote:
> Sabachka <EraseMEPICLISTspammitvma.mit.edu> wrote:
> > I'm not sure if I am the only one, but the page this leads to
> > displays, as far as I can tell, incorrectly. .... It would,
> > however, be good regardless to try switching over to CSS.
> I apologize, Sabachka, and I appreciate the advice.  However,
> that page was last updated over four years ago, and it's not
> going to be changed.  In fact, I'm surprised every time I point
> my web browser to it and see that it's still up.

No worries! I've found a lot of web pages are setup such because people
just don't know better about what is available now for hypertext
documents. If it's 4+ years and counting ... wow ... yeah, not even
worth it if you're amazed it is still up.

Good info, still. Thanks.
Sabachka

--
RemoveMEsabachka-piclistEraseMEspamEraseMEoddmagic.net

"There is no statute of limitations on stupidity."
       -- Randomly produced by a computer program called Markov3.

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestspam_OUTspamKILLspammitvma.mit.edu

2003\08\19@030249 by Wouter van Ooijen

face picon face
> > > I don't see how commenting each line of [an 8-bit + 16-bit
> > > addition] would be either necessary or useful.
> >
> > I frankly can't believe I'm hearing this from a computing
> > professional.

But I realy agree (and I am a computing professional, 15 y or so). The
point is that code with good comments is better than uncommented code,
but code with redundant comments is worse, and code with wrong comments
is purely evil.

But like in a lot of other discussions you can not attach a meanting to
(in this case) 'redundant' without a specific audience in mind. A
seasoned PIC programmer knows that INCF does not affect the carry, so
mentioning this in a comment is redundant (and commenting effort is
better spent on some other aspect). But for a beginner this is a gotcha,
so it should be mentioned. So don't expect code that is written by an
expert, to be optimally readable for an expert, to be likewise readable
for a beginner!

You should comment those aspects that are non-obvious for your intended
audience. I recall a pascal program that had comments like { constants
section } and { variables section }. It was written by a first-month
student, so this was perfectly valid because he was still struggling
with the overall structure of a pascal program. Lateron in my career I
read a coding standard that required such somments (actually multi-line
comment blocks). That is bad, because for a programmer with 1+ y
experience the overall structure of a pascal program should be easy, and
such comment blocks draw both writing effort and reader time away from
more important aspects of the program.

Wouter van Ooijen

-- -------------------------------------------
Van Ooijen Technische Informatica: http://www.voti.nl
consultancy, development, PICmicro products

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestTakeThisOuTspamspammitvma.mit.edu

2003\08\19@071923 by Bob Ammerman
picon face
> But like in a lot of other discussions you can not attach a meanting to
> (in this case) 'redundant' without a specific audience in mind. A
> seasoned PIC programmer knows that INCF does not affect the carry, so
> mentioning this in a comment is redundant (and commenting effort is
> better spent on some other aspect).

But of course even a seasoned pro might get stung during a port from 16F to
18F by the fact that on the 18F INCF does affect the carry. (This is one of
the few poor decisions mChip made on the 18F, IMHO).

Bob Ammerman
RAm Systems

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspamspamspamBeGonemitvma.mit.edu

2003\08\19@072822 by Wouter van Ooijen

face picon face
> > But like in a lot of other discussions you can not attach a
> meanting to
> > (in this case) 'redundant' without a specific audience in mind. A
> > seasoned PIC programmer knows that INCF does not affect the
> carry, so
> > mentioning this in a comment is redundant (and commenting effort is
> > better spent on some other aspect).
>
> But of course even a seasoned pro might get stung during a
> port from 16F to
> 18F by the fact that on the 18F INCF does affect the carry.
> (This is one of
> the few poor decisions mChip made on the 18F, IMHO).

You halfway got me there, but actually it (again) proves my point: know
your audience before you write comments (and know the intended audience
before you judge someone else's comments). And this applies to all
writing, not just comments. And of course it gets problematic when your
adience is diverse and/or unknown, like when posting on this list ;)

Wouter van Ooijen

-- -------------------------------------------
Van Ooijen Technische Informatica: http://www.voti.nl
consultancy, development, PICmicro products

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestKILLspamspammitvma.mit.edu

2003\08\19@073912 by Bob Ammerman

picon face
Yep!

Bob Ammerman
RAm Systems

From: "Wouter van Ooijen" <wouterSTOPspamspamspam_OUTVOTI.NL>


{Quote hidden}

--
http://www.piclist.com hint: To leave the PICList
spamBeGonepiclist-unsubscribe-requestSTOPspamspamEraseMEmitvma.mit.edu

2003\08\19@095536 by Scott Dattalo

face
flavicon
face
On Tue, 19 Aug 2003, Bob Ammerman wrote:

> > But like in a lot of other discussions you can not attach a meanting to
> > (in this case) 'redundant' without a specific audience in mind. A
> > seasoned PIC programmer knows that INCF does not affect the carry, so
> > mentioning this in a comment is redundant (and commenting effort is
> > better spent on some other aspect).
>
> But of course even a seasoned pro might get stung during a port from 16F to
> 18F by the fact that on the 18F INCF does affect the carry. (This is one of
> the few poor decisions mChip made on the 18F, IMHO).

The other short-sighted mistake was the address bit thrown away for
relative branches and relative calls.

At one point, I thought that 3 sets of FSR/INDF's was inadequate (why not
4?). It turns out though, I found it convenient to use 2 sets for the
non-interrupt code and the last set for interrupts.

Scott

--
http://www.piclist.com hint: To leave the PICList
KILLspampiclist-unsubscribe-requestspamBeGonespammitvma.mit.edu

2003\08\19@104508 by Bob Ammerman

picon face
Scott said:
> The other short-sighted mistake was the address bit thrown away for
> relative branches and relative calls.

What do you mean about a wasted bit?

RCALL encoding:

1101 1nnn nnnn nnnn

Target is PC + 2 + 2n

Same thing happens with BRA and conditional branches.

Scott said:
> At one point, I thought that 3 sets of FSR/INDF's was inadequate (why not
> 4?). It turns out though, I found it convenient to use 2 sets for the
> non-interrupt code and the last set for interrupts.

What do you use for a stack pointer?


Bob Ammerman
RAm Systems

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspamEraseMEmitvma.mit.edu

2003\08\19@122307 by Olin Lathrop

face picon face
> The other short-sighted mistake was the address bit thrown away for
> relative branches and relative calls.

What address bit was "thrown away"?  These instructions seem reasonable to
me.  The BRA and RCALL instructions contain 11 bits of relative offset.
That's pretty decent considering there are only 16 bits in the
instruction, and some of these bits of course must be used to distinguish
these instructions from others.

The 11 bits is a signed offset in instructions (not bytes) from the next
address, so each combination represents a meaningful instruction.  They
can branch or call to any instruction up to 1023 behind and 1024 after the
current instruction.  What exactly do you think is wasteful?

If you want the target to be anywhere in program memory, then you can use
the GOTO or CALL instructions.  These contain 20 bits of address
information but take two instruction words.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: To leave the PICList
@spam@piclist-unsubscribe-request@spam@spamspam_OUTmitvma.mit.edu

2003\08\19@123609 by Scott Dattalo

face
flavicon
face
On Tue, 19 Aug 2003, Bob Ammerman wrote:

> Scott said:
> > The other short-sighted mistake was the address bit thrown away for
> > relative branches and relative calls.
>
> What do you mean about a wasted bit?
>
> RCALL encoding:
>
> 1101 1nnn nnnn nnnn
>
> Target is PC + 2 + 2n

Umm - yes, you're right. Somehow I got it stuck in my head backwards...
Sorry for the confusion.

> Scott said:
> > At one point, I thought that 3 sets of FSR/INDF's was inadequate (why not
> > 4?). It turns out though, I found it convenient to use 2 sets for the
> > non-interrupt code and the last set for interrupts.
>
> What do you use for a stack pointer?

I don't use a stack pointer :). But I suppose if I did then 3 sets would
really be inadequate. I've manage to structure most of my subroutines so
that they require 1 or no parameter(s). For the few instances I need more
than one parameter, I go ahead and sacrifice a few global variables. (But
I have to agree, a dedicated extra data stack pointer would be handy.)

Scott

--
http://www.piclist.com hint: To leave the PICList
spamBeGonepiclist-unsubscribe-requestspamKILLspammitvma.mit.edu

2003\08\19@145242 by Olin Lathrop

face picon face
Scott Dattalo wrote:
> I don't use a stack pointer :). But I suppose if I did then 3 sets would
> really be inadequate.

I do use a stack and reserve an FSR exclusively as the stack pointer.  So
far 3 FSRs has worked out just fine for me.  One for the stack, one for
use by the interrupt routine (so that it doesn't have to save/restore an
FSR on interrupt), and one for the foreground code.

> I have to agree, a dedicated extra data stack pointer would be handy.

But they gave you one.  And an extra FSR on top of that.  And, you can
even use the stack pointer as a regular FSR if you don't want a stack
pointer.  Sounds like a good deal to me.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestspam_OUTspammitvma.mit.edu

2003\08\19@160529 by Scott Dattalo

face
flavicon
face
On Tue, 19 Aug 2003, Olin Lathrop wrote:

> Scott Dattalo wrote:
> > I don't use a stack pointer :). But I suppose if I did then 3 sets would
> > really be inadequate.
>
> I do use a stack and reserve an FSR exclusively as the stack pointer.  So
> far 3 FSRs has worked out just fine for me.  One for the stack, one for
> use by the interrupt routine (so that it doesn't have to save/restore an
> FSR on interrupt), and one for the foreground code.
>
> > I have to agree, a dedicated extra data stack pointer would be handy.
>
> But they gave you one.  And an extra FSR on top of that.  And, you can
> even use the stack pointer as a regular FSR if you don't want a stack
> pointer.  Sounds like a good deal to me.

And your point is...? How can you make conclusions about something you
never seen? I find that absolutely incredulous!

I use FSR0* and FSR1* for the non-interrupt code and FSR2* for the
interrupt code. Having two FSR's for shifting blocks of data around is
invaluable. Don't you just love MOVFF POSTINC0, POSTINC1 ?  The project
I'm using the 18F452 on is essentially a data collector/ router. There are
three streams of data coming in and two going out. The data streams are
all asynchronous and consequently need buffering. Hence, having the two
FSR's at my finger tips yields some efficient (and easy to read IMO) code!
I suppose I could temporarily stash one of the FSR?L/H's away and restore
it when I'm done, but that's 8 unnecessary instruction cycles; why write
sloppy, inefficient code if you can write clean efficient code!

Scott

--
http://www.piclist.com hint: To leave the PICList
TakeThisOuTpiclist-unsubscribe-request.....spamTakeThisOuTmitvma.mit.edu

2003\08\19@182744 by Bob Ammerman

picon face
A 4th FSR would be nice:

1 for source
2 for dest
3 for stack
4 for interrupt code

Bob Ammerman
RAm Systems

{Original Message removed}

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