> I currently have an application where in the comms receive > handler, I test the received command byte and branch off > to other places depending on the value. As the values are > not consecutive, is there a better way to test them than just > doing the following time after time? > > ... > movf rx_cmd,w ;get the received command byte > xorlw msg_eg1 ;compare with msg_eg1 (a constant) > btfsc zero ;is it msg_eg1 ? > goto rx_eg1 ;yes, jump to that handler > ... > > Block repeats for as many test cases as I need. > It just seems ugly - is there a better/shorter way? > The msg constants are all over the place value wise. > I can't arrange them to be in continuous blocks. Do you have some program memory to spare? If so, a jump table is probably the simplest and also the clearest and easiest to maintain. Warning, this code was just typed in without being checked: movlw high table ;get high byte of table start address movwf pclath ;init jump address high byte movlw low table ;get low byte of table start address addwf opcode, w ;make low byte of table entry address skip_ncarr ;no carry into upper address byte ? incf pclath ;propagate the carry movwf pcl ;jump to the selected table entry ; ; Opcodes table. There is one table entry for each of the 256 ; possible opcodes. Each table entry jumps to the handler code ; for that opcode. ; ; NOTE: All handler routines must be in the same code page as ; this table. ; table goto handler0 ;opcode 0 goto handler1 ;opcode 1 . . . ******************************************************************** Olin Lathrop, embedded systems consultant in Littleton Massachusetts (978) 742-9014, olin@embedinc.com, http://www.embedinc.com -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu