From: Robert Pengelly Date: Mon, 12 Aug 2024 03:51:06 +0000 (+0100) Subject: Write data to file, bug fixes and improvements X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=919723032fb1bda50a8a8be50805441d7863c685;p=chimaera.git Write data to file, bug fixes and improvements --- diff --git a/build/chimaera.img b/build/chimaera.img index 2979e9a..1a20b31 100644 Binary files a/build/chimaera.img and b/build/chimaera.img differ diff --git a/build/chimaera.vhd b/build/chimaera.vhd index cb81b98..3868ac9 100644 Binary files a/build/chimaera.vhd and b/build/chimaera.vhd differ diff --git a/src/apps/hello/hello.asm b/src/apps/hello/hello.asm index 805c24e..5f26689 100644 --- a/src/apps/hello/hello.asm +++ b/src/apps/hello/hello.asm @@ -19,60 +19,188 @@ _main: mov bx, offset _msg_hello call _writestr - mov ah, HEX (39) - mov dx, offset _new_dir - int HEX (21) - jc .exit - - mov ah, HEX (3A) - mov dx, offset _new_dir - int HEX (21) - jc .exit - - mov bx, offset _msg_status - call _writestr - - mov ah, HEX (3C) - mov cx, HEX (20) - mov dx, offset _new_file - int HEX (21) - jc .exit - - call _writehex - call _crlf - - mov bx, ax - - mov ax, HEX (4202) - mov cx, 4096 - xor dx, dx - int HEX (21) - jc .exit - - call _writehex - call _crlf - - mov ah, HEX (40) - xor cx, cx - xor dx, dx - int HEX (21) - jc .exit - - mov ax, HEX (4200) - mov cx, 1000 - xor dx, dx - int HEX (21) - jc .exit - - call _writehex - call _crlf - - mov ah, HEX (40) - xor cx, cx - xor dx, dx - int HEX (21) - jc .exit - +; mov ax, offset _flags +; push ax +; +; mov ax, offset _existing_file +; push ax +; +; call _fopen +; add sp, 4 +; +; and ax, ax +; jz .exit +; +; mov cs:[_file_pointer], ax +; +; mov bx, offset _msg_status +; call _writestr +; +; mov ax, cs:[_file_pointer] +; push ax +; +; mov ax, 512 +; push ax +; +; mov ax, 1 +; push ax +; +; mov ax, offset _buffer +; push ax +; +; call _fread +; add sp, 8 +; +; and ax, ax +; jz .exit +; +; mov cs:[_to_write], ax +; +; mov ax, cs:[_file_pointer] +; push ax +; +; call _fclose +; add sp, 2 +; +; mov bx, offset _msg_status +; call _writestr +; +; mov ax, offset _flags +; push ax +; +; mov ax, offset _new_file +; push ax +; +; call _fopen +; add sp, 4 +; +; and ax, ax +; jz .exit +; +; mov cs:[_file_pointer], ax +; +; mov bx, offset _msg_status +; call _writestr +; +; mov cx, 5 +; +;.write_loop: +; +; mov ax, cs:[_file_pointer] +; push ax +; +; mov ax, cs:[_to_write] +; push ax +; +; mov ax, 1 +; push ax +; +; mov ax, offset _buffer +; push ax +; +; call _fwrite +; add sp, 8 +; +; cmp ax, cs:[_to_write] +; jne .exit +; +; mov bx, offset _msg_write +; call _writestr +; +; loop .write_loop +; +; mov ah, HEX (3C) +; mov cx, HEX (20) +; mov dx, offset _new_file +; int HEX (21) +; jc .exit +; +; call _writehex +; call _crlf +; +; mov bx, ax +; +; mov ax, HEX (4202) +; mov cx, 4096 +; xor dx, dx +; int HEX (21) +; jc .exit +; +; call _writehex +; call _crlf +; +; mov ah, HEX (40) +; xor cx, cx +; xor dx, dx +; int HEX (21) +; jc .exit +; +; mov ax, HEX (4200) +; xor cx, cx +; xor dx, dx +; int HEX (21) +; jc .exit +; +; mov ah, HEX (40) +; mov cx, 15 +; mov dx, offset _msg_hello +; int HEX (21) +; jc .exit +; +; mov ah, HEX (39) +; mov dx, offset _new_dir +; int HEX (21) +; jc .exit +; +; mov ah, HEX (3A) +; mov dx, offset _new_dir +; int HEX (21) +; jc .exit +; +; mov bx, offset _msg_status +; call _writestr +; +; mov ah, HEX (3C) +; mov cx, HEX (20) +; mov dx, offset _new_file +; int HEX (21) +; jc .exit +; +; call _writehex +; call _crlf +; +; mov bx, ax +; +; mov ax, HEX (4202) +; mov cx, 4096 +; xor dx, dx +; int HEX (21) +; jc .exit +; +; call _writehex +; call _crlf +; +; mov ah, HEX (40) +; xor cx, cx +; xor dx, dx +; int HEX (21) +; jc .exit +; +; mov ax, HEX (4200) +; mov cx, 1000 +; xor dx, dx +; int HEX (21) +; jc .exit +; +; call _writehex +; call _crlf +; +; mov ah, HEX (40) +; xor cx, cx +; xor dx, dx +; int HEX (21) +; jc .exit +; ; mov ah, HEX (3C) ; mov cx, HEX (20) ; mov dx, offset _new_file @@ -194,7 +322,12 @@ _main: ;; Data area. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; _msg_hello: db "Hello, world!", HEX (0D), HEX (0A), HEX (00) -_msg_status: db "File tests", HEX (0D), HEX (0A), HEX (00) -_new_dir: db "temp", HEX (00) -_new_file: db "temp.txt", HEX (00) +;_msg_write: db "Bytes written", HEX (0D), HEX (0A), HEX (00) +;_msg_status: db "File tests", HEX (0D), HEX (0A), HEX (00) +;_new_dir: db "temp", HEX (00) +;_new_file: db "temp.txt", HEX (00) ;_existing_file: db "\\boot\\freeldr\\freeldr.cfg", HEX (00) +;_file_pointer: dw HEX (0000) +;_flags: db "w+b", HEX (00) +;_buffer: db 512 dup (0) +;_to_write: dw HEX (0000) diff --git a/src/apps/pcomm/del.asm b/src/apps/pcomm/del.asm index d9c086e..abd4bd1 100644 --- a/src/apps/pcomm/del.asm +++ b/src/apps/pcomm/del.asm @@ -1,5 +1,5 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; @file type.asm +;; @file del.asm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; %ifndef HEX % define HEX(y) 0x##y @@ -14,7 +14,7 @@ _handler_del: push bp mov bp, sp - sub sp, 4 + sub sp, 6 push ax push bx @@ -25,6 +25,7 @@ _handler_del: push es push ds + mov word ptr [bp - 6], 0 mov word ptr [bp - 4], 0 mov word ptr [bp - 2], 0 @@ -57,7 +58,7 @@ _handler_del.ok: _handler_del.store_arg: cmp byte ptr [bx], 0 - je _handler_del.next + je _handler_del.check2 mov si, bx push si @@ -80,7 +81,7 @@ _handler_del.check: add sp, 4 and ax, ax - jz _handler_del.next + jz _handler_del.check2 mov bx, ax mov byte ptr [bx], 0 @@ -90,7 +91,7 @@ _handler_del.check: _handler_del.skip: cmp byte ptr [bx], 0 - je _handler_del.next + je _handler_del.check2 cmp byte ptr [bx], ' ' ja _handler_del.store_arg @@ -98,15 +99,20 @@ _handler_del.skip: inc bx jmp _handler_del.skip +_handler_del.check2: + + cmp word ptr cs:[_vec_files + 4], 1 + jbe _handler_del.next + _handler_del.next: - mov ax, word ptr [bp - 4] + mov ax, word ptr [bp - 6] inc ax cmp ax, cs:[_vec_files + 4] ja _handler_del.cleanup - mov word ptr [bp - 4], ax + mov word ptr [bp - 6], ax dec ax xor dx, dx @@ -121,107 +127,363 @@ _handler_del.next: mov es, ax mov ax, es:[bx] - mov bx, ax + mov dx, ax pop es + + call _delete_file + jmp _handler_del.next + +_handler_del.cleanup: + + mov ax, cs:[_vec_files + 0] + push ax + + call _free + add sp, 2 + + mov ax, cs + mov es, ax + + mov di, offset _vec_files + xor al, al + + mov cx, 6 + rep stosb -_handler_del.delete: +_handler_del.done: + pop ds + pop es + pop di + pop si + pop dx + pop cx + pop bx + pop ax + + add sp, 6 + pop bp + + ret + +;****************************************************************************** +; @function _delete_file +;****************************************************************************** +_delete_file: + + push ax push bx + push dx + push ds + push cx + + mov di, offset _file_path + mov cx, 256 + + xor al, al + rep stosb + + mov ah, HEX (19) + int HEX (21) + + mov byte ptr [bp - 4], al + mov si, dx + + push si + + call _strlen + add sp, 2 + + cmp ax, 2 + jb _delete_file.got_drive + + cmp byte ptr [si + 1], ':' + jne _delete_file.got_drive + + mov al, [si] + + cmp al, HEX (7A) + ja _delete_file.no_adjust + + cmp al, HEX (61) + jb _delete_file.no_adjust + + mov bl, HEX (20) + not bl + + sub al, 'a' + + mov byte ptr [bp - 4], al + add si, 2 + + jmp _delete_file.got_drive + +_delete_file.no_adjust: + + cmp al, HEX (5A) + ja _delete_file.got_drive + + cmp al, HEX (41) + jb _delete_file.got_drive + + sub al, 'A' + + mov byte ptr [bp - 4], al + add si, 2 + +_delete_file.got_drive: + + mov di, offset _file_path + + mov al, byte ptr [bp - 4] + add al, 'A' + stosb + + mov al, ':' + stosb + + cmp byte ptr [si], '\\' + je _delete_file.initial_setup + + mov al, '\\' + stosb + + push dx + push si + + mov dl, byte ptr [bp - 4] + mov si, di + + mov ah, HEX (47) + int HEX (21) + + pop si + pop dx + + mov di, offset _file_path + push di + + call _strlen + add sp, 2 + + add di, ax + +_delete_file.initial_setup: + + cmp byte ptr es:[di - 1], '\\' + je _delete_file.no_slash + + mov al, '\\' + stosb + +_delete_file.no_slash: + + mov bx, si + +_delete_file.loop: + + mov ax, si + sub ax, bx + cmp ax, 65 + ja _delete_file.check_path + + lodsb + + and al, al + jz _delete_file.check_path + + cmp al, '.' + je _delete_file.check_curr + + cmp al, '\\' + jne _delete_file.convert_alpha + + cmp si, bx + je _delete_file.loop + + cmp byte ptr es:[di - 1], '\\' + je _delete_file.loop + +_delete_file.convert_alpha: + + xor ah, ah + push ax + + call _isalpha + + and ax, ax + jz _delete_file.no_alpha + + call _toupper + add sp, 2 + + jmp _delete_file.char_ok2 + +_delete_file.no_alpha: + + pop ax + jmp _delete_file.char_ok2 + +_delete_file.check_curr: + + cmp byte ptr [si], 0 + je _delete_file.loop + + cmp byte ptr [si], '\\' + je _delete_file.loop + + cmp byte ptr [si], '.' + je _delete_file.check_prev + + jmp _delete_file.char_ok2 + +_delete_file.check_prev: + + cmp byte ptr [si + 1], 0 + je _delete_file.get_prev + + cmp byte ptr [si + 1], '\\' + jne _delete_file.char_ok2 + +_delete_file.get_prev: + + mov cx, offset _file_path + add cx, 3 + + cmp di, cx + je _delete_file.no_slash2 + + cmp byte ptr es:[di - 1], '\\' + jne _delete_file.no_slash2 + + mov byte ptr es:[di - 1], 0 + +_delete_file.no_slash2: + + push si + push ds + + mov ax, cs + mov ds, ax + + mov ax, '\\' + push ax + + mov ax, offset _file_path + push ax + + call _strrchr + add sp, 4 + + pop ds + pop si + + and ax, ax + jnz _delete_file.got_index + + mov di, offset _file_path + jmp _delete_file.zero + +_delete_file.got_index: + + mov di, ax + inc di + +_delete_file.zero: + + mov byte ptr es:[di], 0 + jmp _delete_file.loop + +_delete_file.char_ok2: + + stosb + jmp _delete_file.loop + +_delete_file.check_path: + + cmp byte ptr es:[di - 1], '\\' + jne _delete_file.null + + dec di + +_delete_file.null: + + xor al, al + stosb + +_delete_file.delete: + mov ah, HEX (41) - mov dx, bx + mov dx, offset _file_path int HEX (21) - jnc _handler_del.close + jnc _delete_file.close + + mov cx, offset _file_path cmp ax, 2 - jne _handler_del.check3 + jne _delete_file.check mov bx, offset _err_no_file - jmp _handler_del.error - -_handler_del.check3: + jmp _delete_file.error +_delete_file.check: + cmp ax, 3 - jne _handler_del.unhandled + jne _delete_file.unhandled - mov bx, offset _err_no_dir - jmp _handler_del.error + mov bx, offset _err_no_path + jmp _delete_file.error -_handler_del.unhandled: +_delete_file.unhandled: - pop bx mov bx, offset _err_unhandled -_handler_del.error: +_delete_file.error: call _writestr cmp ax, 3 - ja _handler_del.next + ja _delete_file.done cmp ax, 2 - jb _handler_del.next + jb _delete_file.done - pop bx + mov bx, offset _file_path call _writestr call _crlf - jmp _handler_del.next + jmp _delete_file.done -_handler_del.close: +_delete_file.close: - pop bx mov bx, ax mov ah, HEX (3E) int HEX (21) - - jmp _handler_del.next -_handler_del.cleanup: - - mov ax, cs:[_vec_files + 0] - push ax - - call _free - add sp, 2 - - mov ax, cs - mov es, ax - - mov di, offset _vec_files - xor al, al - - mov cx, 6 - rep stosb - -_handler_del.done: +_delete_file.done: + pop cx pop ds - pop es - pop di - pop si pop dx - pop cx pop bx pop ax - - add sp, 4 - pop bp - ret ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Data area. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; _vec_files: db 6 dup (0) + _cmdline: db 256 dup (0) +_file_path: db 256 dup (0) _err_invalid: db "Invalid number of parameters", HEX (0D), HEX (0A), HEX (00) -_err_no_dir: db "Path not found: ", HEX (00) +_err_no_path: db "Path not found: ", HEX (00) _err_no_file: db "File not found: ", HEX (00) _err_unhandled: db "Unhandled error code", HEX (0D), HEX (0A), HEX (00) diff --git a/src/apps/pcomm/dir.asm b/src/apps/pcomm/dir.asm index 8a86bf5..cbf62ac 100644 --- a/src/apps/pcomm/dir.asm +++ b/src/apps/pcomm/dir.asm @@ -14,7 +14,7 @@ _handler_dir: push bp mov bp, sp - sub sp, 6 + sub sp, 10 push ax push bx @@ -25,8 +25,9 @@ _handler_dir: push es push ds + mov word ptr [bp - 4], 1 + mov word ptr [bp - 6], 0 - mov word ptr [bp - 4], 0 mov word ptr [bp - 2], 0 mov ax, 64 @@ -53,6 +54,7 @@ _handler_dir: jne _handler_dir.got_path mov bx, offset _fn_wild + mov word ptr [bp - 4], 0 _handler_dir.got_path: @@ -200,7 +202,7 @@ _handler_dir.done: pop bx pop ax - add sp, 6 + add sp, 10 pop bp ret @@ -216,22 +218,251 @@ _walk_dir: push ds push cx + mov di, offset _file_path + mov cx, 256 + + xor al, al + rep stosb + mov word ptr [bp - 2], 0 - mov si, offset _file_path + + cmp word ptr [bp - 4], 1 + jb _walk_dir.find_first + + mov ah, HEX (19) + int HEX (21) + + mov byte ptr [bp - 10], al + mov si, dx + + push si + + call _strlen + add sp, 2 + + cmp ax, 2 + jb _walk_dir.got_drive + + cmp byte ptr [si + 1], ':' + jne _walk_dir.got_drive + + mov al, [si] + + cmp al, HEX (7A) + ja _walk_dir.no_adjust + + cmp al, HEX (61) + jb _walk_dir.no_adjust + + mov bl, HEX (20) + not bl + + sub al, 'a' + + mov byte ptr [bp - 10], al + add si, 2 + + jmp _walk_dir.got_drive + +_walk_dir.no_adjust: + + cmp al, HEX (5A) + ja _walk_dir.got_drive + + cmp al, HEX (41) + jb _walk_dir.got_drive + + sub al, 'A' + + mov byte ptr [bp - 10], al + add si, 2 + +_walk_dir.got_drive: + + mov di, offset _file_path + + mov al, byte ptr [bp - 10] + add al, 'A' + stosb + + mov al, ':' + stosb + + cmp byte ptr [si], '\\' + je _walk_dir.initial_setup + + mov al, '\\' + stosb push dx push si - call _xstrcpy + mov dl, byte ptr [bp - 10] + mov si, di + + mov ah, HEX (47) + int HEX (21) + + pop si + pop dx + + mov di, offset _file_path + push di + + call _strlen + add sp, 2 + + add di, ax + +_walk_dir.initial_setup: + + cmp byte ptr es:[di - 1], '\\' + je _walk_dir.no_slash + + mov al, '\\' + stosb + +_walk_dir.no_slash: + + mov bx, si + +_walk_dir.loop: + + mov ax, si + sub ax, bx + + cmp ax, 65 + ja _walk_dir.check_path + + lodsb + + and al, al + jz _walk_dir.check_path + + cmp al, '.' + je _walk_dir.check_curr + + cmp al, '\\' + jne _walk_dir.convert_alpha + + cmp si, bx + je _walk_dir.loop + + cmp byte ptr es:[di - 1], '\\' + je _walk_dir.loop + +_walk_dir.convert_alpha: + + xor ah, ah + push ax + + call _isalpha + + and ax, ax + jz _walk_dir.no_alpha + + call _toupper + add sp, 2 + + jmp _walk_dir.char_ok + +_walk_dir.no_alpha: + + pop ax + jmp _walk_dir.char_ok + +_walk_dir.check_curr: + + cmp byte ptr [si], 0 + je _walk_dir.loop + + cmp byte ptr [si], '\\' + je _walk_dir.loop + + cmp byte ptr [si], '.' + je _walk_dir.check_prev + + jmp _walk_dir.char_ok + +_walk_dir.check_prev: + + cmp byte ptr [si + 1], 0 + je _walk_dir.get_prev + + cmp byte ptr [si + 1], '\\' + jne _walk_dir.char_ok + +_walk_dir.get_prev: + + mov cx, offset _file_path + add cx, 3 + + cmp di, cx + je _walk_dir.no_slash2 + + cmp byte ptr es:[di - 1], '\\' + jne _walk_dir.no_slash2 + + mov byte ptr es:[di - 1], 0 + +_walk_dir.no_slash2: + + push si + push ds + + mov ax, cs + mov ds, ax + + mov ax, '\\' + push ax + + mov ax, offset _file_path + push ax + + call _strrchr add sp, 4 - cmp word ptr [bp - 4], 1 - jb _walk_dir.find_first + pop ds + pop si + + and ax, ax + jnz _walk_dir.got_index + + mov di, offset _file_path + jmp _walk_dir.zero + +_walk_dir.got_index: + + mov di, ax + inc di + +_walk_dir.zero: + + mov byte ptr es:[di], 0 + jmp _walk_dir.loop + +_walk_dir.char_ok: + + stosb + jmp _walk_dir.loop + +_walk_dir.check_path: + + ;cmp byte ptr es:[di - 1], '\\' + ;jne _walk_dir.null + ; + ;dec di + +_walk_dir.null: + + xor al, al + stosb _walk_dir.find_first: mov ah, HEX (4E) xor cx, cx + mov dx, offset _file_path int HEX (21) jnc _walk_dir.check5 @@ -274,7 +505,7 @@ _walk_dir.error: _walk_dir.check5: cmp word ptr [bp - 4], 2 - jb _walk_dir.setup + jb _walk_dir.setup mov bx, offset _file_path call _writestr @@ -290,22 +521,20 @@ _walk_dir.setup: mov ax, cs:[_dta_addr] mov ds, ax -_walk_dir.find: +_walk_dir.found: cmp word ptr [bp - 2], 0 je _walk_dir.ok + mov cx, 4 + +_walk_dir.seperate: + mov ah, HEX (02) mov dl, ' ' int HEX (21) - mov ah, HEX (02) - mov dl, ':' - int HEX (21) - - mov ah, HEX (02) - mov dl, ' ' - int HEX (21) + loop _walk_dir.seperate _walk_dir.ok: @@ -333,7 +562,7 @@ _walk_dir.check: _walk_dir.period_check: - cmp byte ptr [si - 1], '.' + cmp byte ptr [si - 2], '.' jne _walk_dir.pad _walk_dir.output: @@ -367,7 +596,7 @@ _walk_dir.final_pad: int HEX (21) dec cx - jmp short _walk_dir.final_pad + jnz _walk_dir.final_pad _walk_dir.check2: @@ -383,7 +612,7 @@ _walk_dir.next: mov ah, HEX (4F) int HEX (21) - jnc _walk_dir.find + jnc _walk_dir.found cmp word ptr [bp - 2], 0 je _walk_dir.check4 diff --git a/src/apps/pcomm/mkdir.asm b/src/apps/pcomm/mkdir.asm index 416d917..2ecc9e9 100644 --- a/src/apps/pcomm/mkdir.asm +++ b/src/apps/pcomm/mkdir.asm @@ -14,7 +14,7 @@ _handler_mkdir: push bp mov bp, sp - sub sp, 4 + sub sp, 6 push ax push bx @@ -25,6 +25,7 @@ _handler_mkdir: push es push ds + mov word ptr [bp - 6], 0 mov word ptr [bp - 4], 0 mov word ptr [bp - 2], 0 @@ -57,7 +58,7 @@ _handler_mkdir.ok: _handler_mkdir.store_arg: cmp byte ptr [bx], 0 - je _handler_mkdir.next + je _handler_mkdir.check2 mov si, bx push si @@ -80,7 +81,7 @@ _handler_mkdir.check: add sp, 4 and ax, ax - jz _handler_mkdir.next + jz _handler_mkdir.check2 mov bx, ax mov byte ptr [bx], 0 @@ -90,7 +91,7 @@ _handler_mkdir.check: _handler_mkdir.skip: cmp byte ptr [bx], 0 - je _handler_mkdir.next + je _handler_mkdir.check2 cmp byte ptr [bx], ' ' ja _handler_mkdir.store_arg @@ -98,15 +99,20 @@ _handler_mkdir.skip: inc bx jmp _handler_mkdir.skip +_handler_mkdir.check2: + + cmp word ptr cs:[_vec_files + 4], 1 + jbe _handler_mkdir.next + _handler_mkdir.next: - mov ax, word ptr [bp - 4] + mov ax, word ptr [bp - 6] inc ax cmp ax, cs:[_vec_files + 4] ja _handler_mkdir.cleanup - mov word ptr [bp - 4], ax + mov word ptr [bp - 6], ax dec ax xor dx, dx @@ -121,29 +127,11 @@ _handler_mkdir.next: mov es, ax mov ax, es:[bx] - mov bx, ax + mov dx, ax pop es - -_handler_mkdir.open: - - mov ah, HEX (39) - mov dx, bx - int HEX (21) - jnc _handler_mkdir.next - -_handler_mkdir.error: - - push bx - - mov bx, offset _err_failed - call _writestr - - pop bx - - call _writestr - call _crlf + call _create_dir jmp _handler_mkdir.next _handler_mkdir.cleanup: @@ -174,16 +162,302 @@ _handler_mkdir.done: pop bx pop ax - add sp, 4 + add sp, 6 pop bp ret +;****************************************************************************** +; @function _create_dir +;****************************************************************************** +_create_dir: + + push ax + push bx + push dx + push ds + push cx + + mov di, offset _file_path + mov cx, 256 + + xor al, al + rep stosb + + mov ah, HEX (19) + int HEX (21) + + mov byte ptr [bp - 4], al + mov si, dx + + push si + + call _strlen + add sp, 2 + + cmp ax, 2 + jb _create_dir.got_drive + + cmp byte ptr [si + 1], ':' + jne _create_dir.got_drive + + mov al, [si] + + cmp al, HEX (7A) + ja _create_dir.no_adjust + + cmp al, HEX (61) + jb _create_dir.no_adjust + + mov bl, HEX (20) + not bl + + sub al, 'a' + + mov byte ptr [bp - 4], al + add si, 2 + + jmp _create_dir.got_drive + +_create_dir.no_adjust: + + cmp al, HEX (5A) + ja _create_dir.got_drive + + cmp al, HEX (41) + jb _create_dir.got_drive + + sub al, 'A' + + mov byte ptr [bp - 4], al + add si, 2 + +_create_dir.got_drive: + + mov di, offset _file_path + + mov al, byte ptr [bp - 4] + add al, 'A' + stosb + + mov al, ':' + stosb + + cmp byte ptr [si], '\\' + je _create_dir.initial_setup + + mov al, '\\' + stosb + + push dx + push si + + mov dl, byte ptr [bp - 4] + mov si, di + + mov ah, HEX (47) + int HEX (21) + + pop si + pop dx + + mov di, offset _file_path + push di + + call _strlen + add sp, 2 + + add di, ax + +_create_dir.initial_setup: + + cmp byte ptr es:[di - 1], '\\' + je _create_dir.no_slash + + mov al, '\\' + stosb + +_create_dir.no_slash: + + mov bx, si + +_create_dir.loop: + + mov ax, si + sub ax, bx + + cmp ax, 65 + ja _create_dir.check_path + + lodsb + + and al, al + jz _create_dir.check_path + + cmp al, '.' + je _create_dir.check_curr + + cmp al, '\\' + jne _create_dir.convert_alpha + + cmp si, bx + je _create_dir.loop + + cmp byte ptr es:[di - 1], '\\' + je _create_dir.loop + +_create_dir.convert_alpha: + + xor ah, ah + push ax + + call _isalpha + + and ax, ax + jz _create_dir.no_alpha + + call _toupper + add sp, 2 + + jmp _create_dir.char_ok2 + +_create_dir.no_alpha: + + pop ax + jmp _create_dir.char_ok2 + +_create_dir.check_curr: + + cmp byte ptr [si], 0 + je _create_dir.loop + + cmp byte ptr [si], '\\' + je _create_dir.loop + + cmp byte ptr [si], '.' + je _create_dir.check_prev + + jmp _create_dir.char_ok2 + +_create_dir.check_prev: + + cmp byte ptr [si + 1], 0 + je _create_dir.get_prev + + cmp byte ptr [si + 1], '\\' + jne _create_dir.char_ok2 + +_create_dir.get_prev: + + mov cx, offset _file_path + add cx, 3 + + cmp di, cx + je _create_dir.no_slash2 + + cmp byte ptr es:[di - 1], '\\' + jne _create_dir.no_slash2 + + mov byte ptr es:[di - 1], 0 + +_create_dir.no_slash2: + + push si + push ds + + mov ax, cs + mov ds, ax + + mov ax, '\\' + push ax + + mov ax, offset _file_path + push ax + + call _strrchr + add sp, 4 + + pop ds + pop si + + and ax, ax + jnz _create_dir.got_index + + mov di, offset _file_path + jmp _create_dir.zero + +_create_dir.got_index: + + mov di, ax + inc di + +_create_dir.zero: + + mov byte ptr es:[di], 0 + jmp _create_dir.loop + +_create_dir.char_ok2: + + stosb + jmp _create_dir.loop + +_create_dir.check_path: + + ;cmp byte ptr es:[di - 1], '\\' + ;jne _create_dir.null + ; + ;dec di + +_create_dir.null: + + xor al, al + stosb + +_create_dir.create: + + mov ah, HEX (39) + mov dx, offset _file_path + int HEX (21) + jnc _create_dir.done + +_create_dir.error: + + mov bx, offset _err_failed + call _writestr + + cmp ax, 3 + jne _create_dir.done + + mov bx, offset _file_path + + call _writestr + call _crlf + + jmp _create_dir.done + +_create_dir.close: + + mov bx, ax + + mov ah, HEX (3E) + int HEX (21) + +_create_dir.done: + + pop cx + pop ds + pop dx + pop bx + pop ax + ret + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Data area. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; _vec_files: db 6 dup (0) + _cmdline: db 256 dup (0) +_file_path: db 256 dup (0) _err_invalid: db "Invalid number of parameters", HEX (0D), HEX (0A), HEX (00) _err_failed: db "Unable to create directory - ", HEX (00) diff --git a/src/apps/pcomm/rmdir.asm b/src/apps/pcomm/rmdir.asm index bc06adc..4c50cd6 100644 --- a/src/apps/pcomm/rmdir.asm +++ b/src/apps/pcomm/rmdir.asm @@ -1,5 +1,5 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; @file type.asm +;; @file rmdir.asm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; %ifndef HEX % define HEX(y) 0x##y @@ -14,7 +14,7 @@ _handler_rmdir: push bp mov bp, sp - sub sp, 4 + sub sp, 6 push ax push bx @@ -25,6 +25,7 @@ _handler_rmdir: push es push ds + mov word ptr [bp - 6], 0 mov word ptr [bp - 4], 0 mov word ptr [bp - 2], 0 @@ -57,7 +58,7 @@ _handler_rmdir.ok: _handler_rmdir.store_arg: cmp byte ptr [bx], 0 - je _handler_rmdir.next + je _handler_rmdir.check2 mov si, bx push si @@ -80,7 +81,7 @@ _handler_rmdir.check: add sp, 4 and ax, ax - jz _handler_rmdir.next + jz _handler_rmdir.check2 mov bx, ax mov byte ptr [bx], 0 @@ -90,7 +91,7 @@ _handler_rmdir.check: _handler_rmdir.skip: cmp byte ptr [bx], 0 - je _handler_rmdir.next + je _handler_rmdir.check2 cmp byte ptr [bx], ' ' ja _handler_rmdir.store_arg @@ -98,15 +99,20 @@ _handler_rmdir.skip: inc bx jmp _handler_rmdir.skip +_handler_rmdir.check2: + + cmp word ptr cs:[_vec_files + 4], 1 + jbe _handler_rmdir.next + _handler_rmdir.next: - mov ax, word ptr [bp - 4] + mov ax, word ptr [bp - 6] inc ax cmp ax, cs:[_vec_files + 4] ja _handler_rmdir.cleanup - mov word ptr [bp - 4], ax + mov word ptr [bp - 6], ax dec ax xor dx, dx @@ -121,99 +127,366 @@ _handler_rmdir.next: mov es, ax mov ax, es:[bx] - mov bx, ax + mov dx, ax pop es + + call _delete_dir + jmp _handler_rmdir.next + +_handler_rmdir.cleanup: + + mov ax, cs:[_vec_files + 0] + push ax + + call _free + add sp, 2 + + mov ax, cs + mov es, ax + + mov di, offset _vec_files + xor al, al + + mov cx, 6 + rep stosb + +_handler_rmdir.done: + + pop ds + pop es + pop di + pop si + pop dx + pop cx + pop bx + pop ax + + add sp, 6 + pop bp + + ret + +;****************************************************************************** +; @function _delete_dir +;****************************************************************************** +_delete_dir: + + push ax + push bx + push dx + push ds + push cx + + mov di, offset _file_path + mov cx, 256 + + xor al, al + rep stosb + + mov ah, HEX (19) + int HEX (21) + + mov byte ptr [bp - 4], al + mov si, dx + + push si + + call _strlen + add sp, 2 + + cmp ax, 2 + jb _delete_dir.got_drive + + cmp byte ptr [si + 1], ':' + jne _delete_dir.got_drive + + mov al, [si] + + cmp al, HEX (7A) + ja _delete_dir.no_adjust + + cmp al, HEX (61) + jb _delete_dir.no_adjust + + mov bl, HEX (20) + not bl + + sub al, 'a' + + mov byte ptr [bp - 4], al + add si, 2 + + jmp _delete_dir.got_drive + +_delete_dir.no_adjust: + + cmp al, HEX (5A) + ja _delete_dir.got_drive + + cmp al, HEX (41) + jb _delete_dir.got_drive + + sub al, 'A' + + mov byte ptr [bp - 4], al + add si, 2 + +_delete_dir.got_drive: + + mov di, offset _file_path + + mov al, byte ptr [bp - 4] + add al, 'A' + stosb + + mov al, ':' + stosb + + cmp byte ptr [si], '\\' + je _delete_dir.initial_setup + + mov al, '\\' + stosb + + push dx + push si + + mov dl, byte ptr [bp - 4] + mov si, di + + mov ah, HEX (47) + int HEX (21) + + pop si + pop dx + + mov di, offset _file_path + push di + + call _strlen + add sp, 2 + + add di, ax + +_delete_dir.initial_setup: + + cmp byte ptr es:[di - 1], '\\' + je _delete_dir.no_slash + + mov al, '\\' + stosb + +_delete_dir.no_slash: + + mov bx, si + +_delete_dir.loop: + + mov ax, si + sub ax, bx + + cmp ax, 65 + ja _delete_dir.check_path + + lodsb + + and al, al + jz _delete_dir.check_path + + cmp al, '.' + je _delete_dir.check_curr + + cmp al, '\\' + jne _delete_dir.convert_alpha + + cmp si, bx + je _delete_dir.loop + + cmp byte ptr es:[di - 1], '\\' + je _delete_dir.loop + +_delete_dir.convert_alpha: + + xor ah, ah + push ax + + call _isalpha + + and ax, ax + jz _delete_dir.no_alpha + + call _toupper + add sp, 2 + + jmp _delete_dir.char_ok2 + +_delete_dir.no_alpha: + + pop ax + jmp _delete_dir.char_ok2 + +_delete_dir.check_curr: -_handler_rmdir.delete: + cmp byte ptr [si], 0 + je _delete_dir.loop + + cmp byte ptr [si], '\\' + je _delete_dir.loop + + cmp byte ptr [si], '.' + je _delete_dir.check_prev + + jmp _delete_dir.char_ok2 + +_delete_dir.check_prev: + + cmp byte ptr [si + 1], 0 + je _delete_dir.get_prev + + cmp byte ptr [si + 1], '\\' + jne _delete_dir.char_ok2 + +_delete_dir.get_prev: + + mov cx, offset _file_path + add cx, 3 + + cmp di, cx + je _delete_dir.no_slash2 + + cmp byte ptr es:[di - 1], '\\' + jne _delete_dir.no_slash2 + + mov byte ptr es:[di - 1], 0 + +_delete_dir.no_slash2: + + push si + push ds + + mov ax, cs + mov ds, ax + + mov ax, '\\' + push ax + + mov ax, offset _file_path + push ax + + call _strrchr + add sp, 4 + + pop ds + pop si + + and ax, ax + jnz _delete_dir.got_index + + mov di, offset _file_path + jmp _delete_dir.zero + +_delete_dir.got_index: + + mov di, ax + inc di + +_delete_dir.zero: + + mov byte ptr es:[di], 0 + jmp _delete_dir.loop + +_delete_dir.char_ok2: + + stosb + jmp _delete_dir.loop + +_delete_dir.check_path: + + cmp byte ptr es:[di - 1], '\\' + jne _delete_dir.null + + dec di + +_delete_dir.null: + + xor al, al + stosb + +_delete_dir.delete: mov ah, HEX (3A) - mov dx, bx + mov dx, offset _file_path int HEX (21) - jnc _handler_rmdir.next + jnc _delete_dir.close - mov cx, bx + mov cx, offset _file_path cmp ax, 3 - jne _handler_rmdir.check3 + jne _delete_dir.check mov bx, offset _err_failed - jmp _handler_rmdir.error - -_handler_rmdir.check3: + jmp _delete_dir.check2 +_delete_dir.check: + cmp ax, 16 - jne _handler_rmdir.unhandled + jne _delete_dir.unhandled mov bx, offset _err_current - jmp _handler_rmdir.error + jmp _delete_dir.check2 -_handler_rmdir.unhandled: +_delete_dir.unhandled: mov bx, offset _err_unhandled -_handler_rmdir.error: +_delete_dir.check2: cmp ax, 3 - jne _handler_rmdir.error2 + jne _delete_dir.error xchg bx, cx call _writestr xchg bx, cx -_handler_rmdir.error2: +_delete_dir.error: call _writestr cmp ax, 16 - jne _handler_rmdir.next + jne _delete_dir.done mov bx, cx call _writestr call _crlf - jmp _handler_rmdir.next + jmp _delete_dir.done -_handler_rmdir.cleanup: +_delete_dir.close: - mov ax, cs:[_vec_files + 0] - push ax - - call _free - add sp, 2 - - mov ax, cs - mov es, ax - - mov di, offset _vec_files - xor al, al + mov bx, ax - mov cx, 6 - rep stosb + mov ah, HEX (3E) + int HEX (21) -_handler_rmdir.done: +_delete_dir.done: + pop cx pop ds - pop es - pop di - pop si pop dx - pop cx pop bx pop ax - - add sp, 4 - pop bp - ret ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Data area. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; _vec_files: db 6 dup (0) + _cmdline: db 256 dup (0) +_file_path: db 256 dup (0) _err_invalid: db "Invalid number of parameters", HEX (0D), HEX (0A), HEX (00) diff --git a/src/apps/pcomm/touch.asm b/src/apps/pcomm/touch.asm index ca254e9..2f8c2b4 100644 --- a/src/apps/pcomm/touch.asm +++ b/src/apps/pcomm/touch.asm @@ -14,7 +14,7 @@ _handler_touch: push bp mov bp, sp - sub sp, 4 + sub sp, 6 push ax push bx @@ -25,6 +25,7 @@ _handler_touch: push es push ds + mov word ptr [bp - 6], 0 mov word ptr [bp - 4], 0 mov word ptr [bp - 2], 0 @@ -57,7 +58,7 @@ _handler_touch.ok: _handler_touch.store_arg: cmp byte ptr [bx], 0 - je _handler_touch.next + je _handler_touch.check2 mov si, bx push si @@ -80,7 +81,7 @@ _handler_touch.check: add sp, 4 and ax, ax - jz _handler_touch.next + jz _handler_touch.check2 mov bx, ax mov byte ptr [bx], 0 @@ -90,7 +91,7 @@ _handler_touch.check: _handler_touch.skip: cmp byte ptr [bx], 0 - je _handler_touch.next + je _handler_touch.check2 cmp byte ptr [bx], ' ' ja _handler_touch.store_arg @@ -98,15 +99,20 @@ _handler_touch.skip: inc bx jmp _handler_touch.skip +_handler_touch.check2: + + cmp word ptr cs:[_vec_files + 4], 1 + jbe _handler_touch.next + _handler_touch.next: - mov ax, word ptr [bp - 4] + mov ax, word ptr [bp - 6] inc ax cmp ax, cs:[_vec_files + 4] ja _handler_touch.cleanup - mov word ptr [bp - 4], ax + mov word ptr [bp - 6], ax dec ax xor dx, dx @@ -121,100 +127,354 @@ _handler_touch.next: mov es, ax mov ax, es:[bx] - mov bx, ax + mov dx, ax pop es + + call _create_file + jmp _handler_touch.next + +_handler_touch.cleanup: + + mov ax, cs:[_vec_files + 0] + push ax + + call _free + add sp, 2 + + mov ax, cs + mov es, ax + + mov di, offset _vec_files + xor al, al + + mov cx, 6 + rep stosb + +_handler_touch.done: + + pop ds + pop es + pop di + pop si + pop dx + pop cx + pop bx + pop ax + + add sp, 6 + pop bp + + ret -_handler_touch.open: +;****************************************************************************** +; @function _create_file +;****************************************************************************** +_create_file: + push ax push bx + push dx + push ds + push cx + + mov di, offset _file_path + mov cx, 256 + + xor al, al + rep stosb + + mov ah, HEX (19) + int HEX (21) + + mov byte ptr [bp - 4], al + mov si, dx + push si + + call _strlen + add sp, 2 + + cmp ax, 2 + jb _create_file.got_drive + + cmp byte ptr [si + 1], ':' + jne _create_file.got_drive + + mov al, [si] + + cmp al, HEX (7A) + ja _create_file.no_adjust + + cmp al, HEX (61) + jb _create_file.no_adjust + + mov bl, HEX (20) + not bl + + sub al, 'a' + + mov byte ptr [bp - 4], al + add si, 2 + + jmp _create_file.got_drive + +_create_file.no_adjust: + + cmp al, HEX (5A) + ja _create_file.got_drive + + cmp al, HEX (41) + jb _create_file.got_drive + + sub al, 'A' + + mov byte ptr [bp - 4], al + add si, 2 + +_create_file.got_drive: + + mov di, offset _file_path + + mov al, byte ptr [bp - 4] + add al, 'A' + stosb + + mov al, ':' + stosb + + cmp byte ptr [si], '\\' + je _create_file.initial_setup + + mov al, '\\' + stosb + + push dx + push si + + mov dl, byte ptr [bp - 4] + mov si, di + + mov ah, HEX (47) + int HEX (21) + + pop si + pop dx + + mov di, offset _file_path + push di + + call _strlen + add sp, 2 + + add di, ax + +_create_file.initial_setup: + + cmp byte ptr es:[di - 1], '\\' + je _create_file.no_slash + + mov al, '\\' + stosb + +_create_file.no_slash: + + mov bx, si + +_create_file.loop: + + mov ax, si + sub ax, bx + + cmp ax, 65 + ja _create_file.check_path + + lodsb + + and al, al + jz _create_file.check_path + + cmp al, '.' + je _create_file.check_curr + + cmp al, '\\' + jne _create_file.convert_alpha + + cmp si, bx + je _create_file.loop + + cmp byte ptr es:[di - 1], '\\' + je _create_file.loop + +_create_file.convert_alpha: + + xor ah, ah + push ax + + call _isalpha + + and ax, ax + jz _create_file.no_alpha + + call _toupper + add sp, 2 + + jmp _create_file.char_ok2 + +_create_file.no_alpha: + + pop ax + jmp _create_file.char_ok2 + +_create_file.check_curr: + + cmp byte ptr [si], 0 + je _create_file.loop + + cmp byte ptr [si], '\\' + je _create_file.loop + + cmp byte ptr [si], '.' + je _create_file.check_prev + + jmp _create_file.char_ok2 + +_create_file.check_prev: + + cmp byte ptr [si + 1], 0 + je _create_file.get_prev + + cmp byte ptr [si + 1], '\\' + jne _create_file.char_ok2 + +_create_file.get_prev: + + mov cx, offset _file_path + add cx, 3 + + cmp di, cx + je _create_file.no_slash2 + + cmp byte ptr es:[di - 1], '\\' + jne _create_file.no_slash2 + + mov byte ptr es:[di - 1], 0 + +_create_file.no_slash2: + + push si + push ds + + mov ax, cs + mov ds, ax + + mov ax, '\\' + push ax + + mov ax, offset _file_path + push ax + + call _strrchr + add sp, 4 + + pop ds + pop si + + and ax, ax + jnz _create_file.got_index + + mov di, offset _file_path + jmp _create_file.zero + +_create_file.got_index: + + mov di, ax + inc di + +_create_file.zero: + + mov byte ptr es:[di], 0 + jmp _create_file.loop + +_create_file.char_ok2: + + stosb + jmp _create_file.loop + +_create_file.check_path: + + ;cmp byte ptr es:[di - 1], '\\' + ;jne _create_file.null + ; + ;dec di + +_create_file.null: + + xor al, al + stosb + +_create_file.open: + mov ax, HEX (3D02) - mov dx, bx + mov dx, offset _file_path int HEX (21) - jnc _handler_touch.close + jnc _create_file.close mov ah, HEX (3C) mov cx, HEX (20) - mov dx, bx + mov dx, offset _file_path int HEX (21) - jnc _handler_touch.close + jnc _create_file.close cmp ax, 3 - jne _handler_touch.unhandled + jne _create_file.unhandled mov bx, offset _err_no_path - jmp _handler_touch.error + jmp _create_file.error -_handler_touch.unhandled: +_create_file.unhandled: - pop bx mov bx, offset _err_unhandled -_handler_touch.error: +_create_file.error: call _writestr cmp ax, 3 - jne _handler_touch.next + jne _create_file.done - pop bx + mov bx, offset _file_path call _writestr call _crlf - jmp _handler_touch.next + jmp _create_file.done -_handler_touch.close: +_create_file.close: - pop bx mov bx, ax mov ah, HEX (3E) int HEX (21) - - jmp _handler_touch.next -_handler_touch.cleanup: - - mov ax, cs:[_vec_files + 0] - push ax - - call _free - add sp, 2 - - mov ax, cs - mov es, ax - - mov di, offset _vec_files - xor al, al - - mov cx, 6 - rep stosb - -_handler_touch.done: +_create_file.done: + pop cx pop ds - pop es - pop di - pop si pop dx - pop cx pop bx pop ax - - add sp, 4 - pop bp - ret ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Data area. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; _vec_files: db 6 dup (0) + _cmdline: db 256 dup (0) +_file_path: db 256 dup (0) _err_invalid: db "Invalid number of parameters", HEX (0D), HEX (0A), HEX (00) -_err_no_path: db "Path not found: ", HEX (00) +_err_no_path: db "Path not found: ", HEX (00) _err_unhandled: db "Unhandled error code", HEX (0D), HEX (0A), HEX (00) diff --git a/src/apps/pcomm/type.asm b/src/apps/pcomm/type.asm index da8d569..95c126a 100644 --- a/src/apps/pcomm/type.asm +++ b/src/apps/pcomm/type.asm @@ -14,7 +14,7 @@ _handler_type: push bp mov bp, sp - sub sp, 4 + sub sp, 6 push ax push bx @@ -25,6 +25,7 @@ _handler_type: push es push ds + mov word ptr [bp - 6], 0 mov word ptr [bp - 4], 0 mov word ptr [bp - 2], 0 @@ -57,7 +58,7 @@ _handler_type.ok: _handler_type.store_arg: cmp byte ptr [bx], 0 - je _handler_type.next + je _handler_type.check2 mov si, bx push si @@ -80,7 +81,7 @@ _handler_type.check: add sp, 4 and ax, ax - jz _handler_type.next + jz _handler_type.check2 mov bx, ax mov byte ptr [bx], 0 @@ -90,7 +91,7 @@ _handler_type.check: _handler_type.skip: cmp byte ptr [bx], 0 - je _handler_type.next + je _handler_type.check2 cmp byte ptr [bx], ' ' ja _handler_type.store_arg @@ -98,15 +99,20 @@ _handler_type.skip: inc bx jmp _handler_type.skip +_handler_type.check2: + + cmp word ptr cs:[_vec_files + 4], 1 + jbe _handler_type.next + _handler_type.next: - mov ax, word ptr [bp - 4] + mov ax, word ptr [bp - 6] inc ax cmp ax, cs:[_vec_files + 4] ja _handler_type.cleanup - mov word ptr [bp - 4], ax + mov word ptr [bp - 6], ax dec ax xor dx, dx @@ -121,80 +127,358 @@ _handler_type.next: mov es, ax mov ax, es:[bx] - mov bx, ax + mov dx, ax pop es + + call _read_file + jmp _handler_type.next + +_handler_type.cleanup: + + mov ax, cs:[_vec_files + 0] + push ax + + call _free + add sp, 2 + + mov ax, cs + mov es, ax + + mov di, offset _vec_files + xor al, al + + mov cx, 6 + rep stosb -_handler_type.open: +_handler_type.done: + pop ds + pop es + pop di + pop si + pop dx + pop cx + pop bx + pop ax + + add sp, 6 + pop bp + + ret + +;****************************************************************************** +; @function _read_file +;****************************************************************************** +_read_file: + + push ax push bx + push dx + push ds + push cx + + mov di, offset _file_path + mov cx, 256 + + xor al, al + rep stosb + + mov ah, HEX (19) + int HEX (21) + + mov byte ptr [bp - 4], al + mov si, dx + push si + + call _strlen + add sp, 2 + + cmp ax, 2 + jb _read_file.got_drive + + cmp byte ptr [si + 1], ':' + jne _read_file.got_drive + + mov al, [si] + + cmp al, HEX (7A) + ja _read_file.no_adjust + + cmp al, HEX (61) + jb _read_file.no_adjust + + mov bl, HEX (20) + not bl + + sub al, 'a' + + mov byte ptr [bp - 4], al + add si, 2 + + jmp _read_file.got_drive + +_read_file.no_adjust: + + cmp al, HEX (5A) + ja _read_file.got_drive + + cmp al, HEX (41) + jb _read_file.got_drive + + sub al, 'A' + + mov byte ptr [bp - 4], al + add si, 2 + +_read_file.got_drive: + + mov di, offset _file_path + + mov al, byte ptr [bp - 4] + add al, 'A' + stosb + + mov al, ':' + stosb + + cmp byte ptr [si], '\\' + je _read_file.initial_setup + + mov al, '\\' + stosb + + push dx + push si + + mov dl, byte ptr [bp - 4] + mov si, di + + mov ah, HEX (47) + int HEX (21) + + pop si + pop dx + + mov di, offset _file_path + push di + + call _strlen + add sp, 2 + + add di, ax + +_read_file.initial_setup: + + cmp byte ptr es:[di - 1], '\\' + je _read_file.no_slash + + mov al, '\\' + stosb + +_read_file.no_slash: + + mov bx, si + +_read_file.loop: + + mov ax, si + sub ax, bx + + cmp ax, 65 + ja _read_file.check_path + + lodsb + + and al, al + jz _read_file.check_path + + cmp al, '.' + je _read_file.check_curr + + cmp al, '\\' + jne _read_file.convert_alpha + + cmp si, bx + je _read_file.loop + + cmp byte ptr es:[di - 1], '\\' + je _read_file.loop + +_read_file.convert_alpha: + + xor ah, ah + push ax + + call _isalpha + + and ax, ax + jz _read_file.no_alpha + + call _toupper + add sp, 2 + + jmp _read_file.char_ok2 + +_read_file.no_alpha: + + pop ax + jmp _read_file.char_ok2 + +_read_file.check_curr: + + cmp byte ptr [si], 0 + je _read_file.loop + + cmp byte ptr [si], '\\' + je _read_file.loop + + cmp byte ptr [si], '.' + je _read_file.check_prev + + jmp _read_file.char_ok2 + +_read_file.check_prev: + + cmp byte ptr [si + 1], 0 + je _read_file.get_prev + + cmp byte ptr [si + 1], '\\' + jne _read_file.char_ok2 + +_read_file.get_prev: + + mov cx, offset _file_path + add cx, 3 + + cmp di, cx + je _read_file.no_slash2 + + cmp byte ptr es:[di - 1], '\\' + jne _read_file.no_slash2 + + mov byte ptr es:[di - 1], 0 + +_read_file.no_slash2: + + push si + push ds + + mov ax, cs + mov ds, ax + + mov ax, '\\' + push ax + + mov ax, offset _file_path + push ax + + call _strrchr + add sp, 4 + + pop ds + pop si + + and ax, ax + jnz _read_file.got_index + + mov di, offset _file_path + jmp _read_file.zero + +_read_file.got_index: + + mov di, ax + inc di + +_read_file.zero: + + mov byte ptr es:[di], 0 + jmp _read_file.loop + +_read_file.char_ok2: + + stosb + jmp _read_file.loop + +_read_file.check_path: + + ;cmp byte ptr es:[di - 1], '\\' + ;jne _read_file.null + ; + ;dec di + +_read_file.null: + + xor al, al + stosb + +_read_file.open: + mov ax, HEX (3D02) - mov dx, bx + mov dx, offset _file_path int HEX (21) - jnc _handler_type.file_open + jnc _read_file.file_open cmp ax, 2 - jne _handler_type.check3 + jne _read_file.check3 mov bx, offset _err_no_file - jmp _handler_type.error + jmp _read_file.error -_handler_type.check3: +_read_file.check3: cmp ax, 3 - jne _handler_type.unhandled + jne _read_file.unhandled mov bx, offset _err_no_dir - jmp _handler_type.error + jmp _read_file.error -_handler_type.unhandled: +_read_file.unhandled: - pop bx mov bx, offset _err_unhandled -_handler_type.error: +_read_file.error: call _writestr cmp ax, 3 - ja _handler_type.cleanup + ja _read_file.done cmp ax, 2 - jb _handler_type.cleanup + jb _read_file.done - pop bx + mov bx, offset _file_path call _writestr call _crlf - jmp _handler_type.cleanup + jmp _read_file.done -_handler_type.file_open: +_read_file.file_open: - pop bx mov bx, ax -_handler_type.clear_and_read: +_read_file.clear_and_read: mov ah, HEX (3F) mov cx, 255 mov dx, offset _buffer int HEX (21) - jc _handler_type.close + jc _read_file.close and ax, ax - jz _handler_type.close + jz _read_file.close mov si, dx mov cx, ax -_handler_type.print_loop: +_read_file.print_loop: lodsb cmp al, HEX (0A) - jne _handler_type.char_ok + jne _read_file.char_ok push ax @@ -204,66 +488,41 @@ _handler_type.print_loop: pop ax -_handler_type.char_ok: +_read_file.char_ok: mov ah, HEX (02) mov dl, al int HEX (21) dec cx - jnz _handler_type.print_loop + jnz _read_file.print_loop -_handler_type.read_done: +_read_file.read_done: - jmp _handler_type.clear_and_read + jmp _read_file.clear_and_read -_handler_type.close: +_read_file.close: mov ah, HEX (3E) int HEX (21) - - jmp _handler_type.next - -_handler_type.cleanup: - - mov ax, cs:[_vec_files + 0] - push ax - - call _free - add sp, 2 - - mov ax, cs - mov es, ax - - mov di, offset _vec_files - xor al, al - - mov cx, 6 - rep stosb -_handler_type.done: +_read_file.done: + pop cx pop ds - pop es - pop di - pop si pop dx - pop cx pop bx pop ax - - add sp, 4 - pop bp - ret ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Data area. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; _vec_files: db 6 dup (0) - _cmdline: db 256 dup (0) -_buffer: db 256 dup (0) + +_file_path: db 256 dup (0) +_buffer: dw 256 dup (0) _err_invalid: db "Invalid number of parameters", HEX (0D), HEX (0A), HEX (00) diff --git a/src/apps/pcomm/xstrcpy.asm b/src/apps/pcomm/xstrcpy.asm index cbe9400..4812b53 100644 --- a/src/apps/pcomm/xstrcpy.asm +++ b/src/apps/pcomm/xstrcpy.asm @@ -19,14 +19,14 @@ _xstrcpy: push si push di - mov si, [bp + 6] - mov di, [bp + 4] + mov si, word ptr [bp + 6] + mov di, word ptr [bp + 4] _xstrcpy.loop: lodsb - or al, al + and al, al jz _xstrcpy.done cmp al, HEX (7A) diff --git a/src/kernel/file.asm b/src/kernel/file.asm index 92957e2..091aef0 100644 --- a/src/kernel/file.asm +++ b/src/kernel/file.asm @@ -566,6 +566,8 @@ _open_file.found_dir: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; test cl, HEX (18) jz _open_file.got_file + + jmp _open_file.error _open_file.name_error: @@ -1162,7 +1164,7 @@ _write_file: push bp mov bp, sp - sub sp, 16 + sub sp, 26 push bx push cx @@ -1187,49 +1189,66 @@ _write_file: ;; If theres zero byte to write then truncate/extend. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; and cx, cx - jz _write_file.zero + jz .L5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; For now we'll jump to error + ;; Check if the file needs expaning. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - jmp _write_file.error - -_write_file.zero: - - mov ax, es:[si + 66] - mov dx, es:[si + 68] + mov word ptr [bp - 22], dx + mov word ptr [bp - 20], cx - cmp dx, es:[si + 64] - ja _write_file.truncate + mov ax, es:[si + 62] + mov dx, es:[si + 64] - cmp dx, es:[si + 64] - jne _write_file.extend + cmp dx, es:[si + 68] + ja .L20 - cmp ax, es:[si + 62] - jb _write_file.extend + cmp dx, es:[si + 68] + jne .L19 + + cmp ax, es:[si + 66] + jbe .L19 -_write_file.truncate: +.L20: - mov bx, es:[si + 58] - or bx, es:[si + 60] + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Make sure the disk buffer is cleared. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + push es + push di + push ax - and bx, bx - jz _write_file.done + mov cx, es:[si + 50] + xor di, di + + mov ax, cs:[_disk_scratch] + mov es, ax + + xor al, al + rep stosb + + pop ax + pop di + pop es + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Now we need to figure out how many clusters are in use and how many + ;; are in total. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov cx, es:[si + 50] div cx mov bx, ax and dx, dx - jz .L3 + jz .L21 inc bx -.L3: +.L21: - mov ax, es:[si + 62] - mov dx, es:[si + 64] + mov ax, es:[si + 66] + mov dx, es:[si + 68] mov cx, es:[si + 50] div cx @@ -1237,122 +1256,116 @@ _write_file.truncate: mov cx, ax and dx, dx - jz .L3 + jz .L22 inc cx -.L4: +.L22: + mov word ptr [bp - 12], bx xchg bx, cx + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Check if we have a starting cluster. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov ax, es:[si + 58] mov dx, es:[si + 60] - -_write_file.truncate_walk: - - mov word ptr [bp - 6], ax - mov word ptr [bp - 4], dx - - and bx, bx - jz _write_file.truncate_ok - - dec cx - - call cs:[_next_cluster] - jc _write_file.truncate_ok - - dec bx - jnz _write_file.truncate_walk - -_write_file.truncate_ok: - - mov ax, word ptr [bp - 6] - mov dx, word ptr [bp - 4] - - mov word ptr [bp - 16], ax - mov word ptr [bp - 14], dx - - mov word ptr [bp - 12], cx - -_write_file.truncate_loop: - - and cx, cx - jz _write_file.truncate_done - call cs:[_next_cluster] - jc _write_file.truncate_done - - push si - push di + mov di, ax + or di, dx - mov si, ax - mov di, dx + and di, di + jnz .L27 - mov ax, word ptr [bp - 6] - mov dx, word ptr [bp - 4] + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Try find a new cluster. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + call cs:[_find_free] + jc _write_file.done - mov word ptr [bp - 6], si - mov word ptr [bp - 4], di + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx - pop di - pop si + dec cx push bx push cx - xor bx, bx - xor cx, cx - + mov bx, HEX (FFFF) + mov cx, HEX (FFF8) call cs:[_update_cluster] pop cx pop bx - mov ax, word ptr [bp - 6] - mov dx, word ptr [bp - 4] + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the file info. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov es:[si + 58], ax + mov es:[si + 60], dx - dec cx - jnz _write_file.truncate_loop - -_write_file.truncate_done: - - mov ax, word ptr [bp - 6] - mov dx, word ptr [bp - 4] + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the sector on disk. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + push bx + push cx + call cs:[_convert_cluster] + + pop cx + pop bx + jc _write_file.done + push es push bx push cx - xor bx, bx - xor cx, cx + xor ch, ch + mov cl, cs:[_sectors_per_cluster] - call cs:[_update_cluster] + mov bx, cs:[_disk_scratch] + mov es, bx + + xor bx, bx + call _write_sectors pop cx pop bx + pop es - xor ax, ax - xor dx, dx - - mov bx, es:[si + 62] - or bx, es:[si + 64] + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Jump straight to writing the sectors. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + jmp .L26 + +.L27: + + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx and bx, bx - jz _write_file.truncate_update + jz .L26 - mov cx, word ptr [bp - 12] + call cs:[_next_cluster] + jc .L26 - cmp cx, 2 - jb _write_file.truncate_expand_done + dec word ptr [bp - 12] + dec cx - sub cx, 2 + dec bx + jnz .L27 -_write_file.truncate_expand: +.L26: + + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] + +.L28: and cx, cx - jz _write_file.truncate_expand_done + jz .L29 call cs:[_find_free] - jc _write_file.truncate_expand_done + jc _write_file.done push bx push cx @@ -1360,44 +1373,97 @@ _write_file.truncate_expand: mov bx, dx mov cx, ax - mov ax, word ptr [bp - 16] - mov dx, word ptr [bp - 14] + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] call cs:[_update_cluster] - mov word ptr [bp - 16], cx - mov word ptr [bp - 14], bx + push bx + push cx + call cs:[_convert_cluster] + + pop cx + pop bx + jc .L29 + + push es + push bx + push cx + + xor ch, ch + mov cl, cs:[_sectors_per_cluster] + + mov bx, cs:[_disk_scratch] + mov es, bx + + xor bx, bx + call _write_sectors + + pop cx + pop bx + pop es + + mov ax, cx + mov dx, bx + + mov bx, HEX (FFFF) + mov cx, HEX (FFF8) + call cs:[_update_cluster] pop cx pop bx + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx + dec cx - jnz _write_file.truncate_expand + jnz .L28 -_write_file.truncate_expand_done: +.L29: + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Preserve registers. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + push es push bx push cx + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Mark the current cluster as EOF. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov bx, HEX (FFFF) mov cx, HEX (FFF8) - mov ax, word ptr [bp - 16] - mov dx, word ptr [bp - 14] + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] call cs:[_update_cluster] + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the sector on disk. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + call cs:[_convert_cluster] + jc _write_file.done + + xor ch, ch + mov cl, cs:[_sectors_per_cluster] + + mov bx, cs:[_disk_scratch] + mov es, bx + + xor bx, bx + call _write_sectors + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Restore registers. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; pop cx pop bx + pop es - mov ax, es:[si + 58] - mov dx, es:[si + 60] - -_write_file.truncate_update: - - mov es:[si + 58], ax - mov es:[si + 60], dx - + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the stack variables. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov ax, es:[si + 58] mov word ptr [bp - 6], ax @@ -1462,11 +1528,15 @@ _write_file.truncate_update: mov ax, cs:[_info_sector] and ax, ax - jz _write_file.done + jz .L19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Update the free clusters. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + push es + push bx + push cx + xor dx, dx add ax, cs:[_hidden_sectors] @@ -1484,8 +1554,8 @@ _write_file.truncate_update: mov ax, es:[di + 8] mov dx, es:[di + 10] - add ax, word ptr [bp - 12] - adc dx, 0 + sub ax, word ptr [bp - 12] + sbb dx, 0 mov es:[di + 8], ax mov es:[di + 10], dx @@ -1499,34 +1569,781 @@ _write_file.truncate_update: mov cx, 1 call _write_sectors - jmp _write_file.done + pop cx + pop bx + pop es -_write_file.extend: +.L19: - push es - push di - push ax - - xor cx, es:[si + 50] - - mov ax, cs:[_disk_scratch] - mov es, ax - xor di, di - - xor al, al - rep stosb + mov word ptr [bp - 12], 0 + mov word ptr [bp - 2], 0 - pop ax - pop di - pop es + mov ax, es:[si + 62] + mov dx, es:[si + 64] mov cx, es:[si + 50] div cx - mov bx, ax - and dx, dx - jz .L2 + jz .L30 + + inc ax + +.L30: + + mov word ptr [bp - 26], ax + + mov ax, es:[si + 66] + mov dx, es:[si + 68] + + mov cx, es:[si + 50] + div cx + + and dx, dx + jz .L31 + + inc ax + +.L31: + + mov word ptr [bp - 24], ax + +.L32: + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Make sure the disk buffer is cleared. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + push es + push di + push ax + + mov cx, es:[si + 50] + xor di, di + + mov ax, cs:[_disk_scratch] + mov es, ax + + xor al, al + rep stosb + + pop ax + pop di + pop es + +.L34: + + mov cx, word ptr [bp - 20] + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; If we don't have any data left then we're done. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + and cx, cx + jz .L42 + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Figure out what cluster we need to write to. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov ax, es:[si + 62] + mov dx, es:[si + 64] + + mov cx, es:[si + 50] + div cx + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Copy the value in the dx register (offset into the cluster) into + ;; the di register. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov di, dx + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Calaculate how many bytes need to be write. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov cx, es:[si + 50] + sub cx, di + + cmp cx, word ptr [bp - 20] + jb .L35 + + mov cx, word ptr [bp - 20] + +.L35: + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Are we writing to an existing cluster or a new one? + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + cmp ax, word ptr [bp - 24] + jae .L36 + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Copy the cluster count to the bx register. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov bx, ax + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Get the starting cluster. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov ax, es:[si + 58] + mov dx, es:[si + 60] + +.L37: + + and bx, bx + jz .L38 + + call cs:[_next_cluster] + jc .L42 + + dec bx + jnz .L37 + +.L38: + + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx + + push si + push es + push ds + push cx + + push bx + push cx + call cs:[_convert_cluster] + + pop cx + pop bx + jc .L43 + + push ax + push dx + push si + push di + push cx + + mov bx, es:[si + 70] + mov es, bx + + xor ch, ch + mov cl, cs:[_sectors_per_cluster] + + xor bx, bx + call _read_sectors + + pop cx + push cx + + mov si, word ptr [bp - 22] + rep movsb + + pop cx + pop di + pop si + pop dx + pop ax + + sub word ptr [bp - 20], cx + add word ptr [bp - 2], cx + add word ptr [bp - 22], cx + + xor ch, ch + mov cl, cs:[_sectors_per_cluster] + + xor bx, bx + call _write_sectors + + pop cx + pop ds + pop es + pop si + + add es:[si + 62], cx + adc word ptr es:[si + 64], 0 + + jmp .L34 + +.L36: + + mov ax, es:[si + 58] + mov dx, es:[si + 60] + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Do we have a starting cluster? + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov bx, ax + or bx, dx + + and bx, bx + jnz .L39 + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Try find a new cluster. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + call cs:[_find_free] + jc .L42 + + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx + + push bx + push cx + + mov bx, HEX (FFFF) + mov cx, HEX (FFF8) + call cs:[_update_cluster] + + pop cx + pop bx + + inc word ptr [bp - 12] + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the file info. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov es:[si + 58], ax + mov es:[si + 60], dx + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the sector on disk. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + push bx + push cx + call cs:[_convert_cluster] + + pop cx + pop bx + jc _write_file.done + + push es + push bx + push cx + + xor ch, ch + mov cl, cs:[_sectors_per_cluster] + + mov bx, cs:[_disk_scratch] + mov es, bx + + xor bx, bx + call _write_sectors + + pop cx + pop bx + pop es + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Jump straight to writing the sectors. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + jmp .L40 + +.L39: + + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx + + call cs:[_next_cluster] + jc .L41 + + jmp .L39 + +.L41: + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Try find a new cluster. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + call cs:[_find_free] + jc .L42 + + push bx + push cx + push ax + push dx + + mov bx, dx + mov cx, ax + + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] + call cs:[_update_cluster] + + pop dx + pop ax + pop cx + pop bx + + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx + + push bx + push cx + + mov bx, HEX (FFFF) + mov cx, HEX (FFF8) + call cs:[_update_cluster] + + pop cx + pop bx + + inc word ptr [bp - 12] + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the sector on disk. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + push bx + push cx + call cs:[_convert_cluster] + + pop cx + pop bx + jc _write_file.done + + push es + push bx + push cx + + xor ch, ch + mov cl, cs:[_sectors_per_cluster] + + mov bx, cs:[_disk_scratch] + mov es, bx + + xor bx, bx + call _write_sectors + + pop cx + pop bx + pop es + +.L40: + + push si + push es + push ds + push cx + + push ax + push di + push cx + + mov ax, es:[si + 70] + mov cx, es:[si + 50] + + mov es, ax + + xor al, al + rep stosb + + pop cx + pop di + pop ax + + push si + push di + push cx + + mov si, word ptr [bp - 22] + rep movsb + + pop cx + pop di + pop si + + push cx + push bx + + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] + call cs:[_convert_cluster] + + pop bx + pop cx + jc .L43 + + sub word ptr [bp - 20], cx + add word ptr [bp - 2], cx + add word ptr [bp - 22], cx + + xor ch, ch + mov cl, cs:[_sectors_per_cluster] + + xor bx, bx + call _write_sectors + + pop cx + pop ds + pop es + pop si + + add es:[si + 62], cx + adc word ptr es:[si + 64], 0 + + jmp .L34 + +.L43: + + pop cx + pop ds + pop es + pop si + +.L42: + + mov ax, es:[si + 62] + mov dx, es:[si + 64] + + cmp dx, es:[si + 68] + ja .L44 + + cmp dx, es:[si + 68] + jne .L45 + + cmp ax, es:[si + 66] + jb .L45 + +.L44: + + mov ax, es:[si + 62] + mov dx, es:[si + 64] + + mov es:[si + 66], ax + mov es:[si + 68], dx + +.L45: + + mov ax, es:[si + 66] + mov word ptr [bp - 10], ax + + mov ax, es:[si + 68] + mov word ptr [bp - 8], ax + + mov ax, es:[si + 58] + mov word ptr [bp - 6], ax + + mov ax, es:[si + 60] + mov word ptr [bp - 4], ax + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the entry on disk. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov ax, es:[si + 74] + mov dx, es:[si + 76] + + mov cx, es:[si + 78] + mov di, es:[si + 80] + + push es + push bx + push ax + push dx + push cx + + mov bx, cs:[_disk_scratch] + mov es, bx + + xor bx, bx + call _read_sectors + + mov ax, word ptr [bp - 6] + mov es:[di + 26], ax + + mov ax, word ptr [bp - 4] + mov es:[di + 20], ax + + mov ax, word ptr [bp - 10] + mov es:[di + 28], ax + + mov ax, word ptr [bp - 8] + mov es:[di + 30], ax + + pop cx + pop dx + pop ax + + xor bx, bx + call _write_sectors + + pop bx + pop es + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Check if we have an info sector. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + mov ax, cs:[_info_sector] + + and ax, ax + jz _write_file.done + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; Update the free clusters. + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + xor dx, dx + + add ax, cs:[_hidden_sectors] + adc dx, cs:[_hidden_sectors + 2] + + mov bx, cs:[_disk_scratch] + mov es, bx + xor bx, bx + + mov cx, 1 + call _read_sectors + + mov di, HEX (01E0) + + mov ax, es:[di + 8] + mov dx, es:[di + 10] + + sub ax, word ptr [bp - 12] + sbb dx, 0 + + mov es:[di + 8], ax + mov es:[di + 10], dx + + mov ax, cs:[_info_sector] + xor dx, dx + + add ax, cs:[_hidden_sectors] + adc dx, cs:[_hidden_sectors + 2] + + mov cx, 1 + call _write_sectors + + jmp _write_file.done + +.L5: + + mov ax, es:[si + 66] + mov dx, es:[si + 68] + + cmp dx, es:[si + 64] + ja .L18 + + cmp dx, es:[si + 64] + jne .L17 + + cmp ax, es:[si + 62] + jb .L17 + +.L18: + + mov bx, es:[si + 58] + or bx, es:[si + 60] + + and bx, bx + jz _write_file.done + + mov cx, es:[si + 50] + div cx + + mov bx, ax + + and dx, dx + jz .L3 + + inc bx + +.L3: + + mov ax, es:[si + 62] + mov dx, es:[si + 64] + + mov cx, es:[si + 50] + div cx + + mov cx, ax + + and dx, dx + jz .L3 + + inc cx + +.L4: + + xchg bx, cx + + mov ax, es:[si + 58] + mov dx, es:[si + 60] + +.L7: + + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx + + and bx, bx + jz .L8 + + dec cx + + call cs:[_next_cluster] + jc .L8 + + dec bx + jnz .L7 + +.L8: + + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] + + mov word ptr [bp - 16], ax + mov word ptr [bp - 14], dx + + mov word ptr [bp - 12], cx + +.L9: + + and cx, cx + jz .L10 + + call cs:[_next_cluster] + jc .L10 + + push si + push di + + mov si, ax + mov di, dx + + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] + + mov word ptr [bp - 6], si + mov word ptr [bp - 4], di + + pop di + pop si + + push bx + push cx + + xor bx, bx + xor cx, cx + + call cs:[_update_cluster] + + pop cx + pop bx + + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] + + dec cx + jnz .L9 + +.L10: + + mov ax, word ptr [bp - 6] + mov dx, word ptr [bp - 4] + + push bx + push cx + + xor bx, bx + xor cx, cx + + call cs:[_update_cluster] + + pop cx + pop bx + + xor ax, ax + xor dx, dx + + mov bx, es:[si + 62] + or bx, es:[si + 64] + + and bx, bx + jz .L12 + + mov cx, word ptr [bp - 12] + + cmp cx, 2 + jb .L6 + + sub cx, 2 + +.L11: + + and cx, cx + jz .L6 + + call cs:[_find_free] + jc .L6 + + push bx + push cx + + mov bx, dx + mov cx, ax + + mov ax, word ptr [bp - 16] + mov dx, word ptr [bp - 14] + + call cs:[_update_cluster] + + mov word ptr [bp - 16], cx + mov word ptr [bp - 14], bx + + pop cx + pop bx + + dec cx + jnz .L11 + +.L6: + + push bx + push cx + + mov bx, HEX (FFFF) + mov cx, HEX (FFF8) + + mov ax, word ptr [bp - 16] + mov dx, word ptr [bp - 14] + + call cs:[_update_cluster] + + pop cx + pop bx + + mov ax, es:[si + 58] + mov dx, es:[si + 60] + +.L12: + + mov es:[si + 58], ax + mov es:[si + 60], dx + + mov word ptr [bp - 18], 1 + jmp .L23 + +.L17: + + mov word ptr [bp - 18], 0 + + push es + push di + push ax + + mov cx, es:[si + 50] + + mov ax, cs:[_disk_scratch] + mov es, ax + xor di, di + + xor al, al + rep stosb + + pop ax + pop di + pop es + + mov cx, es:[si + 50] + div cx + + mov bx, ax + + and dx, dx + jz .L2 inc bx @@ -1546,6 +2363,8 @@ _write_file.extend: inc cx .L1: + + mov word ptr [bp - 12], cx mov ax, es:[si + 58] mov dx, es:[si + 60] @@ -1554,7 +2373,7 @@ _write_file.extend: or di, dx and di, di - jnz _write_file.extend_walk + jnz .L13 call cs:[_find_free] jc _write_file.done @@ -1562,6 +2381,16 @@ _write_file.extend: mov word ptr [bp - 6], ax mov word ptr [bp - 4], dx + push bx + push cx + + mov bx, HEX (FFFF) + mov cx, HEX (FFF8) + call cs:[_update_cluster] + + pop cx + pop bx + mov es:[si + 58], ax mov es:[si + 60], dx @@ -1587,38 +2416,39 @@ _write_file.extend: call _write_sectors pop cx + dec cx + pop bx pop es - jmp _write_file.extend_no_walk + jmp .L14 -_write_file.extend_walk: +.L13: mov word ptr [bp - 6], ax mov word ptr [bp - 4], dx and bx, bx - jz _write_file.extend_no_walk + jz .L14 call cs:[_next_cluster] - jc _write_file.extend_no_walk + jc .L14 + dec word ptr [bp - 12] dec cx dec bx - jnz _write_file.extend_walk + jnz .L13 -_write_file.extend_no_walk: +.L14: mov ax, word ptr [bp - 6] mov dx, word ptr [bp - 4] - - mov word ptr [bp - 12], cx -_write_file.extend_loop: +.L15: and cx, cx - jz _write_file.extend_update + jz .L16 call cs:[_find_free] jc _write_file.done @@ -1640,7 +2470,7 @@ _write_file.extend_loop: pop cx pop bx - jc _write_file.extend_update + jc .L16 push es push bx @@ -1659,16 +2489,23 @@ _write_file.extend_loop: pop bx pop es - mov word ptr [bp - 6], cx - mov word ptr [bp - 4], bx + mov ax, cx + mov dx, bx + + mov bx, HEX (FFFF) + mov cx, HEX (FFF8) + call cs:[_update_cluster] pop cx pop bx + mov word ptr [bp - 6], ax + mov word ptr [bp - 4], dx + dec cx - jnz _write_file.extend_loop + jnz .L15 -_write_file.extend_update: +.L16: push es push bx @@ -1697,7 +2534,9 @@ _write_file.extend_update: pop cx pop bx pop es - + +.L23: + mov ax, es:[si + 58] mov word ptr [bp - 6], ax @@ -1784,9 +2623,21 @@ _write_file.extend_update: mov ax, es:[di + 8] mov dx, es:[di + 10] + cmp word ptr [bp - 18], 1 + jb .L25 + + add ax, word ptr [bp - 12] + adc dx, 0 + + jmp .L24 + +.L25: + sub ax, word ptr [bp - 12] sbb dx, 0 - + +.L24: + mov es:[di + 8], ax mov es:[di + 10], dx @@ -1801,17 +2652,17 @@ _write_file.extend_update: _write_file.done: - mov word ptr es:[si + 54], 1 - pop ds pop es pop di pop si + mov word ptr es:[si + 54], 1 + pop dx pop cx pop bx - add sp, 16 + add sp, 26 clc mov ax, word ptr [bp - 2] @@ -1821,17 +2672,17 @@ _write_file.done: _write_file.error: - mov word ptr es:[si + 54], 1 - pop ds pop es pop di pop si + mov word ptr es:[si + 54], 1 + pop dx pop cx pop bx - add sp, 16 + add sp, 26 stc mov ax, 5 diff --git a/src/kernel/int21.asm b/src/kernel/int21.asm index 7e3899a..eb28c8f 100644 --- a/src/kernel/int21.asm +++ b/src/kernel/int21.asm @@ -2588,9 +2588,6 @@ _int21_3C: push es push ds - xor bh, bh - mov bl, al - mov si, dx call _get_disk_info @@ -2610,7 +2607,11 @@ _int21_3C.create: mov word ptr [bp - 4], si mov word ptr [bp - 2], di + push cx + call _walk_path + pop cx + jc _int21_3C.error_path ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -3452,9 +3453,6 @@ _int21_41: push es push ds - xor bh, bh - mov bl, al - mov si, dx call _get_disk_info @@ -3521,7 +3519,7 @@ _int21_41.open: or bx, dx and bx, bx - jz _int21_41.update + jz _int21_41.no_start ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Convert the size into the amount of cluster. @@ -3578,6 +3576,11 @@ _int21_41.clear: dec cx jnz _int21_41.clear +_int21_41.no_start: + + xor cx, cx + push cx + _int21_41.update: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -4183,6 +4186,8 @@ _int21_4E: push es push ds + mov word ptr [bp -8], cx + mov ax, cs:[_selected_disk] mov word ptr [bp - 10], ax