Прерывания DOS и работа с файлами, страница 2

FAT_OFFSET dw 0        ; первый сектор FAT

ROOT_OFFSET dw 0       ; первый сектор root

SECTORSIZE dw 512      ; размер сектора

CLUSTERSTRING db 'Number: $'

NUMCLUSTER dw 0

OSET dw 0

CURRENT_SECTOR dw 0 ; текущий сектор отн начала FAT

OFFSET_IN_SECTOR dw 0 ; смещение байта в секторе

FIRST_BYTE db 0

SECOND_BYTE db 0

ODD dw 0

data ends

stk segment  stack

db 256 dup(0)

stk ends

code segment  para public 'code'

outtitle macro Title

push ax

push dx

mov ah,9

lea dx,Title

int 21h

pop dx

pop ax

endm

byte_to_string macro argbyte, String  ; преобразование байта в строку.

; Строка оканчивается нулевым байтом.

; Могут быть лидирующие нули

push ax

push cx

push di

xor ax,ax

lea di,String

mov al,argbyte

mov cl,100

div cl

add al,30h

mov [di],al

inc di

mov al,ah

mov ah,00h

mov cl,10

div cl

add al,30h

mov [di],al

inc di

add ah,30h

mov [di],ah

inc di

mov byte ptr [di],00h

pop di

pop cx

pop ax

endm

asciiz_print proc   ; печать строки, оканчивающейся нулём или символом $

assume cs:code,ds:data,ss:stk,es:data

push bp

mov bp,sp

push ax

push cx

push dx

push di

xor cx,cx

mov di,[bp+4]

circ:   cmp byte ptr [di],00h

je print

cmp byte ptr [di],'$'

je print

inc cx

inc di

jmp circ

print:

cmp cx,00h

je exit_p

mov ah,40h

mov bx,1

mov dx,[bp+4]

int 21h

exit_p: pop di

pop dx

pop cx

pop ax

pop bp

ret

asciiz_print endp

asciiz_input proc ; ввод строки ASCIIZ c клавиатуры

; входной параметр- адрес строки

push bp

mov bp,sp

push si

push bx

push dx

push ax

mov bx,[bp+4]

mov byte ptr [bx],0FEh

mov ah,0Ah

mov dx,bx

int 21h

inc bx

xor ax,ax

mov al,[bx]

inc bx

mov [bp+4],bx

add bx,ax

mov [bx],byte ptr 00h

pop ax

pop dx

pop bx

pop si

pop bp

ret

asciiz_input endp

file_work proc   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

push bp

mov bp,sp

push ax

push bx

push cx

push dx

filecircle:

outtitle M_FILE_CLOSE

outtitle M_FILE_WRITE

outtitle M_FILE_READ

outtitle M_QUIT

mov ah,00h

int 16h

cmp ah,01h

jne file_L1

jmp file_exit

file_L1:

cmp ah,02h

jne  file_L2

mov ah,3Eh

mov bx,[bp+4]

int 21h

jnc jm

jmp file_error

jm:     jmp file_exit

file_L2:  ; запись в файл

cmp ah,03h

jne  file_L3

outtitle M_INPUT_STRING

xor ax,ax

mov ah,0Ah

lea dx,PATH

mov bx,dx

mov byte ptr [bx],0FFh

int 21h

xor cx,cx

mov cl,[bx+1]

add dx,2

mov bx,[bp+4]

mov ah,40h

int 21

jnc filecircle

file_L3:

cmp ah,04h

jne filecircle

xor ax,ax

mov ah,3Fh

mov bx,[bp+4]

mov cx,0200h

lea dx,PATH

int 21

jc file_error

outtitle T_READED_BYTES

mov cl,al

byte_to_string cl,String

lea cx,String

push cx

CALL asciiz_print

pop cx

jmp filecircle

file_error:

outtitle M_ERROR

mov dl,al     

byte_to_string dl,String

lea ax,String

push ax

CALL asciiz_print

pop ax

jmp filecircle

file_exit:

pop dx

pop cx

pop bx

pop ax

pop bp

ret

file_work endp

outstring macro NL,Title,String,ilength

push ax

