Applied patch #2762516
[fw/sdcc] / device / lib / z80 / memmove.s
index 273a1eb55536e489c2281c5d9a89f7162997bb32..d718c910c17b4abdd902f6a7d954a1968e78a1b7 100644 (file)
@@ -1,5 +1,5 @@
 ; Copyright (C) 2008-2009
-; Philipp Klaus Krause
+; Philipp Klaus Krause, Marco Bodrato
 
 ; This file is part of the sdcc Library.  This library is free
 ; software; you can redistribute it and/or modify it under the
 ; The Z80 has the ldir and lddr instructions, which are perfect for implementing memmove().
 
 _memmove:
-       push    ix
-       ld      ix, #0
-       add     ix, sp
-       ld      c, 8(ix)
-       ld      b, 9(ix)
+       pop     af
+       pop     hl
+       pop     de
+       pop     bc
+       push    bc
+       push    de
+       push    hl
+       push    af
        ld      a, c
        or      a, b
-       jr      Z, memmove_end
-       ld      e, 4(ix)
-       ld      d, 5(ix)
-       ld      l, 6(ix)
-       ld      h, 7(ix)
-       ld      a, l
-       sbc     hl, de  ; or above cleared carry.
-       ld      l, a
-       ld      h, 7(ix)
-       jr      NC, memmove_up
+       ret     Z
+       push    hl
+       sbc     hl, de          ; or above cleared carry.
+       add     hl, de          ; same carry as the line before
+       jr      C, memmove_up
 memmove_down:
+       dec     bc
        add     hl, bc
-       dec     hl
-       ld      a, e
-       add     a, c
-       ld      e, a
-       ld      a, d
-       adc     a, b
-       ld      d, a
-       dec     de
+       ex      de, hl
+       add     hl, bc
+       inc     bc
        lddr
-       jr      memmove_end
+       pop     hl
+       ret
 memmove_up:
+       ex      de, hl
        ldir
-memmove_end:
-       ld      l, 4(ix)
-       ld      h, 5(ix)
-       pop     ix
+       pop     hl
        ret