BufSize  equ  2048 

TRUE     equ  -1
FALSE    equ  0

code            segment byte public
                assume  cs:code, ds:code

		org	100h

textview        proc    far

start:          jmp     initialize

include \nxdos\src\toolbox.asm 

initialize:

main:
        _newline
        _echo '>'
        lea  di, filename
        mov  cx, 127
        call getdata

        push cs
        pop  ds
        lea  dx, filename
        mov  cx, 0
        call fopen
        mov  handle, ax

; read file into buffer



;OPEN FILE
    mov  dx, offset filespec
    mov  al,0           ;open for reading
    mov  ah,3dh         ;open file
    int  21h
    mov  [handle],ax    ;save handle
    jnc  FileLoop
    mov  si, offset tverror
    call WriteString    ;print error message
    jmp  ExitToDos

;READ FILE
FileLoop:
    mov  ah,3Fh         ;read file
    mov  bx,[handle]
    mov  cx,BufSize     ;get full buffer
    mov  dx, offset Store       ;start of buffer
    int  21h
    jc   Error
    mov  [BytesRead],ax ;store number of bytes read
    cmp  ax,cx          ;was buffer full?
    je   RFDone         ;Yes, so Read file done
SetEOF:
    mov  BYTE [EOF],TRUE     ;No, so EOF reached
    jmp  RFDone
Error:
    cmp  ax,0      ;was it because EOF reached?
    je   SetEOF    ;yes so set flag
RFDone:

;PRINT BUFFER
    push ax
    push bx
    push dx
    push si
    push cx   ;max characters to print
    mov  cx,[BytesRead]
    mov  bx, offset Store  ;point to start of store
    mov  si,0      ;count bytes
MoreChars:
    mov  dl,[bx+si]
    cmp  dl,10
    jnz  NotLF
    inc  BYTE [LineCount]
NotLF:
    mov  ah,2
    int  21h  ;print character
;SCREEN FULL YET?
    cmp  BYTE [LineCount],23
    jne  NoPause
    call PagePause ;pause
    mov  BYTE [LineCount],0  ;set line count
NoPause:
    inc  si
    cmp  si,[BytesRead]        ;count chars
    jb   MoreChars

    pop  cx
    pop  si
    pop  dx
    pop  bx
    pop  ax
    cmp  BYTE [EOF],FALSE
    je   FileLoop

ExitToDos:
    mov  ax,4C00h
    int  21h

PagePause:
    push ax
    push bx
    push dx
    push si

    mov  si, offset PauseMessage
    call WriteString
    mov  ah,0
    int  16h       ;wait for keypress
    call CRLF
    pop  si
    pop  dx
    pop  bx
    pop  ax
    ret

WriteString:
    push ax
    push si
WriteChar:
    mov  al,[si]
    inc  si
    or   al,al     ;al=0
    jz   WriteExit
    mov  dl,al
    mov  ah,2
    int  21h  ;print character
    jmp  WriteChar
WriteExit:
    pop  si
    pop  ax
    ret

CRLF:
    push ax
    push dx
    mov  ah,2
    mov  dl,13
    int  21h
    mov  dl,10
    int  21h
    pop  dx
    pop  ax
    ret

exitptr:
          int  20h

sizefile proc far
         mov  ah, 42h
         mov  al, 2             ; 2 = EOF 
         mov  bx, [handle]
         xor  cx, cx
         mov  dx, cx
         int  21h
         ; dx:ax size
         ret
sizefile endp

string_length dw   0

filespec      db   127 dup(0),0

message       db   'Enter file path: ',0
tverror       db   'Bad file name, path not found, or access denied',13,10,0
PauseMessage  db   'Press any key to continue ...',0
EOF           db   0
LineCount     db   0

handle        dw   0
BytesRead     dw   0

Store         db BufSize dup(0),0

textview        endp

code            ends

ProgramEnd      EQU $-01h

		end	start



