Searching \ for 'command parsing hints sought' 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/index.htm?key=command+parsing
Search entire site for: 'command parsing hints sought'.

Truncated match.
PICList Thread
'command parsing hints sought'
1997\02\05@135057 by robin.bussell

picon face
Hello to all you pic list members who are giving me some great hints as
I lurk here! (and a big hooray! to microchip for producing some
wonderful components )

I was wondering if anyone would care to pass on some advice on coding
command processing efficiently.... I'm trying to pass serial commands to
a 16c84 and
get various things done accordingly, leaving aside the whole serial
protocol issue for the moment I'm wondering if there are any efficient
ways of implementing a
structure similar to the C switch statement.
Say I've recieved the first byte of a command, it's in a register called
COMND and
I  now want to test it's value against a set of opcodes. The way that
seems obvious to me is...


       MOVFW   COMND
       XORLW   VALUE1  ; Test against first value
       BTFSC   W,ZERO  ; check zero bit skip next if non zero
       GOTO    ROUTINE1;

       MOVFW   COMND
       XORLW   VALUE2
       BTFSC   W,ZERO
       GOTO    ROUTINE2

and so on...  Has anyone come across a different way of doing this? Any
shortcuts incolved if the command values are restricted to say 1,2,3,4,5
or a similar consecutive sequence ( maybe some lookup table shennanigins
possible?) All help will be much appreciated.
Thanks for letting me think out loud :-)
Cheers,
    Robin.

1997\02\05@151155 by Eric T. Brewer

flavicon
face
At 10:37 AM -0800 2/6/97, robin bussell wrote:
{Quote hidden}

Robin,

The solution to your problem is very simple. The low 8 bits of the PIC's
program counter (PC) is available as register 2. The following code shows
dispatching a command which is from 0 to 7. The handlers (such as
IRSend) need to in be in the current code page.

Hope it helps,
eric

PC              Equ     0x02    ; Program Counter
PCHigh          Equ     0x0A    ; High byte of PC for write

       MovF    Command,W       ; Make sure the command is in range
       AndLW   #0b1111_1000
       BrNZ    MainError       ; Command is out range - goto MainError

       MovLW   #0x01   ; Make sure the high bits of the PC point
       MovWF   PCHigh  ; to our current code page (not needed for 5x)
       MovF    Command,W       ; Dispatch the command
       AddWF   PC

       Goto    ModifyBit       ; 0 = Modify a bit in a register
       Goto    MainError       ; 1 = Command not immplemented
       Goto    PortDirection   ; 2 = Set port direction for Port A & B
       Goto    IRSend          ; 3 = Send stuff across IR
       Goto    IRParams        ; 4 = Setup IR transmit parameters
       Goto    IRLeader        ; 5 = IR leader info
       Goto    IRTrailer       ; 6 = IR trailer info
       Goto    MainError       ; 7 = Command not immplemented

1997\02\05@153232 by Dennis Frost

flavicon
face
How about this:

       movfw   COMND   ;get you command into w
               addwf   PCL             ;add offset to pc to generate
                               ;computed goto
       goto    Routine1
       goto    Routine2
       goto    Routine3
       goto    Routine4
       .....
       goto    RoutineX

Make sure you never give COMND a command value higher than the number of
goto
statements you have!

Cheers dennis

____________________________________________________
FROST - Electronic Design, Manufacture & Consulting.
Dennis Frost
Tel:   +27 331 965125
Cel:   +83 2275216
Email: spam_OUTdennis.frostTakeThisOuTspampixie.co.za
Pietermaritzburg, South Africa
Products:       Medical Motivational equipment
               Timers for food processing
               Random number generator
               Temperature controllers
____________________________________________________

PS Anyone looking for someone in the hardware/software development fields,
I am available. I intend leaving South Africa for the West coast of the US
early this year.



{Quote hidden}

1997\02\05@160634 by mike

