        org 0100h
                JP KEZDES
BTAB             EQU 500H
FORRAS  EQU 4000H
EOF             EQU 0E4H
KEZDES  ld sp,0100h
        ld a,0fdh
        out (0b1h),a
        ld a,0fah
        out (0b2h),a
        ld a,0fbh
        out (0b3h),a
        ld hl,TAB
        ld de,TAB+1
        ld bc,100h
        ld (hl),l
        ldir
        ld a,1
        ld de,filename
        exos 1
        AND A
        JP NZ,HIBA
        ld a,1
        ld de,FORRAS
        ld bc,-FORRAS
        exos 6
                CP EOF
                JP NZ,HIBA
        push de
        ld a,1
        exos 3
        pop hl
        ld de,FORRAS
        and a
        sbc hl,de
                LD (FILEH),HL
        ld B,h
        ld C,l
                PUSH BC
        di
                LD IX,FORRAS
                LD HL,BTAB
KUTAT           LD L,(IX)
                INC (HL)
                LD A,(HL)
                OUT (81H),A
                JR NZ,KUTAT1
                DEC (HL)
KUTAT1          INC IX
                DEC BC
                LD A,B
                OR C
                JR NZ,KUTAT
                LD E,0
KUTAT2          LD L,0
                LD D,-1
KUTAT3          LD A,(HL)
                CP D
                JR NC,KUTAT4
                LD A,L
                OUT (81H),A
                LD E,A
                LD D,(HL)
                LD A,D
                AND A
                JR Z,JELKESZ
KUTAT4          INC L
                JR NZ,KUTAT3
JELKESZ LD A,E
        LD (JEL),A
TOMOR   LD HL,1000H
        ld ix,FORRAS
        pop bc
        push bc
kov     ld a,(jel)
        cp (ix)
        jr z,tom
        ld a,(ix)
        cp (ix+1)
        jr nz,ntom
        cp (ix+2)
        jr nz,ntom
        cp (ix+3)
        jr nz,ntom
tom     ld d,0
        ld e,a
szamol  ld a,d
        add a,1
                OUT (81H),A
        ld d,a
        jr c,sok
        pop bc
        dec bc
        ld a,b
        or c
        jr z,veget
        push bc
        inc ix
        ld a,(ix)
        cp e
        jr z,szamol
        jr tov
sok     pop bc
        dec bc
        ld a,b
        or c
        jr z,veget
        push bc
        inc ix
tov     ld a,(jel)
                ld (HL),A
                INC HL
                LD (HL),D
                INC HL
                LD (HL),E
                INC HL
        jr kov
ntom    ld b,(ix)
                LD (HL),B
                INC HL
        inc ix
        pop bc
        dec bc
        ld a,b
        or c
        push bc
        jr nz,kov
        jr zar
veget   ld a,(jel)
                LD (HL),A
                INC HL
                LD (HL),D
                INC HL
                LD (HL),E
                INC HL
ZAR
                LD A,H
                SUB 10H
                LD B,A
                LD C,L
                LD (BYTTH),BC
                ADD A,40H
                LD D,A
                LD E,L
                DEC DE
                DEC HL
                LDDR
TAB             EQU 600H   ;200 hosszu
REND            EQU 800H   ;100 -,,-
SZAMTAB         EQU 900H   ;200 -,,-
FILEK           EQU 4000H
TOMFK           EQU 1000H
                JP STATOZ
TOMFILE         DB 2,"DT"
BYTTH           DW 0
FILEH           DW 0
RENDH           DW 0
BYTE            NOP
BAZIS           NOP
DATB            EQU 0
BEVB            EQU $-BAZIS
                NOP
BIT8            EQU $-BAZIS
                NOP
DATH            EQU $-BAZIS
                NOP
STATOZ
                CALL STATISZ
                CALL OPTIMUM
                CALL TOMORIT
                CALL KIMENT
                LD DE,EXDOS
                EXOS 1AH
                JP 100H
