mirror of
https://gitlab.os-k.eu/os-k-team/kvisc.git
synced 2023-08-25 14:05:46 +02:00
include
This commit is contained in:
parent
b21b289c6d
commit
2ca9869233
10
Makefile
10
Makefile
@ -5,16 +5,18 @@ all: kas
|
||||
|
||||
kpc:
|
||||
@cd pc && make --no-print-directory
|
||||
@mv pc/k.exe out/k.exe
|
||||
|
||||
kas: kpc as/k-as.py as/regs.lst
|
||||
@cp pc/instrs/instrs.lst as
|
||||
@rm -f pc/instrs/instrs.lst
|
||||
|
||||
asm: os/dos.k
|
||||
@cd os && ../as/k-as.py dos.k 0x100000 a.out
|
||||
asm: dos/dos.k dos/print.k
|
||||
@as/k-as.py dos/dos.k 0x100000 out/a.out
|
||||
|
||||
test: kas asm
|
||||
@pc/k.exe os/a.out > os/stdout.txt
|
||||
@out/k.exe out/a.out > out/stdout.txt
|
||||
|
||||
disasm: kas asm
|
||||
@pc/k.exe os/a.out -d
|
||||
@out/k.exe os/a.out -d
|
||||
@mv fwprog.dis out
|
||||
|
154
as/k-as.py
154
as/k-as.py
@ -14,24 +14,26 @@ if len(sys.argv) != 4:
|
||||
.format(sys.argv[0]))
|
||||
sys.exit(1)
|
||||
|
||||
instrs = open(".{}.instr".format(sys.argv[3]), "w+")
|
||||
b_data = open(".{}.data".format(sys.argv[3]), "w+b")
|
||||
b_text = open(".{}.text".format(sys.argv[3]), "w+b")
|
||||
source = TemporaryFile(mode="w+")
|
||||
instrs = TemporaryFile(mode="w+")
|
||||
b_data = TemporaryFile(mode="w+b")
|
||||
b_text = TemporaryFile(mode="w+b")
|
||||
|
||||
lst_regs = open(os.path.join(sys.path[0], "regs.lst"))
|
||||
lst_instrs = open(os.path.join(sys.path[0], "instrs.lst"))
|
||||
|
||||
fi = open(sys.argv[1])
|
||||
main_src = open(sys.argv[1])
|
||||
b_out = open(sys.argv[3], "wb")
|
||||
|
||||
start_addr = int(sys.argv[2], base=0)
|
||||
|
||||
def leave():
|
||||
fi.close()
|
||||
source.close()
|
||||
instrs.close()
|
||||
b_out.close()
|
||||
b_data.close()
|
||||
b_text.close()
|
||||
main_src.close()
|
||||
lst_regs.close()
|
||||
lst_instrs.close()
|
||||
|
||||
@ -95,6 +97,102 @@ def parse_lst_instrs():
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
inc_depth = 0
|
||||
inc_depth_max = 16
|
||||
|
||||
# Quickly goes through source file and resolves "include" directives ONLY
|
||||
def do_includes(fi):
|
||||
global inc_depth
|
||||
for _, line in enumerate(fi):
|
||||
line = line.rstrip()
|
||||
tok = line.split(' ', 1)
|
||||
|
||||
if len(tok) == 0:
|
||||
continue
|
||||
|
||||
if tok[0] == "include":
|
||||
if len(tok) == 1:
|
||||
print("Missing parameter for include directive")
|
||||
leave()
|
||||
sys.exit(1)
|
||||
|
||||
if tok[1][0] not in "'\"" or tok[1][-1] != tok[1][0]:
|
||||
print("Invalid format for include directive: {}".format(line))
|
||||
leave()
|
||||
sys.exit(1)
|
||||
|
||||
inc = tok[1][1:-1]
|
||||
|
||||
try:
|
||||
new_fi = open(inc, "r")
|
||||
|
||||
except:
|
||||
print("Couldn't open file: {}".format(line))
|
||||
leave()
|
||||
sys.exit(1)
|
||||
|
||||
inc_depth += 1
|
||||
if inc_depth >= inc_depth_max:
|
||||
print("Maximal include depth reached: {}".format(line))
|
||||
leave()
|
||||
sys.exit(1)
|
||||
|
||||
do_includes(new_fi)
|
||||
|
||||
else:
|
||||
source.write("{}\n".format(line))
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
def parse():
|
||||
global ptext
|
||||
global plastlabel
|
||||
|
||||
source.seek(0)
|
||||
|
||||
for count, line in enumerate(source):
|
||||
print(line)
|
||||
line = line.rstrip()
|
||||
|
||||
if len(line) == 0:
|
||||
continue
|
||||
|
||||
for i in range(len(line)):
|
||||
if line[i] in '#;@!/':
|
||||
line = line[:i].rstrip()
|
||||
break
|
||||
|
||||
if len(line) == 0:
|
||||
continue
|
||||
|
||||
if line[0] == ' ' or line[0] == '\t':
|
||||
line = line.lstrip()
|
||||
ptext += parse_instr(line)
|
||||
instrs.write("\n")
|
||||
|
||||
continue
|
||||
|
||||
# Preprocessor or label?
|
||||
if line[-1] == ':':
|
||||
if name_valid(line[:-1]):
|
||||
label = line[:-1]
|
||||
if label[0] == '.':
|
||||
label = plastlabel + label
|
||||
else:
|
||||
plastlabel = label
|
||||
plabels_text[label] = ptext
|
||||
else:
|
||||
print("Bad label name: {}".format(line[:-1]))
|
||||
leave()
|
||||
sys.exit(1)
|
||||
continue
|
||||
|
||||
# Preprocessor, .data, or invalid
|
||||
parse_preproc(line)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
def parse_preproc(line):
|
||||
global pdata
|
||||
|
||||
@ -180,51 +278,6 @@ def parse_preproc(line):
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
def parse():
|
||||
global ptext
|
||||
global plastlabel
|
||||
|
||||
for count, line in enumerate(fi):
|
||||
line = line.rstrip()
|
||||
|
||||
if len(line) == 0:
|
||||
continue
|
||||
|
||||
for i in range(len(line)):
|
||||
if line[i] in '#;@!/':
|
||||
line = line[:i].rstrip()
|
||||
break
|
||||
|
||||
if len(line) == 0:
|
||||
continue
|
||||
|
||||
if line[0] == ' ' or line[0] == '\t':
|
||||
line = line.lstrip()
|
||||
ptext += parse_instr(line)
|
||||
instrs.write("\n")
|
||||
|
||||
continue
|
||||
|
||||
# Preprocessor or label?
|
||||
if line[-1] == ':':
|
||||
if name_valid(line[:-1]):
|
||||
label = line[:-1]
|
||||
if label[0] == '.':
|
||||
label = plastlabel + label
|
||||
else:
|
||||
plastlabel = label
|
||||
plabels_text[label] = ptext
|
||||
else:
|
||||
print("Bad label name: {}".format(line[:-1]))
|
||||
leave()
|
||||
sys.exit(1)
|
||||
continue
|
||||
|
||||
# Preprocessor, .data, or invalid
|
||||
parse_preproc(line)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
def parse_instr(line):
|
||||
if line == None or len(line) == 0:
|
||||
return 0
|
||||
@ -461,6 +514,7 @@ def genout():
|
||||
|
||||
parse_lst_instrs()
|
||||
parse_lst_regs()
|
||||
do_includes(main_src)
|
||||
parse()
|
||||
gentext()
|
||||
genout()
|
||||
|
20
dos/dos.k
Normal file
20
dos/dos.k
Normal file
@ -0,0 +1,20 @@
|
||||
; The OS/K Team licences this file to you under the MIT license.
|
||||
; See the LICENSE file in the project root for more information.
|
||||
|
||||
hw = "Hello World\n:)"
|
||||
|
||||
;
|
||||
; Entry point
|
||||
;
|
||||
main:
|
||||
; Initializes the stack
|
||||
mov rbp, 0x200000
|
||||
mov rsp, rbp
|
||||
|
||||
mov ax0, hw
|
||||
call print
|
||||
|
||||
stop
|
||||
|
||||
include "dos/print.k"
|
||||
|
@ -1,21 +1,6 @@
|
||||
; The OS/K Team licences this file to you under the MIT license.
|
||||
; See the LICENSE file in the project root for more information.
|
||||
|
||||
hw = "Hello World\n:)"
|
||||
|
||||
;
|
||||
; Entry point
|
||||
;
|
||||
main:
|
||||
; Initializes the stack
|
||||
mov rbp, 0x200000
|
||||
mov rsp, rbp
|
||||
|
||||
mov ax0, hw
|
||||
call print
|
||||
|
||||
stop
|
||||
|
||||
;
|
||||
; Max amount of characters that print() will print
|
||||
;
|
||||
@ -44,7 +29,7 @@ print:
|
||||
;
|
||||
print_n:
|
||||
enter
|
||||
mov rcx, ax1 ; text
|
||||
mov rcx, ax1
|
||||
|
||||
.1:
|
||||
prn b[ax0]
|
0
out/.placeholder
Normal file
0
out/.placeholder
Normal file
Loading…
Reference in New Issue
Block a user