Skip to content

Commit 84d69b4

Browse files
committed
UNSTABLE | added draft libc functions
1 parent 7dfb19b commit 84d69b4

File tree

8 files changed

+319
-21
lines changed

8 files changed

+319
-21
lines changed

src/libc/memchr.src

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.assume adl=1
2+
3+
.section .text
4+
5+
.global _memchr
6+
.type _memchr, @function
7+
8+
.ifdef PREFER_CE_LIBC
9+
10+
.set _memchr, 0x00009C
11+
12+
.else
13+
14+
_memchr:
15+
ld iy, -1
16+
add iy, sp
17+
ld bc, (iy + 10)
18+
sbc hl, hl
19+
add hl, bc
20+
jr nc, .L.ret_zero
21+
ld hl, (iy + 4)
22+
ld a, (iy + 7)
23+
cpir
24+
dec hl
25+
ret z ; found match
26+
or a, a
27+
.L.ret_zero:
28+
; return NULL
29+
sbc hl, hl
30+
ret
31+
32+
.endif

src/libc/memcmp.src

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.assume adl=1
2+
3+
.section .text
4+
5+
.global _memcmp
6+
.type _memcmp, @function
7+
8+
.ifdef PREFER_CE_LIBC
9+
10+
.set _memcmp, 0x0000A0
11+
12+
.else
13+
14+
_memcmp:
15+
ld iy, 0
16+
add iy, sp
17+
ld bc, (iy + 9)
18+
sbc hl, hl
19+
adc hl, bc
20+
ld hl, (iy + 6)
21+
ld de, (iy + 3)
22+
call nz, .L.start ; z means BC was zero
23+
.L.ret_zero:
24+
sbc hl, hl
25+
ret
26+
27+
.L.loop:
28+
ret po
29+
inc de
30+
.L.start:
31+
ld a, (de)
32+
cpi
33+
jr z, .L.loop
34+
.L.finish:
35+
ld sp, iy ; ret
36+
dec hl
37+
sub a, (hl)
38+
sbc hl, hl
39+
ld l, a
40+
ret
41+
42+
.endif

src/libc/os.src

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,12 @@
44
.global _longjmp
55
.type _longjmp, @function
66
.set _longjmp, 0x000098
7-
.global _memchr
8-
.type _memchr, @function
9-
.set _memchr, 0x00009C
10-
.global _memcmp
11-
.type _memcmp, @function
12-
.set _memcmp, 0x0000A0
137
.global _setjmp
148
.type _setjmp, @function
159
.set _setjmp, 0x0000B8
16-
.global _strcat
17-
.type _strcat, @function
18-
.set _strcat, 0x0000C0
19-
.global _strchr
20-
.type _strchr, @function
21-
.set _strchr, 0x0000C4
22-
.global _strcpy
23-
.type _strcpy, @function
24-
.set _strcpy, 0x0000CC
2510
.global _strncat
2611
.type _strncat, @function
2712
.set _strncat, 0x0000D8
28-
.global _strncpy
29-
.type _strncpy, @function
30-
.set _strncpy, 0x0000E0
31-
.global _strstr
32-
.type _strstr, @function
33-
.set _strstr, 0x0000F0
3413
.global _strtok
3514
.type _strtok, @function
3615
.set _strtok, 0x0000F4

src/libc/strcat.src

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.assume adl=1
2+
3+
.section .text
4+
5+
.global _strcat
6+
.type _strcat, @function
7+
8+
.ifdef PREFER_CE_LIBC
9+
10+
.set _strcat, 0x0000C0
11+
12+
.else
13+
14+
_strcat:
15+
pop bc
16+
pop hl
17+
pop de
18+
push de
19+
push hl
20+
push bc
21+
22+
push hl ; return value
23+
; move to the end of dst
24+
xor a, a
25+
ld bc, 0
26+
cpir
27+
dec hl
28+
ex de, hl
29+
; move to the end of src
30+
cpir
31+
add hl, bc ; rewind to the beginning of src
32+
push hl
33+
sbc hl, hl
34+
sbc hl, bc
35+
ex (sp), hl
36+
pop bc
37+
; DE = dst + strlen(dst) + 1
38+
; HL = src
39+
; BC = strlen(src) + 1
40+
ldir
41+
pop hl ; return dst
42+
ret
43+
44+
.endif

