This commit is contained in:
julianb0 2019-09-07 16:45:03 +02:00
parent bd4a38592b
commit bdfd5ea9cf
No known key found for this signature in database
GPG Key ID: 9C7ACF0C053FB8A1
23 changed files with 711 additions and 691 deletions

View File

@ -74,22 +74,42 @@ pcurfile = sys.argv[1]
#-------------------------------------------------------------------------------
pregs = {
'$r0': 0, '$rax': 0, '$r1': 1, '$rbx': 1,
'$r2': 2, '$rcx': 2, '$r3': 3, '$rdx': 3,
'$r4': 4, '$rsi': 4, '$r5': 5, '$rdi': 5,
'$r6': 6, '$ax0': 6, '$r7': 7, '$ax1': 7,
'$r8': 8, '$ax2': 8, '$r9': 9, '$ax3': 9,
'$r10': 10, '$ax4': 10, '$r11': 11, '$ax5': 11,
'$r12': 12, '$nx0': 12, '$r13': 13, '$nx1': 13,
'$r14': 14, '$nx2': 14, '$r15': 15, '$nx3': 15,
'$r16': 16, '$nx4': 16, '$r17': 17, '$nx5': 17,
'$r18': 18, '$nx6': 18, '$r19': 19, '$nx7': 19,
'$r20': 20, '$nx8': 20, '$r21': 21, '$grp': 21,
'$r22': 22, '$trp': 22, '$r23': 23, '$srp': 23,
'$r24': 24, '$tmp': 24, '$r25': 25, '$rad': 25,
'$r26': 26, '$cr0': 26, '$r27': 27, '$cr1': 27,
'$r28': 28, '$rip': 28, '$r29': 29, '$rbp': 29,
'$r30': 30, '$rsp': 30, '$r31': 31, '$zero': 31,
'$r0': 0, '$eax': 0, '$rax': 0,
'$r1': 1, '$ebx': 1, '$rbx': 1,
'$r2': 2, '$ecx': 2, '$rcx': 2,
'$r3': 3, '$edx': 3, '$rdx': 3,
'$r4': 4, '$esi': 4, '$rsi': 4,
'$r5': 5, '$edi': 5, '$rdi': 5,
'$r6': 6, '$ax0': 6,
'$r7': 7, '$ax1': 7,
'$r8': 8, '$ax2': 8,
'$r9': 9, '$ax3': 9,
'$r10': 10, '$ax4': 10,
'$r11': 11, '$ax5': 11,
'$r12': 12, '$nx0': 12,
'$r13': 13, '$nx1': 13,
'$r14': 14, '$nx2': 14,
'$r15': 15, '$nx3': 15,
'$r16': 16, '$nx4': 16,
'$r17': 17, '$nx5': 17,
'$r18': 18, '$nx6': 18,
'$r19': 19, '$nx7': 19,
'$r20': 20, '$nx8': 20,
'$r21': 21, '$grp': 21,
'$r22': 22, '$trp': 22,
'$r23': 23, '$srp': 23,
'$r24': 24, '$tmp': 24,
'$r25': 25, '$rad': 25,
'$r26': 26, '$cr0': 26,
'$r27': 27, '$cr1': 27,
'$r28': 28, '$eip': 28, '$rip': 28,
'$r29': 29, '$ebp': 29, '$rbp': 29,
'$r30': 30, '$esp': 30, '$rsp': 30,
'$r31': 31, '$nul': 31, '$zero': 31,
}
def parse_lst_instrs():

View File

@ -12,21 +12,21 @@ cmdstart:
; COMMAND.COM guarantees that programs
; are always loaded on a 16KB boundary
; This is guaranteed to be the case
; in all future versions as well
; in all future veesions as well
#define FILE_LOADP 0x108000 // + 32KB
start:
mov rsp, CMDCOM_STACK
mov rbp, zero
mov esp, CMDCOM_STACK
mov ebp, zero
call main
mov rax, Sys.EnterHaltMode
mov eax, Sys.EnterHaltMode
trap 0
crash
cmd.versionstr = "COMMAND.COM, version 0.1 (KVISC)\nCopyright (C) 2019, The OS/K Team\nMIT license (permissive), see LICENCE file in source tree"
cmd.veesionstr = "COMMAND.COM, veesion 0.1 (KVISC)\nCopyright (C) 2019, The OS/K Team\nMIT license (permissive), see LICENCE file in source tree"
argbuf.size := 256
argbuf = [argbuf.size]
@ -38,12 +38,12 @@ stdin_echoing = 1
prompt = [32]
main:
mov rsi, prompt
mov b[rsi+0], 'C'
mov b[rsi+1], ':'
mov b[rsi+2], '\'
mov b[rsi+3], '>'
mov b[rsi+4], ' '
mov esi, prompt
mov b[esi+0], 'C'
mov b[esi+1], ':'
mov b[esi+2], '\'
mov b[esi+3], '>'
mov b[esi+4], ' '
.print_prompt:
call print, prompt
@ -56,7 +56,7 @@ main:
; call nprint, argv0, argbuf.size
; iterator through argbuf
nul rcx
nul ecx
.input_loop:
pause
@ -64,22 +64,22 @@ main:
pause
; Fill .buf with user input
scan rax
scan eax
jraxz .input_loop
; ESC key pressed?
beq rax, 0x1B, .handle_EXIT
beq eax, 0x1B, .handle_EXIT
; backspace character?
bne rax, 8, .handle_input
bne eax, 8, .handle_input
; anything to delete?
jrcxz .input_loop ; no
; yes, delete it
dec rcx
add rdx, rcx, argbuf
nul b[rdx]
dec ecx
add edx, ecx, argbuf
nul b[edx]
; update screen
bzr b[stdin_echoing], .input_loop
@ -89,19 +89,19 @@ main:
.handle_input:
bzr b[stdin_echoing], .se.z
prn rax
prn eax
.se.z:
beq rax, 10, .extract_argv0
beq eax, 10, .extract_argv0
; when max line length is reached,
; force a newline
beq rcx, argbuf.size, .extract_argv0
beq ecx, argbuf.size, .extract_argv0
; add character to buffer and increase iterator (rcx)
add rdx, rcx, argbuf
mov b[rdx], rax
inc rcx
; add character to buffer and increase iterator (ecx)
add edx, ecx, argbuf
mov b[edx], eax
inc ecx
; another one
jmp .input_loop
@ -112,43 +112,43 @@ main:
jrcxz .print_prompt
; find first whitespace or null-terminator
mov rcx, argbuf.size
mov rdx, argbuf
scasb rdx, ' '
mov ecx, argbuf.size
mov edx, argbuf
scasb edx, ' '
; argv1 exists? if so, save its position
mov rsi, rdx
mov esi, edx
.next_space:
mov rcx, b[rsi]
mov ecx, b[esi]
jrcxz .do_extract
; skip spaces
bne rcx, ' ', .not_a_space
inc rsi
bne ecx, ' ', .not_a_space
inc esi
jmp .next_space
.not_a_space:
; if we're here, we found a
; non-zero non-space character
mov q[argv1pos], rsi
mov q[argv1pos], esi
; fallthrough
.do_extract:
; how much do we copy?
sub rcx, rdx, argbuf
sub ecx, edx, argbuf
jrcxz .detect_builtin
dec rcx
dec ecx
mov rdi, argbuf
mov rsi, argv0
mov edi, argbuf
mov esi, argv0
.copy_loop:
mov b[rsi], b[rdi]
mov b[esi], b[edi]
inc rdi
inc rsi
inc edi
inc esi
loop .copy_loop
@ -238,12 +238,12 @@ main:
.handle_DATE:
call GetTimeUTC
mov rcx, b[rax+4]
inc rcx
mov ecx, b[eax+4]
inc ecx
push b[rax+3], rcx, w[rax+6]
push b[eax+3], ecx, w[eax+6]
call printf, .datefmt
add rsp, 40
add esp, 40
jmp .print_prompt
@ -254,22 +254,22 @@ main:
jmp .print_prompt
.handle_ECHO:
mov rax, q[argv1pos]
mov eax, q[argv1pos]
jraxz .echo.end
call print, rax
call print, eax
.echo.end:
prn 10
jmp .print_prompt
.handle_EXIT:
mov rax, Sys.Shutdown
mov eax, Sys.Shutdown
trap 0
jmp .print_prompt
.handle_HALT:
mov rax, Sys.EnterHaltMode
mov eax, Sys.EnterHaltMode
trap 0
jmp .print_prompt
@ -284,16 +284,16 @@ main:
.handle_TIME:
call GetTimeUTC
push b[rax], b[rax+1], b[rax+2]
push b[eax], b[eax+1], b[eax+2]
call printf, .timefmt
add rsp, 24
add esp, 24
jmp .print_prompt
.timefmt = "%d:%d:%d\n"
.handle_VERS:
call print, cmd.versionstr
call print, cmd.veesionstr
prn 10
jmp .print_prompt
@ -333,12 +333,12 @@ main:
.helpmsg.prompt = " PROMPT Change the command line prompt\n"
.helpmsg.remove = " REMOVE Delete a file (permanently)\n"
.helpmsg.time = " TIME Display current time of day\n"
.helpmsg.ver = " VERS Display current COMMAND.COM version\n"
.helpmsg.ver = " VERS Display current COMMAND.COM veesion\n"
.exec_not_found:
push argv0
call printf, .enf_errmsg
add rsp, 8
add esp, 8
jmp .print_prompt
@ -347,7 +347,7 @@ main:
.file_not_found:
push q[argv1pos], argv0
call printf, .fnf_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -356,7 +356,7 @@ main:
.empty_file:
push q[argv1pos], argv0
call printf, .ef_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -365,7 +365,7 @@ main:
.couldnt_open:
push q[argv1pos], argv0
call printf, .cno_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -374,7 +374,7 @@ main:
.couldnt_remove:
push q[argv1pos], argv0
call printf, .cne_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -383,7 +383,7 @@ main:
.couldnt_read:
push q[argv1pos], argv0
call printf, .cnr_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt

