Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions src/crt/llmulu_b.src
Original file line number Diff line number Diff line change
@@ -1,68 +1,75 @@
assume adl=1

section .text

public __llmulu_b

__llmulu_b:
push af
; Multiplies BC:UDE:UHL by (SP) and returns the 64-bit unsigned product bc:ude:uhl.
; I: (SP) = 8-bit multiplier, BC:UDE:UHL = multiplicand, ADL=1
; O: bc:ude:uhl = BC:UDE:UHL * (SP)
; CC: 101*r(PC)+21*r(SPL)+18*w(SPL)+33
; CC: 100 bytes | 101F + 21R + 18W + 33
push iy
ld iy, 0
add iy, sp
push de
push hl
ld a, (iy + 9)
ld h, a
push bc
ld b, (iy + 6)
ld c, 0
ld h, b
mlt hl
ld (iy - 6), l
ld d, a
ld d, b
ld e, (iy - 5)
mlt de
ld l, h
ld h, 0
ld h, c
add hl, de
ld (iy - 5), l
ld d, a
ld d, b
ld e, (iy - 4)
mlt de
ld l, h
ld h, 0
ld h, c
add hl, de
ld (iy - 4), l
ld d, a
ld d, b
ld e, (iy - 3)
mlt de
ld l, h
ld h, 0
ld h, c
add hl, de
ld (iy - 3), l
ld d, a
ld d, b
ld e, (iy - 2)
mlt de
ld l, h
ld h, 0
ld h, c
add hl, de
ld (iy - 2), l
ld d, a
ld d, b
ld e, (iy - 1)
mlt de
ld l, h
ld h, 0
ld h, c
add hl, de
ld (iy - 1), l
ld d, a
ld e, c
mlt de

pop de
ld l, h
ld h, 0
ld c, d
ld d, b
mlt bc
ld h, c
mlt de

add hl, de
ld b, h
ld c, l
ld d, a
ld e, b
mlt de
ld a, h
add a, e
ld b, a

pop hl
pop de
pop iy
pop af
ret
6 changes: 3 additions & 3 deletions src/crt/lmulu_b.src
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ __lmulu_b:
; Multiplies EUHL by A and returns the 32-bit product euhl.
; I: A=multiplier, EUHL=multiplicand, ADL=1
; O: euhl=EUHL*A
; CC: 43*r(PC)+12*r(SPL)+9*w(SPL)+13
; CC: 42 bytes | 43F + 12R + 9W + 13
; CC: 43*r(PC)+12*r(SPL)+9*w(SPL)+17
; CC: 42 bytes | 43F + 12R + 9W + 17
Mul_EUHL_A_EUHL:
push bc
push de
Expand Down Expand Up @@ -45,7 +45,7 @@ Mul_EUHL_A_EUHL:
add hl, hl
add hl, hl
add hl, hl

mlt de ; DE = A * L
add hl, de ; UHL = AH.hi + AU.lo, AH.lo + AL.hi, AL.lo

Expand Down
6 changes: 3 additions & 3 deletions src/crt/lmulu_b_fast.src
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ __lmulu_b_fast:
; Multiplies EUHL by A and returns the 32-bit product euhl.
; I: A=multiplier, EUHL=multiplicand, ADL=1
; O: euhl=EUHL*A
; CC: 37*r(PC)+6*r(SPL)+3*w(SPL)+13
; CC: 36 bytes | 37F + 6R + 3W + 13
; CC: 37*r(PC)+6*r(SPL)+3*w(SPL)+17
; CC: 36 bytes | 37F + 6R + 3W + 17
Mul_EUHL_A_EUHL:
dec sp
push hl
Expand Down Expand Up @@ -42,7 +42,7 @@ Mul_EUHL_A_EUHL:
add hl, hl
add hl, hl
add hl, hl

mlt de ; DE = A * L
add hl, de ; UHL = AH.hi + AU.lo, AH.lo + AL.hi, AL.lo

Expand Down
61 changes: 61 additions & 0 deletions src/libc/atoi.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
assume adl=1

section .text

public _atoi

_atoi:
pop de
ex (sp), hl
push de
; inlined isspace
.whitespace_loop:
ld a, (hl)
inc hl
cp a, 32
jr z, .whitespace_loop
sub a, 9
add a, -5
jr nc, .whitespace_loop

; A = (HL - 1) - 9 + -5
; A = (HL - 1) - 14
xor a, '-' - 14
push af
jr z, .minus_sign
xor a, ('+' - 14) xor ('-' - 14)
jr z, .plus_sign
dec hl
.plus_sign:
.minus_sign:
; carry is cleared
ex de, hl
sbc hl, hl
; DE = start of the digits
; HL = 0
jr .start
.loop:
; 21F + 4R + 3W + 1 per digit
push hl
pop bc
add hl, hl ; *= 2
add hl, hl ; *= 4
add hl, bc ; *= 5
add hl, hl ; *= 10
ld bc, 0
ld c, a
add hl, bc
inc de ; next digit
.start:
ld a, (de)
sub a, 48
cp a, 10
jr c, .loop
.finish:
pop af
; carry is cleared
ret nz ; A != '-' positive
; A == '-' negative
jp __ineg

extern __ineg
84 changes: 70 additions & 14 deletions src/libc/atol.src
Original file line number Diff line number Diff line change
@@ -1,21 +1,77 @@
assume adl=1

section .text
public _atoi, _atol
_atoi:

public _atol

_atol:
pop bc
ex (sp),hl
push bc
ld bc,10
push bc
ld c,b
push bc
pop de
ex (sp), hl
push de
; inlined isspace
.whitespace_loop:
ld a, (hl)
inc hl
cp a, 32
jr z, .whitespace_loop
sub a, 9
add a, -5
jr nc, .whitespace_loop

; A = (HL - 1) - 9 + -5
; A = (HL - 1) - 14
xor a, '-' - 14
push af
jr z, .minus_sign
xor a, ('+' - 14) xor ('-' - 14)
jr z, .plus_sign
dec hl
.plus_sign:
.minus_sign:
; carry is cleared
push hl
call _strtol
pop af
pop af
pop iy
sbc hl, hl
ld e, l
; IY = start of the digits
; E:UHL = 0
jr .start
.loop:
; 32F + 4R + 3W + 1 per digit
ld d, a
ld a, e
push hl
pop bc
; *= 2
add hl, hl
rla
; *= 4
add hl, hl
rla
; *= 5
add hl, bc
adc a, e
; *= 10
add hl, hl
rla
; += digit
ld bc, 0
ld c, d
add hl, bc
adc a, b
ld e, a
; next digit
inc iy
.start:
ld a, (iy)
sub a, 48
cp a, 10
jr c, .loop
.finish:
pop af
ret
; carry is cleared
ret nz ; A != '-' positive
; A == '-' negative
jp __lneg

extern _strtol
extern __lneg
8 changes: 0 additions & 8 deletions src/libc/strtol.c

This file was deleted.

Loading
Loading