src/libc/strchr.src

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.assume adl=1
2+
3+
.section .text
4+
5+
.global _strchr
6+
.type _strchr, @function
7+
8+
.ifdef PREFER_CE_LIBC
9+
10+
.set _strchr, 0x0000C4
11+
12+
.else
13+
14+
_strchr:
15+
pop bc
16+
pop hl
17+
pop de
18+
push de
19+
push hl
20+
push bc
21+
xor a, a
22+
ld bc, 0
23+
cpir
24+
ld a, e
25+
ex de, hl
26+
; calculate strlen(str) + 1
27+
sbc hl, hl
28+
sbc hl, bc
29+
ex de, hl
30+
; rewind HL to the beginning of str
31+
add hl, bc
32+
push de
33+
pop bc
34+
cpir
35+
dec hl
36+
ret z ; found match
37+
; return NULL
38+
or a, a
39+
sbc hl, hl
40+
ret
41+
42+
.endif

src/libc/strcpy.src

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.assume adl=1
2+
3+
.section .text
4+
5+
.global _strcpy
6+
.type _strcpy, @function
7+
8+
.ifdef PREFER_CE_LIBC
9+
10+
.set _strcpy, 0x0000CC
11+
12+
.else
13+
14+
_strcpy:
15+
pop bc
16+
pop de
17+
ex (sp), hl
18+
push de
19+
push bc
20+
21+
push de ; return value
22+
xor a, a
23+
ld bc, 0
24+
; move to the end of src
25+
cpir
26+
add hl, bc ; rewind to the beginning of src
27+
push hl
28+
sbc hl, hl
29+
sbc hl, bc
30+
ex (sp), hl
31+
pop bc
32+
; DE = dst
33+
; HL = src
34+
; BC = strlen(src) + 1
35+
ldir
36+
pop hl ; return dst
37+
ret
38+
39+
.endif

src/libc/strncpy.src

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
.assume adl=1
2+
3+
.section .text
4+
5+
.global _strncpy
6+
.type _strncpy, @function
7+
8+
.ifdef PREFER_CE_LIBC
9+
10+
.set _strncpy, 0x0000E0
11+
12+
.else
13+
14+
; currently this just copies the stpncpy implementation, and could probably be optimized further
15+
_strncpy:
16+
ld iy, 0
17+
add iy, sp
18+
ld bc, (iy + 9) ; max_len
19+
20+
; inlined strnlen
21+
xor a, a
22+
sbc hl, hl
23+
sbc hl, bc
24+
jr z, .L.zero_size
25+
add hl, bc
26+
ld de, (iy + 6) ; src
27+
sbc hl, de
28+
ex de, hl
29+
cpir
30+
jr z, .L.finish_strnlen
31+
inc hl
32+
.L.finish_strnlen:
33+
xor a, a
34+
adc hl, de
35+
.L.zero_size:
36+
37+
; copy strnlen bytes from src
38+
push hl
39+
ld de, (iy + 3) ; dst
40+
jr z, .L.zero_byte_copy
41+
ld hl, (iy + 6) ; src
42+
pop bc
43+
push bc
44+
ldir
45+
.L.zero_byte_copy:
46+
pop bc
47+
48+
; zero pad the remainder
49+
ld hl, (iy + 9) ; max_len
50+
scf
51+
sbc hl, bc ; clear_size - 1 = max_len - src_len - 1
52+
ex de, hl
53+
jr c, .L.finish ; clear_size <= 0 (or max_len <= src_len)
54+
; HL = dst + src_len
55+
; DE = clear_size - 1
56+
add hl, de
57+
ld (hl), a
58+
jr z, .L.finish ; clear_size == 1
59+
push de
60+
pop bc
61+
push hl
62+
pop de
63+
dec de
64+
lddr
65+
.L.finish:
66+
ld hl, (iy + 3)
67+
ret
68+
69+
.endif

src/libc/strstr.src

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.assume adl=1
2+
3+
.section .text
4+
5+
.global _strstr
6+
.type _strstr, @function
7+
8+
.ifdef PREFER_CE_LIBC
9+
10+
.set _strstr, 0x0000F0
11+
12+
.else
13+
14+
; strstr can probably be optimized further beyond just wrapping memmem
15+
_strstr:
16+
pop bc
17+
pop de
18+
ex (sp), hl
19+
push de
20+
push bc
21+
22+
push hl
23+
xor a, a
24+
ld bc, 0
25+
cpir
26+
sbc hl, hl
27+
dec hl
28+
sbc hl, bc ; carry will be cleared
29+
ex (sp), hl ; strlen(needle)
30+
push hl ; needle
31+
32+
ex de, hl
33+
push hl
34+
ld bc, 0
35+
cpir
36+
sbc hl, hl
37+
dec hl
38+
sbc hl, bc
39+
ex (sp), hl ; strlen(haystack)
40+
push hl ; haystack
41+
42+
call _memmem
43+
pop bc
44+
pop bc
45+
pop bc
46+
pop bc
47+
ret
48+
49+
.extern _memmem
50+
51+
.endif

0 commit comments

Comments
 (0)