; ****************************************************************************
; FILENAME.ASM (by Erdogan Tan)
;
; TRDOS Kernel Test program for
; INT 21h Function 56h, Rename File
;
; 18/09/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
                push ds
                pop es

                xor bh, bh
               
                mov si, PSP_CommandTail
                mov bl, byte ptr [SI]
                or bl, bl                               
                jnz short loc_rename_file_get_name1  ; jump if not zero

loc_rename_no_file_name:
                mov si, offset msg_usage
                call PROC_PRINTMSG

                int 20h

loc_rename_file_get_name1:
                inc si
                mov byte ptr [SI][BX], bh     ; 0 
loc_rename_file_get_name1_next:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h                   ; is it SPACE ?
                ja short loc_rename_file_move_first_name
                dec bl                                  
                jnz short loc_rename_file_get_name1_next
                jmp short loc_rename_no_file_name

loc_rename_file_move_first_name:
                mov di, offset old_file_name
                stosb
                inc si 
               ;mov cx, 63
                mov cl, 63
loc_rename_file_move_first_name_next:
                lodsb
                cmp al, 20h
                je short loc_rename_file_move_first_name_ok   
                jb short loc_rename_no_file_name
                stosb
                dec bl
                jz short loc_rename_no_file_name
                loop loc_rename_file_move_first_name_next
loc_rename_file_move_first_name_ok:
                mov byte ptr [DI], 0

loc_rename_file_get_name2:
loc_rename_file_get_name2_next:
                mov al, byte ptr [SI]
                cmp al, 20h                   ; is it SPACE ?
                ja short loc_rename_file_move_second_name
                inc si 
                dec bl                                  
                jnz short loc_rename_file_get_name2_next
                jmp short loc_rename_no_file_name

loc_rename_file_move_second_name:
                mov cl, 63
                mov di, offset new_file_name 
loc_rename_file_move_second_name_next:
                lodsb
                cmp al, 20h
                jna short loc_rename_file_move_second_name_ok
                stosb
                dec bl
                jz short loc_rename_no_file_name
                loop loc_rename_file_move_second_name_next

loc_rename_file_move_second_name_ok:
                mov byte ptr [DI], 0

loc_rename_file_int21h_call:
                mov dx, offset old_file_name
                mov di, offset new_file_name
                mov ah, 56h
                int 21h 
                jnc short loc_rename_file_ok_msg

loc_rename_file_int21h_error:
                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_rename_file_ok_msg:
                mov si, offset msg_ok
	        call proc_printmsg
              
                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


;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
; 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
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Msg_Usage:      db 07h
                db "filename <old file name> <new file name>"
                db 0Dh, 0Ah, 0
Msg_Ok:         db "OK."
                db 0Dh, 0Ah, 0  

Error_Number_Str:
                db "Error Number: "
Error_Number:
		dd  0
                db "h"
		db  0Dh, 0Ah, 0
           
old_file_name:  db 64 dup(0)
new_file_name:  db 64 dup(0)

SINGLIXBOOT     ends

                end     START_CODE
