Thank you all for your comments.. However, the program runs in my emulator without problems.
Perhaps you didn't simulate it thoroughly enough. I tried simulating your code in MPLAB 8 and found a problem here:-
circle
btfsc portb,6 ;check switch
call add1
btfss intcon,2 ;check tmr0 flag,skip next on true
goto circleIf the switch is held closed then it will continuously add 1 to var2. This loop is only a few instructions so it counts very quickly (about 9us per count). When the switch is opened again at some later time, var2 will be a random number between 0 and 255.
This is a problem because later you stop the flashing sequence
only if var2 = 4...
movlw 4
nop
subwf var2,w ;checking switch count
btfss status,2 ;<-- skpz = skip if zero (ie. if var2 = 4)
goto first
;lights off back to preset
goto start
...but since the value of var2 is random, there is only one chance in 256 of the sequence stopping!
Is the 'call add1' in 'circle' some kind of debounce timer? If so then perhaps you should only call add1 only
after timer 0 overflows, stop counting (in add1) if/when it gets to 255, and use skpc instead of skpz when testing the switch count so that it will skip on a count which is equal to
or higher than the debounce value you are comparing it to.
When using mechanical switches you have to assume that the switch could be on or off for long periods of time, or even flicking between on and off. When simulating a switch I set breakpoints at places in the code where the switch's action should trigger a response (eg. starting the flashing light sequence, turning the lights off and restarting). I then toggle the I/0 port to simulate turning the switch on and let the program
run until it hits a breakpoint, then
continue running from the breakpoint (without toggling the switch off) to see where it will end up next. This should show up any 'holes' in the program that let it fall through to places you don't want it to reach while the switch is held down.
A few notes on style:-
1. Use the official register names and bits defined in the device's include file, eg. 'bcf INTCON,TMR0IF'
2. Use built in macros such as 'skpz', rather than the obtuse 'btfss status,2'
3. Assign symbolic names to raw numbers wherever practicable, eg. '#define SWITCH 6', 'btfsc PORTB, SWITCH'.
This will make your code easier to follow and more maintainable. It is much easier and less error prone to change one define, rather than having to hunt through all your code looking for a particular number just to change a port pin!