View File

@ -50,7 +50,7 @@
#include "crt/fmt/printf.k"
exit:
mov rax, Sys.Exit
mov eax, Sys.Exit
trap 0
abort:

View File

@ -6,73 +6,73 @@
; int doprnt(PUTC putc, int n, const char *fmt, va_list ap)
;
doprnt:
push rbp
mov rbp, rsp
push ebp
mov ebp, esp
push r12, r13, r14
push r15, r16, r17
push nx0, nx1, nx2
push nx3, nx4, nx5
mov r12, ax2 ; fmt
mov r14, ax3 ; va_list
mov r15, ax1 ; n
mov r17, ax0 ; putc
nul r16 ; return value
mov nx0, ax2 ; fmt
mov nx2, ax3 ; va_list
mov nx3, ax1 ; n
mov nx5, ax0 ; putc
nul nx4 ; return value
.main_loop:
; find '%' or null-terminator
mov rcx, STRLEN_MAX
mov r13, r12
scasb r13, '%'
mov ecx, STRLEN_MAX
mov nx1, nx0
scasb nx1, '%'
; everything below r13 is a regular character; print it
; everything below nx1 is a regular character; print it
.print_regular:
beq r12, r13, .check_modf
beq nx0, nx1, .check_modf
call .doput, b[r12]
call .doput, b[nx0]
inc r12
inc nx0
jmp .print_regular
.check_modf:
; did we find a '%' ?
; if not, then we found fmt's null-terminator; we're done
bne b[r12], '%', .epilogue
bne b[nx0], '%', .epilogue
; we did find a modifier / '%'
mov rax, b[r12+1]
add r12, 2
mov eax, b[nx0+1]
add nx0, 2
beq rax, 's', .modf_s
beq rax, 'c', .modf_c
beq rax, 'p', .modf_p
beq rax, 'x', .modf_x
beq rax, 'd', .modf_d
beq rax, 'o', .modf_o
beq rax, 'b', .modf_b
beq rax, '%', .modf_percent
beq eax, 's', .modf_s
beq eax, 'c', .modf_c
beq eax, 'p', .modf_p
beq eax, 'x', .modf_x
beq eax, 'd', .modf_d
beq eax, 'o', .modf_o
beq eax, 'b', .modf_b
beq eax, '%', .modf_percent
; unrecognized
jmp .bad_modifier
.modf_s:
; get string address
mov r13, q[r14]
add r14, 8
mov nx1, q[nx2]
add nx2, 8
bzr r13, .nullstring
bzr nx1, .nullstring
.print_string:
movzx ax0, b[r13]
movzx ax0, b[nx1]
bzr ax0, .main_loop
inc r13
inc nx1
call .doput
jmp .print_string
.modf_c:
call .doput, q[r14]
add r14, 8
call .doput, q[nx2]
add nx2, 8
jmp .main_loop
@ -99,24 +99,24 @@ doprnt:
.print_number:
; allocate itoa conversion buffer
sub rsp, 80
mov r13, rsp
sub esp, 80
mov nx1, esp
; assume modifier already set up ax2
call itoa, rsp, q[r14]
add r14, 8
call itoa, esp, q[nx2]
add nx2, 8
.print_itoa_buf:
movzx ax0, b[r13]
movzx ax0, b[nx1]
bzr ax0, .pib_end_loop
inc r13
inc nx1
call .doput
jmp .print_itoa_buf
.pib_end_loop:
add rsp, 80
add esp, 80
jmp .main_loop
.modf_percent:
@ -142,11 +142,11 @@ doprnt:
jmp .main_loop
.epilogue:
mov rax, r16
mov eax, nx4
pop r17, r16
pop r15, r14
pop r13, r12
pop nx5, nx4
pop nx3, nx2
pop nx1, nx0
leave
ret
@ -155,23 +155,23 @@ doprnt:
;
.doput:
; update print count
inc r16
inc nx4
; if n==0, don't print
; we follow the C convention that sprintf()-like functions
; should return the number of characters that would have
; been printed/written if 'n' were big enough
bzr r15, .r
bzr nx3, .r
; decrement n and print
dec r15
call r17
dec nx3
call nx5
; did putc fail?
jraxz .r
; yes, so artificially set n=0
nul r15
nul nx3
.r:
ret

View File

@ -19,8 +19,8 @@ utoa:
; void ltostr(char *buf, int num, int base, bool signed)
;
ltostr:
mov rax, ax0
nul rcx
mov eax, ax0
nul ecx
; make sure base is in [2, 32]
bltu ax2, 2, .bad
@ -34,7 +34,7 @@ ltostr:
bzr ax3, .conv
bne ax2, 10, .conv ; base 10
shr rcx, ax1, 63 ; extract ax1 sign
shr ecx, ax1, 63 ; extract ax1 sign
jrcxz .conv
neg ax1 ; NEG if negative
@ -43,18 +43,18 @@ ltostr:
.conv:
bzr ax1, .fini
rem rdx, ax1, ax2 ; ax1 % base
rem edx, ax1, ax2 ; ax1 % base
blt 9, rdx, .nondec ; rdx > 9 ?
blt 9, edx, .nondec ; edx > 9 ?
add rdx, '0'
add edx, '0'
jmp .next
.nondec:
add rdx, 55 ; 'A' - 10
add edx, 55 ; 'A' - 10
.next:
mov b[ax0], rdx
mov b[ax0], edx
inc ax0
@ -70,7 +70,7 @@ ltostr:
.cxz:
nul b[ax0]
call strrev2, rax
call strrev2, eax
ret
;

View File

@ -6,7 +6,7 @@
;
putc:
prn ax0
nul rax
nul eax
ret
;
@ -16,7 +16,7 @@ printf:
mov ax2, ax0
mov ax0, putc
mov ax1, STRLEN_MAX
add ax3, rsp, 8
add ax3, esp, 8
jmp doprnt
;
@ -25,19 +25,19 @@ printf:
nprintf:
mov ax2, ax0
mov ax0, putc
add ax3, rsp, 8
add ax3, esp, 8
jmp doprnt
;
; Print a string
; Guaranteed to only affect rcx and ax0
; Guaranteed to only affect ecx and ax0
;
print:
.l:
movzx rax, b[ax0]
movzx eax, b[ax0]
jraxz .r
prn rax
prn eax
inc ax0
jmp .l
@ -49,7 +49,7 @@ print:
; Print exactly ax1 characters
;
nprint:
mov rcx, ax1
mov ecx, ax1
jrcxz .r
.l:

View File