flavicon
picon face
In message  <.....199702060030.WAA02629KILLspamspam@spam@phantom.pix.za> PICLISTspamKILLspamMITVMA.MIT.EDU
writes:
> How about this:
>
>         movfw   COMND   ;get you command into w
>                 addwf   PCL             ;add offset to pc to generate
>                                 ;computed goto
>         goto    Routine1
>         goto    Routine2
>         goto    Routine3
>         goto    Routine4
>         .....
>         goto    RoutineX
>
> Make sure you never give COMND a command value higher than the number of
> goto
> statements you have!
>

You should also clear the carry before the "addwf pcl"


Regards,


Mike Watson

1997\02\05@164818 by Andrew Warren

face
flavicon
face
Mayes uk <.....mikeKILLspamspam.....d-m-g.demon.co.uk> wrote:

> >         movfw   COMND   ;get you command into w
> >         addwf   PCL     ;add offset to pc to generate
> >                         ;computed goto
>
> You should .... clear the carry before the "addwf pcl"

Mike:

Nope... That's unnecessary, since the PIC's "addwf" command doesn't
care about the incoming carry bit.

-Andy

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

1997\02\05@164822 by Philippe TECHER

flavicon
face
On Wed, 5 Feb 1997 20:59, Mike Watson wrote

>You should also clear the carry before the "addwf pcl"

WHY ????

Read the PIC data sheet, the instruction ADDWF something is expected to do:
       W + F in (W or F) and that's all, there is no carry addition !

To avoid surprize, just take care about PCLATH register depending the PIC you
are using the final address will be a mix of high PCLATH bit and the address
you specify in the instruction, for 'C84:

ADDWF PCL -> final PC will be: 8bit result + (PCLATH SHL 8)
GOTO X    -> final PC will be: (X AND 0x07FF) + ((PCLATH SHL 8) AND 0x1800)


Regards,
       Philippe.

1997\02\05@171538 by Andrew Warren

face
flavicon
face
robin bussell <robin.bussellspamspam_OUTlucent.com> wrote:

> Say I've recieved the first byte of a command, it's in a register
> called COMND and I  now want to test it's value against a set of
> opcodes. The way that seems obvious to me is...
>
>
>         MOVFW   COMND
>         XORLW   VALUE1  ; Test against first value
>         BTFSC   W,ZERO  ; check zero bit skip next if non zero
>         GOTO    ROUTINE1;
>
>         MOVFW   COMND
>         XORLW   VALUE2
>         BTFSC   W,ZERO
>         GOTO    ROUTINE2
>
> and so on...  Has anyone come across a different way of doing this?
> Any shortcuts incolved if the command values are restricted to say
> 1,2,3,4,5 or a similar consecutive sequence ( maybe some lookup
> table shennanigins possible?)

Robin:

A couple of people have already posted lookup-table solutions; if,
for whatever reason, you decide not to use a table, you can save one
instruction per test by doing this:

   MOVF    COMND           ;Grab the command.

   XORLW   VALUE1          ;Test it against the first value.
   BZ      ROUTINE1        ;Jump if it matches.

   XORLW   VALUE2^VALUE1   ;Test it against the second value.
   BZ      ROUTINE2        ;Jump if it matches.

   XORLW   VALUE3^VALUE2   ;Test it against the third value.
   BZ      ROUTINE3        ;Jump if it matches.

   etc...

Note that "BZ label" is a pseudo-op built into MPASM that translates
to:

   BTFSC   STATUS,ZERO
   GOTO    label

-Andy


=== Andrew Warren - @spam@fastfwdKILLspamspamix.netcom.com                 ===
=== Fast Forward Engineering - Vista, California          ===
===                                                       ===
=== Did the information in this post help you?  Consider  ===
=== contributing to the PICLIST Fund.  Details are at:    ===
=== http://www.geocities.com/SiliconValley/2499/fund.html ===

1997\02\05@173621 by Craig Knotts

flavicon
face
Attachment converted: wonderlandfive:ATTACH01.TXT (TEXT/CSOm) (0000B557)

1997\02\05@192018 by John Payson

picon face
> How about this:
>
>         movfw   COMND   ;get you command into w
>                 addwf   PCL             ;add offset to pc to generate
>                                 ;computed goto
>
> Make sure you never give COMND a command value higher than the number of
> goto
> statements you have!

