Ejemplo n.º 1
0
    def rotate(self, operand):
        # R0X	ROTATE the bit pattern in register R one bit to the right X times. Each time place the bit that
        # started at the low-order end at the high-order end.
        count = 0
        register_address = operand[0]
        moves_right = operand[2]

        # Get contents of register and convert to bits.
        byte = self.register[register_address]
        register_bin = str(hex_to_binary(byte[0]) + hex_to_binary(byte[1]))
        register_bin_list = [bit for bit in register_bin]

        # convert moves_right to actual int
        for num in range(len(HEX_NUMBER)):
            if HEX_NUMBER[num] == moves_right:
                moves_right = count
                break
            count += 1

        # Move bits to right.
        for i in range(moves_right):
            num = register_bin_list.pop()
            register_bin_list.insert(0, num)
        # Convert back to hex.
        answer_byte_bin = ''.join(register_bin_list)
        nibble1_bin = answer_byte_bin[:4]
        nibble2_bin = answer_byte_bin[4:]
        nibble1_hex = binary_to_hex(nibble1_bin)
        nibble2_hex = binary_to_hex(nibble2_bin)
        answer_byte_hex = nibble1_hex + nibble2_hex

        self.register[register_address] = answer_byte_hex
        print(
            f"Rotate the bit pattern in register {register_address} one bit to the right {count} times"
        )
Ejemplo n.º 2
0
    def xor(self, operand):
        #RST	EXCLUSIVE OR the bit patterns in registers S and T and place the result in register R.

        register_recipient_address = operand[0]
        register_1_address = operand[1]
        register_2_address = operand[2]

        # get hex numbers to convert
        register_1_hex_num = self.register[register_1_address]
        register_2_hex_num = self.register[register_2_address]

        # Convert hex numbers to binary
        register_1_bin = str(
            hex_to_binary(register_1_hex_num[0]) +
            hex_to_binary(register_1_hex_num[1]))
        register_2_bin = str(
            hex_to_binary(register_2_hex_num[0]) +
            hex_to_binary(register_2_hex_num[1]))

        # Separate bin numbers into lists for comparison
        register_1_bin_list = [num for num in register_1_bin]
        register_2_bin_list = [num for num in register_2_bin]

        # Compare lists
        answer_byte = list()
        for i in range(8):
            if register_1_bin_list[i] != register_2_bin_list[i]:
                answer_byte.append('1')
            else:
                answer_byte.append('0')
                # Convert List to hex byte
        answer_byte_bin = ''.join(answer_byte)
        nibble1_bin = answer_byte_bin[:4]
        nibble2_bin = answer_byte_bin[4:]
        nibble1_hex = binary_to_hex(nibble1_bin)
        nibble2_hex = binary_to_hex(nibble2_bin)
        answer_byte_hex = nibble1_hex + nibble2_hex
        self.register[register_recipient_address] = answer_byte_hex
        print(
            f"Xor the bit patterns in register {register_1_address} and register {register_2_address} and store in {register_recipient_address}"
        )
Ejemplo n.º 3
0
    def add_compliment(self, operand):
        # RST	ADD the bit patterns in registers S and T as though they were two’s complement representations
        # and leave the result in register R.

        register_recipient_address = operand[0]
        register_1_address = operand[1]
        register_2_address = operand[2]

        # get hex numbers to convert
        register_1_hex_num = self.register[register_1_address]
        register_2_hex_num = self.register[register_2_address]

        # Convert hex numbers to binary
        register_1_bin = str(
            hex_to_binary(register_1_hex_num[0]) +
            hex_to_binary(register_1_hex_num[1]))
        register_2_bin = str(
            hex_to_binary(register_2_hex_num[0]) +
            hex_to_binary(register_2_hex_num[1]))

        # Perform arithmetic on binary numbers
        answer_byte = self.binary_arithmetic(register_1_bin, register_2_bin)

        # Convert List to hex byte
        answer_byte_bin = ''.join(answer_byte)
        nibble1_bin = answer_byte_bin[:4]
        nibble2_bin = answer_byte_bin[4:]
        nibble1_hex = binary_to_hex(nibble1_bin)
        nibble2_hex = binary_to_hex(nibble2_bin)
        answer_byte_hex = nibble1_hex + nibble2_hex

        # Leave answer byte in register
        self.register[register_recipient_address] = answer_byte_hex
        print(
            f"Add the bit patterns at register {register_1_address} and {register_2_address} and store in register {register_recipient_address}"
        )