@ -4,8 +4,8 @@
;
; int strtol(const char *str, int base)
;
; rax = integer extracted from str
; rdx = pointer to first invalid byte
; eax = integer extracted from str
; edx = pointer to first invalid byte
;
strtol:
mov ax2, 1
@ -14,8 +14,8 @@ strtol:
;
; int strtoul(const char *str, int base)
;
; rax = integer extracted from str
; rdx = pointer to first invalid byte
; eax = integer extracted from str
; edx = pointer to first invalid byte
;
strtoul:
nul ax2
@ -27,49 +27,49 @@ strtoul:
; guesses base when 'base'=0
;
strtoq:
nul rax, rsi
mov rdx, ax0
nul eax, esi
mov edx, ax0
; make sure base is in [2, 32]
beq ax1, 1, .bad
bltu 36, ax1, .bad
; empty string?
bzr b[rdx], .done
bzr b[edx], .done
.skip_spc:
bne b[rdx], ' ', .no_spc
inc rdx
bne b[edx], ' ', .no_spc
inc edx
jmp .skip_spc
.no_spc:
; skip +
bne b[rdx], '+', .no_plus
inc rdx
bne b[edx], '+', .no_plus
inc edx
.no_plus:
; unsigned?
bzr ax2, .unsigned
; parse '-'
bne b[rdx], '-', .unsigned
inc rdx
mov rsi, 1
bne b[edx], '-', .unsigned
inc edx
mov esi, 1
.unsigned:
; base 0
bzr ax1, .base_0
; base prefix?
bne b[rdx], '0', .main_loop
bne b[edx], '0', .main_loop
inc rdx
movzx rcx, b[rdx]
inc edx
movzx ecx, b[edx]
; "0x"/"0b" prefix
jrcxz .done ; "0"
beq rcx, 'x', .parsed_0x
beq rcx, 'b', .parsed_0b
beq ecx, 'x', .parsed_0x
beq ecx, 'b', .parsed_0b
; may be octal, but we don't care
; we accept "0110101010" (despite base=2) for instance
@ -77,40 +77,40 @@ strtoq:
.parsed_0x:
; are we in base 16?
; if not, leave rax = 0 and *rdx = 'x'
; if not, leave eax = 0 and *edx = 'x'
bne ax1, 16, .done
; else
inc rdx
inc edx
jmp .main_loop
.parsed_0b:
; are we in base 2?
; if not, leave rax = 0 and *rdx = 'b'
; if not, leave eax = 0 and *edx = 'b'
bne ax1, 2, .done
; else
inc rdx
inc edx
jmp .main_loop
.base_0:
; guess base
beq b[rdx], '0', .b0_not10
beq b[edx], '0', .b0_not10
; must be base 10
mov ax1, 10
jmp .main_loop
.b0_not10:
inc rdx
inc edx
bne b[rdx], 'x', .b0_not16
inc rdx
bne b[edx], 'x', .b0_not16
inc edx
mov ax1, 16
jmp .main_loop
.b0_not16:
bne b[rdx], 'b', .b0_not2
inc rdx
bne b[edx], 'b', .b0_not2
inc edx
mov ax1, 2
jmp .main_loop
@ -119,45 +119,45 @@ strtoq:
mov ax1, 8
.main_loop:
movzx rcx, b[rdx]
inc rdx
movzx ecx, b[edx]
inc edx
; between 0 and 9?
bltu rcx, '0', .done
bltu '9', rcx, .not_digit10
bltu ecx, '0', .done
bltu '9', ecx, .not_digit10
; yes
sub rcx, '0'
sub ecx, '0'
jmp .next
.not_digit10:
bltu rcx, 'A', .done
bltu 'Z', rcx, .not_digitAZ
bltu ecx, 'A', .done
bltu 'Z', ecx, .not_digitAZ
sub rcx, 55 ; 'A' - 10
sub ecx, 55 ; 'A' - 10
jmp .next
.not_digitAZ:
bltu rcx, 'a', .done
bltu 'z', rcx, .done
bltu ecx, 'a', .done
bltu 'z', ecx, .done
sub rcx, 87 ; 'a' - 10
sub ecx, 87 ; 'a' - 10
jmp .next
.next:
; too large for base?
blteu ax1, rcx, .done
blteu ax1, ecx, .done
mul rax, ax1
add rax, rcx
mul eax, ax1
add eax, ecx
jmp .main_loop
.done:
; negative?
bzr rsi, .r
bzr esi, .r
; yes
neg rax
neg eax
.r:
ret

View File

@ -5,12 +5,12 @@
; void memcpy(void *, const void *, int)
;
memcpy:
mov rcx, ax2
mov ecx, ax2
jrcxz .r
.l:
sub rdx, ax2, rcx
mov b[ax0+rdx], b[ax1+rdx]
sub edx, ax2, ecx
mov b[ax0+edx], b[ax1+edx]
loop .l
.r:
@ -20,7 +20,7 @@ memcpy:
; void memzero(void *, int)
;
memzero:
mov rcx, ax1
mov ecx, ax1
jrcxz .r
.l:

View File

@ -5,21 +5,21 @@
; int strnlen(char *, int)
;
strnlen:
mov rcx, ax1
mov ecx, ax1
scasb ax0, zero
sub rax, ax1, rcx
sub eax, ax1, ecx
ret
;
; int strlen(char *)
;
strlen:
mov rcx, STRLEN_MAX
mov rdx, rcx
mov ecx, STRLEN_MAX
mov edx, ecx
scasb ax0, zero
sub rax, rdx, rcx
sub eax, edx, ecx
ret
;
@ -27,8 +27,8 @@ strlen:
;
strcpy:
.l:
mov rcx, b[ax1]
mov b[ax0], rcx
mov ecx, b[ax1]
mov b[ax0], ecx
jrcxz .r
@ -44,7 +44,7 @@ strcpy:
; void strncpy(char *, const char *, int)
;
strncpy:
mov rcx, ax2
mov ecx, ax2
jrcxz .r
.l:
@ -62,12 +62,12 @@ strncpy:
; void strnzcpy(char *, const char *, int)
;
strnzcpy:
mov rcx, ax2
mov ecx, ax2
jrcxz .r
.l:
mov rax, b[ax1]
mov b[ax0], rax
mov eax, b[ax1]
mov b[ax0], eax
jraxz .r
@ -91,68 +91,68 @@ strnzcpy:
; <0 if the first character that does not match has a lower value in str1 than in str2
;
strcmp:
nul rsi
nul esi
.l:
movzx rax, b[ax0+rsi]
movzx rdx, b[ax1+rsi]
movzx eax, b[ax0+esi]
movzx edx, b[ax1+esi]
bne rax, rdx, .r
bne eax, edx, .r
; both zero?
add rcx, rax, rdx
add ecx, eax, edx
jrcxz .r
inc rsi
inc esi
jmp .l
.r:
sub rax, rdx
sub eax, edx
ret
;
; int strncmp(const char *str1, const char *str2, int maxn)
;
strncmp:
mov rcx, ax2
mov ecx, ax2
jrcxz .r
.l:
movzx rax, b[ax0]
movzx rdx, b[ax1]
movzx eax, b[ax0]
movzx edx, b[ax1]
bne rax, rdx, .r
bne eax, edx, .r
inc ax0
inc ax1
loop .l
.r:
sub rax, rdx
sub eax, edx
ret
;
; char *strchrnul(const char *str, int ch)
;
strchrnul:
mov rcx, STRLEN_MAX
mov ecx, STRLEN_MAX
scasb ax0, ax1
mov rax, ax0
mov eax, ax0
ret
;
; char *strchr(const char *str, int ch)
;
strchr:
mov rcx, STRLEN_MAX
mov ecx, STRLEN_MAX
scasb ax0, ax1
bnz b[ax0], .r
nul rax
nul eax
ret
.r:
mov rax, ax0
mov eax, ax0
ret
;
@ -164,11 +164,11 @@ strrev:
bzr b[ax1], .z
; save str's location
mov rsi, ax1
mov esi, ax1
; go to str's end, just before
; the null terminator
mov rcx, STRLEN_MAX
mov ecx, STRLEN_MAX
scasb ax1, zero
dec ax1
@ -177,7 +177,7 @@ strrev:
; and forward through buf
mov b[ax0], b[ax1]
beq ax1, rsi, .r
beq ax1, esi, .r
inc ax0
dec ax1
@ -203,7 +203,7 @@ strrev2:
; go to str's end, just before
; the null terminator
mov rcx, STRLEN_MAX
mov ecx, STRLEN_MAX
scasb ax1, zero
dec ax1

View File

@ -21,22 +21,22 @@
; int DaysInYear(int year)
;
DaysInYear:
mov rax, 365
mov eax, 365
; divisible by 4?
rem rcx, ax0, 4
rem ecx, ax0, 4
jrcxnz .end
; divisible by 100?
rem rcx, ax0, 100
rem ecx, ax0, 100
jrcxnz .leap
; divisible by 400?
rem rcx, ax0, 400
rem ecx, ax0, 400
jrcxnz .end
.leap:
inc rax
inc eax
.end:
ret
@ -47,37 +47,37 @@ DaysInYear:
GetTimeUTC:
ytime
mov rdi, .buf
mov edi, .buf
; seconds
rem rsi, rax, 60
mov b[rdi], rsi
rem esi, eax, 60
mov b[edi], esi
; minutes
div rsi, rax, 60
rem rsi, 60
mov b[rdi+1], rsi
div esi, eax, 60
rem esi, 60
mov b[edi+1], esi
; hours
div rsi, rax, 3600
rem rsi, 24
mov b[rdi+2], rsi
div esi, eax, 3600
rem esi, 24
mov b[edi+2], esi
; month days
div rsi, rax, 3600*24
mov b[rdi+3], rsi
div esi, eax, 3600*24
mov b[edi+3], esi
; month
mov b[rdi+4], rbx
mov b[edi+4], rbx
; years
mov w[rdi+6], rcx
mov w[edi+6], ecx
;
; ydays (TODO)
;
mov rax, .buf
mov eax, .buf
ret
.buf = [24]

View File

@ -26,8 +26,8 @@ __sysmain:
; Entry point
;
start:
mov rsp, DOSKRNL_STACK
mov rbp, zero
mov esp, DOSKRNL_STACK
mov ebp, zero
;dump

View File

@ -7,8 +7,8 @@ start:
#include "crt/crt.k"
main:
mov rsp, 0x104000
nul rbp
mov esp, 0x104000
nul ebp
call print, .hellow