TFHO            DW 0
OPTREND         DW 0
RSZTAB          DB 1,2,3,8,2,2,3,8,1,3,3,8,2,3,3,8
                DB 3,3,3,8,1,2,4,8,2,2,4,8,1,3,4,8
                DB 2,3,4,8,3,3,4,8,1,4,4,8,2,4,4,8
                DB 3,4,4,8,4,4,4,8,1,2,5,8,2,2,5,8
                DB 1,3,5,8,2,3,5,8,3,3,5,8,1,4,5,8
                DB 2,4,5,8,3,4,5,8,4,4,5,8,1,5,5,8
                DB 2,5,5,8,3,5,5,8,4,5,5,8,5,5,5,8
                DB 1,2,6,8,2,2,6,8,1,3,6,8,2,3,6,8
                DB 3,3,6,8,1,4,6,8,2,4,6,8,3,4,6,8
                DB 4,4,6,8,1,5,6,8,2,5,6,8,3,5,6,8
                DB 4,5,6,8,5,5,6,7
RSZSZAM         EQU ($-RSZTAB)/4
OPTIMUM         LD IY,RSZTAB
                LD B,RSZSZAM
                LD HL,-1
                LD (TFHO),HL
                PUSH IY
OPT1            CALL HELYFOG
                LD DE,(TFHO)
                AND A
                SBC HL,DE
                JR NC,OPT2
                ADD HL,DE
                LD (TFHO),HL
                POP HL
                PUSH IY
OPT2            DJNZ OPT1
                POP HL
                DEC HL
                DEC HL
                DEC HL
                DEC HL
                LD (OPTREND),HL
                RET
HELYF1          DW 0
HELYF2          DW 0
HELYFOG         PUSH BC
                LD IX,SZAMTAB
                CALL OSSZEAD1
                LD (HELYF1),HL
                LD (HELYF2),BC
                INC IY
                CALL OSSZEAD1
                CALL OSSZEAD2
                INC IY
                CALL OSSZEAD1
                CALL OSSZEAD2
                INC IY
                PUSH IX
                POP DE
                LD HL,SZAMTAB
                AND A
                SBC HL,DE
                RR H
                RR L
                LD A,L
                CALL OSSZEAD3
                CALL OSSZEAD2
                INC IY
                LD HL,(HELYF1)
                LD DE,(HELYF2)
                LD B,3
                AND A
HELYFC          RR D
                RR E
                RR H
                RR L
                DJNZ HELYFC
                POP BC
                RET
OSSZEAD1        LD B,(IY)
                LD A,1
OSSZE11         RLCA
                DJNZ OSSZE11
OSSZEAD3        LD B,A
                LD HL,0
OSSZE12         LD E,(IX)
                INC IX
                LD D,(IX)
                INC IX
                ADD HL,DE
                DJNZ OSSZE12
                LD D,H
                LD E,L
                LD C,B
                ADD HL,HL
                JR NC,OSSZE13
                INC C
OSSZE13         LD A,(IY)
OSSZE14         ADD HL,DE
                JR NC,OSSZE15
                INC C
OSSZE15         DEC A
                JR NZ,OSSZE14
                RET
OSSZEAD2        LD DE,(HELYF1)
                ADD HL,DE
                JR NC,OSSZE21
                INC BC
OSSZE21         LD (HELYF1),HL
                LD HL,(HELYF2)
                ADD HL,BC
                LD (HELYF2),HL
                RET
KIMENT          LD A,1
                LD DE,TOMFILE
                EXOS 2
                LD HL,REND
                LD BC,(RENDH)
                LD A,C
                AND A
                JR NZ,RENDMENT
                LD B,1
