; ****************************************************************************
; MFP.ASM (by Erdogan Tan)
;
; TRDOS Kernel Test program for
; INT 21h Function 42h, Move File Pointer,

; 09/04/2011
;
; ****************************************************************************

PSP_CommandTail equ 80h

.8086

SINGLIXBOOT     SEGMENT PUBLIC 'CODE'
                assume cs:SINGLIXBOOT,ds:SINGLIXBOOT,es:SINGLIXBOOT,ss:SINGLIXBOOT

                org 100h
START_CODE:

proc_start      proc near

                xor bh, bh
               
                mov si, PSP_CommandTail
                mov bl, byte ptr [SI]
                or bl, bl                               
                jnz short loc_open_file_int21h  ; jump if not zero

                int 20h

loc_open_file_int21h:
                inc si
                mov byte ptr [SI][BX], bh     ; 0 
loc_open_file_int21h_next:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h                   ; is it SPACE ?
                ja short loc_open_file_int21h_fcall

                dec bl                                  
                jnz short loc_open_file_int21h_next
                
                int 20h

loc_open_file_int21h_fcall:
                call proc_open_file
                jc short loc_open_file_int21h_err

                mov word ptr [File_Handle], ax
                jmp short loc_open_file_int21h_ok 

loc_open_file_int21h_err:
                push ax
                mov al, ah
                call proc_hex 
                mov word ptr [Error_Number], ax
                pop ax 
                call proc_hex 
                mov word ptr [Error_Number]+2, ax

                mov si, offset Error_Number_Str
                call PROC_PRINTMSG

                int 20h 

loc_open_file_int21h_ok:
                mov al, 0
                mov bx, ax
                add bx, 30h  
                mov byte ptr [MFP_Mode], bl
                mov cx, 0FFFFh
                mov dx, 0FFFFh
                mov bx, word ptr [File_Handle]  
                call proc_move_file_pointer

                push ax
                mov ax, dx

                call proc_hex_double

                mov word ptr [File_Pointer_hw], dx
                mov word ptr [File_Pointer_hw]+2, ax
                
                pop ax

                call proc_hex_double

                mov word ptr [File_Pointer_lw], dx
                mov word ptr [File_Pointer_lw]+2, ax

                mov si, offset File_Ptr_Str  
                call PROC_PRINTMSG
               
                mov al, 1
                mov bx, ax
                add bx, 30h  
                mov byte ptr [MFP_Mode], bl
                mov cx, 0FFFFh
                mov dx, 0FFFFh
                mov bx, word ptr [File_Handle]  
                call proc_move_file_pointer

                push ax
                mov ax, dx

                call proc_hex_double

                mov word ptr [File_Pointer_hw], dx
                mov word ptr [File_Pointer_hw]+2, ax
                
                pop ax

                call proc_hex_double

                mov word ptr [File_Pointer_lw], dx
                mov word ptr [File_Pointer_lw]+2, ax

                mov si, offset File_Ptr_Str  
                call PROC_PRINTMSG

                mov al, 2
                mov bx, ax
                add bx, 30h  
                mov byte ptr [MFP_Mode], bl
                mov cx, 0FFFFh
                mov dx, 0FFFFh
                mov bx, word ptr [File_Handle]  
                call proc_move_file_pointer

                push ax
                mov ax, dx

                call proc_hex_double

                mov word ptr [File_Pointer_hw], dx
                mov word ptr [File_Pointer_hw]+2, ax
                
                pop ax

                call proc_hex_double

                mov word ptr [File_Pointer_lw], dx
                mov word ptr [File_Pointer_lw]+2, ax

                mov si, offset File_Ptr_Str  
                call PROC_PRINTMSG

                mov al, 0
                mov bx, ax
                add bx, 30h  
                mov byte ptr [MFP_Mode], bl
                mov cx, 0
                mov dx, 1
                mov bx, word ptr [File_Handle]  
                call proc_move_file_pointer

                push ax
                mov ax, dx

                call proc_hex_double

                mov word ptr [File_Pointer_hw], dx
                mov word ptr [File_Pointer_hw]+2, ax
                
                pop ax

                call proc_hex_double

                mov word ptr [File_Pointer_lw], dx
                mov word ptr [File_Pointer_lw]+2, ax

                mov si, offset File_Ptr_Str  
                call PROC_PRINTMSG
               
                mov al, 1
                mov bx, ax
                add bx, 30h  
                mov byte ptr [MFP_Mode], bl
                mov cx, 0FFFFh
                mov dx, 0FFFEh
                mov bx, word ptr [File_Handle]  
                call proc_move_file_pointer

                push ax
                mov ax, dx

                call proc_hex_double

                mov word ptr [File_Pointer_hw], dx
                mov word ptr [File_Pointer_hw]+2, ax
                
                pop ax

                call proc_hex_double

                mov word ptr [File_Pointer_lw], dx
                mov word ptr [File_Pointer_lw]+2, ax

                mov si, offset File_Ptr_Str  
                call PROC_PRINTMSG

                mov al, 2
                mov bx, ax
                add bx, 30h  
                mov byte ptr [MFP_Mode], bl
                mov cx, 0FFh
                mov dx, 0FFFFh
                mov bx, word ptr [File_Handle]  
                call proc_move_file_pointer

                push ax
                mov ax, dx

                call proc_hex_double

                mov word ptr [File_Pointer_hw], dx
                mov word ptr [File_Pointer_hw]+2, ax
                
                pop ax

                call proc_hex_double

                mov word ptr [File_Pointer_lw], dx
                mov word ptr [File_Pointer_lw]+2, ax

                mov si, offset File_Ptr_Str  
                call PROC_PRINTMSG