View File

@ -2,122 +2,122 @@
; See the LICENSE file in the project root for more information.
dumprf:
push rbp
mov rbp, rsp
push ebp
mov ebp, esp
push r12, r13
mov r12, ax0
mov r13, rsp
push nx0, nx1
mov nx0, ax0
mov nx1, esp
call RFS.LoadReg, r12, $cr1
push rax
call RFS.LoadReg, nx0, $cr1
push eax
call RFS.LoadReg, r12, $cr0
push rax
call RFS.LoadReg, nx0, $cr0
push eax
call RFS.LoadReg, r12, $rip
push rax
call RFS.LoadReg, nx0, $eip
push eax
push r12
push nx0
call printf, .dmp1
call RFS.LoadReg, r12, $srp
push rax
call RFS.LoadReg, nx0, $srp
push eax
call RFS.LoadReg, r12, $trp
push rax
call RFS.LoadReg, nx0, $trp
push eax
push zero # fixme
call RFS.LoadReg, r12, $rbp
push rax
call RFS.LoadReg, nx0, $ebp
push eax
call RFS.LoadReg, r12, $rsp
push rax
call RFS.LoadReg, nx0, $esp
push eax
call printf, .dmp2
call RFS.LoadReg, r12, $ax5
push rax
call RFS.LoadReg, nx0, $ax5
push eax
call RFS.LoadReg, r12, $ax4
push rax
call RFS.LoadReg, nx0, $ax4
push eax
call RFS.LoadReg, r12, $ax3
push rax
call RFS.LoadReg, nx0, $ax3
push eax
call RFS.LoadReg, r12, $ax2
push rax
call RFS.LoadReg, nx0, $ax2
push eax
call RFS.LoadReg, r12, $ax1
push rax
call RFS.LoadReg, nx0, $ax1
push eax
call RFS.LoadReg, r12, $ax0
push rax
call RFS.LoadReg, nx0, $ax0
push eax
call printf, .dmp3
call RFS.LoadReg, r12, $rdi
push rax
call RFS.LoadReg, nx0, $edi
push eax
call RFS.LoadReg, r12, $rsi
push rax
call RFS.LoadReg, nx0, $esi
push eax
call RFS.LoadReg, r12, $rdx
push rax
call RFS.LoadReg, nx0, $edx
push eax
call RFS.LoadReg, r12, $rcx
push rax
call RFS.LoadReg, nx0, $ecx
push eax
call RFS.LoadReg, r12, $rbx
push rax
call RFS.LoadReg, nx0, $ebx
push eax
call RFS.LoadReg, r12, $rax
push rax
call RFS.LoadReg, nx0, $eax
push eax
call printf, .dmp4
call RFS.LoadReg, r12, $r17
push rax
call RFS.LoadReg, nx0, $nx5
push eax
call RFS.LoadReg, r12, $r16
push rax
call RFS.LoadReg, nx0, $nx4
push eax
call RFS.LoadReg, r12, $r15
push rax
call RFS.LoadReg, nx0, $nx3
push eax
call RFS.LoadReg, r12, $r14
push rax
call RFS.LoadReg, nx0, $nx2
push eax
call RFS.LoadReg, r12, $r13
push rax
call RFS.LoadReg, nx0, $nx1
push eax
call RFS.LoadReg, r12, $r12
push rax
call RFS.LoadReg, nx0, $nx0
push eax
call printf, .dmp5
call RFS.LoadReg, r12, $r20
push rax
call RFS.LoadReg, nx0, $nx8
push eax
call RFS.LoadReg, r12, $r19
push rax
call RFS.LoadReg, nx0, $nx7
push eax
call RFS.LoadReg, r12, $r18
push rax
call RFS.LoadReg, nx0, $nx6
push eax
call printf, .dmp6
mov rsp, r13
pop r13, r12
mov esp, nx1
pop nx1, nx0
leave
ret
.dmp1 = "Environ #1:\nfrm=0d%d rip=0x%x\ncr0=0x%x cr1=0x%x\n\n"
.dmp2 = "Environ #2:\nrsp=0x%x rbp=0x%x ind=0d%d\ngrp=0x%x trp=0x%x srp=0x%x\n\n"
.dmp1 = "Environ #1:\nfrm=0d%d eip=0x%x\ncr0=0x%x cr1=0x%x\n\n"
.dmp2 = "Environ #2:\nesp=0x%x ebp=0x%x ind=0d%d\ngrp=0x%x trp=0x%x srp=0x%x\n\n"
.dmp3 = "Argument:\nax0=0x%x ax1=0x%x ax2=0x%x\nax3=0x%x ax4=0x%x ax5=0x%x\n\n"
.dmp4 = "Volatile:\nrax=0x%x rbx=0x%x rcx=0x%x\nrdx=0x%x rsi=0x%x rdi=0x%x\n\n"
.dmp5 = "Persistent:\nr12=0x%x r13=0x%x r14=0x%x\nr15=0x%x r16=0x%x r17=0x%x\n"
.dmp6 = "r18=0x%x r19=0x%x r20=0x%x\n"
.dmp4 = "Volatile:\neax=0x%x ebx=0x%x ecx=0x%x\nrdx=0x%x esi=0x%x edi=0x%x\n\n"
.dmp5 = "Persistent:\nnx0=0x%x nx1=0x%x nx2=0x%x\nr15=0x%x nx4=0x%x nx5=0x%x\n"
.dmp6 = "nx6=0x%x nx7=0x%x nx8=0x%x\n"

View File

@ -2,8 +2,8 @@
; See the LICENSE file in the project root for more information.
ScreenOfDeath:
push r12
mov r12, ax0
push nx0
mov nx0, ax0
prn 10
prn PRN_CLEAR
@ -13,13 +13,13 @@ ScreenOfDeath:
call printf, .scr1
pop
bnz r14, .not_con
bnz nx2, .not_con
push .scr2_con
jmp .do_print
.not_con:
; rframes #2-#3-#4 belong to kernel
blt 4, r14, .not_krn
blt 4, nx2, .not_krn
push .scr2_krn
jmp .do_print
@ -27,11 +27,11 @@ ScreenOfDeath:
push .scr2_usr
.do_print:
push r14, r12
push nx2, nx0
call printf, .scr2
add rsp, 24
add esp, 24
call dumprf, r14
call dumprf, nx2
prn 10
call print, .scr3
@ -40,11 +40,11 @@ ScreenOfDeath:
pause
pause
scan rax
scan eax
jraxz .loop
beq rax, 0x0A, DefaultTrapHandler.handle_Exit
beq rax, 0x1B, DefaultTrapHandler.handle_Shutdown
beq eax, 0x0A, DefaultTrapHandler.handle_Exit
beq eax, 0x1B, DefaultTrapHandler.handle_Shutdown
jmp .loop

View File

@ -2,14 +2,14 @@
; See the LICENSE file in the project root for more information.
DefaultExceptionHandler:
mov rsp, EXCT0_STACK
mov ax1, r12
mov esp, EXCT0_STACK
mov ax1, nx0
bzr r12, DefaultTrapHandler.handle_Shutdown
bltu 11, r12, .unknown
bzr nx0, DefaultTrapHandler.handle_Shutdown
bltu 11, nx0, .unknown
mov rsi, .err_ukn
lea ax0, b[rsi + r12 * 32]
mov esi, .err_ukn
lea ax0, b[esi + nx0 * 32]
call ScreenOfDeath
.unknown:

View File

