Skip to content

Commit d3c7f67

Browse files
calc84maniacmateoconlechuga
authored andcommitted
Optimize strnlen, strndup, and strdup
1 parent e1a3e1d commit d3c7f67

File tree

3 files changed

+77
-68
lines changed

3 files changed

+77
-68
lines changed

src/libc/strdup.src

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@
33
section .text
44
public _strdup
55
_strdup:
6-
ld hl,-3
7-
call __frameset
8-
ld bc,(ix+6)
9-
push bc
6+
pop de
7+
ex (sp), hl
8+
push de
9+
10+
push hl
11+
12+
push hl
1013
call _strlen
11-
pop bc
14+
pop de
15+
1216
inc hl
17+
push hl
18+
1319
push hl
1420
call _malloc
21+
pop de
22+
1523
pop bc
16-
ld (ix+-3),hl
17-
add hl,de
18-
or a,a
19-
sbc hl,de
20-
jr z,MallocFail
21-
ld bc,(ix+6)
22-
push bc
23-
ld bc,(ix+-3)
24-
push bc
25-
call _strcpy
26-
pop bc
27-
pop bc
28-
MallocFail:
29-
ld hl,(ix+-3)
30-
ld sp,ix
31-
pop ix
24+
pop de
25+
26+
add hl, bc
27+
or a, a
28+
sbc hl, bc
29+
ret z
30+
31+
push hl
32+
ex de, hl
33+
ldir
34+
pop hl
3235
ret
3336

34-
extern __frameset
35-
extern _malloc
3637
extern _strlen
37-
extern _strcpy
38+
extern _malloc

src/libc/strndup.src

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,46 @@
33
section .text
44
public _strndup
55
_strndup:
6-
ld hl,-6
7-
call __frameset
8-
ld bc,(ix+6)
9-
push bc
10-
call _strlen
11-
ld (ix+-3),hl
12-
ex de,hl
13-
ld hl,(ix+9)
14-
or a,a
15-
sbc hl,de
16-
jr nc,Greater
17-
ld hl,(ix+9)
18-
ld (ix+-3),hl
19-
Greater:
20-
ld bc,(ix+-3)
21-
inc bc
6+
pop bc
7+
pop de
8+
ex (sp),hl
9+
push de
2210
push bc
11+
12+
push de
13+
14+
push hl
15+
push de
16+
call _strnlen
17+
pop de
18+
pop de
19+
20+
inc hl
21+
push hl
22+
23+
push hl
2324
call _malloc
24-
ld (ix+-6),hl
25-
add hl,bc
26-
or a,a
27-
sbc hl,bc
28-
jr z,MallocFail
29-
ld bc,(ix+-3)
30-
ld hl,(ix+-6)
31-
add hl,bc
32-
ld (hl),0
33-
or a,a
34-
sbc hl,bc
35-
push bc
36-
ld bc,(ix+6)
37-
push bc
25+
pop de
26+
27+
pop bc
28+
pop de
29+
30+
add hl, bc
31+
xor a, a
32+
sbc hl, bc
33+
ret z
34+
35+
ld (hl), a
36+
cpi
37+
dec hl
38+
ret po
39+
3840
push hl
39-
call _memcpy
40-
MallocFail:
41-
ld sp,ix
42-
pop ix
41+
ex de, hl
42+
ldir
43+
ld (de), a
44+
pop hl
4345
ret
4446

45-
extern __frameset
46-
extern _strlen
47+
extern _strnlen
4748
extern _malloc
48-
extern _memcpy

src/libc/strnlen.src

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
section .text
44
public _strnlen
55
_strnlen:
6-
pop hl,de,bc
7-
push bc,de,hl
8-
push de
96
pop hl
10-
xor a,a
11-
inc bc
7+
pop de
8+
pop bc
9+
push bc
10+
push de
11+
push hl
12+
xor a, a
13+
sbc hl, hl
14+
sbc hl, bc
15+
ret z
16+
add hl, bc
17+
sbc hl, de
18+
ex de, hl
1219
cpir
13-
scf
14-
sbc hl,de
20+
add hl, de
21+
ret z
22+
inc hl
1523
ret

0 commit comments

Comments
 (0)