1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/ka/usr/cmd/dir.k
julianb0 ce8411be8e
vm
2019-07-22 13:18:13 +02:00

123 lines
2.0 KiB
Plaintext

; The OS/K Team licenses this file to you under the MIT license.
; See the LICENSE file in the project root for more information.
NAME_MAX := 256
builtins.dir:
push rbp
mov rbp, rsp
push r12
mov r12, zero # no. of files found
mov rcx, STRLEN_MAX
mov rdx, .dirmsg
prns.rep.nz rdx
.dirmsg = "Directory of C:\\\n\n"
.first:
mov rax, Sys.FindFirst
mov ax0, .buf
mov ax1, NAME_MAX
trap 0
jmp .list
.next:
mov rax, Sys.FindNext
mov ax0, .buf
mov ax1, NAME_MAX
trap 0
.list:
b.z rax, 0, .end
; found something
add r12, r12, 1
; separate extension from file name
mov rcx, NAME_MAX
mov rsi, .buf
mov rdi, rsi
scasb.rep.nz rsi, '.'
; print file name
sub rcx, rsi, rdi
prns.rep rdi
; calculate where to put extension
sub rdi, rsi, .buf
sub rdi, rdi, 1
.ext_pad:
; print at least 11 non-space characters before extension
b.ae rdi, 11, .print_ext
prn ' '
add rdi, rdi, 1
jmp .ext_pad
.print_ext:
; here we print at least 4 characters excluding '.'
mov rcx, 4
prn ' '
cmp b[rsi], '.'
add.z rsi, rsi, 1
.print_ext.1:
b.z b[rsi], 0, .print_ext.2
; print and decrease rcx, unless it's already 0
mov rbx, b[rsi]
prn rbx
add rsi, rsi, 1
sub.cxnz rcx, rcx, 1
jmp .print_ext.1
.print_ext.2:
; did we print at least 4 bytes?
jmp.cxz .print_bytes ; yes, carry on
prn.rep ' '
.print_bytes:
; print file size in bytes
prn ' '
prn ' '
prn ' '
shr rax, rdx, 10
and rdx, rdx, 1023
push rdx
push rax
call printf, .bytesstr
add rsp, rsp, 16
.bytesstr = "%d kilobytes + %d bytes"
.prepare_next:
; go find next entry
prn 10
jmp .next
.end:
push r12
call printf, .endstr1
add rsp, rsp, 8
call print, .endstr2
pop r12
leave
ret
.buf = [256]
.endstr1 = " %d file(s)\n"
.endstr2 = " 0 dir(s)\n"