@ -2,27 +2,27 @@
; See the LICENSE file in the project root for more information.
TrapHandlers.prolog:
nul rbp
nul ebp
; rax = caller's cr1
call RFS.LoadReg, r14, $cr1
; eax = caller's cr1
call RFS.LoadReg, nx2, $cr1
; we don't preserve the r12 we got
mov r12, rax
nul rdx
; we don't preserve the nx0 we got
mov nx0, eax
nul edx
jmp rcx
jmp ecx
TrapHandlers.epilog:
; TRAP return values: RAX-RDX
; TRAP return values: eax-edx
mov ax2, rax
call RFS.StoreReg, r14, $rax
mov ax2, eax
call RFS.StoreReg, nx2, $eax
mov ax2, rdx
call RFS.StoreReg, r14, $rdx
mov ax2, edx
call RFS.StoreReg, nx2, $edx
call IDT.DoneHandling, r13
call IDT.DoneHandling, nx1
iret
@ -32,42 +32,42 @@ TrapHandlers.epilog:
DefaultTrapHandler:
.init:
mov rcx, .text
mov rsp, TRAP0_STACK
mov ecx, .text
mov esp, TRAP0_STACK
jmp TrapHandlers.prolog
.text:
call RFS.LoadReg, r14, $rax
call RFS.LoadArgs, r14
call RFS.LoadReg, nx2, $eax
call RFS.LoadArgs, nx2
; will be optimized with a table
; when we have a "finished" consistent API
jraxz .handle_Shutdown
beq rax, Sys.Exit, .handle_Exit
beq rax, Sys.ExecuteInFrame, .handle_EIF
beq rax, Sys.ReadFile, .handle_ReadFile
beq rax, Sys.OpenFile, .handle_OpenFile
beq rax, Sys.CloseFile, .handle_CloseFile
beq rax, Sys.CreateFile, .handle_CreateFile
beq rax, Sys.RemoveFile, .handle_RemoveFile
beq rax, Sys.FindNext, .handle_FindNext
beq rax, Sys.FindFirst, .handle_FindFirst
beq rax, Sys.EnterHaltMode, .handle_HaltMode
beq eax, Sys.Exit, .handle_Exit
beq eax, Sys.ExecuteInFrame, .handle_EIF
beq eax, Sys.ReadFile, .handle_ReadFile
beq eax, Sys.OpenFile, .handle_OpenFile
beq eax, Sys.CloseFile, .handle_CloseFile
beq eax, Sys.CreateFile, .handle_CreateFile
beq eax, Sys.RemoveFile, .handle_RemoveFile
beq eax, Sys.FindNext, .handle_FindNext
beq eax, Sys.FindFirst, .handle_FindFirst
beq eax, Sys.EnterHaltMode, .handle_HaltMode
.sod:
call ScreenOfDeath, .badsyscall, r13
call ScreenOfDeath, .badsyscall, nx1
.wrong:
call ScreenOfDeath, .badparams, r13
call ScreenOfDeath, .badparams, nx1
.fini.savecx:
mov ax2, rcx
call RFS.StoreReg, r14, $rcx
mov ax2, ecx
call RFS.StoreReg, nx2, $ecx
.fini:
jmp TrapHandlers.epilog
.badsyscall = "Invalid syscall number in RAX"
.badsyscall = "Invalid syscall number in eax"
.badparams = "Invalid syscall parameters"
;------------------------------------------------;
@ -79,46 +79,46 @@ DefaultTrapHandler:
;
.handle_Exit:
; deactivate current rframe
bzr r14, .hE_nz ; unless it's 0...
call RFS.DeactivateFrame, r14
bzr nx2, .hE_nz ; unless it's 0...
call RFS.DeactivateFrame, nx2
.hE_nz:
; open COMMAND.COM
call DISK.OpenFile, .cmdcom
; crash on failure
bltz rax, abort
bltz eax, abort
; load at CMDCOM_LOADP
mov ax1, CMDCOM_LOADP
mov ax2, CMDCOM_MAXSZ
call DISK.ReadFile, rax
call DISK.ReadFile, eax
; assume that COMMAND.COM being
; less then 4KB means something
; went wrong
blt rax, 0x1000, abort
blt eax, 0x1000, abort
; close the handle
call DISK.CloseFile, rax
call DISK.CloseFile, eax
; code address
mov ax2, 0x100000
call RFS.StoreReg, zero, $rip
call RFS.StoreReg, zero, $eip
; usermode
mov ax2, 3
call RFS.StoreReg, zero, $cr0
mov rcx, CMDCOM_LOADP
sub rcx, 0x100000
mov ecx, CMDCOM_LOADP
sub ecx, 0x100000
; code offset
mov ax2, rcx
mov ax2, ecx
call RFS.StoreReg, zero, $cr1
; return frame
nul r14
nul nx2
jmp .fini
@ -127,39 +127,39 @@ DefaultTrapHandler:
.handle_EIF:
blt ax1, 5, .wrong
; RIP can't be <1MB
mov rcx, 0x100000
blt ax0, rcx, .wrong
; eip can't be <1MB
mov ecx, 0x100000
blt ax0, ecx, .wrong
; add old CR1
add ax0, r12
add ax0, nx0
; real RIP can't be <1MB+32KB either
; real eip can't be <1MB+32KB either
; (kernel + cmdcom personal space)
add rdx, rcx, 0x8000
blt ax0, rcx, .wrong
add edx, ecx, 0x8000
blt ax0, ecx, .wrong
; save rframe and compute new CR1
mov rbx, ax1
sub rdi, ax0, rcx
mov ebx, ax1
sub edi, ax0, ecx
; activate rframe
call RFS.ActivateFrame, rbx
call RFS.ActivateFrame, ebx
; save new CR1
mov ax2, rdi
call RFS.StoreReg, rbx, $cr1
mov ax2, edi
call RFS.StoreReg, ebx, $cr1
; interruptible user mode
mov ax2, 3
call RFS.StoreReg, rbx, $cr0
call RFS.StoreReg, ebx, $cr0
; set RIP = 1MB
mov ax2, rcx
call RFS.StoreReg, rbx, $rip
; set eip = 1MB
mov ax2, ecx
call RFS.StoreReg, ebx, $eip
; change return frame
mov r14, rbx
mov nx2, ebx
jmp .fini
@ -167,27 +167,27 @@ DefaultTrapHandler:
; Disk API
;
.handle_FindFirst:
add ax0, r12
add ax0, nx0
call DISK.FindFirst
jmp .fini.savecx
.handle_FindNext:
add ax0, r12
add ax0, nx0
call DISK.FindNext
jmp .fini.savecx
.handle_OpenFile:
add ax0, r12
add ax0, nx0
call DISK.OpenFile
jmp .fini
.handle_CreateFile:
add ax0, r12
add ax0, nx0
call DISK.CreateFile
jmp .fini
.handle_RemoveFile:
add ax0, r12
add ax0, nx0
call DISK.RemoveFile
jmp .fini
@ -196,7 +196,7 @@ DefaultTrapHandler:
jmp .fini
.handle_ReadFile:
add ax1, r12
add ax1, nx0
call DISK.ReadFile
jmp .fini

View File

@ -13,7 +13,7 @@ PrintBootMsg:
InitSyscalls:
call RFS.ActivateFrame, 1
mov ax1, $rip
mov ax1, $eip
mov ax2, DefaultTrapHandler
call RFS.StoreReg
@ -26,14 +26,14 @@ InitSyscalls:
InitExcepts:
call RFS.ActivateFrame, 2
mov ax1, $rip
mov ax1, $eip
mov ax2, DefaultExceptionHandler
call RFS.StoreReg
call IDT.AddHandler, zero, 2
SwitchToCMD:
mov rax, Sys.Exit
mov eax, Sys.Exit
trap 0
;

View File

@ -4,20 +4,20 @@
#define N 11
.handle_DIR:
push rbp
mov rbp, rsp
push ebp
mov ebp, esp
push r12, r13
push r14, r15
push nx0, nx1
push nx2, nx3
nul r12 ; no. of files found
nul r13 ; no. of directories found
nul r14 ; total amount of bytes found
nul nx0 ; no. of files found
nul nx1 ; no. of directories found
nul nx2 ; total amount of bytes found
call print, .dirmsg
.dir_first:
mov rax, Sys.FindFirst
mov eax, Sys.FindFirst
mov ax0, .dir_buf
mov ax1, FNAME_MAX
trap 0
@ -25,7 +25,7 @@
jmp .dir_list
.dir_next:
mov rax, Sys.FindNext
mov eax, Sys.FindNext
mov ax0, .dir_buf
mov ax1, FNAME_MAX
trap 0
@ -33,54 +33,54 @@
.dir_list:
jraxz .dir_end
mov r15, rcx ; file size
add r14, rcx
mov nx3, ecx ; file size
add nx2, ecx
; directory?
bnz rdx, .dir_is_dir
bnz edx, .dir_is_dir
; found a file
inc r12
inc nx0
; separate extension from file name
mov rcx, FNAME_MAX
mov rsi, .dir_buf
mov rdi, rsi
scasb rsi, '.'
mov ecx, FNAME_MAX
mov esi, .dir_buf
mov edi, esi
scasb esi, '.'
; print file name
sub ax1, rsi, rdi
sub ax1, esi, edi
dec ax1
call nprint, rdi
call nprint, edi
; calculate where to put extension
sub rdi, rsi, .dir_buf
dec rdi
sub edi, esi, .dir_buf
dec edi
.dir_ext_pad:
; print at least N non-space characters before extension
blte N, rdi, .dir_print_ext
blte N, edi, .dir_print_ext
prn ' '
inc rdi
inc edi
jmp .dir_ext_pad
.dir_print_ext:
prn ' '
; here we print at least 4 characters excluding '.'
mov rcx, 4
mov ecx, 4
bne b[rsi], '.', .dir_print_ext.1
inc rsi
bne b[esi], '.', .dir_print_ext.1
inc esi
.dir_print_ext.1:
bzr b[rsi], .dir_print_ext.2
bzr b[esi], .dir_print_ext.2
; print and decrease rcx, unless it's already 0
prn b[rsi]
inc rsi
; print and decrease ecx, unless it's already 0
prn b[esi]
inc esi
jrcxz .dir_print_ext.1
dec rcx
dec ecx
jmp .dir_print_ext.1
.dir_print_ext.2:
@ -97,12 +97,12 @@
prn ' '
prn ' '
shr rax, r15, 10
and r15, 1023
shr eax, nx3, 10
and nx3, 1023
push r15, rax
push nx3, eax
call printf, .dir_bytesstr
add rsp, 16
add esp, 16
.dir_prepare_next:
; go find next entry
@ -110,37 +110,37 @@
jmp .dir_next
.dir_end:
shr rax, r14, 10
shr rdx, rax, 10
and rax, 1023
and r14, 1023
shr eax, nx2, 10
shr edx, eax, 10
and eax, 1023
and nx2, 1023
push r14, rax, rdx
push nx2, eax, edx
call printf, .dir_endstr0
add rsp, 24
add esp, 24
push r13, r12
push nx1, nx0
call printf, .dir_endstr1
add rsp, 16
add esp, 16
pop r15, r14
pop r13, r12
pop nx3, nx2
pop nx1, nx0
leave
jmp .print_prompt
; special case: direcory
.dir_is_dir:
inc r13
inc nx1
; use printf instead of print
; because it returns the # of
; printed characters
call printf, .dir_buf
blte N, rax, .dir_no_pad
sub rcx, N, rax
dec rcx
blte N, eax, .dir_no_pad
sub ecx, N, eax
dec ecx
.dir.l:
prn ' '

