def isSignedImmediateOperandOverFlow12bits(num) -> bool:
"""
This function is to check 12-bit signed number is overflow.
:param num:
:return bool:
"""
if num <= 4095 and num >= -4096 :
return False
else :
return True
def isUnsignedImmediateOperandOverFlow12bits(num) -> bool :
"""
This function is to check 12-bit unsigned number is overflow.
:param num:
:return bool:
"""
if num >= 0 and num <= 4095 :
return False
else :
return True
def isSignedImmediateOperandOverFlow20bits(num) -> bool :
"""
This function is to check 20-bit signed number is overflow.
:param num:
:return bool:
"""
if num >= -1048576 and num <= 1048575 :
return False
else :
return True
def isUnsignedImmediateOperandOverFlow20bits(num) -> bool :
"""
This function is to check 20-bit unsigned number is overflow.
:param num:
:return bool:
"""
if num >= 0 and num <= 1048575 :
return False
else :
return True
def isSignedImmediateOperandOverFlow32bits(num) -> bool :
"""
This function is to check 32-bit signed number is overflow.
:param num:
:return bool:
"""
if num >= -4294967296 and num <= 4294967295 :
return False
else :
return True
def isUndefinedRegister(rs) -> bool :
"""
This function is to check if the variable is undefined.
:param rs:
:return bool:
"""
if rs:
return False
else :
return True
def ifhex(immediate) -> bool :
"""
This function is to check if the immediate number user entered is a hex number.
:param immediate:
:return bool:
"""
if immediate[0] == '0' and immediate[1].lower() == 'x' :
return True
else :
return False
def checkInstructionResResImm(listInst, ifSignedNumber) :
"""
This function is to check the Instruction if is like the format 'ADDI rs1, rs2, 0xAFF'.
It check four aspects :
0. if enter wrong number of variables 1. if enter the undefined register 2. if enter a hex(16进制) 3. if enter an overflow immediate operand(signed and unsigned number)
:param listInst:
:return:
"""
if len(listInst) == 3 :
if not isUndefinedRegister(listInst[0]) :
if not isUndefinedRegister(listInst[1]) :
if ifhex(listInst[2]) :
if ifSignedNumber == 1 :
if not isSignedImmediateOperandOverFlow12bits(int(listInst[2], 16)) :
return True
else :
print("The immediate operand(between -0x1000 and 0xFFF) is overflow. ")
return False
elif ifSignedNumber == 0:
if not isUnsignedImmediateOperandOverFlow12bits(int(listInst[2], 16)) :
return True
else :
print("The immediate operand(between 0x0 and 0xFFF) is overflow. ")
return False
else :
print("The immediate operand should be a hex.")
return False
else :
print("The second register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The destination register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The instruction is not correct. The format is like ADDI rs3, rs1, 0xAFF")
return False
def checkInstructionResResRes(listInst) :
"""
This function is to check the Instruction if is like the format 'ADD rs1, rs2, rs3'.
It check two aspects :
0. if enter wrong number of variables 1. if enter the undefined register
:param listInst:
:return:
"""
if len(listInst) == 3 :
if not isUndefinedRegister(listInst[0]) :
if not isUndefinedRegister(listInst[1]) :
if not isUndefinedRegister(listInst[2]) :
return True
else :
print("The third register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The second register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The destination register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The instruction is not correct. The format is like ADDI rs3, rs1, 0xAFF")
return False
def checkInstructionResRes(listInst) :
"""
This function is to check the Instruction if is like the format 'BEQ rs1, rs2'.
It checks two aspects :
0. if enter wrong number of variables 1. if enter the undefined register
:param listInst:
:return bool:
"""
if len(listInst) == 2 :
if not isUndefinedRegister(listInst[0]) :
if not isUndefinedRegister(listInst[1]) :
return True
else :
print("The second register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The first register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The instruction is not correct. The format is like BEQ rs1, rs2.")
return False
def checkInstructionRes(listInst) :
"""
This function is to check the Instruction if is like the format 'RDCYCLEH RDS'.
It checks two aspects :
0. if enter wrong number of variables 1. if enter the undefined register
:param listInst:
:return bool:
"""
if len(listInst) == 1 :
if not isUndefinedRegister(listInst[0]) :
return True
else :
print("The register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The instruction is not correct. The format is like RDCYCLEH RDS.")
return False
def checkInstructionResAdd(listInst) :
"""
This function is to check the Instruction if is like the format 'LW rs1, #0'.
It check three aspects :
0. if enter wrong number of variables 1. if enter the undefined register 2. if enter the undefined memory address
:param listInst:
:return:
"""
if len(listInst) == 2 :
if not isUndefinedRegister(listInst[0]) :
if listInst[1] == '#0' or listInst[1] == '#1' or listInst[1] == '#2':
return True
else :
print("The address you entered is undefined. Please enter #0, #1 or #2.")
return False
else :
print("The register you entered is undefined. Please enter rs1, rs2 or rs3.")
return False
else :
print("The instruction is not correct. The format is like LOAD rs1, #0.")
return False
def checkInstructionAddRes(listInst) :
"""
This function is to check the Instruction if is like the format 'SW #0, RS1'.
It checks three aspects :
0. if enter wrong number of variables 1. if enter the undefined register 2. if enter the undefined memory address
:param listInst:
:return:
"""
if len(listInst) == 2 :
if not isUndefinedRegister(listInst[1]) :
if listInst[0] == '#0' or listInst[0] == '#1' or listInst[0] == '#2':
return True
else :
print("The address you entered is undefined. Please enter #0, #1 or #2.")
return False
else :
print("The register you en
评论0