Ejemplo n.º 1
0
def main():
    print('\nCyclic Redundancy Check:\n')
    print("Sample input Polynomial:")
    print("x3+x2+1")
    print("x7+x4+x2+x1")
    print("\nCoefficient of all non zero terms of polynomial must be 1")
    print("Constant term must be 1\n")
    InputDividendPolynomial = input(
        "Enter Message Polynomial (Dividend Polynomial):\n").strip()
    InputDivisorPolynomial = input(
        "Enter Check Polynomial (Divisor Polynomial):\n").strip()
    try:
        ValidityChecker(InputDividendPolynomial, InputDivisorPolynomial)
        print('\n')
    except CE.NoInputError:
        print(CE.NoInputError())
        print("Try again")
        main()
    except CE.InputLengthMismatchError:
        print(CE.InputLengthMismatchError())
        print("Try again")
        main()
    except CE.InputNotANumberError:
        print(CE.InputNotANumberError())
        print("Try again")
        main()
    except CE.InvalidInputError:
        print(CE.InvalidInputError())
        print("Try again")
        main()
    finally:
        print("Main Function executed successfully")
    return 0
Ejemplo n.º 2
0
def instr_Int(instruction, pointers, fo, assembly_failed):
    if len(instruction) < 3:
        CustomError.ERR_missingArgument(instruction[-1], '1')
        return False
    if instruction[1][0] == 'd':
        temp = int(instruction[1][1:])
        if 0 > temp > 255:
            CustomError.ERR_invalidValue(instruction[-1], '1')
            return False
        else:
            if not assembly_failed:
                fo.write("{}{}{}".format("14", "0e f0", "%2.2x" % temp))
            return True
    elif len(instruction[1]) == 3 and instruction[1][0] == 'h':
        for c in instruction[1][1:]:
            if '0' <= c <= '9' or 'a' <= c <= 'f':
                continue
            else:
                CustomError.ERR_invalidValue(instruction[-1], '1')
                return False
        if not assembly_failed:
            fo.write("{}{}{}".format("14", "0e f0", instruction[1][1:]))
        return True
    else:
        CustomError.ERR_invalidArgumentType(instruction[-1], '2',
                                            "d (decimal), h (hexadecimal)")
        return False
Ejemplo n.º 3
0
def main():
    print("\nNetworkID and HostID of IPv4 Address:")
    InputIPAddress = input("Enter a valid IPv4 address: \n")
    try:
        NetworkAndHostIDFinder(InputIPAddress)
        print('\n')
    except CE.NoInputError:
        print(CE.NoInputError())
        print("Try again")
        main()
    except CE.InputOutOfRangeError:
        print(CE.InputOutOfRangeError())
        print("Try again")
        main()
    except CE.InputNotANumberError:
        print(CE.InputNotANumberError())
        print("Try again")
        main()
    except CE.InvalidInputError:
        print(CE.InvalidInputError())
        print("Try again")
        main()
    finally:
        print("Main Function executed successfully")
    return 0
Ejemplo n.º 4
0
def main():
    print("\nDistance Vector Routing Algorithm")
    NumberOfNodes = input("Enter the number of nodes in the network: \n")
    for char in NumberOfNodes:
        if char not in list("0123456789"):
            raise CE.InputNotANumberError
    NumberOfNodes = int(NumberOfNodes)
    try:
        CostMatrixOfNodes = MinCostMatrixFinder(NumberOfNodes)
        print('\n')
        for startingNode in range(NumberOfNodes):
            for endingNode in range(NumberOfNodes):
                print(
                    f"Min distance between {startingNode + 1} and {endingNode + 1} is {CostMatrixOfNodes[startingNode][endingNode]}")
            print('\n')
        print('\n')
    except CE.NoInputError:
        print(CE.NoInputError())
        print("Try again")
        main()
    except CE.InputLengthMismatchError:
        print(CE.InputLengthMismatchError())
        print("Try again")
        main()
    except CE.InputNotANumberError:
        print(CE.InputNotANumberError())
        print("Try again")
        main()
    except CE.InvalidInputError:
        print(CE.InvalidInputError())
        print("Try again")
        main()
    finally:
        print("Main Function executed successfully")
    return 0
