ORG 100H

        LXI H,2
P1      LXI D,2
P2      MOV A,E
        SUB L
        MOV A,D
        SBB H
        JP P4
        PUSH D
        PUSH H
        CALL DIV
        ORA D
        POP H
        POP D
        JZ P5
P3      INX D
        JMP P2
P4      PUSH H
        CALL PUTNUM
        CALL CRLF
        POP H
P5      INX H
        JMP P1
        RET
CRLF    MVI E,0DH
        MVI C,2
        CALL 5
        MVI E,0AH
        MVI C,2
        CALL 5
        RET

PUTNUM  PUSH H
        LXI D,0AH
        CALL DIV
        PUSH A
        MOV A,H
        ORA L
        JZ ZERO
        CALL PUTNUM
ZERO    POP A
        POP H
        ADI 30H
        MOV E,A
        MVI C,2
        CALL 5
        RET

DIV     LXI B,0
        PUSH B
        LXI B,1
L1      MOV A,E
        SUB L
        MOV A,D
        SBB H
        JP L2
        MOV A,E
        ORA A
        RAL
        MOV E,A
        MOV A,D
        RAL
        MOV D,A
        MOV A,C
        ORA A
        RAL
        MOV C,A
        MOV A,B
        RAL
        MOV B,A
        JMP L1
L2      MOV A,L
        SUB E
        MOV A,H
        SBB D
        JM L3
        MOV A,L
        SUB E
        MOV L,A
        MOV A,H
        SBB D
        MOV H,A
        XTHL
        MOV A,L
        ADD C
        MOV L,A
        MOV A,H
        ADC B
        MOV H,A
        XTHL
L3      MOV A,D
        ORA A
        RAR
        MOV D,A
        MOV A,E
        RAR
        MOV E,A
        MOV A,B
        ORA A
        RAR
        MOV B,A
        MOV A,C
        RAR
        MOV C,A
        JNC L2
        MOV D,H
        MOV E,L
        MOV A,L
        POP H
        RET
