View Single Post
Old 26 January 2007, 10:01   #41
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
Right seeing as nobody fancied it here is the full code to the previous routine.

Code:
        lea       TEXT,a0       Address of TEXT into a0
        move.b    (a0),d0        Copy count into d0 (5)
        addq.l    #1,a0          Increase a0 to point to first char of word
        lea        COPY,a1       address of COPY buffer into a1

LOOP    move.b    (a0),(a1)      Copy contents of address pointed by a0 to address pointed by a1
        addq.l    #1,a0          Increase address in a0 by 1
        addq.l    #1,a1          and same for a1
        subq.b    #1,d0          Decrease loop count by 1
        bne        LOOP          if d0 is unequal to 0 loop

        move.b    #0,(a1)        add terminating NULL
        rts                      End program

TEXT    dc.b      5,"HELLO"
COPY    ds.b      6
The 68k family of processors has an addressing mode which allows us to refine and improve this code to make it smaller and quicker. The addressing modes are called Address Register Indirect with Post Increment and Address Register Indirect with Pre Decrement.

With Post Increment the code is copied and then the source address is automatically increased by 1 for byte, 2 for word and 4 for long-word. To achieve this a + is added directly after the address register (a0)+, so the following code

Code:
move.b (a0), d0
addq #1,a0
can be replaced with the following which does exactly the same

Code:
move.b (a0)+,d0
What's happening is the contents pointed to by a0 is being copied into d0 and then the adress in a0 is increased by 1.

Auto Decrement works by first decreasing the address and then copying the data. Example:

Code:
move.b -(a0),d0
Our program will now look like the following

Code:
        lea       TEXT,a0        Address of TEXT into a0
        move.b    (a0)+,d0       Copy count into d0 (5) and increase address in a0
        lea       COPY,a1        address of COPY buffer into a1

LOOP    move.b    (a0)+,(a1)+    Copy contents of address pointed by a0 to address pointed by a1 and increase both by 1
        subq.b    #1,d0          Decrease loop count by 1
        bne       LOOP           if d0 is unequal to 0 loop

        move.b    #0,(a1)        add terminating NULL
        rts                      End program

TEXT    dc.b      5,"HELLO"
COPY    ds.b      6
This code can be improved further by using DBcc loops, does someone NOT experienced want to have a go at researching this and posting the code?

Last edited by BippyM; 26 January 2007 at 10:10.
BippyM is offline  
 
Page generated in 0.04570 seconds with 11 queries