View Single Post
Old 28 January 2007, 11:29   #42
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
DBcc loops

To improve our program further we can use a DBcc loop (Test Condition - Decrement and Branch) the cc represents the test and loop type.

The DBcc instructions tests both the status flags and the data registers. There are differences between loops written using dbcc loops and those using conventional loops (bcc (bra, beq, bne etc)). If the condition being tested is satisfied then control passes to the instructions following the dbcc. If it is not satisfied then the low word of the data register is reduced by 1 and the loop is taken. The loop is continued until the result in the data register equals -1.

Code:
LOOP    move.b    (a0)+,(a1)+    Copy characters
        dbra      d0,LOOP        deduct 1 off d0 and loop until d0 equals-1
This code will keep on looping until d0 equals -1. When it does the program resumes.

Now because dbcc tests until the result equals -1 we need to reduce the count in d0 by 1 otherwise it'll do 1 too many loops. A subq.b #1,d0 is all that is needed

Code:
        lea TEXT, a0               address into a0
        move.b move.b (a0+), d0    Copy counter into d0 and increase a0
        sub.b #1, d0               Decrease d0 by 1 for the dbra loop
        lea COPY, a1               address of dest into a1

LOOP    move.b (a0)+, (a1)+        Copy and increment
        dbra d0, LOOP              Test is d0 =-1 and if not decrease and loop
        move.b #0, (a1)            Add terminating 0
        rts

TEXT    dc.b 5, "HELLO"
COPY    ds.b 6
BippyM is offline  
 
Page generated in 0.17193 seconds with 11 queries