📌  相关文章
📜  如何在Python使用不同的寻址模式执行11位指令?

📅  最后修改于: 2021-06-28 14:24:14             🧑  作者: Mango

在这里,我们有一条11位指令,其中前2位用于表示寻址模式,后3位用于操作码,后6位用于两个操作数,每个3位。

我们将使用四种不同的寻址模式执行此11位指令:

  1. 直接模式:在此模式下,在指令中指定两个操作数的地址。我们可以直接从内存地址接收实际数据。
  2. 间接模式:在此模式下,指令中提到的地址指向操作数的有效地址。
  3. 立即模式:在此模式下,实际数据在指令本身中提及。
  4. 寄存器模式:在此模式下,指令包含包含实际数据的寄存器的地址。

我们将使用前2位以这种方式表示四种不同的寻址模式:

For direct mode- 00
For indirect mode- 01
For immediate mode- 10
For register mode- 11

接下来的3位代表操作码,因此我们最多可以使用8种不同的操作。我们将定义6个操作,而其他两个将以这种方式保留给将来使用:

000- Do nothing
001- Addition
010- Subtraction
011- Multiplication
100-Division
101-Transfer operand2 to operand1
110-Reserve for future
111-Reserve for future

接下来的3位用于操作数1,最后3位用于操作数2,因此在立即模式下,操作数的值范围为0到7。
对于直接,间接和寄存器模式,我们需要定义一个存储器阵列和一个寄存器阵列。由于我们只有3位代表地址,因此这些数组中的最大元素数将为8。

memory=[2,15,40,25,7,36,64,19]
register=[17,20,43,52,None,None,None,None]

此处,存储器包含8个数据,而寄存器包含4个数据。上面讨论的概念将以下列方式工作:

例子:

Python
memory=[2,15,40,25,7,36,64,19]
register=[17,20,43,52,None,None,None,None]
  
#This function execute the instruction and print the result. 
def execute(st):
    mode=st[:2]
    opcode=st[2:5]
    operand1=st[5:8]
    operand2=st[8:]
    print()
    print("Instruction mode:",mode)
    print("Opcode:",opcode)
    print("operand1:",operand1)
    print("operand2:",operand2)
  
    #For direct mode
    if mode=='00':
        idx1=int(operand1,2)
        idx2=int(operand2,2)
        if opcode=='000':
            print("Do nothing")
        elif opcode=='001':
            print("RESULT")
            print(memory[idx1]+memory[idx2])
        elif opcode=='010':
            print("RESULT")
            print(memory[idx1]-memory[idx2])
        elif opcode=='011':
            print("RESULT")
            print(memory[idx1]*memory[idx2])
        elif opcode=='100':
            print("RESULT")
            print(memory[idx1]/memory[idx2])
        elif opcode=='101':
            print("RESULT")
            print("operand1=:")
            print(int(operand2,2))
        else:
            print("Reserve For Future")
  
    #For indirect mode
    elif mode=='01':
        idx1=int(operand1,2)
        idx2=int(operand2,2)
        idx1=memory[idx1]
        idx2=memory[idx2]
        if opcode=='000':
            print("Do nothing")
        elif opcode=='001':
            print("RESULT")
            print(memory[idx1]+memory[idx2])
        elif opcode=='010':
            print("RESULT")
            print(memory[idx1]-memory[idx2])
        elif opcode=='011':
            print("RESULT")
            print(memory[idx1]*memory[idx2])
        elif opcode=='100':
            print("RESULT")
            print(memory[idx1]/memory[idx2])
        elif opcode=='101':
            print("RESULT")
            print("operand1=:")
            print(int(operand2,2))
        else:
            print("Reserve For Future")
  
    #For immediate mode
    elif mode=='10':
        idx1=int(operand1,2)
        idx2=int(operand2,2)
        if opcode=='000':
            print("Do nothing")
        elif opcode=='001':
            print("RESULT")
            print(idx1+idx2)
        elif opcode=='010':
            print("RESULT")
            print(idx1-idx2)
        elif opcode=='011':
            print("RESULT")
            print(idx1*idx2)
        elif opcode=='100':
            print("RESULT")
            print(idx1/idx2)
        elif opcode=='101':
            print("RESULT")
            print("operand1=:")
            print(int(operand2,2))
        else:
            print("Reserve For Future")
  
    #For register mode
    else:
        idx1=int(operand1,2)
        idx2=int(operand2,2)
        if idx1>3 or idx2>3:
            print("Invalid")
            exit()
        if opcode=='000':
            print("Do nothing")
        elif opcode=='001':
            print("RESULT")
            print(register[idx1]+register[idx2])
        elif opcode=='010':
            print("RESULT")
            print(register[idx1]-register[idx2])
        elif opcode=='011':
            print("RESULT")
            print(register[idx1]*register[idx2])
        elif opcode=='100':
            print("RESULT")
            print(register[idx1]/register[idx2])
        elif opcode=='101':
            print("RESULT")
            print("operand1=:")
            print(int(operand2,2))
        else:
            print("Reserve For Future")
  
#driver code
st="00001000001"
execute(st);
st="01001000100"
execute(st);
st="10001000001"
execute(st);
st="11001000001"
execute(st);


输出:

Instruction mode: 00
Opcode: 001
operand1: 000
operand2: 001
RESULT
17

Instruction mode: 01
Opcode: 001
operand1: 000
operand2: 100
RESULT
59

Instruction mode: 10
Opcode: 001
operand1: 000
operand2: 001
RESULT
1

Instruction mode: 11
Opcode: 001
operand1: 000
operand2: 001
RESULT
37