RENDMENT        ADD HL,BC
                DEC HL
                LD DE,TOMFK
                DEC DE
                LDDR
                LD C,2
                LD HL,RENDH+1
                LDDR
                LD C,4
                LD HL,(OPTREND)
                INC HL
                INC HL
                INC HL
                LDDR
                LD A,(JEL)
                LD (DE),A
                DEC DE
                LD C,2
                LD HL,FILEH+1
                LDDR
                EX DE,HL
                PUSH IX
                POP DE
                PUSH DE
                LD A,D
                SUB 10H
                LD (HL),A
                DEC HL
                LD (HL),E
                POP DE
                EX DE,HL
                AND A
                SBC HL,DE
                LD B,H
                LD C,L
                LD A,1
                EXOS 8
                LD A,1
                EXOS 3
                RET
TOMORIT         LD IY,BAZIS
                LD BC,(BYTTH)
                LD DE,FILEK
                LD IX,TOMFK
                LD A,1
                LD (IY+BIT8),A
TOMCIK          LD A,(DE)
                LD (BYTE),A
                PUSH BC
                PUSH DE
                CALL BEAZON
                CALL KIAD
                POP DE
                INC DE
                POP BC
                PUSH AF
                DEC BC
                LD A,B
                OR C
                JR Z,TOMVEG
                POP AF
                JR TOMCIK
TOMVEG          POP AF
                RET C
URTOL           CALL BITKI
                JR NC,URTOL
                RET
BEAZON          LD HL,REND
                LD DE,(OPTREND)
                LD (IY+BEVB),0
BEALLC          LD (IY+DATB),0
                LD A,(DE)
                LD (IY+DATH),A
                LD B,A
                XOR A
                INC A
BEALL1          RLA
                DJNZ BEALL1
                LD B,A
                LD A,(BYTE)
BEALL2          CP (HL)
                RET Z
                INC (IY+DATB)
                INC HL
                DJNZ BEALL2
                INC (IY+BEVB)
                INC DE
                JR BEALLC
BITKI           RL (IX)
BITELL          RLC (IY+BIT8)
                RET NC
                EX AF,AF'
                LD A,(IX)
                OUT (81H),A
                EX AF,AF'
                INC IX
                RET
KIAD            RRC (IY+BEVB)
                RRC (IY+BEVB)
                RL (IY+BEVB)
                CALL BITKI
                RL (IY+BEVB)
                CALL BITKI
                LD B,(IY+DATH)
                PUSH BC
                LD A,(IY+DATB)
ELOTOL          RRCA
                DJNZ ELOTOL
                POP BC
KIRAK           RLA
                CALL BITKI
                DJNZ KIRAK
                RET
STATISZ         LD HL,TAB
                LD DE,TAB+1
                LD BC,4FFH
                LD (HL),0
                LDIR
                LD DE,TAB
                LD HL,FILEK
                LD BC,(BYTTH)
SZAM1           LD E,(HL)
                EX DE,HL
                INC (HL)
                JR NZ,SZAM2
                INC H
                INC (HL)
                DEC H
SZAM2           EX DE,HL
                INC HL
                DEC BC
                LD A,B
                OR C
                JR NZ,SZAM1
                LD IX,REND
                LD IY,SZAMTAB
REND1           LD HL,TAB
                LD A,-1
                EXX
                LD HL,0
                EXX
REND2           INC H
                LD D,(HL)
                DEC H
                CP D
                JR Z,REND4
                LD E,(HL)
                PUSH DE
                EXX
                POP DE
                AND A
                SBC HL,DE
                ADD HL,DE
                JR NC,REND3
                EX AF,AF'
                EX DE,HL
                EXX
                LD A,L
                EXX
                EX AF,AF'
REND3           EXX
REND4           INC L
                JR NZ,REND2
                EXX
                LD A,H
                OR L
                JR Z,REND5
                LD (IY),L
                INC IY
                LD (IY),H
                INC IY
                EXX
                EX AF,AF'
                LD L,A
                EX AF,AF'
                INC H
                LD (HL),-1
                LD (IX),L
                INC IX
                DJNZ REND1
REND5           PUSH IX
                POP HL
                LD A,L
                LD (RENDH),A
                RET
HIBA            LD DE,EXDOS
                EXOS 26D
         JP 100H
EXDOS           DEFB 5,"EXDOS"
jel     db 0
filename db 3
        defm "prg"
