# Path: VM/util.py
# def to_int(data: bytes, signed=False):
# def to_signed(num: int, bytes: int) -> int:
# def is_signed_out_of_range(num: int, size: int) -> bool:
#     def __init__(cls, name, bases, dct):
#     def __init__(cls, name, bases, dct):
#     def load_instruction(cls, instruction, opcode, implementation):
#     def __init__(self):
# class MissingOpcodeError(RuntimeError):
# class InstructionMeta(type):
# class CPUMeta(type):
# class Instruction(metaclass=InstructionMeta):
# class CPU(metaclass=CPUMeta):


from ..util import Instruction, SegmentRegs
from functools import partialmethod as P
    import logging


if __debug__:
    logger = logging.getLogger(__name__)

MAXVALS = [None, (1 << 8) - 1, (1 << 16) - 1, None, (1 << 32) - 1]  # MAXVALS[n] is the maximum value of an unsigned n-bit number
SIGNS   = [None, 1 << 8 - 1, 1 << 16 - 1, None, 1 << 32 - 1]  # SIGNS[n] is the maximum absolute value of a signed n-bit number


####################
# STOSB / STOSW / STOSD
####################
class STOS(Instruction):
    def __init__(self):
        self.opcodes = {
            0xAA: P(self.m, _8bit=True),
            0xAB: P(self.m, _8bit=False)
        }

    def m(vm, _8bit: bool) -> True:
        sz = 1 if _8bit else vm.operand_size

        eax = vm.reg.get(0, sz)
        edi = vm.reg.get(7, vm.address_size)

        vm.mem.segment_override = SegmentRegs.ES
        vm.mem.set(edi, sz, eax)

# Path: VM/util.py
# def to_int(data: bytes, signed=False):
# def to_signed(num: int, bytes: int) -> int:
# def is_signed_out_of_range(num: int, size: int) -> bool:
#     def __init__(cls, name, bases, dct):
#     def __init__(cls, name, bases, dct):
#     def load_instruction(cls, instruction, opcode, implementation):
#     def __init__(self):
# class MissingOpcodeError(RuntimeError):
# class InstructionMeta(type):
# class CPUMeta(type):
# class Instruction(metaclass=InstructionMeta):
# class CPU(metaclass=CPUMeta):


# Path: VM/instructions/repeatables.py
from ..util import Instruction, SegmentRegs
from functools import partialmethod as P
    import logging



if __debug__:
    logger = logging.getLogger(__name__)

MAXVALS = [None, (1 << 8) - 1, (1 << 16) - 1, None, (1 << 32) - 1]  # MAXVALS[n] is the maximum value of an unsigned n-bit number
SIGNS   = [None, 1 << 8 - 1, 1 << 16 - 1, None, 1 << 32 - 1]  # SIGNS[n] is the maximum absolute value of a signed n-bit number


####################
# STOSB / STOSW / STOSD
####################
class STOS(Instruction):
    def __init__(self):
        self.opcodes = {
            0xAA: P(self.m, _8bit=True),
            0xAB: P(self.m, _8bit=False)
        }

    def m(vm, _8bit: bool) -> True:
        sz = 1 if _8bit else vm.operand_size

        eax = vm.reg.get(0, sz)
        edi = vm.reg.get(7, vm.address_size)

        vm.mem.segment_override = SegmentRegs.ES
        vm.mem.set(edi, sz, eax)

        vm.mem.segment_override = SegmentRegs.DS