76d21a2ccede241ebc57ef7d6c12bca906f53633
[fw/sdcc] / device / lib / z80 / memmove.s
1         .area   _CODE
2
3         .globl _memmove
4
5 ; The Z80 has the ldir and lddr instructions, which are perfect for implementing memmove().
6
7 _memmove:
8         push    ix
9         ld      ix, #0
10         add     ix, sp
11         ld      c, 8(ix)
12         ld      b, 9(ix)
13         ld      a, c
14         or      a, b
15         jr      Z, memmove_end
16         ld      e, 4(ix)
17         ld      d, 5(ix)
18         ld      l, 6(ix)
19         ld      h, 7(ix)
20         ld      a, l
21         sbc     hl, de  ; or above cleared carry.
22         ld      l, a
23         ld      h, 7(ix)
24         jr      NC, memmove_up
25 memmove_down:
26         add     hl, bc
27         dec     hl
28         ld      a, e
29         add     a, c
30         ld      e, a
31         ld      a, d
32         adc     a, b
33         ld      d, a
34         dec     de
35         lddr
36         jr      memmove_end
37 memmove_up:
38         ldir
39 memmove_end:
40         ld      l, 4(ix)
41         ld      h, 5(ix)
42         pop     ix
43         ret
44