]> git.sur5r.net Git - cc65/blob - libsrc/common/memmove.s
f344f9df8014cb9fd6e7d3a49702d5a33f932d35
[cc65] / libsrc / common / memmove.s
1 ;
2 ; Ullrich von Bassewitz, 2003-08-20
3 ;
4 ; void* __fastcall__ memmove (void* dest, const void* src, size_t size);
5 ;
6 ; NOTE: This function uses entry points from memcpy!
7 ;
8
9         .export         _memmove
10         .import         memcpy_getparams, memcpy_upwards
11         .importzp       ptr1, ptr2, ptr3, ptr4, tmp1
12
13         .macpack        generic
14         .macpack        longbranch
15
16 ; ----------------------------------------------------------------------
17 _memmove:
18         sta     ptr4
19         stx     ptr4+1          ; Size -> ptr4
20
21         jsr     memcpy_getparams
22
23 ; Check for the copy direction. If dest < src, we must copy upwards (start at
24 ; low addresses and increase pointers), otherwise we must copy downwards
25 ; (start at high addresses and decrease pointers).
26
27         sec
28         sbc     ptr1
29         txa
30         sbc     ptr1+1
31         jcc     memcpy_upwards  ; Branch if dest < src (upwards copy)
32
33 ; Copy downwards. Adjust the pointers to the end of the memory regions.
34
35         lda     ptr1+1
36         add     ptr4+1
37         sta     ptr1+1
38
39         lda     ptr2+1
40         add     ptr4+1
41         sta     ptr2+1
42
43 ; Load the low offset into Y, and the counter low byte into X.
44
45         ldy     ptr4
46         ldx     ptr3
47         jmp     @L2
48
49 ; Copy loop
50
51 @L1:    dey
52         lda     (ptr1),y
53         sta     (ptr2),y
54
55 @L2:    inx                     ; Bump counter low byte
56         bne     @L1
57         dec     ptr1+1
58         dec     ptr2+1
59         inc     ptr3+1          ; Bump counter high byte
60         bne     @L1
61
62 ; Done, return dest
63
64 done:   lda     ptr2
65         ldx     tmp1            ; get function result (dest)
66         rts
67