loc_close_file_int21h:
                mov bx, word ptr [File_Handle] 
                mov ah, 3Eh ; MS DOS Function = Close File
                int 21h
               ;jc short loc_open_file_int21h_err

                int 20h

proc_start      endp


PROC_PRINTMSG     proc near
 
LOC_PRINTMSG_LOOP:
                lodsb                           ; Load byte at DS:SI to AL
                and     AL,AL            
                jz      short LOC_PRINTMSG_OK       
                mov     AH,0Eh                  
                mov     BX,07h             
                int     10h                     ; BIOS Service func ( ah ) = 0Eh
                                                ; Write char as TTY
                                                ;AL-char BH-page BL-color
                jmp     short LOC_PRINTMSG_LOOP           

LOC_PRINTMSG_OK:
                retn

PROC_PRINTMSG     endp

proc_open_file proc near

                mov     ah,0Eh
                mov     bx,07h
                mov     al,0Dh
                int     10h
                mov     al,0Ah
                int     10h

loc_open_file:
                mov dx, si
                mov ah, 3Dh ; MS DOS Function = Open File
                mov al, 0
                int 21h
                               
                retn

proc_open_file endp


;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
; From binary (byte) to hexadecimal (character) converter    ;
;                                                            ;
; input -> AL = byte (binary number) to be converted         ;
; output -> AH = First character of hexadecimal number       ;
; output -> AL = Second character of hexadecimal number      ;
;                                                            ;
; (c) Erdogan TAN  1998 - 1999                               ;
;............................................................;

; 1998

proc_hex        proc    near

		db 0D4h,10h                     ; Undocumented inst. AAM
						; AH = AL / 10h
						; AL = AL MOD 10h
		or AX,'00'                      ; Make it ZERO (ASCII) based

                xchg AH,AL 

; 1999
		cmp AL,'9'
		jna pass_cc_al
		add AL,7
pass_cc_al:
		cmp AH,'9'
		jna pass_cc_ah
		add AH,7
pass_cc_ah:

; 1998
		retn

proc_hex        endp


;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
; From binary (word) to hexadecimal (character) converter    ;
;                                                            ;
; input -> AX = word (binary number) to be converted         ;
; output -> DX = First 2 characters of hexadecimal number    ;
; output -> AX = Last 2 characters of hexadecimal number     ;
;                                                            ;
; (c) Erdogan Tan  [ 28/1/2002 ]                             ;
;............................................................;

proc_hex_double  proc    near

                 push cx
                 xor dx, dx
                 mov cx, 10h
                 div cx      ; Q in AX, R in DX (DL)
                 xchg dh, dl ; DH= 0, R in DL <- CX= 10h 
                 push dx     ; R in DH, DL= 0
                 xor dh, dh
                 div cx
                 push dx     ; R in DL, DH= 0, AX <= FFh
                 div cl      ; AX <= 0Fh
                             ; R in AH, Q in AL, AL (Q) is 0
                 push ax
                 pop dx      ; High Word Characters is in DX

                 pop cx      ; R in CL
                 pop ax      ; R in AH, AL = 0
                 or al, cl
                 
                 or DX,'00'

                 cmp DL,'9'
                 jna short pass_dcc_dl
                 add DL,7
pass_dcc_dl:
                 cmp DH,'9'
                 jna short pass_dcc_dh
                 add DH,7
pass_dcc_dh:
                 or AX, '00'

                 cmp AL,'9'
                 jna short pass_dcc_al
                 add AL,7
pass_dcc_al:
                 cmp AH,'9'
                 jna short pass_dcc_ah
                 add AH,7
pass_dcc_ah:
                 pop cx

                 retn

proc_hex_double  endp


proc_move_file_pointer proc near
                mov ah, 42h
               ;mov bx, word ptr [File_Handle]
                int 21h
                retn
proc_move_file_pointer  endp

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  data
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

File_Ptr_Str: db 'File Pointer '
              db '('
MFP_Mode:     db 0
              db ') = '    
File_Pointer_hw: dd 30303030h
File_Pointer_lw: dd 30303030h
                 db 'h '
                 db 0Dh, 0Ah, 0 
                    
Error_Number_Str:
                db "Error Number: "
Error_Number:
		dd  0
                db "h"
		db  0Dh, 0Ah, 0

File_Read_Error_Str:
                db 'File Read Error !' 
Next_Line:
		db  0Dh, 0Ah, 0

Show_Byte_Count: dw 0

Page_Number:    dw 0

File_Handle:    dw 0

File_Buffer:    db 1 dup (0)

FBuffer_Size    equ $ - offset File_Buffer
                db 0 
                
              
SINGLIXBOOT     ends

                end     START_CODE