View File

@ -3,15 +3,15 @@
.try_exec:
; try without appending ".com"
mov rax, Sys.OpenFile
mov eax, Sys.OpenFile
mov ax0, argv0
trap 0
; we good?
blte zero, rax, .do_exec
blte zero, eax, .do_exec
; nope, append ".com" and try again
sub rcx, argbuf.size, 5
sub ecx, argbuf.size, 5
scasb ax0, zero ; find null-term
mov b[ax0+0], '.' ; ".com"
mov b[ax0+1], 'c'
@ -20,34 +20,34 @@
nul b[ax0+4]
; try again
mov rax, Sys.OpenFile
mov eax, Sys.OpenFile
mov ax0, argv0
trap 0
; still no good?
bltz rax, .exec_not_found
bltz eax, .exec_not_found
.do_exec:
; load file into memory
mov ax0, rax
mov ax0, eax
mov ax1, FILE_LOADP
mov ax2, FILE_MAXSZ
mov rax, Sys.ReadFile
mov eax, Sys.ReadFile
trap 0
; save load address
mov rcx, rax
mov ecx, eax
; close file
mov rax, Sys.CloseFile
mov eax, Sys.CloseFile
trap 0
; read anything?
bltz rcx, .couldnt_read
bltz ecx, .couldnt_read
jrcxz .empty_file
; all good, let's go
mov rax, Sys.ExecuteInFrame
mov eax, Sys.ExecuteInFrame
mov ax0, FILE_LOADP
mov ax1, 5
trap 0

View File

@ -2,59 +2,59 @@
; See the LICENSE file in the project root for more information.
.handle_ERASE:
mov rax, Sys.RemoveFile
mov eax, Sys.RemoveFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .couldnt_remove
bltz eax, .couldnt_remove
jmp .handle_MAKE ; re-create it back
.handle_MAKE:
mov rax, Sys.CreateFile
mov eax, Sys.CreateFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .couldnt_open
bltz eax, .couldnt_open
jmp .print_prompt
.handle_PRINT:
mov rax, Sys.OpenFile
mov eax, Sys.OpenFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .file_not_found
bltz eax, .file_not_found
mov ax0, rax
mov ax0, eax
mov ax1, FILE_LOADP
mov ax2, FILE_MAXSZ
mov rax, Sys.ReadFile
mov eax, Sys.ReadFile
trap 0
mov rcx, rax
mov ecx, eax
mov rax, Sys.CloseFile
mov eax, Sys.CloseFile
trap 0
bltz rcx, .couldnt_read
bltz ecx, .couldnt_read
jrcxz .empty_file
call nprint, FILE_LOADP, rcx
call nprint, FILE_LOADP, ecx
jmp .print_prompt
.handle_REMOVE:
mov rax, Sys.RemoveFile
mov eax, Sys.RemoveFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .couldnt_remove
bltz eax, .couldnt_remove
jmp .print_prompt

View File

