Unsigned short in assembler, fix sull'allocazione di memoria
This commit is contained in:
parent
9d1cadf7d4
commit
34d7d3c85f
@ -6,8 +6,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
std::ifstream f;
|
std::ifstream bytecode_if;
|
||||||
std::streamsize fsize;
|
std::streamsize bytecode_size;
|
||||||
uint8_t * bytecode;
|
uint8_t * bytecode;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
@ -18,14 +18,13 @@ int main(int argc, char *argv[]) {
|
|||||||
/*
|
/*
|
||||||
reading bytecode
|
reading bytecode
|
||||||
*/
|
*/
|
||||||
f.open(argv[1], std::ios::binary | std::ios::ate);
|
bytecode_if.open(argv[1], std::ios::binary | std::ios::ate);
|
||||||
fsize = f.tellg();
|
bytecode_size = bytecode_if.tellg();
|
||||||
f.seekg(0, std::ios::beg);
|
bytecode_if.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
bytecode = new uint8_t[fsize];
|
bytecode = new uint8_t[bytecode_size];
|
||||||
f.read((char*)bytecode, fsize);
|
bytecode_if.read((char*)bytecode, bytecode_size);
|
||||||
|
VM vm(bytecode, bytecode_size);
|
||||||
VM vm(bytecode, sizeof(bytecode));
|
|
||||||
vm.run();
|
vm.run();
|
||||||
vm.status();
|
vm.status();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -36,22 +36,23 @@ assembled = bytearray()
|
|||||||
def to_uint8(data):
|
def to_uint8(data):
|
||||||
alphanum = re.compile("^[0-9]+$")
|
alphanum = re.compile("^[0-9]+$")
|
||||||
if isinstance(data, int):
|
if isinstance(data, int):
|
||||||
return struct.pack("<b", data)
|
return struct.pack("<B", data)
|
||||||
elif data.startswith("0x"):
|
elif data.startswith("0x"):
|
||||||
return struct.pack("<b", int(data, 16))
|
return struct.pack("<B", int(data, 16))
|
||||||
elif alphanum.match(data): # only numbers
|
elif alphanum.match(data): # only numbers
|
||||||
return struct.pack("<b", int(data))
|
return struct.pack("<B", int(data))
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def to_uint16(data):
|
def to_uint16(data):
|
||||||
alphanum = re.compile("^[0-9]+$")
|
alphanum = re.compile("^[0-9]+$")
|
||||||
if isinstance(data, int):
|
if isinstance(data, int):
|
||||||
return struct.pack("<h", data)
|
return struct.pack("<H", data)
|
||||||
elif data.startswith("0x"):
|
elif data.startswith("0x"):
|
||||||
return struct.pack("<h", int(data, 16))
|
return struct.pack("<H", int(data, 16))
|
||||||
elif alphanum.match(data): # only numbers
|
elif alphanum.match(data): # only numbers
|
||||||
return struct.pack("<h", int(data))
|
return struct.pack("<H", int(data))
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -62,6 +63,25 @@ def is_reg(data):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def movi(op, dst, src):
|
||||||
|
global assembled
|
||||||
|
if (is_reg(dst)):
|
||||||
|
op_val = to_uint8(ops[op])
|
||||||
|
dst_val = to_uint8(regs[dst])
|
||||||
|
src_val = to_uint16(src)
|
||||||
|
if op_val and dst_val and src_val:
|
||||||
|
assembled += op_val + dst_val + src_val
|
||||||
|
else:
|
||||||
|
sys.stderr.write(
|
||||||
|
"ERROR WHILE ASSEMBLING UNKNOWN VALUES\n")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
sys.stderr.write(
|
||||||
|
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst))
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def assemble_code(line):
|
def assemble_code(line):
|
||||||
global assembled
|
global assembled
|
||||||
sys.stdout.write("CODE: ")
|
sys.stdout.write("CODE: ")
|
||||||
@ -74,16 +94,7 @@ def assemble_code(line):
|
|||||||
return False
|
return False
|
||||||
sys.stdout.write("{} {}\n".format(op_name, ", ".join(instruction[1::])))
|
sys.stdout.write("{} {}\n".format(op_name, ", ".join(instruction[1::])))
|
||||||
if op_name == "MOVI":
|
if op_name == "MOVI":
|
||||||
op = ops[op_name]
|
movi(op_name, instruction[1], instruction[2])
|
||||||
dst = instruction[1]
|
|
||||||
if (is_reg(dst)):
|
|
||||||
dst = to_uint8(regs[dst])
|
|
||||||
src = to_uint16(instruction[2])
|
|
||||||
else:
|
|
||||||
sys.stderr.write(
|
|
||||||
"ERROR WHILE ASSEMBLING UNKNOWN REGISTER: {}\n".format(dst))
|
|
||||||
return False
|
|
||||||
assembled += to_uint8(op) + dst + src
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -94,8 +105,13 @@ def assemble_data(line):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
global assembled
|
global assembled
|
||||||
with open("./test.gipu", 'r') as f:
|
if (len(sys.argv) < 2):
|
||||||
|
print("Usage: {} <file_to_assemble>".format(sys.argv[0]))
|
||||||
|
return
|
||||||
|
|
||||||
|
with open(sys.argv[1], 'r') as f:
|
||||||
gen = (line.strip("\n") for line in f if line != "\n")
|
gen = (line.strip("\n") for line in f if line != "\n")
|
||||||
|
flag = None
|
||||||
|
|
||||||
for line in gen:
|
for line in gen:
|
||||||
if line.startswith(tuple(section_names)):
|
if line.startswith(tuple(section_names)):
|
||||||
@ -105,6 +121,8 @@ def main():
|
|||||||
assemble_data(line)
|
assemble_data(line)
|
||||||
elif flag == section_flags["CODE:"]:
|
elif flag == section_flags["CODE:"]:
|
||||||
assemble_code(line)
|
assemble_code(line)
|
||||||
|
if not flag:
|
||||||
|
sys.stderr.write("Nothing was assembled! Did you use the section delimiters?\n")
|
||||||
with open("./out.gipu", 'wb') as f:
|
with open("./out.gipu", 'wb') as f:
|
||||||
f.write(assembled)
|
f.write(assembled)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user