include	clib.inc
include	string.inc

_TEXT	SEGMENT

qwtostr proc dist pascal public odx:dword, oax:dword
	push	si
	push	di
	mov	ax,ds
	mov	es,ax
	mov	di,offset result
	mov	cx,10
	mov	ax,'00'
	cld
	rep	stosw
	mov	dx,word ptr oax
	mov	ax,dx
	and	ax,15
	add	al,'0'
	mov	di,offset result
	cmp	al,'9'
	jle	qwtostr_00
	add	al,246
	inc	[result+1]
    qwtostr_00:
	stosb
	call	DO_QWORD
	mov	dx,offset result
	mov	ax,3000h
	mov	cx,19
	mov	di,dx
	add	di,19
	std
    qwtostr_trunc:
	cmp	[di],ah
	jne	qwtostr_rev
	stosb
	dec	cx
	jnz	qwtostr_trunc
    qwtostr_rev:
	cld
	mov	si,di
	mov	di,dx
    qwtostr_xchg:
	mov	al,[di]
	movsb
	mov	[si-1],al
	sub	si,2
	cmp	di,si
	jb	qwtostr_xchg
	mov	ax,dx
	MMOV	dx,ds
	pop	di
	pop	si
	ret
qwtostr endp

ADD_STRING:
	push	ax
	push	si
	push	cx
	mov	si,[bx]
	mov	di,offset result
	mov	cx,17
	add	byte ptr [di],6
	call	FIX_BYTE
	inc	di
    ADDSTR_00:
	lodsb
	test	al,al
	jz	ADDSTR_01
	add	al,-48
	add	[di],al
	call	FIX_BYTE
	inc	di
	dec	cx
	jnz	ADDSTR_00
    ADDSTR_01:
	call	FIX_BYTE
	pop	cx
	pop	si
	pop	ax
	ret
    FIX_BYTE:
	mov	ax,[di]
	cmp	al,'9'
	jle	FIX_BYTE_00
	add	al,246
	inc	ah
	mov	[di],ax
    FIX_BYTE_00:
	ret

DO_QWORD:
	mov	si,dx
	mov	dx,[bp+ARG1+2]
	mov	cx,0704h
	mov	bx,offset str_off
	call	DO_DWORD
	mov	cx,0800h
	mov	si,[bp+ARG1+4]
	mov	dx,[bp+ARG1+6]
DO_DWORD:
	mov	ax,si
	shr	ax,cl
	and	ax,15
	jz	DWORD_01
    DWORD_00:
	call	ADD_STRING
	dec	ax
	jnz	DWORD_00
    DWORD_01:
	add	cl,4
	cmp     cl,16
	jb	DWORD_02
	mov	si,dx
	xor	cl,cl
    DWORD_02:
	add	bx,2
	dec	ch
	jnz	DO_DWORD
    DWORD_03:
	ret

_TEXT	ENDS

_DATA	SEGMENT

string_01	db	'1',0
string_02	db	'52',0
string_03	db	'904',0
string_04	db	'3556',0
string_05	db	'758401',0
string_06	db	'1277761',0
string_07	db	'54534862',0
string_08	db	'927694924',0
string_09	db	'3767491786',0
string_10	db	'777261159901',0
string_11	db	'1444068129571',0
string_12	db	'56017679474182',0
string_13	db	'940737269953054',0
string_14	db	'3972973049575027',0
string_15	db	'796486064051292511',0
string_16	db	'1615590737044764481',0
result		db 	'00000000000000000000',0

str_off		dw	string_01
		dw	string_02
		dw	string_03
		dw	string_04
		dw	string_05
		dw	string_06
		dw	string_07
		dw	string_08
		dw	string_09
		dw	string_10
		dw	string_11
		dw	string_12
		dw	string_13
		dw	string_14
		dw	string_15
		dw	string_16

_DATA	ENDS

	END
