-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstring.h
249 lines (219 loc) · 3.28 KB
/
string.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* @file string.h
* @brief String functions.
* @author Miguel I. Garcia Lopez / FloppySoftware
*
* String functions, for MESCC (Mike's Enhanced
* Small C Compiler for Z80 & CP/M).
*
* Revisions:
* - 19 Mar 2001 : Last revision.
* - 16 Apr 2007 : GPL'd.
* - 15 Aug 2016 : Documented. GPL v3.
*
* Copyright (c) 1999-2016 Miguel I. Garcia Lopez / FloppySoftware.
*
* Licensed under the GNU General Public License v3.
*
* http://www.floppysoftware.es
* floppysoftware@gmail.com
*/
#ifndef STRING_H
#define STRING_H
/**
* @fn int strlen(char *str)
* @brief Return string length.
* @param str - string
* @return length in characters
*/
#asm
strlen:
LD D,H
LD E,L
LD BC,0FFFFH
XOR A
CPIR
OR A
SBC HL,DE
DEC HL
RET
#endasm
/**
* @fn char *strcpy(char *dst, char *src)
* @brief Copy string.
* @param dst - destination string
* @param src - source string
* @return pointer to dst
*/
#asm
strcpy:
POP BC
POP HL
POP DE
PUSH DE
PUSH HL
PUSH BC
PUSH DE
strcpy2:
LD A,(HL)
LD (DE),A
INC HL
INC DE
OR A
JR NZ,strcpy2
POP HL
RET
#endasm
/**
* @fn char *strcat(char *dst, char *src)
* @brief Copy string at the end of another string.
* @param dst - destination string
* @param src - source string
* @return pointer to dst
*/
#asm
strcat:
POP BC
POP HL
POP DE
PUSH DE
PUSH HL
PUSH BC
PUSH DE
strcat2
LD A,(DE)
OR A
JR Z,strcpy2
INC DE
JR strcat2
#endasm
/**
* @fn int strcmp(char *str1, char *str2)
* @brief Compare two strings.
* @param str1 - a string
* @param str2 - a string
* @return <0 on str1 < str2; =0 on str1 == str2; >0 on str1 > str2
*/
#asm
strcmp
POP BC
POP HL
POP DE
PUSH DE
PUSH HL
PUSH BC
strcmp1
LD A,(DE)
CP (HL)
JR NZ,strcmp2
OR A
JR Z,strcmp2
INC DE
INC HL
JR strcmp1
strcmp2
LD HL,0
RET Z
JR NC,strcmp3
DEC HL
RET
strcmp3
INC L
RET
#endasm
/**
* @fn char *strchr(char *str, char ch)
* @brief Search a character in a string.
* @param str - the string where to search
* @param ch - the character to find
* @return pointer to ch in the string, or NULL on failure
*/
#asm
strchr
POP BC
POP DE
POP HL
PUSH HL
PUSH DE
PUSH BC
strchr2
LD A,(HL)
CP E
RET Z
INC HL
OR A
JR NZ,strchr2
LD H,A
LD L,A
RET
#endasm
/**
* @fn char *strupr(char *str)
* @brief Convert a string to upper case.
* @param str - a string
* @return pointer to str
*/
#asm
strupr
POP BC
POP HL
PUSH HL
PUSH BC
PUSH HL
strupr1
LD A,(HL)
OR A
JR Z,strupr3
CP 'a'
JR C,strupr2
CP 'z'+1
JR NC,strupr2
SUB 32
LD (HL),A
strupr2
INC HL
JR strupr1
strupr3
POP HL
RET
#endasm
/**
* @fn int atoi(char *s)
* @brief Convert string to a integer.
*
* This function parses a string, interpreting its content as
* a decimal integer number, until the end of the string, or
* a non decimal digit:
*
* [+|-][[0..9]...][ZERO|NON_DECIMAL_DIGIT]
*
* Examples:
* - "-256" == -256
* - "64" == 64
* - "1024 bytes" == 1024
* - "what?" == 0
*
* @param s - a string
* @return integer value
*/
atoi(s)
char *s;
{
int sign, val;
if(*s == '+')
{
++s; sign = 1;
}
else if(*s == '-')
{
++s; sign = -1;
}
else
sign = 1;
val=0;
while(*s >= '0' && *s <= '9')
val = val * 10 + (*s++ - '0');
return val * sign;
}
#endif