label offset corretto

This commit is contained in:
Giulio De Pasquale 2017-05-20 19:13:04 +02:00
parent 6cb8019f5f
commit b3d8ee9825

View File

@ -55,7 +55,6 @@ class VMAssembler:
self.functions = [] self.functions = []
self.decrypt_ops(key) self.decrypt_ops(key)
self.parse_functions() self.parse_functions()
print(self.functions)
main = next((x for x in self.functions if x.name == "main"), None) main = next((x for x in self.functions if x.name == "main"), None)
if main == None: if main == None:
print("Main has to be defined") print("Main has to be defined")
@ -102,7 +101,6 @@ class VMAssembler:
""" """
Intel syntax -> REG, IMM Intel syntax -> REG, IMM
""" """
print(instruction.args)
opcode = instruction.opcode opcode = instruction.opcode
reg = instruction.args[0] reg = instruction.args[0]
imm = instruction.args[1] imm = instruction.args[1]
@ -219,6 +217,7 @@ class VMAssembler:
for i in f.instructions: for i in f.instructions:
if i.label == dst.name: if i.label == dst.name:
val = f.offset_of_label(dst) + f.offset val = f.offset_of_label(dst) + f.offset
if val == None: if val == None:
raise AssemblerException() raise AssemblerException()
# resolving the symbol # resolving the symbol
@ -269,14 +268,11 @@ class VMFunction:
line = code[i] line = code[i]
ins = instruction_re.match(line) ins = instruction_re.match(line)
label = label_re.match(line) label = label_re.match(line)
print(line)
if label: if label:
label_name = label.group(1) label_name = label.group(1)
print("APPENDING {} TO {}".format(code[i], self.name))
self.instructions.append(VMInstruction(code[i+1], label_name)) self.instructions.append(VMInstruction(code[i+1], label_name))
i += 2 i += 2
elif ins: elif ins:
print("APPENDING {} TO {}".format(code[i], self.name))
self.instructions.append(VMInstruction(line)) self.instructions.append(VMInstruction(line))
else: else:
raise InvalidOperation(line) raise InvalidOperation(line)
@ -293,9 +289,10 @@ class VMFunction:
def offset_of_label(self, label): def offset_of_label(self, label):
offset = 0 offset = 0
for i in self.instructions: for i in self.instructions:
offset += i.size if str(i.label) == str(label):
if i.label == label:
break break
offset += i.size
return offset return offset
def __repr__(self): def __repr__(self):
@ -324,9 +321,7 @@ class VMInstruction:
raise InvalidOperation(opcode) raise InvalidOperation(opcode)
args = [x for x in ins.groups()[1:] if x is not None] args = [x for x in ins.groups()[1:] if x is not None]
print(args)
for a in args: for a in args:
print(a)
if immediate_re.match(a) or symcall: if immediate_re.match(a) or symcall:
# directly append the immediate # directly append the immediate
self.args.append(VMComponent(a, a)) self.args.append(VMComponent(a, a))