	cpu LMM
	.module util.c
	.area text(rom, con, rel)
	.dbfile ./util.c
	.dbfunc e ctoi _ctoi fc
;              c -> X-4
_ctoi::
	.dbline -1
	push X
	mov X,SP
	.dbline 4
; #include "util.h"
; 
; unsigned char ctoi(char c)
; {
	.dbline 5
; 	if (c>='0' && c<='9')
	cmp [X-4],48
	jc L2
X0:
	mov A,57
	cmp A,[X-4]
	jc L2
X1:
	.dbline 6
; 		return c-'0';
	mov REG[0xd0],>__r0
	mov A,[X-4]
	sub A,48
	xjmp L1
L2:
	.dbline 8
; 	
; 	if (c>='A' && c<='F')
	cmp [X-4],65
	jc L4
X2:
	mov A,70
	cmp A,[X-4]
	jc L4
X3:
	.dbline 9
; 		return c-'A'+10;
	mov REG[0xd0],>__r0
	mov A,[X-4]
	sub A,65
	mov [__r0],A
	add [__r0],10
	mov A,[__r0]
	xjmp L1
L4:
	.dbline 11
; 		
; 	if (c>='a' && c<='f')
	cmp [X-4],97
	jc L6
X4:
	mov A,102
	cmp A,[X-4]
	jc L6
X5:
	.dbline 12
; 		return c-'a'+10;
	mov REG[0xd0],>__r0
	mov A,[X-4]
	sub A,97
	mov [__r0],A
	add [__r0],10
	mov A,[__r0]
	xjmp L1
L6:
	.dbline 14
; 	
; 	return 0;
	mov REG[0xd0],>__r0
	mov A,0
	.dbline -2
L1:
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l c -4 c
	.dbend
	.dbfunc e stoi _stoi fI
;            acc -> X+0
;            len -> X-6
;              b -> X-5
_stoi::
	.dbline -1
	push X
	mov X,SP
	add SP,2
	.dbline 18
; }
; 
; int stoi(char *b, unsigned char len)
; {
	.dbline 21
; 	int acc;
; 	
; 	acc=0;
	mov [X+1],0
	mov [X+0],0
	xjmp L10
L9:
	.dbline 24
	.dbline 25
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov A,[X-5]
	mov REG[0xd4],A
	mvi A,[__r1]
	push A
	xcall _ctoi
	add SP,-1
	mov REG[0xd0],>__r0
	mov [__r1],A
	mov [__r0],0
	mov A,[X+1]
	mov [__r3],A
	mov A,[X+0]
	mov [__r2],A
	asl [__r3]
	rlc [__r2]
	asl [__r3]
	rlc [__r2]
	asl [__r3]
	rlc [__r2]
	asl [__r3]
	rlc [__r2]
	mov A,[__r3]
	add A,[__r1]
	mov [X+1],A
	mov A,[__r2]
	adc A,[__r0]
	mov [X+0],A
	.dbline 26
	dec [X-6]
	.dbline 27
	inc [X-4]
	adc [X-5],0
	.dbline 28
L10:
	.dbline 23
; 	
; 	while (len!=0)
	cmp [X-6],0
	jnz L9
	.dbline 30
; 		{
; 		acc=acc*16+ctoi(*b);
; 		len--;
; 		b++;
; 		}	
; 
; 	return acc;
	mov REG[0xd0],>__r0
	mov A,[X+1]
	mov [__r1],A
	mov A,[X+0]
	mov [__r0],A
	.dbline -2
L8:
	add SP,-2
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l acc 0 I
	.dbsym l len -6 c
	.dbsym l b -5 pc
	.dbend
	.dbfunc e stoi2 _stoi2 fI
;            acc -> X+0
;            len -> X-6
;              b -> X-5
_stoi2::
	.dbline -1
	push X
	mov X,SP
	add SP,2
	.dbline 35
; }
; 
; 
; int stoi2(char *b, unsigned char len)
; {
	.dbline 38
; 	int acc;
; 	
; 	acc=0;
	mov [X+1],0
	mov [X+0],0
	xjmp L14
L13:
	.dbline 41
	.dbline 42
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov A,[X-5]
	mov REG[0xd4],A
	mvi A,[__r1]
	push A
	xcall _ctoi
	add SP,-1
	mov REG[0xd0],>__r0
	mov [__r1],A
	mov [__r0],0
	mov A,[X+1]
	mov [__r3],A
	mov A,[X+0]
	mov [__r2],A
	asl [__r3]
	rlc [__r2]
	asl [__r3]
	rlc [__r2]
	asl [__r3]
	rlc [__r2]
	asl [__r3]
	rlc [__r2]
	mov A,[__r3]
	add A,[__r1]
	mov [X+1],A
	mov A,[__r2]
	adc A,[__r0]
	mov [X+0],A
	.dbline 43
	dec [X-6]
	.dbline 44
	inc [X-4]
	adc [X-5],0
	.dbline 45
L14:
	.dbline 40
; 	
; 	while (len!=0)
	cmp [X-6],0
	jnz L13
	.dbline 47
; 		{
; 		acc=(acc<<4)+ctoi(*b);
; 		len--;
; 		b++;
; 		}	
; 
; 	return acc;
	mov REG[0xd0],>__r0
	mov A,[X+1]
	mov [__r1],A
	mov A,[X+0]
	mov [__r0],A
	.dbline -2
L12:
	add SP,-2
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l acc 0 I
	.dbsym l len -6 c
	.dbsym l b -5 pc
	.dbend
	.dbfunc e itoh _itoh fc
;              v -> X-4
_itoh::
	.dbline -1
	push X
	mov X,SP
	.dbline 51
; }
; 
; char itoh(unsigned char v)
; {
	.dbline 52
; 	if (v<10)
	cmp [X-4],10
	jnc L17
X6:
	.dbline 53
; 		return v+'0';
	mov REG[0xd0],>__r0
	mov A,[X-4]
	add A,48
	xjmp L16
L17:
	.dbline 54
; 	return v+55;
	mov REG[0xd0],>__r0
	mov A,[X-4]
	add A,55
	.dbline -2
L16:
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l v -4 c
	.dbend
	.dbfunc e abs _abs fI
;              v -> X-5
_abs::
	.dbline -1
	push X
	mov X,SP
	.dbline 58
; }
; 
; int abs(int v)
; {
	.dbline 59
; 	if (v<0)
	mov A,[X-4]
	sub A,0
	mov A,[X-5]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jnc L20
X7:
	.dbline 60
; 		return -v;
	mov REG[0xd0],>__r0
	mov A,[X-4]
	cpl A
	sub A,-1
	mov [__r1],A
	mov A,[X-5]
	cpl A
	sbb A,-1
	mov [__r0],A
	xjmp L19
L20:
	.dbline 61
; 	return v;
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov A,[X-5]
	mov [__r0],A
	.dbline -2
L19:
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l v -5 I
	.dbend
	.dbfunc e sgn _sgn fI
;              v -> X-5
_sgn::
	.dbline -1
	push X
	mov X,SP
	.dbline 65
; }
; 
; int sgn(int v)
; {
	.dbline 66
; 	if (v<0)
	mov A,[X-4]
	sub A,0
	mov A,[X-5]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jnc L23
X8:
	.dbline 67
; 		return -1;
	mov REG[0xd0],>__r0
	mov [__r1],-1
	mov [__r0],-1
	xjmp L22
L23:
	.dbline 68
; 	return 1;
	mov REG[0xd0],>__r0
	mov [__r1],1
	mov [__r0],0
	.dbline -2
L22:
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l v -5 I
	.dbend
