How to execute a 11-digit instruction using different addressing modes in Python?
Here, we have an 11 bit instruction which has first 2 bits for representing addressing mode, next 3 bits for the opcode and the last 6 bits are for the two operands, 3 bits each.
We will execute this 11 bit Instruction using four different addressing modes:-
- Direct mode: In this mode, the addresses of two operands are specified in the instruction. We can receive the actual data directly from the memory addresses.
- Indirect mode: In this mode, the addresses mentioned in the instruction are point to the effective addresses of the operands.
- Immediate mode: In this mode, the actual data is mentioned in the instruction itself.
- Register mode: In this mode, the instruction contains the addresses of the registers which contain the actual data.
We will use the first 2 bits for representing the four different addressing modes in this way:
For direct mode- 00
For indirect mode- 01
For immediate mode- 10
For register mode- 11
Next 3 bits are for representing opcode, so we can use 8 different operations maximum. We will define 6 operations and the other two will be reserve for future in this way:
000- Do nothing
001- Addition
010- Subtraction
011- Multiplication
100-Division
101-Transfer operand2 to operand1
110-Reserve for future
111-Reserve for future
The next 3 bits are for operand1 and the last 3 bits are for operand2, so the values of operands will be ranging from 0 to 7 in immediate mode.
For the direct, indirect and register mode we need to define a memory array and a register array. As we have only 3 bits for representing the addresses so the maximum number of elements in these arrays will be 8.
memory=[2,15,40,25,7,36,64,19]
register=[17,20,43,52,None,None,None,None]
Here, the memory contains 8 data and the register contain 4 data. The concept discussed above will work in the following way:
Input: 01001000100
Here, from left to right
Mode-01- Indirect mode
Opcode-001- Addition
Operand1-000- 0
Operand2 – 100- 4
As it is the indirect mode, so these operands gives the addresses of the effective address of the data.
Means, the effective addresses are present in the memory location 0 and 4 , which are 2 and 7.
And the actual data is present in the memory location 2 and 7 which are 40 and 19.
So, the result will be the addition of 40 and 19.
Output: 59
Example:
Python
memory = [ 2 , 15 , 40 , 25 , 7 , 36 , 64 , 19 ]
register = [ 17 , 20 , 43 , 52 , None , None , None , None ]
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)
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" )
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" )
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" )
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" )
st = "00001000001"
execute(st);
st = "01001000100"
execute(st);
st = "10001000001"
execute(st);
st = "11001000001"
execute(st);
|
Output:
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
Last Updated :
01 Nov, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...