def main():
    print('\nHamming Distance Calculator: \n')
    InputString1 = input("Enter a binary data string: \n")
    InputString2 = input("Enter another binary data string: \n")
    try:
        CalculateHammingDistance(InputString1, InputString2)
        print('\n')
    except CE.InputNotANumberError:
        print(CE.InputNotANumberError())
        print("Try again")
        main()
    except CE.InputLengthMismatchError:
        print(CE.InputLengthMismatchError())
        print("Try again")
        main()
    except CE.InvalidInputError:
        print(CE.InvalidInputError())
        print("Try again")
        main()
    except CE.NoInputError:
        print(CE.NoInputError())
        print("Try again")
        main()
    finally:
        print("Main Function executed successfully")
    return 0
Ejemplo n.º 6
0
def writeFieldWithStackInstruction(instruction, pointers, fo, opcode,
                                   assembly_failed):
    if len(instruction) < 3:
        CustomError.ERR_missingArgument(instruction[-1], '1')
        return False
    if instruction[1][0] != 'r':
        CustomError.ERR_invalidArgumentType(instruction[-1], '1',
                                            "register reference")
        return False
    topmid_byte_top_nibble = registers.get(instruction[1], None)
    if topmid_byte_top_nibble == None:
        CustomError.ERR_invalidValue(instruction[-1], '1')
        return False
    if not assembly_failed:
        fo.write("{}{}{}".format(opcode, topmid_byte_top_nibble, "e f000"))
    return True
Ejemplo n.º 7
0
def fetch_corresponding_major(student_id):
    '''
    Fetches the information of major corresponding to a given student from the Student table in the database
    :param student_id: id from the Student table
    :return: Major_id corresponding to a given student
    '''
    try:
        cursor = connect_db()
        cursor.execute("select * from Student where id = " + str(student_id))
        table = cursor.fetchall()
        if len(table) > 1:
            raise CustomError("Integrity Error! More than one entry for a student_id.")
        elif len(table) < 1:
            raise CustomError("No Student exists with the given student_id.")
        else:
            return table[0][3]
    except CustomError as e:
        print("Exception: ", e.value)
Ejemplo n.º 8
0
def instr_Call(instruction, pointers, fo, assembly_failed):
    if len(instruction) < 3:
        CustomError.ERR_missingArgument(instruction[-1], '1')
        return False
    if instruction[1][0] != '&':
        CustomError.ERR_invalidArgumentType(instruction[-1], '1',
                                            "label reference")
        return False
    temp = pointers.get(instruction[1], None)
    if temp == None:
        CustomError.ERR_labelMissing(instruction[-1])
        return False
    low_bytes = int(temp) * 2
    if low_bytes > 65535:
        CustomError.ERR_outOfBounds(instruction[-1])
        return False
    if not assembly_failed:
        fo.write("{} {}".format("0efe", "%.4x" % (low_bytes)))
    return True