@ -991,21 +991,21 @@ abort:
; COMMAND.COM guarantees that programs
; are always loaded on a 16KB boundary
; This is guaranteed to be the case
; in all future versions as well
; in all future veesions as well
start:
mov rsp, 0x104000
mov rbp, zero
mov esp, 0x104000
mov ebp, zero
call main
mov rax, Sys.EnterHaltMode
mov eax, Sys.EnterHaltMode
trap 0
crash
cmd.versionstr = "COMMAND.COM, version 0.1 (KVISC)\nCopyright (C) 2019, The OS/K Team\nMIT license (permissive), see LICENCE file in source tree"
cmd.veesionstr = "COMMAND.COM, veesion 0.1 (KVISC)\nCopyright (C) 2019, The OS/K Team\nMIT license (permissive), see LICENCE file in source tree"
argbuf.size := 256
argbuf = [argbuf.size]
@ -1017,12 +1017,12 @@ stdin_echoing = 1
prompt = [32]
main:
mov rsi, prompt
mov b[rsi+0], 'C'
mov b[rsi+1], ':'
mov b[rsi+2], '\'
mov b[rsi+3], '>'
mov b[rsi+4], ' '
mov esi, prompt
mov b[esi+0], 'C'
mov b[esi+1], ':'
mov b[esi+2], '\'
mov b[esi+3], '>'
mov b[esi+4], ' '
.print_prompt:
call print, prompt
@ -1035,7 +1035,7 @@ main:
; call nprint, argv0, argbuf.size
; iterator through argbuf
nul rcx
nul ecx
.input_loop:
pause
@ -1043,22 +1043,22 @@ main:
pause
; Fill .buf with user input
scan rax
scan eax
jraxz .input_loop
; ESC key pressed?
beq rax, 0x1B, .handle_EXIT
beq eax, 0x1B, .handle_EXIT
; backspace character?
bne rax, 8, .handle_input
bne eax, 8, .handle_input
; anything to delete?
jrcxz .input_loop ; no
; yes, delete it
dec rcx
add rdx, rcx, argbuf
nul b[rdx]
dec ecx
add edx, ecx, argbuf
nul b[edx]
; update screen
bzr b[stdin_echoing], .input_loop
@ -1068,19 +1068,19 @@ main:
.handle_input:
bzr b[stdin_echoing], .se.z
prn rax
prn eax
.se.z:
beq rax, 10, .extract_argv0
beq eax, 10, .extract_argv0
; when max line length is reached,
; force a newline
beq rcx, argbuf.size, .extract_argv0
beq ecx, argbuf.size, .extract_argv0
; add character to buffer and increase iterator (rcx)
add rdx, rcx, argbuf
mov b[rdx], rax
inc rcx
; add character to buffer and increase iterator (ecx)
add edx, ecx, argbuf
mov b[edx], eax
inc ecx
; another one
jmp .input_loop
@ -1091,43 +1091,43 @@ main:
jrcxz .print_prompt
; find first whitespace or null-terminator
mov rcx, argbuf.size
mov rdx, argbuf
scasb rdx, ' '
mov ecx, argbuf.size
mov edx, argbuf
scasb edx, ' '
; argv1 exists? if so, save its position
mov rsi, rdx
mov esi, edx
.next_space:
mov rcx, b[rsi]
mov ecx, b[esi]
jrcxz .do_extract
; skip spaces
bne rcx, ' ', .not_a_space
inc rsi
bne ecx, ' ', .not_a_space
inc esi
jmp .next_space
.not_a_space:
; if we're here, we found a
; non-zero non-space character
mov q[argv1pos], rsi
mov q[argv1pos], esi
; fallthrough
.do_extract:
; how much do we copy?
sub rcx, rdx, argbuf
sub ecx, edx, argbuf
jrcxz .detect_builtin
dec rcx
dec ecx
mov rdi, argbuf
mov rsi, argv0
mov edi, argbuf
mov esi, argv0
.copy_loop:
mov b[rsi], b[rdi]
mov b[esi], b[edi]
inc rdi
inc rsi
inc edi
inc esi
loop .copy_loop
@ -1210,20 +1210,20 @@ main:
.handle_DIR:
push rbp
mov rbp, rsp
push ebp
mov ebp, esp
push r12, r13
push r14, r15
push nx0, nx1
push nx2, nx3
nul r12 ; no. of files found
nul r13 ; no. of directories found
nul r14 ; total amount of bytes found
nul nx0 ; no. of files found
nul nx1 ; no. of directories found
nul nx2 ; total amount of bytes found
call print, .dirmsg
.dir_first:
mov rax, Sys.FindFirst
mov eax, Sys.FindFirst
mov ax0, .dir_buf
mov ax1, 0x80
trap 0
@ -1231,7 +1231,7 @@ main:
jmp .dir_list
.dir_next:
mov rax, Sys.FindNext
mov eax, Sys.FindNext
mov ax0, .dir_buf
mov ax1, 0x80
trap 0
@ -1239,54 +1239,54 @@ main:
.dir_list:
jraxz .dir_end
mov r15, rcx ; file size
add r14, rcx
mov nx3, ecx ; file size
add nx2, ecx
; directory?
bnz rdx, .dir_is_dir
bnz edx, .dir_is_dir
; found a file
inc r12
inc nx0
; separate extension from file name
mov rcx, 0x80
mov rsi, .dir_buf
mov rdi, rsi
scasb rsi, '.'
mov ecx, 0x80
mov esi, .dir_buf
mov edi, esi
scasb esi, '.'
; print file name
sub ax1, rsi, rdi
sub ax1, esi, edi
dec ax1
call nprint, rdi
call nprint, edi
; calculate where to put extension
sub rdi, rsi, .dir_buf
dec rdi
sub edi, esi, .dir_buf
dec edi
.dir_ext_pad:
; print at least 11 non-space characters before extension
blte 11, rdi, .dir_print_ext
blte 11, edi, .dir_print_ext
prn ' '
inc rdi
inc edi
jmp .dir_ext_pad
.dir_print_ext:
prn ' '
; here we print at least 4 characters excluding '.'
mov rcx, 4
mov ecx, 4
bne b[rsi], '.', .dir_print_ext.1
inc rsi
bne b[esi], '.', .dir_print_ext.1
inc esi
.dir_print_ext.1:
bzr b[rsi], .dir_print_ext.2
bzr b[esi], .dir_print_ext.2
; print and decrease rcx, unless it's already 0
prn b[rsi]
inc rsi
; print and decrease ecx, unless it's already 0
prn b[esi]
inc esi
jrcxz .dir_print_ext.1
dec rcx
dec ecx
jmp .dir_print_ext.1
.dir_print_ext.2:
@ -1303,12 +1303,12 @@ main:
prn ' '
prn ' '
shr rax, r15, 10
and r15, 1023
shr eax, nx3, 10
and nx3, 1023
push r15, rax
push nx3, eax
call printf, .dir_bytesstr
add rsp, 16
add esp, 16
.dir_prepare_next:
; go find next entry
@ -1316,37 +1316,37 @@ main:
jmp .dir_next
.dir_end:
shr rax, r14, 10
shr rdx, rax, 10
and rax, 1023
and r14, 1023
shr eax, nx2, 10
shr edx, eax, 10
and eax, 1023
and nx2, 1023
push r14, rax, rdx
push nx2, eax, edx
call printf, .dir_endstr0
add rsp, 24
add esp, 24
push r13, r12
push nx1, nx0
call printf, .dir_endstr1
add rsp, 16
add esp, 16
pop r15, r14
pop r13, r12
pop nx3, nx2
pop nx1, nx0
leave
jmp .print_prompt
; special case: direcory
.dir_is_dir:
inc r13
inc nx1
; use printf instead of print
; because it returns the # of
; printed characters
call printf, .dir_buf
blte 11, rax, .dir_no_pad
sub rcx, 11, rax
dec rcx
blte 11, eax, .dir_no_pad
sub ecx, 11, eax
dec ecx
.dir.l:
prn ' '
@ -1372,15 +1372,15 @@ main:
.try_exec:
; try without appending ".com"
mov rax, Sys.OpenFile
mov eax, Sys.OpenFile
mov ax0, argv0
trap 0
; we good?
blte zero, rax, .do_exec
blte zero, eax, .do_exec
; nope, append ".com" and try again
sub rcx, argbuf.size, 5
sub ecx, argbuf.size, 5
scasb ax0, zero ; find null-term
mov b[ax0+0], '.' ; ".com"
mov b[ax0+1], 'c'
@ -1389,34 +1389,34 @@ main:
nul b[ax0+4]
; try again
mov rax, Sys.OpenFile
mov eax, Sys.OpenFile
mov ax0, argv0
trap 0
; still no good?
bltz rax, .exec_not_found
bltz eax, .exec_not_found
.do_exec:
; load file into memory
mov ax0, rax
mov ax0, eax
mov ax1, 0x108000
mov ax2, 0x8000
mov rax, Sys.ReadFile
mov eax, Sys.ReadFile
trap 0
; save load address
mov rcx, rax
mov ecx, eax
; close file
mov rax, Sys.CloseFile
mov eax, Sys.CloseFile
trap 0
; read anything?
bltz rcx, .couldnt_read
bltz ecx, .couldnt_read
jrcxz .empty_file
; all good, let's go
mov rax, Sys.ExecuteInFrame
mov eax, Sys.ExecuteInFrame
mov ax0, 0x108000
mov ax1, 5
trap 0
@ -1429,59 +1429,59 @@ main:
; See the LICENSE file in the project root for more information.
.handle_ERASE:
mov rax, Sys.RemoveFile
mov eax, Sys.RemoveFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .couldnt_remove
bltz eax, .couldnt_remove
jmp .handle_MAKE ; re-create it back
.handle_MAKE:
mov rax, Sys.CreateFile
mov eax, Sys.CreateFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .couldnt_open
bltz eax, .couldnt_open
jmp .print_prompt
.handle_PRINT:
mov rax, Sys.OpenFile
mov eax, Sys.OpenFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .file_not_found
bltz eax, .file_not_found
mov ax0, rax
mov ax0, eax
mov ax1, 0x108000
mov ax2, 0x8000
mov rax, Sys.ReadFile
mov eax, Sys.ReadFile
trap 0
mov rcx, rax
mov ecx, eax
mov rax, Sys.CloseFile
mov eax, Sys.CloseFile
trap 0
bltz rcx, .couldnt_read
bltz ecx, .couldnt_read
jrcxz .empty_file
call nprint, 0x108000, rcx
call nprint, 0x108000, ecx
jmp .print_prompt
.handle_REMOVE:
mov rax, Sys.RemoveFile
mov eax, Sys.RemoveFile
mov ax0, q[argv1pos]
bzr ax0, .need_params
trap 0
bltz rax, .couldnt_remove
bltz eax, .couldnt_remove
jmp .print_prompt
# 230 "command.k" 2
@ -1496,12 +1496,12 @@ main:
.handle_DATE:
call GetTimeUTC
mov rcx, b[rax+4]
inc rcx
mov ecx, b[eax+4]
inc ecx
push b[rax+3], rcx, w[rax+6]
push b[eax+3], ecx, w[eax+6]
call printf, .datefmt
add rsp, 40
add esp, 40
jmp .print_prompt
@ -1512,22 +1512,22 @@ main:
jmp .print_prompt
.handle_ECHO:
mov rax, q[argv1pos]
mov eax, q[argv1pos]
jraxz .echo.end
call print, rax
call print, eax
.echo.end:
prn 10
jmp .print_prompt
.handle_EXIT:
mov rax, Sys.Shutdown
mov eax, Sys.Shutdown
trap 0
jmp .print_prompt
.handle_HALT:
mov rax, Sys.EnterHaltMode
mov eax, Sys.EnterHaltMode
trap 0
jmp .print_prompt
@ -1542,16 +1542,16 @@ main:
.handle_TIME:
call GetTimeUTC
push b[rax], b[rax+1], b[rax+2]
push b[eax], b[eax+1], b[eax+2]
call printf, .timefmt
add rsp, 24
add esp, 24
jmp .print_prompt
.timefmt = "%d:%d:%d\n"
.handle_VERS:
call print, cmd.versionstr
call print, cmd.veesionstr
prn 10
jmp .print_prompt
@ -1591,12 +1591,12 @@ main:
.helpmsg.prompt = " PROMPT Change the command line prompt\n"
.helpmsg.remove = " REMOVE Delete a file (permanently)\n"
.helpmsg.time = " TIME Display current time of day\n"
.helpmsg.ver = " VERS Display current COMMAND.COM version\n"
.helpmsg.ver = " VERS Display current COMMAND.COM veesion\n"
.exec_not_found:
push argv0
call printf, .enf_errmsg
add rsp, 8
add esp, 8
jmp .print_prompt
@ -1605,7 +1605,7 @@ main:
.file_not_found:
push q[argv1pos], argv0
call printf, .fnf_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -1614,7 +1614,7 @@ main:
.empty_file:
push q[argv1pos], argv0
call printf, .ef_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -1623,7 +1623,7 @@ main:
.couldnt_open:
push q[argv1pos], argv0
call printf, .cno_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -1632,7 +1632,7 @@ main:
.couldnt_remove:
push q[argv1pos], argv0
call printf, .cne_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt
@ -1641,7 +1641,7 @@ main:
.couldnt_read:
push q[argv1pos], argv0
call printf, .cnr_errmsg
add rsp, 16
add esp, 16
jmp .print_prompt

View File

