;PRINT STRING (HL) Len in B

func_print:

LD B,(HL)
LD A,B
AND A
RET Z

func_print_1:

INC HL
LD A,(HL)
RST 18H
DJNZ func_print_1
RET

;PRINT STRING (HL) fino a 0

func_print_0:

LD A,(HL)
AND A
RET Z
RST 18H
INC HL
JR func_print_0

;CARRIAGE RETURN

func_crlf:

LD A,$D
RST 18H
LD A,$A
RST 18H
RET

;TAB o VIRGOLA

func_tab:

LD A,9
RST 18H
RET


i16tostr:
    
    ld a,h
    or l
    jr z,i16tostr_1

    bit 7,h
    jr z,i16tostr_2

    ld a,'-'
    ld (de),a
    inc de

    ld a,h
    cpl
    ld h,a
    ld a,l
    cpl
    ld l,a
    inc hl

    ld a,h
    or l
    jr nz,i16tostr_2
    
    dec de

i16tostr_1:
    ld a,48
    ld (de),a
    xor a
    inc de
    ld (de),a
    ret

i16tostr_2:

    call i16tostr_3
    xor a
    ld (de),a
    ret

i16tostr_3:

    push hl
    pop ix

    ld l,1

    ld bc,-10000
    call i16tostr_one
    ld bc,-1000
    call i16tostr_one
    ld bc,-100
    call i16tostr_one
    ld bc,-10
    call i16tostr_one
    ld c,-1
i16tostr_one:
    ld a,47
i16tostr_two:
    inc a
    add ix,bc
    jr c,i16tostr_two
    push bc
    push af
    ld a,b
    cpl
    ld b,a
    ld a,c
    cpl
    ld c,a
    inc bc
    call c, i16tostr_carry
    pop af
    add ix,bc
    pop bc
    ld h,a
    ld a,l
    cp 1
    jr z,i16tostr_two_2

    ld a,h
    ld (de),a
    inc de
    ret

i16tostr_two_2:
    ld a,h
    cp 48
    ret z
    ld l,0
    ld (de),a
    inc de
    ret

i16tostr_carry:
    dec bc
    ret
ret
