Added timing and asm implementations of strcpy, strcmp, memcpy.
[fw/sdcc] / device / lib / z80 / asm_strings.s
1         ;; Implementation of some string functions in
2         ;; assembler.
3
4         ;; Why - because I want a better dhrystone score :)
5
6 ; char *strcpy(char *dest, const char *source)
7 _strcpy::
8         push    de
9         push    ix
10         ld      ix,#0
11         add     ix,sp
12         ld      l,6(ix)
13         ld      h,7(ix)
14         ld      e,8(ix)
15         ld      d,9(ix)
16
17         push    hl
18 1$:     
19         ld      a,(de)
20         ld      (hl),a
21         inc     hl
22         inc     de
23         or      a,a
24         jr      nz,1$
25
26         pop     hl
27         pop     ix
28         pop     de
29         ret
30
31 ; void *memcpy(void *dest, const void *source, int count)
32 _memcpy::
33         push    de
34         push    bc
35         push    ix
36         ld      ix,#0
37         add     ix,sp
38         ld      l,8(ix)
39         ld      h,9(ix)
40         ld      e,10(ix)
41         ld      d,11(ix)
42         ld      c,12(ix)
43         ld      b,13(ix)
44
45         inc     b
46         inc     c
47         push    hl
48
49         jr      2$
50 1$:
51         ld      a,(de)
52         ld      (hl),a
53         inc     de
54         inc     hl
55 2$:
56         dec     c
57         jr      nz,1$
58         dec     b
59         jr      nz,1$   
60
61         pop     hl
62         pop     ix
63         pop     bc
64         pop     de
65         ret
66
67 ; int strcmp(const char *s1, const char *s2) 
68 _strcmp::
69         push    de
70         push    ix
71         ld      ix,#0
72         add     ix,sp
73         ld      e,6(ix)
74         ld      d,7(ix)
75         ld      l,8(ix)
76         ld      h,9(ix)
77
78         jr      1$
79 2$:     
80         ld      a,(de)
81         sub     (hl)
82         jr      nz,4$
83         ;; A == 0
84         cp      (hl)
85         jr      z,3$
86 1$:     
87         inc     de
88         inc     hl
89         jr      2$
90
91 3$:
92         ld      hl,#0
93         jr      5$
94 4$:
95         ld      hl,#1
96         jr      nc,5$
97         ld      hl,#-1
98 5$:
99         pop     ix
100         pop     de
101         ret
102