How to execute a 11-digit instruction using different addressing modes in Python?
Last Updated :
01 Nov, 2020
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]
#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);
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
Similar Reads
Python program to convert any base to decimal by using int() method
Given a number and its base, the task is to convert the given number into its corresponding decimal number. The base of number can be anything like digits between 0 to 9 and A to Z. Where the value of A is 10, value of B is 11, value of C is 12 and so on. Examples: Input : '1011' base = 2 Output : 1
2 min read
Python program to determine if the given IPv4 Address is reserved using ipaddress module
Given a IPv4 Address, the task is to determine whether it is reserved (i.e belongs to class E) or not. What is class E? IP addresses belonging to class E are reserved for experimental and research purposes. IP addresses of class E range from 240.0.0.0 â 255.255.255.254. This class doesnât have any s
1 min read
Python Program to Find and Print Address of Variable
In this article, we are going to see how to find and print the address of the Python variable. It can be done in these ways:Using id() functionUsing addressof() functionUsing hex() functionMethod 1: Find and Print Address of Variable using id()We can get an address using id() function, id() function
2 min read
How to create an array of zeros in Python?
Our task is to create an array of zeros in Python. This can be achieved using various methods, such as numpy.zeros(), which is efficient for handling large datasets. Other different methods are:Using the In-Build method numpy.zeros() methodUsing simple multiplication Using List comprehensionUsing it
5 min read
Python Program for Reverse of a Number Using Type Casting
Reversing a number is a common problem-solving exercise in programming, often used to teach the basics of algorithms and data manipulation. In Python, reversing a number can be efficiently achieved using type casting.Example:Input: 12345Output: 54321Input: -12345Output: -54321Input: 123.45Output: 54
4 min read
Python program to print an array of bytes representing an integer
Given an integer N, the task is to write a Python program to represent the bytes of this number as an array. A byte is a group of 8 bits. Any integer can be represented in the form of bytes and bits. We generally use hexadecimal codes to represent a byte. A single hexadecimal character can represent
3 min read
Python Program to Convert a Number into 32-Bit Binary Format
Binary representation is a fundamental concept in computer science, and converting numbers into binary format is a common task for programmers. In this article, we will explore some simple and generally used methods to convert a number into a 32-bit binary format using Python. Each method provides a
2 min read
Python program to print number of bits to store an integer and also the number in Binary format
Given an integer, the task is to write a Python program to print the number of bits to store that integer and also print the same number in Binary format. Example: Input: n = 10 Output: Number of bits to store the number: 4 Binary value: 0b1010 Input: n = 120 Output: Number of bits to store the numb
4 min read
Python - Interconvert Tuple to Byte Integer
Sometimes, while working with Python data, we can have a problem in which we need to perform conversion of tuple values, into combined byte and then to integer and vice-versa. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed. I
2 min read
How to Convert Binary Data to Float in Python?
We are given binary data and we need to convert these binary data into float using Python and print the result. In this article, we will see how to convert binary data to float in Python. Example: Input: b'\x40\x49\x0f\xdb' <class 'bytes'>Output: 3.1415927410125732 <class 'float'>Explana
2 min read