Unsigned short in assembler, fix sull'allocazione di memoria

This commit is contained in:
Giulio De Pasquale 2017-05-15 13:24:30 +02:00
parent 9d1cadf7d4
commit 34d7d3c85f
2 changed files with 43 additions and 26 deletions

View File

@ -6,8 +6,8 @@
#include <stdlib.h>
int main(int argc, char *argv[]) {
std::ifstream f;
std::streamsize fsize;
std::ifstream bytecode_if;
std::streamsize bytecode_size;
uint8_t * bytecode;
if (argc < 2) {
@ -18,14 +18,13 @@ int main(int argc, char *argv[]) {
/*
reading bytecode
*/
f.open(argv[1], std::ios::binary | std::ios::ate);
fsize = f.tellg();
f.seekg(0, std::ios::beg);
bytecode_if.open(argv[1], std::ios::binary | std::ios::ate);
bytecode_size = bytecode_if.tellg();
bytecode_if.seekg(0, std::ios::beg);
bytecode = new uint8_t[fsize];
f.read((char*)bytecode, fsize);
VM vm(bytecode, sizeof(bytecode));
bytecode = new uint8_t[bytecode_size];
bytecode_if.read((char*)bytecode, bytecode_size);
VM vm(bytecode, bytecode_size);
vm.run();
vm.status();
return 0;

View File

@ -36,22 +36,23 @@ assembled = bytearray()
def to_uint8(data):
alphanum = re.compile("^[0-9]+$")
if isinstance(data, int):
return struct.pack("<b", data)
return struct.pack("<B", data)
elif data.startswith("0x"):
return struct.pack("<b", int(data, 16))
return struct.pack("<B", int(data, 16))
elif alphanum.match(data): # only numbers
return struct.pack("<b", int(data))
return struct.pack("<B", int(data))
else:
return None
def to_uint16(data):
alphanum = re.compile("^[0-9]+$")
if isinstance(data, int):
return struct.pack("<h", data)
return struct.pack("<H", data)
elif data.startswith("0x"):
return struct.pack("<h", int(data, 16))
return struct.pack("<H", int(data, 16))
elif alphanum.match(data): # only numbers
return struct.pack("<h", int(data))
return struct.pack("<H", int(data))
else:
return None
@ -62,6 +63,25 @@ def is_reg(data):
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):
global assembled
sys.stdout.write("CODE: ")
@ -74,16 +94,7 @@ def assemble_code(line):
return False
sys.stdout.write("{} {}\n".format(op_name, ", ".join(instruction[1::])))
if op_name == "MOVI":
op = ops[op_name]
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
movi(op_name, instruction[1], instruction[2])
return True
@ -94,8 +105,13 @@ def assemble_data(line):
def main():
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")
flag = None
for line in gen:
if line.startswith(tuple(section_names)):
@ -105,6 +121,8 @@ def main():
assemble_data(line)
elif flag == section_flags["CODE:"]:
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:
f.write(assembled)