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

python language please, using bitwise operations Part III: Decode a Machine Inst

ID: 3716409 • Letter: P

Question

python language please, using bitwise operations

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, op1, op2: Performs an addition. dest is the desination register where the sum of regis- ters opl and op2 will be saved. (dest = op1 + op2) . sub dest, op1, op2: Performs a subtraction. dest is the desination register where the difference of registers opl and op2 will be saved. (destopop2) mul dest, op1, op2: Performs a multiplication. dest is the desination register where the product of registers op1 and op2 will be saved. (dest -opl op2) div of registers op1 and op2 will be saved. (dest-opl 17 op2) dest, opl, op2: Performs an integer division. dest is the desination register where the quotient . 11 dest, immediate: Stores the value immediate in register dest. Gie.dest - immediate) Recall from lecture that an immediate value is simply a constant. As with the real MIPS computer, the KIPS computer has 10 registers numbered $t0, $t1, ..., $t 9. In the above dest, op1 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

def decode_instruction(number):



omask=0b11111111000000000000000000000000
opcode=number&omask
opcode=opcode>>24
dregmask=0b00000000111111110000000000000000
destreg=number&dregmask
destreg=destreg>>16
if opcode==4:
    imask=0b00000000000000001111111111111111
    ival=number&imask
  
    t=(opcode,destreg,ival)
else:
    oper1mask=0b00000000000000001111111100000000
    op1=number&oper1mask
    op1=op1>>8
    oper2mask=0b11111111
    op2=number&oper2mask
  
  
    if opcode>4 or destreg>9 or op1>9 or op2>9:
        t=(-1,-1,-1,-1)
    else:
        t=(opcode,destreg,op1,op2)
return t