Searching \ for '[PIC]: How to repeat a loop in assembler ?' 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/devices.htm?key=pic
Search entire site for: 'How to repeat a loop in assembler ?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: How to repeat a loop in assembler ?'
2001\09\23@113502 by Lasse Madsen

flavicon
face
Hi all
Im new to the list and im relatively new to assembler.
I woud like to know how i can "repeat" a loop X times
heres my code which i would like to have repeated for instance 50 times:

<CODE>
step1 BCF porta, 01
CALL    pulse
BSF     porta, 01
CALL    farve_1
goto step1
</CODE>

How can i repeat the above 50 times insted of forever ?
Thanks to all who reads this and especially to those who answer this.

Best regards

Lasse Madsen

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


2001\09\23@121221 by Anders_Mejl=E6nder_Jakhelln?=

flavicon
face
Try something like this:

movlw 50
movwf counter
loop:
---------------
your code
---------------
decfsz counter
goto loop

/Anders

PS: Instruction set summary are available at Microchip's web site

{Original Message removed}

2001\09\23@172034 by Olin Lathrop

face picon face
> I woud like to know how i can "repeat" a loop X times


    movlw   n_times    ;get number of times to loop (must be > 0)
    movwf   loopcnt    ;init number of iterations left
loop                    ;back here each new iteration
    ;
    ;   This stuff gets run N_TIMES.
    ;
    decfsz  loopcnt    ;count one less iteration left
    goto    loop       ;back to do next iteration


Of course this assumes that the direct register bank is set for access to
LOOPCNT, and that 1 <= N_TIMES <= 255.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, .....olinKILLspamspam@spam@embedinc.com, http://www.embedinc.com

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


2001\09\23@220135 by Gennette, Bruce

flavicon
face
Just before your piece of code to loop you load a variable (register) with
the number of times the loop is to run.  At the end of your piece of code
you decrement the variable and test to see if it has reached zero with the
command 'decfsz variable'.  Then you have the 'goto step1' as the command to
skip when the variable has been decremented to zero.

For tight memory situations you can use the fsr register to hold the
counter.

For loops greater than 256 you can nest counter variables.

Use equates and lots of labels - they cost you nothing in the final hex
file, but aid a great deal in organising and self documenting your source
code.


loadloop                                ;label the start of the routine
       movlw   loopnumber              ;get the number from an equate in
your header file
                                       ;(or from a variable if it has to be
calculated each time)
       movwf loopcounter               ;copy it to the counter variable
step1                                   ;label the start of the loop
       nop
       nop                             ;do your loop
       nop
       decfsc loopcounter      ;decrement and test the counter = zero
       goto step1                      ;jump back if loopcounter <> zero
step1end                                ;label the end of the routine

       nop                             ;continue on
       nop



Bye.


{Original Message removed}

2001\09\23@220548 by Gennette, Bruce

flavicon
face
Typing too fast - of course this -

       decfsc loopcounter      ;decrement and test the counter = zero

should have been this -

       decfsz loopcounter      ;decrement and test the counter = zero

Bye.

{Original Message removed}

2001\09\24@012202 by BENEDICIC SAMO

flavicon
face
Hi!


<CODE>

movlw   .50             ;you want to repeat 50 times
movwf   WHATEVER        ;store this number somewheare

step1 BCF porta, 01
CALL    pulse
BSF     porta, 01
CALL    farve_1

decfsz  WHATEVER,f      ;decrement WHATEVER every time you pass through loop, skip
goto step1             ;next instruction when TEMP=0 (50th pass)
</CODE>

Regards, Samo

{Original Message removed}

2001\09\24@090751 by Olin Lathrop

face picon face
> Use equates and lots of labels - they cost you nothing in the final hex
> file, but aid a great deal in organising and self documenting your source
> code.
>
>
> loadloop                                ;label the start of the routine
>         movlw   loopnumber              ;get the number from an equate in

I certainly agree about using symbolic constants, but using labels to
document sections of code is a bad idea.  Use comments instead.  A label in
code is a warning that execution can end up there due to a CALL or GOTO and
not just due to falling thru from the previous instructions.  All GOTO
targets should have a comment explaining why or when something jumps there.
Because labels require extra attention in understanding code, they should
only be used when necessary.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, .....olinKILLspamspam.....embedinc.com, 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


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