Ejemplo n.º 9
0
def instr_Mov(instruction, pointers, fo, assembly_failed):
    if len(instruction) < 4:
        if len(instruction) < 3:
            CustomError.ERR_missingArgument(instruction[-1], '1')
            return False
        CustomError.ERR_missingArgument(instruction[-1], '2')
        return False
    if instruction[1][0] != 'r':
        CustomError.ERR_invalidArgumentType(instruction[-1], '1',
                                            "register reference")
        return False
    topmid_byte_top_nibble = registers.get(instruction[1], None)
    if topmid_byte_top_nibble == None:
        CustomError.ERR_invalidValue(instruction[-1], '1')
        return False
    if instruction[2][0] == 'r':
        topmid_bottom_nibble = registers.get(instruction[2], None)
        if topmid_bottom_nibble == None:
            CustomError.ERR_invalidArgumentType(instruction[-1], '1',
                                                "register reference")
            return False
        if not assembly_failed:
            fo.write("{}{}{} {}".format("00", topmid_byte_top_nibble,
                                        topmid_byte_bottom_nibble, "0000"))
        return True
    elif instruction[2][0] == 'd':
        if -32768 > int(instruction[2][1:]) > 32767:
            CustomError.ERR_invalidValue(instruction[-1], '2')
            return False
        else:
            if not assembly_failed:
                fo.write("{}{}{} {}".format(
                    "00", topmid_byte_top_nibble, '0',
                    "%4.4x" % (int(instruction[2][1:]))))
            return True
    elif len(instruction[2]) == 5 and instruction[2][0] == 'h':
        for c in instruction[2][1:]:
            if '0' <= c <= '9' or 'a' <= c <= 'f':
                continue
            else:
                CustomError.ERR_invalidValue(instruction[-1], '2')
                return False
        if not assembly_failed:
            fo.write("{}{}{} {}".format("00", topmid_byte_top_nibble, '0',
                                        instruction[2][1:]))
        return True
    else:
        CustomError.ERR_invalidArgumentType(
            instruction[-1], '2',
            "d (decimal), h (hexadecimal) or r (register reference)")
        return False
Ejemplo n.º 10
0
def writeAndRead1Instruction(instruction, pointers, fo, opcode,
                             assembly_failed):
    top_byte = opcode
    if len(instruction) < 4:
        if len(instruction) < 3:
            CustomError.ERR_missingArgument(instruction[-1], '1')
            return False
        CustomError.ERR_missingArgument(instruction[-1], '2')
        return False
    if instruction[1][0] != 'r':
        CustomError.ERR_invalidArgumentType(instruction[-1], '1',
                                            "register reference")
        return False
    elif instruction[2][0] != 'r':
        CustomError.ERR_invalidArgumentType(instruction[-1], '2',
                                            "register reference")
        return False
    topmid_byte_top_nibble = registers.get(instruction[1], None)
    if topmid_byte_top_nibble == None:
        CustomError.ERR_invalidValue(instruction[-1], '1')
        return False
    botmid_byte_top_nibble = registers.get(instruction[2], None)
    if botmid_byte_top_nibble == None:
        CustomError.ERR_invalidValue(instruction[-1], '2')
        return False
    if not assembly_failed:
        fo.write("{}{}{} {}{}".format(top_byte, topmid_byte_top_nibble, '0',
                                      botmid_byte_top_nibble, "000"))
    return True
Ejemplo n.º 11
0
def instr_Jmp(instruction, pointers, fo, assembly_failed):
    if len(instruction) < 4:
        if len(instruction) < 3:
            CustomError.ERR_missingArgument(instruction[-1], '1')
            return False
        CustomError.ERR_missingArgument(instruction[-1], '2')
        return False
    topmid_byte_bottom_nibble = jump_flags.get(instruction[1], None)
    if topmid_byte_bottom_nibble == None:
        CustomError.ERR_invalidArgument(instruction[-1], '1')
        return False
    if instruction[2][0] != '&':
        CustomError.ERR_invalidArgumentType(instruction[-1], '2',
                                            "label reference")
        return False
    temp = pointers.get(instruction[2], None)
    if temp == None:
        CustomError.ERR_labelMissing(instruction[-1])
        return False
    low_bytes = int(temp) * 2
    if low_bytes > 65535:
        CustomError.ERR_outOfBounds(instruction[-1])
        return False
    if not assembly_failed:
        fo.write("{}{} {}".format("0d0", topmid_byte_bottom_nibble,
                                  "%.4x" % (low_bytes)))
    return True
