Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part III: Decode a Machine Instruction (20 points) The Fictional KIPS Computer F

ID: 3718315 • Letter: P

Question

Part III: Decode a Machine Instruction (20 points) The Fictional KIPS Computer For this part you will implement a function that decodes machine language instructions of a fictional computer we will call the KIPS. You should read through the Unit 9 lecture notes before starting this part. KIPS supports the following instructions that the CPU can execute: add dest, opl, op2: Performs an addition. dest is the desination register where the sum of regis ters op1 and op2 will be saved. (dest-opl op2) . sub dest, opl, op2: Performs a subtraction. dest is the desination register where the difference of registers op1 and op2 will be saved. (destopl- op2) mul dest, opl, op2: Performs a multiplication. dest is the desination register where the product of registers op1 and op2 will be saved. (dest-opl op2) of registers opl and op2 will be saved. (dest-opl /7 op2) Recall from lecture that an immediate value is simply a constant. . div dest, opl, op2: Performs an integer division. dest is the desination register where the quotient ·li dest, immediate: Stores the value immediate in register dest. (i.e.,dest = immediate) As with the real MIPS computer, the KIPS computer has 10 registers numbered $tO, $t1,.., $t 9. In the above list of instructions, dest, opl and op2 are registers. immediate is a positive integer (or zero). Instruction Formats Every KIPS instruction is exactly 32 bits in length. An arithmetical instruction's bits are divided up as follows

Explanation / Answer

Answer :

#Define function
def decode_instruction(inst):

#Define value for opcode mask
opMask = 0b11111111000000000000000000000000

#Perform operation
opcode = inst & opMask

#Compute opcode
opcode = opcode>>24

#Declare variable
dest = 0

#Declare variable
imm = 0

#Declare variable
op1 = 0

#Declare variable
op2 = 0

#Create variable
retVal = tuple()

#Create mask for destination
destMask = 0b00000000111111110000000000000000

#Perform operation
dest = inst & destMask

#Compute destination
dest = dest >>16

#If li instruction
if(opcode==4):

#Create variable
immdMask = 0b00000000000000001111111111111111

#Find immediate value
imm = inst & immdMask

#Set value
retVal = (opcode, dest, imm)

#Check condition
if(dest <0 or dest >10):

#Set value
retVal = (-1, -1, -1, -1)

#If add, sub , mul, div instruction
elif (opcode ==0 or opcode ==1 or opcode == 2 or opcode==3):

#Define mask for opcode 1
op1Mask = 0b00000000000000001111111100000000

#Compute value
op1 = inst & op1Mask

#find opcode 1
op1 = op1>>8

#Define mask for opcode 2
op2Mask = 0b00000000000000000000000011111111

#Find opcode 2
op2 = inst & op2Mask

#Set value
retVal = (opcode, dest, op1, op2)

#Check condition
if(dest <0 or dest >10):

#Set value
retVal = (-1, -1, -1, -1)

#Check condition
if(op1 <0 or op1 >10):

#Set value
retVal = (-1, -1, -1, -1)

#Check condition
if(op2 <0 or op2 >10):

#Set value
retVal = (-1, -1, -1, -1)

#Else
else:

#Set value
retVal = (-1, -1, -1, -1)

#Return
return retVal

#Sample Run
#Call function
print(decode_instruction(0x01050000))
print(decode_instruction(0x00040407))
print(decode_instruction(0x01010207))
print(decode_instruction(0x00090400))
print(decode_instruction(0x00090005))
print(decode_instruction(0x01050600))
print(decode_instruction(0x04060614))
print(decode_instruction(0x02000c05))
print(decode_instruction(0x07020403))
print(decode_instruction(0x04110752))