; count.asm  Self incrementing program (writes to itself to increment the
;            count value  Outputs a HEX value onscreen. (up to 255 decimal)
;
;            Third byte in the com file is the actual count value.
;
        .model  tiny
        .code

        org     100h

Begin:
        jmp     short Start

Counter     db  0
FileName    db  'COUNT.COM',0

Start   proc
        inc     Counter      ; add one to Counter
        mov     ax,3D01h     ; open file for read/write
        lea     dx,FileName
        int     21h
        mov     bx,ax        ; bx contains file handle
        mov     ah,40h       ; write to a file with handle
        mov     cx,3         ; number of bytes to write
        lea     dx,Begin
        int     21h
        mov     bl,Counter
        mov     cl,4
        call    PutHex
PutHex:
        ror     bl,cl
        mov     al,bl
        and     al,0fh
        cmp     al,10
        sbb     al,69h
        das
        int     29h
        ret
        endp

        end Begin

