Example #1
0
class Context(object):

    def __init__(self, instructions):
        self.registers = Registers()
        self.flags = Flags()
        self.instructions = instructions
        self.heap = Memory(size=40, mode=HEAP, start_address=0x0000)
        self.stack = Memory(size=40, mode=STACK, start_address=0xFFFF)
        self.registers.set(SP, 0xFFFF)

    def run(self):
        """
        initialize the context and execute the first instruction
        """
        self.registers.reset()

    def step(self):
        """
        execute the next instruction whose address in the EIP value
        :return:
        """
        self.registers.tick()
        self.flags.tick()
        self.heap.tick()
        self.stack.tick()
        next_address = self.registers.get(IP).value
        if next_address < len(self.instructions):
            next_instruction = self.instructions[next_address]
            next_instruction.execute(self)
            self.registers.set(IP, next_address + 1)
            return True
        else:
            return False