@ -1005,8 +1005,8 @@ abort:
; Entry point
;
start:
mov rsp, 0x104000
mov rbp, zero
mov esp, 0x104000
mov ebp, zero
;dump
@ -1151,14 +1151,14 @@ DISK.WriteFile:
; See the LICENSE file in the project root for more information.
DefaultExceptionHandler:
mov rsp, 0x105800
mov ax1, r12
mov esp, 0x105800
mov ax1, nx0
bzr r12, DefaultTrapHandler.handle_Shutdown
bltu 11, r12, .unknown
bzr nx0, DefaultTrapHandler.handle_Shutdown
bltu 11, nx0, .unknown
mov rsi, .err_ukn
lea ax0, b[rsi + r12 * 32]
mov esi, .err_ukn
lea ax0, b[esi + nx0 * 32]
call ScreenOfDeath
.unknown:
@ -1182,8 +1182,8 @@ DefaultExceptionHandler:
; See the LICENSE file in the project root for more information.
ScreenOfDeath:
push r12
mov r12, ax0
push nx0
mov nx0, ax0
prn 10
prn 0x8BF00001
@ -1193,13 +1193,13 @@ ScreenOfDeath:
call printf, .scr1
pop
bnz r14, .not_con
bnz nx2, .not_con
push .scr2_con
jmp .do_print
.not_con:
; rframes #2-#3-#4 belong to kernel
blt 4, r14, .not_krn
blt 4, nx2, .not_krn
push .scr2_krn
jmp .do_print
@ -1207,11 +1207,11 @@ ScreenOfDeath:
push .scr2_usr
.do_print:
push r14, r12
push nx2, nx0
call printf, .scr2
add rsp, 24
add esp, 24
call dumprf, r14
call dumprf, nx2
prn 10
call print, .scr3
@ -1220,11 +1220,11 @@ ScreenOfDeath:
pause
pause
scan rax
scan eax
jraxz .loop
beq rax, 0x0A, DefaultTrapHandler.handle_Exit
beq rax, 0x1B, DefaultTrapHandler.handle_Shutdown
beq eax, 0x0A, DefaultTrapHandler.handle_Exit
beq eax, 0x1B, DefaultTrapHandler.handle_Shutdown
jmp .loop
@ -1243,27 +1243,27 @@ ScreenOfDeath:
; See the LICENSE file in the project root for more information.
TrapHandlers.prolog:
nul rbp
nul ebp
; rax = caller's cr1
call RFS.LoadReg, r14, $cr1
; eax = caller's cr1
call RFS.LoadReg, nx2, $cr1
; we don't preserve the r12 we got
mov r12, rax
nul rdx
; we don't preserve the nx0 we got
mov nx0, eax
nul edx
jmp rcx
jmp ecx
TrapHandlers.epilog:
; TRAP return values: RAX-RDX
; TRAP return values: eax-edx
mov ax2, rax
call RFS.StoreReg, r14, $rax
mov ax2, eax
call RFS.StoreReg, nx2, $eax
mov ax2, rdx
call RFS.StoreReg, r14, $rdx
mov ax2, edx
call RFS.StoreReg, nx2, $edx
call IDT.DoneHandling, r13
call IDT.DoneHandling, nx1
iret
@ -1273,42 +1273,42 @@ TrapHandlers.epilog:
DefaultTrapHandler:
.init:
mov rcx, .text
mov rsp, 0x104800
mov ecx, .text
mov esp, 0x104800
jmp TrapHandlers.prolog
.text:
call RFS.LoadReg, r14, $rax
call RFS.LoadArgs, r14
call RFS.LoadReg, nx2, $eax
call RFS.LoadArgs, nx2
; will be optimized with a table
; when we have a "finished" consistent API
jraxz .handle_Shutdown
beq rax, Sys.Exit, .handle_Exit
beq rax, Sys.ExecuteInFrame, .handle_EIF
beq rax, Sys.ReadFile, .handle_ReadFile
beq rax, Sys.OpenFile, .handle_OpenFile
beq rax, Sys.CloseFile, .handle_CloseFile
beq rax, Sys.CreateFile, .handle_CreateFile
beq rax, Sys.RemoveFile, .handle_RemoveFile
beq rax, Sys.FindNext, .handle_FindNext
beq rax, Sys.FindFirst, .handle_FindFirst
beq rax, Sys.EnterHaltMode, .handle_HaltMode
beq eax, Sys.Exit, .handle_Exit
beq eax, Sys.ExecuteInFrame, .handle_EIF
beq eax, Sys.ReadFile, .handle_ReadFile
beq eax, Sys.OpenFile, .handle_OpenFile
beq eax, Sys.CloseFile, .handle_CloseFile
beq eax, Sys.CreateFile, .handle_CreateFile
beq eax, Sys.RemoveFile, .handle_RemoveFile
beq eax, Sys.FindNext, .handle_FindNext
beq eax, Sys.FindFirst, .handle_FindFirst
beq eax, Sys.EnterHaltMode, .handle_HaltMode
.sod:
call ScreenOfDeath, .badsyscall, r13
call ScreenOfDeath, .badsyscall, nx1
.wrong:
call ScreenOfDeath, .badparams, r13
call ScreenOfDeath, .badparams, nx1
.fini.savecx:
mov ax2, rcx
call RFS.StoreReg, r14, $rcx
mov ax2, ecx
call RFS.StoreReg, nx2, $ecx
.fini:
jmp TrapHandlers.epilog
.badsyscall = "Invalid syscall number in RAX"
.badsyscall = "Invalid syscall number in eax"
.badparams = "Invalid syscall parameters"
;------------------------------------------------;
@ -1320,46 +1320,46 @@ DefaultTrapHandler:
;
.handle_Exit:
; deactivate current rframe
bzr r14, .hE_nz ; unless it's 0...
call RFS.DeactivateFrame, r14
bzr nx2, .hE_nz ; unless it's 0...
call RFS.DeactivateFrame, nx2
.hE_nz:
; open COMMAND.COM
call DISK.OpenFile, .cmdcom
; crash on failure
bltz rax, abort
bltz eax, abort
; load at 0x108000
mov ax1, 0x108000
mov ax2, 0x80000
call DISK.ReadFile, rax
call DISK.ReadFile, eax
; assume that COMMAND.COM being
; less then 4KB means something
; went wrong
blt rax, 0x1000, abort
blt eax, 0x1000, abort
; close the handle
call DISK.CloseFile, rax
call DISK.CloseFile, eax
; code address
mov ax2, 0x100000
call RFS.StoreReg, zero, $rip
call RFS.StoreReg, zero, $eip
; usermode
mov ax2, 3
call RFS.StoreReg, zero, $cr0
mov rcx, 0x108000
sub rcx, 0x100000
mov ecx, 0x108000
sub ecx, 0x100000
; code offset
mov ax2, rcx
mov ax2, ecx
call RFS.StoreReg, zero, $cr1
; return frame
nul r14
nul nx2
jmp .fini
@ -1368,39 +1368,39 @@ DefaultTrapHandler:
.handle_EIF:
blt ax1, 5, .wrong
; RIP can't be <1MB
mov rcx, 0x100000
blt ax0, rcx, .wrong
; eip can't be <1MB
mov ecx, 0x100000
blt ax0, ecx, .wrong
; add old CR1
add ax0, r12
add ax0, nx0
; real RIP can't be <1MB+32KB either
; real eip can't be <1MB+32KB either
; (kernel + cmdcom personal space)
add rdx, rcx, 0x8000
blt ax0, rcx, .wrong
add edx, ecx, 0x8000
blt ax0, ecx, .wrong
; save rframe and compute new CR1
mov rbx, ax1
sub rdi, ax0, rcx
mov ebx, ax1
sub edi, ax0, ecx
; activate rframe
call RFS.ActivateFrame, rbx
call RFS.ActivateFrame, ebx
; save new CR1
mov ax2, rdi
call RFS.StoreReg, rbx, $cr1
mov ax2, edi
call RFS.StoreReg, ebx, $cr1
; interruptible user mode
mov ax2, 3
call RFS.StoreReg, rbx, $cr0
call RFS.StoreReg, ebx, $cr0
; set RIP = 1MB
mov ax2, rcx
call RFS.StoreReg, rbx, $rip
; set eip = 1MB
mov ax2, ecx
call RFS.StoreReg, ebx, $eip
; change return frame
mov r14, rbx
mov nx2, ebx
jmp .fini
@ -1408,27 +1408,27 @@ DefaultTrapHandler:
; Disk API
;
.handle_FindFirst:
add ax0, r12
add ax0, nx0
call DISK.FindFirst
jmp .fini.savecx
.handle_FindNext:
add ax0, r12
add ax0, nx0
call DISK.FindNext
jmp .fini.savecx
.handle_OpenFile:
add ax0, r12
add ax0, nx0
call DISK.OpenFile
jmp .fini
.handle_CreateFile:
add ax0, r12
add ax0, nx0
call DISK.CreateFile
jmp .fini
.handle_RemoveFile:
add ax0, r12
add ax0, nx0
call DISK.RemoveFile
jmp .fini
@ -1437,7 +1437,7 @@ DefaultTrapHandler:
jmp .fini
.handle_ReadFile:
add ax1, r12
add ax1, nx0
call DISK.ReadFile
jmp .fini

View File

@ -986,8 +986,8 @@ abort:
# 8 "hello.k" 2
main:
mov rsp, 0x104000
nul rbp
mov esp, 0x104000
nul ebp
call print, .hellow