I prefer to use [assuming, e.g., 28 commands]:

       movf    COMND,w         ; "Official" form for movfw, though movwf is
                               ;   nice if your assembler will take it.
       andlw   31              ; Next power of two over number the number of
                               ;   commands, minus one
       addwf   PC              ; Or addwf PCL
       [the 28 commands]
       goto    Oops
       goto    Oops
       goto    Oops
       goto    Oops

If the number of commands is slightly over a largish power of two, then I
spring for the compare-against-maximum, but otherwise the andlw is cheap
insurance (one cycle, and one byte for it and every "goto Oops" that's
needed).

If the number of "goto Oops"s would be objectionable and my program needs to
have any data tables within it, I sometimes do this (assuming I have a 40
entry command table, a 20 byte data table, and an 8 byte data table)

CommandParse:
       movf    command,w
       andlw   63
       addwf   PC
       [the 40 commands]
Data1:
       andlw   31
       addwf   PC
       [the 20 RETLW's for the first table]
Data2:
       andlw   7
       addwf   PC
       [the 8 RETLW's for the second table]
       retlw   0               ; In case Data1 was called with 30 or 31
       retlw   0               ; In case Data1 was called with 30 or 31

Using this approach, an overrun on the commandparse table will result in an
RETLW [something] being executed in fairly short order, as will an overrun
on either of the data tables.  The extra code required, however, is nothing
beyond the "andlw"'s and the two extra RETLW's at the end.  Not at all bad
given the level of bullet-proofing it achieves.

1997\02\06@034538 by mike

flavicon
picon face
In message  <KILLspam199702052150.WAA27200KILLspamspamgolgoth.inlandsys.com> RemoveMEPICLISTTakeThisOuTspamMITVMA.MIT.EDU
writes:
> On Wed, 5 Feb 1997 20:59, Mike Watson wrote
>
> >You should also clear the carry before the "addwf pcl"
>
> WHY ????
>
> Read the PIC data sheet, the instruction ADDWF something is expected to do:
>         W + F in (W or F) and that's all, there is no carry addition !
>

Whoops! My mistake. What was I thinking?

Thanks for pointing it out.


Mike

1997\02\06@110729 by Andy David

flavicon
face
Andy Warren wrote:

> A couple of people have already posted lookup-table solutions; if,
> for whatever reason, you decide not to use a table, you can save one
> instruction per test by doing this:
>
>     MOVF    COMND           ;Grab the command.
>
>     XORLW   VALUE1          ;Test it against the first value.
>     BZ      ROUTINE1        ;Jump if it matches.
>

ah! you left out the ",w" on the "movf" Andy!

Another non-lookup table method which saves an instruction per
test over Andy W's method, but limits you to only 8 possible
jumps.

       btfsc   COMND,0 ; bitmask to enable routine 1 is
       goto    routine1        ; XXXXXXX1

       btfsc   COMND,1 ; bitmask to enable routine 2 is
       goto    routine2        ; XXXXXX10

...and so on.


- Andy.

---------------------------------------------
Andrew David, Software Manager, Ultronics Ltd.
spamBeGoneAndyspamBeGonespamUltronics.co.uk
---------------------------------------------

1997\02\06@184303 by Andrew Warren

face
flavicon
face
I wrote:
>
> > A couple of people have already posted lookup-table solutions; if,
> > for whatever reason, you decide not to use a table, you can save
> > one instruction per test by doing this:
> >
> >     MOVF    COMND           ;Grab the command.
> >
> >     XORLW   VALUE1          ;Test it against the first value.
> >     BZ      ROUTINE1        ;Jump if it matches.

and Andrew David replied:

> ah! you left out the ",w" on the "movf" Andy!

   Right you are.  My apologies.

   -Andy

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

1997\02\06@233641 by tjaart

flavicon
face
The ideas given so far are all based on a single byte type command.
What is the most efficient way of recognising strings? (For instance
communicating with a Hayes modem.)

--
Friendly Regards

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

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