Truncated match.
PICList
Thread
'switch (case) for the PIC'
1996\11\27@085051
by
Rildo Pragana
|
Does anyone have a simpler switch (multiple case) than this below? I would
like to decide where to go based on W contents, but not for 256 cases, only
a sparse number of them. Look at the code, table, and give your comments,
please. It uses 2 temporary storage for compared byte and loop counter.
I call it putting in W the value to be compared, the destination case
returns at the end.
casech equ 0ch
tmp equ 0dh
;
switch movwf casech
clrf tmp
swloop movf tmp,w
addwf tmp,w ;2*n gets in a 'retlw' instruction
call swtable
;andlw 0ffh ;(optional) return if found NULL
;btfsc _z
;return
subwf casech,w
btfsc _z
goto swjmp
incf tmp,f
goto swloop
swjmp movf tmp,w
addwf tmp,w
addlw 1 ;2*n+1 targets a 'goto' instruction
;goto swtable ;(optional if table don't follows)
swtable addwf PCL,f
retlw case_a
goto rot_a
retlw case_b
goto rot_b
...
...
retlw case_n
goto rot_n
retlw 0 ;NULL marks end (optional)
;
rot_a ...
...
return ;returns to main (switch caller)
best regards,
Rildo Pragana
1996\11\27@095116
by
Ray Gardiner
>Does anyone have a simpler switch (multiple case)
<snip>
The easiest I know of is something like,...
; enter with value in W.
ANDLW 0x07 ; constrain to within jump table.
ADDWF PC,F ; add to PC
GOTO CASE_0
GOTO CASE_1
GOTO CASE_2
GOTO CASE_3
GOTO CASE_4
GOTO CASE_5
GOTO CASE_6
GOTO CASE_7
;
; never get's here
;
Ray Gardiner, Shepparton, Victoria, Australia, spam_OUTrayTakeThisOuT
netspace.net.au
1996\11\27@190004
by
Robert Lunn
>The easiest I know of is something like,...
>
>; enter with value in W.
>
> ANDLW 0x07 ; constrain to within jump table.
>snip...
Ray, this caters for a _short_ table, not a _sparse_ table.
___Bob
1996\11\27@233049
by
Robert Lunn
Andrew Warren has given an optimal implementation of a
switch statement. However, for its interest, here is
a modestly tidied-up version of the original loop.
casech equ 0ch
tmp equ 0dh
;
switch movwf casech
clrf tmp
;
swloop call swtable
incf tmp,f
;
subwf casech,w
btfsc _z
goto swtable
;
incf tmp,f
goto swloop
;
;
swtable movf tmp,w
addwf PCL,f
;
retlw case_a
goto rot_a
retlw case_b
goto rot_b
...
___Bob
1996\11\28@013104
by
John Payson
|
> Andrew Warren has given an optimal implementation of a
> switch statement. However, for its interest, here is
> a modestly tidied-up version of the original loop.
[table-driven switch statement deleted]
Cute, but your approach uses 13 cycles per case, at a savings of at most
one byte per. On that basis I would have to regard Andy's as being better
all around. That does not mean it cannot be improved upon, though. :-)
On a non-5x/non-508 part, you may use the addlw instruction to improve
things a bit. For example, [hopefully I'm doing this right]... suppose you
need to distinguish Y or y, N or n, and 0-9.
Switcher:
addlw 256-':' ; One character past '9'
btfss C
goto DigCheck
addlw 256+':'-'Y'
btfsc Z
goto Yep
addlw 256+'Y'-'y'
btfsc Z
goto Yep
addlw 'y'-'N'
btfsc Z
goto Nope
addlw 256+'N'-'n'
btfsc Z
goto Nope
goto Reject
DigCheck:
addlw ':'-'0'
btfss C
goto Reject
goto GotDigit ; W is digit value 0..9
The use of 'addlw' rather than 'xorlw' allows the program to check for ranges,
and to perform a binary search. The only downside is that the code can get
more complicated (even if it's faster/shorter) than with the 'xorlw' approach.
1996\11\28@175228
by
Robert Lunn
|
>> Andrew Warren has given an optimal implementation of a
>> switch statement. However, for its interest, here is
>> a modestly tidied-up version of the original loop.
>
>[table-driven switch statement deleted]
>
>Cute, but your approach uses 13 cycles per case, at a savings of at most
>one byte per. On that basis I would have to regard Andy's as being better
>all around.
Cute??
My intention wasn't to produce a time-optimised version of
a sparse table lookup. Rildo Pragana put up an implementation
that used array indexing and a loop, and I wanted to show
Rildo a variation of this technique.
Looking at Rildo's code you see he keeps an array index, and
then calculates an array offset on each of the two occasions
he accesses the array. This is suggestive of a high level
programmer who has 'hand compiled' the code.
Most low level programmers would take the approach of just
stepping straight through the array. I wanted to show this.
{Quote hidden}>That does not mean it cannot be improved upon, though. :-)
>On a non-5x/non-508 part, you may use the addlw instruction to improve
>things a bit. For example, [hopefully I'm doing this right]... suppose you
>need to distinguish Y or y, N or n, and 0-9.
>
>[switch statement deleted]
>
>The use of 'addlw' rather than 'xorlw' allows the program to check for ranges,
>and to perform a binary search. The only downside is that the code can get
>more complicated (even if it's faster/shorter) than with the 'xorlw' approach.
The problem specification was to search a list of arbitrary
values. To redefine the problem specification to include
value ranges and sorted values, and to then declare a solution
to this modified problem an 'improvement' is kinda cute.
___Bob
More... (looser matching)
- Last day of these posts
- In 1996
, 1997 only
- Today
- New search...