push cx

push dx

push bx

mov ah,40h

mov bx,1

mov cx,2

lea dx,NL

int 21h

mov ah,9

lea dx,Title

int 21h

mov ah,40h

mov cx,ilength

mov dx,offset String

int 21h

pop bx

pop dx

pop cx

pop ax

endm

outnumber macro NL,Title,Source,Buffer,ilength

local circle

local continue1

local continue2

push ax

push cx

push dx

push bx

push si

push di

mov ah,40h

mov cx,2

mov dx,offset NL

int 21h

mov ah,9

mov dx,offset Title

int 21h

mov cx,ilength

mov bx, offset Source

mov si,ilength

sub si,1

mov di, offset Buffer

circle :      

mov al,[bx][si]

mov dl,al

shr al,4

add al,30h

cmp al,39h

jle continue1

add al,7h

continue1:    

mov [di],al

inc di

mov al,dl

and al,0fh

add al,30h

cmp al,39h

jle continue2

add al,7h

continue2:    

mov [di],al

inc di

dec si

loop circle

mov byte ptr [di],26h

mov ah,40h

mov bx,1

mov cx,ilength*2

mov dx,offset Buffer

int 21h

pop di

pop si

pop bx

pop dx

pop cx

pop ax 

endm

main proc

assume cs:code,ds:data,ss:stk,es:data

mov ax,data

mov ds,ax

mov es,ax

cld

circle:

outtitle M_CATALOGDETECT

outtitle M_DRIVECOUNT

outtitle M_CREATECATALOG

outtitle M_CREATEFILE

outtitle M_OPENFILE

outtitle M_QUIT

mov ah,00h

int 16h

cmp ah,01h

jne  L1       

jmp exit

L1:     cmp ah,02h     ;текущие каталог и диск

jne  L2

mov ah,19h

int 21h

mov bl,al

outtitle T_DRIVE

byte_to_string bl,String

lea ax,String

push ax

CALL asciiz_print

pop ax

mov ah,47h

mov dl,00h

lea si,PATH

int 21h

outtitle T_FOLDER

lea ax,PATH

push ax

CALL asciiz_print

pop ax

jmp circle

L2:   cmp ah,03h       ;количество дисков

jne L3

mov ah,19h

int 21h

mov dl,al

mov ah,0eh

int 21h

mov bl,al

outtitle T_DISKCOUNT

byte_to_string bl,String

lea ax,String

push ax

CALL asciiz_print

pop ax

jmp circle

L3:     cmp ah,04h     ;создание каталога

jne L4

outtitle T_CATALOG_NAME

lea ax,PATH

push ax

CALL asciiz_input

pop dx

mov ah,3Ch

mov cx,10h

int 21h

jc error

outtitle M_FOLDER_CREATED

jmp circle

error: outtitle M_ERROR

mov bl,al     

byte_to_string bl,String

lea ax,String

push ax

CALL asciiz_print

pop ax

jmp circle

L4:     cmp ah,05h     ;создание файла

jne L5

outtitle T_FILE_NAME

lea ax,PATH

push ax

CALL asciiz_input

pop dx

mov ah,3Ch

mov cx,00h

int 21h

jc create_error

outtitle M_FILE_CREATED

mov FILEDESC,ax

push ax

CALL file_work ; работа с созданным файлом

pop ax

jmp circle

create_error: outtitle M_ERROR

mov bl,al     

byte_to_string bl,String

lea ax,String

push ax

CALL asciiz_print

pop ax

jmp circle

L5 :    cmp ah,06h

jne L7

outtitle T_FILE_NAME

lea ax,PATH

push ax

CALL asciiz_input

pop dx

mov ah,3Dh

mov al,02h

int 21h

jc open_error

outtitle M_FILE_OPENED

mov FILEDESC,ax

push ax

CALL file_work ; работа с открытым файлом

pop ax

jmp circle

open_error: outtitle M_ERROR

mov bl,al     

byte_to_string bl,String

lea ax,String

push ax

CALL asciiz_print

pop ax

jmp circle

L7:     jmp circle

exit:

mov ax,4c00h

int 21h

main endp

code ends

end main