kvisc/ka/usr/cmd/dir.k

163 lines
2.6 KiB
Plaintext
Raw Normal View History

2019-07-10 17:17:45 +02:00
; The OS/K Team licenses this file to you under the MIT license.
; See the LICENSE file in the project root for more information.
2019-08-05 14:56:22 +02:00
N := 11
2019-07-10 17:17:45 +02:00
builtins.dir:
2019-07-22 13:18:13 +02:00
push rbp
mov rbp, rsp
2019-07-01 13:16:17 +02:00
2019-08-08 18:39:12 +02:00
push r12, r13
push r14, r15
2019-08-05 14:56:22 +02:00
2019-08-14 20:23:05 +02:00
nul r12 # no. of files found
nul r13 # no. of directories found
nul r14 # total amount of bytes found
2019-07-01 13:16:17 +02:00
2019-08-03 17:41:44 +02:00
call print, .dirmsg
2019-07-01 13:16:17 +02:00
.first:
2019-07-10 12:26:15 +02:00
mov rax, Sys.FindFirst
2019-07-01 13:16:17 +02:00
mov ax0, .buf
2019-08-03 18:20:36 +02:00
mov ax1, FNAME_MAX
2019-07-10 12:26:15 +02:00
trap 0
2019-07-01 13:16:17 +02:00
jmp .list
.next:
2019-07-10 12:26:15 +02:00
mov rax, Sys.FindNext
2019-07-01 13:16:17 +02:00
mov ax0, .buf
2019-08-03 18:20:36 +02:00
mov ax1, FNAME_MAX
2019-07-10 12:26:15 +02:00
trap 0
2019-07-01 13:16:17 +02:00
.list:
2019-08-14 09:52:39 +02:00
jraxz .end
2019-07-01 13:16:17 +02:00
2019-08-14 09:52:39 +02:00
mov r15, rcx ; file size
2019-08-05 15:55:46 +02:00
inc r14, rcx
2019-07-01 13:16:17 +02:00
2019-08-05 14:56:22 +02:00
; directory?
2019-08-14 09:52:39 +02:00
bnz rdx, .is_dir
2019-08-05 14:56:22 +02:00
2019-08-05 15:55:46 +02:00
; found a file
inc r12, 1
2019-07-01 13:16:17 +02:00
; separate extension from file name
2019-08-03 18:20:36 +02:00
mov rcx, FNAME_MAX
2019-07-22 13:18:13 +02:00
mov rsi, .buf
mov rdi, rsi
2019-08-14 20:23:05 +02:00
scasb rsi, '.'
2019-07-01 13:16:17 +02:00
2019-07-02 20:13:05 +02:00
; print file name
2019-08-14 09:52:39 +02:00
sub ax1, rsi, rdi
dec ax1, 1
call nprint, rdi
2019-07-01 13:16:17 +02:00
; calculate where to put extension
2019-07-22 13:18:13 +02:00
sub rdi, rsi, .buf
2019-08-03 17:41:44 +02:00
dec rdi, 1
2019-07-01 13:16:17 +02:00
.ext_pad:
2019-08-05 14:56:22 +02:00
; print at least N non-space characters before extension
2019-08-14 09:52:39 +02:00
blte N, rdi, .print_ext
2019-07-01 13:16:17 +02:00
prn ' '
2019-08-03 17:41:44 +02:00
inc rdi, 1
2019-07-01 13:16:17 +02:00
jmp .ext_pad
.print_ext:
; here we print at least 4 characters excluding '.'
mov rcx, 4
prn ' '
2019-08-14 09:52:39 +02:00
bne b[rsi], '.', .print_ext.1
inc rsi, 1
2019-07-01 13:16:17 +02:00
.print_ext.1:
2019-08-14 09:52:39 +02:00
bzr b[rsi], .print_ext.2
2019-07-01 13:16:17 +02:00
; print and decrease rcx, unless it's already 0
2019-08-03 17:41:44 +02:00
prn b[rsi]
inc rsi, 1
2019-08-14 09:52:39 +02:00
jrcxz .print_ext.1
2019-07-01 13:16:17 +02:00
2019-08-14 09:52:39 +02:00
dec rcx, 1
2019-07-01 13:16:17 +02:00
jmp .print_ext.1
.print_ext.2:
; did we print at least 4 bytes?
2019-08-14 09:52:39 +02:00
jrcxz .print_bytes ; yes, carry on
2019-07-01 13:16:17 +02:00
2019-08-14 09:52:39 +02:00
.pe2.l:
prn ' '
loop .pe2.l
2019-07-01 13:16:17 +02:00
.print_bytes:
; print file size in bytes
2019-08-14 09:52:39 +02:00
prn ' '
prn ' '
prn ' '
2019-07-01 13:16:17 +02:00
2019-08-05 15:55:46 +02:00
shr rax, r15, 10
2019-08-14 09:52:39 +02:00
and r15, 1023
2019-07-01 13:16:17 +02:00
2019-08-14 09:52:39 +02:00
push r15, rax
2019-07-18 22:49:31 +02:00
call printf, .bytesstr
2019-08-03 17:41:44 +02:00
inc rsp, 16
2019-07-01 13:16:17 +02:00
.prepare_next:
; go find next entry
prn 10
jmp .next
.end:
2019-08-05 15:55:46 +02:00
shr rax, r14, 10
shr rdx, rax, 10
2019-08-08 18:39:12 +02:00
and rax, 1023
and r14, 1023
2019-08-05 15:55:46 +02:00
2019-08-08 18:39:12 +02:00
push r14, rax, rdx
2019-08-05 15:55:46 +02:00
call printf, .endstr0
inc rsp, 24
2019-08-08 18:39:12 +02:00
push r13, r12
2019-07-18 22:49:31 +02:00
call printf, .endstr1
2019-08-05 15:55:46 +02:00
inc rsp, 16
2019-07-01 13:16:17 +02:00
2019-08-08 18:39:12 +02:00
pop r15, r14
pop r13, r12
2019-07-22 13:18:13 +02:00
leave
2019-07-01 13:16:17 +02:00
ret
2019-08-05 14:56:22 +02:00
; special case: direcory
.is_dir:
2019-08-05 15:55:46 +02:00
inc r13, 1
2019-08-14 09:52:39 +02:00
; use printf instead of print
; because it returns # of printed
; characters
call printf, .buf
blte N, rax, .dir_no_pad
sub rcx, N, rax
dec rcx, 1
2019-08-05 14:56:22 +02:00
2019-08-14 09:52:39 +02:00
.dir.l:
prn ' '
loop .dir.l
2019-08-05 14:56:22 +02:00
2019-08-14 09:52:39 +02:00
.dir_no_pad:
2019-08-05 14:56:22 +02:00
call print, .dir_ext
jmp .print_bytes
2019-08-03 18:20:36 +02:00
.buf = [FNAME_MAX]
2019-08-14 09:52:39 +02:00
.dir_ext = " <DIR> "
2019-08-05 15:55:46 +02:00
.endstr0 = " total %dMB + %dKB + %dB\n"
.endstr1 = " found %d file(s), %d dir(s)\n"
2019-08-05 14:56:22 +02:00
.dirmsg = "Directory of C:\\\n\n"
2019-08-05 15:55:46 +02:00
2019-08-05 14:56:22 +02:00
.bytesstr = "%d kilobytes + %d bytes"
2019-08-05 15:55:46 +02:00
# .bytesstr = "%dMB + %dKB + %dB" # too soon
2019-07-01 14:04:32 +02:00