; ****************************************************************************
; OPENFILE.ASM (by Erdogan Tan)
;
; Test program for
; INT 21h Function 3Dh, Open File
; 26/02/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
                jnc 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

loc_open_file_int21h_ok:
                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, 2
                int 21h
                jc short loc_open_file_ok
               
                push ax
                mov al, ah
                call proc_hex 
                mov word ptr [File_Number], ax
                pop ax 
                call proc_hex 
                mov word ptr [File_Number]+2, ax
 
                mov si, offset File_Number_Str
                call PROC_PRINTMSG

loc_open_file_ok:
                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


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  data
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

File_Number_Str:
                db "File Number: "
File_Number:
		dd  0
                db "h"
		db  0Dh, 0Ah, 0

Error_Number_Str:
                db "Error Number: "
Error_Number:
		dd  0
                db "h"
		db  0Dh, 0Ah, 0
              
SINGLIXBOOT     ends

                end     START_CODE