def main():
    print("\nLink State Routing Algorithm: ")
    NumberOfNodes = input("Enter the number of nodes in the network: \n")
    for char in NumberOfNodes:
        if char not in list("0123456789"):
            raise CE.InputNotANumberError
    NumberOfNodes = int(NumberOfNodes)

    StartNode = input(
        f"Enter a Start Node in the range 0 to {NumberOfNodes}: \n")
    for char in StartNode:
        if char not in list("0123456789"):
            raise CE.InputNotANumberError
    StartingNode = int(StartNode)
    if not 0 <= StartingNode < NumberOfNodes:
        print("Invalid Start Node")
        raise CE.InvalidInputError
    try:
        MinCostMatrixFinder(NumberOfNodes, StartingNode)
        print('\n')
    except CE.NoInputError:
        print(CE.NoInputError())
        print("Try again")
        main()
    except CE.InputLengthMismatchError:
        print(CE.InputLengthMismatchError())
        print("Try again")
        main()
    except CE.InputNotANumberError:
        print(CE.InputNotANumberError())
        print("Try again")
        main()
    except CE.InvalidInputError:
        print(CE.InvalidInputError())
        print("Try again")
        main()
    finally:
        print("Main Function executed successfully")
    return 0
def main():
    print('\nCharacter stuffing and destuffing:\n')
    InputString = input("Enter a string of characters:\n")
    FlagSequence = input("Enter the flag sequence:\n")
    EscapeSequence = input("Enter the escape sequence:\n")
    try:
        StuffedCharacterString = CharacterStuffing(
            InputString, FlagSequence, EscapeSequence)
        CharacterDestuffing(StuffedCharacterString,
                            FlagSequence, EscapeSequence)
        print('\n')
    except CE.InvalidInputError:
        print(CE.InvalidInputError())
        print("Try again")
        main()
    except CE.NoInputError:
        print(CE.NoInputError())
        print("Try again")
        main()
    finally:
        print("Main Function executed successfully")
    return 0
Ejemplo n.º 14
0
def main():
    print('\nBit stuffing and destuffing:\n')
    InputString = input("Enter a binary data string:\n")
    try:
        StuffedBitString = BitStuffing(InputString)
        BitDestuffing(StuffedBitString)
        print('\n')
    except CE.InputNotANumberError:
        print(CE.InputNotANumberError())
        print("Try again")
        main()
    except CE.InvalidInputError:
        print(CE.InvalidInputError())
        print("Try again")
        main()
    except CE.NoInputError:
        print(CE.NoInputError())
        print("Try again")
        main()
    finally:
        print("Main Function executed successfully")
    return 0
Ejemplo n.º 15
0
def parseAssembly(fi, instructions, pointers):
    counter = 0
    true_counter = 0

    for instruction in fi:
        if not instruction:  #check to see if the line was empty or not
            continue
        if instruction[0][0] == '*':  #check to see if the statement is a label
            reference = '&' + instruction[0][1:]
            if reference in pointers:
                CustomError.ERR_labelDefined(true_counter + 1)
            else:
                pointers.update({reference: counter})
                counter -= 1
        elif instruction[0][
                0] != '/':  #check to see if the statement is a comment in the source file, if not, then that means it's probably an instruction
            instruction.append(true_counter + 1)
            instructions.append(instruction)
        true_counter += 1
        counter += 1
Ejemplo n.º 16
0
#run through the source file and split it up to lines of code with each word being a "statement". said words are also used as tokens for later logic
fi = open("input.txt", "r")
temp = fi.readlines()
for line in temp:
    input_file.append(line.split())
BC2parser.parseAssembly(input_file, instructions, pointers)
fi.close()

fo = open("Output.txt", "w")

#loop that tries to assemble the source file into "machine code" (though it is in text form, reason being, that's what "Logisim" accepts as input)
#the assembly process will run through the whole file to catch all errors but will only write to an output until no errors have been found
for instruction in instructions:
    wasResolved = BC2instructions.resolveInstruction(instruction, pointers, fo,
                                                     assembly_failed)
    if wasResolved == False:
        assembly_failed = True
    counter += 1
    if counter % 4 == 0:
        fo.write("\n")
    else:
        fo.write('\t')

if assembly_failed == 1:
    print(CustomError.ERR_assemblyFailed())
    fo.close()
    file_delete = open("Output.txt", "w")
    file_delete.write(CustomError.ERR_assemblyFailed())
    file_delete.close()