MPMC Assignment - Madhumitha
MPMC Assignment - Madhumitha
Programming
Assignment – 1
T E Madhumitha
ICE – A
110121111
1. Multiply two 8 bit numbers
Algorithm:
1. Load the value 4H into register A and store it in memory location 2050H
2. Load the value 3H into register A and store it in memory location 2051H
3. Load the 16-bit data from memory location 2050H into the HL register pair
4. Exchange the contents of the HL register pair with the DE register pair
5. Move the contents of register D into register C.
6. Load the immediate value 00H into register D and 0000H in HL register pair
7. Loop through the instructions from step 10 to step 12 until the value in register C
becomes zero
8. Store the contents of the HL register pair into memory location 2090H
Program:
MVI A,4H;
STA 2050H;
MVI A,3H;
STA 2051H;
LHLD 2050H;
XCHG;
MOV C,D;
MVI D,00H;
LXI H,0000H;
LOOP:DAD D;
DCR C;
JNZ LOOP;
SHLD 2090H;
Program Size:
Instruction Number Number of Bytes
1 2
2 3
3 3
4 1
5 3
6 1
7 1
8 1
9 3
10 1
11 1
12 3
13 3
Observation:
The result of the multiplication is stored in the memory address 2090 and the value of the
memory address is 0C
Result:
Let the input be,
2050H 2051H
04 03
Algorithm:
1. Load the immediate value 20H into register A and store it in memory location
2050H
2. Load the immediate value 30H into register A and store it in memory location
2060H
3. Load the immediate value 40H into register A and store it in location 2070H
4. Load the 16-bit data from memory location 2050H into the HL register pair and
exchange data from HL register pair with the DE register pair
5. Load the 16-bit data from memory location 2060H into the HL register pair and
exchange the data from DE register pair to the HL register pair
6. Exchange the contents of the HL register pair with the DE register pair
7. Load the 16-bit data from memory location 2070H into the HL register pair
8. Add the contents of the DE register pair to the HL register pair
9. Store the contents of the HL register pair into memory location 2090H
Program:
MVI A,20H ;
STA 2050H ;
MVI A,30H;
STA 2060H ;
MVI A,40H ;
STA 2070H ;
LHLD 2050H ;
XCHG ;
LHLD 2060H ;
DAD D ;
XCHG ;
LHLD 2070H ;
DAD D;
SHLD 2090H ;
Program Size:
Instruction Number Number of Bytes
1 2
2 3
3 2
4 3
5 2
6 3
7 2
8 1
9 3
10 1
11 1
12 3
13 1
14 3
Observation:
The program takes the three 16-bit numbers, already being stored in the register pairs BC,
DE & HL, and stores their sum in the HL register pair. Here, DAD instruction adds the content
of DE to HL, and stores the value in HL. Then, in the next step, data of B & C is also copied to
D & E, respectively which in turn again gets added by DAD instruction
Result:
209H
90
3. Find the largest numbers from an array of N numbers
Algorithm:
1. Get the size of the array and store it
2. Take the first two numbers and compare them
3. Store the value of bigger of them
4. Take the third number and bigger of the first two numbers and compare them
5. Store the bigger of this and go on the same till the last number
6. Store the bigger number after the execution till last number.
Program:
LXI H,4000H;
MOV C,M;
INX H;
MOV B,M;
DCR C;
NEXT: INX H;
MOV A,M;
CMP B;
JC SKIP;
MOV B,A;
SKIP: DCR C;
JNZ NEXT;
LXI H,4050H;
MOV M,B;
HLT;
Program Size:
Instruction Number Number of bytes
1 3
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 3
10 1
11 1
12 3
13 3
14 1
15 1
Observation:
• The size of the array is stored initially in reg C and the first number is loaded
• to reg B and next number to Accumulator.
• Both are compared in the 8th instruction [ CMP B ] which sets the CY if
• A<B or resets the Z if A>B.
• This is repeated for all the numbers in the array.
• The final result in B is moved to M which is stored in 4050H.
Result:
Let the input be
4000H 4001H 4002H 4003H
3 BA 3E 5C
4050H
BA
4. Separate even numbers from a given array of numbers
The length of the series is already stored in the memory location 4000H. The series
begins from the memory location 4001H. The new array which contains only even
numbers will begin with the address 4100H.
Algorithm:
1. Here we’re assigning 2 memory pointers HL and DE register pairs for the
existing and
2. the new array (only even) respectively.
3. Register C is used as a counter, in which the length of the series (stored in
4000H) is stored.
4. Using the fact that “Any even number gives zero as a result, when AND
operation is done with itself and 01H.
5. After a number is tested, it is stored accordingly and the counter is
decremented by one.
Program:
LXI H,4001H ;
LXI D,4100H;
LDA 4000H;
MOV C,A ;
LOOP1: MOV A,M;
ANI 01H ;
JNZ LOOP2;
MOV A,M;
STAX D ;
INX D ;
LOOP2: INX H ;
DCR C ;
JNZ LOOP1;
HLT;
Program Size :
Observation:
• In the 7th instruction [JNZ LOOP2;], if it is a odd number, it won’t be stored in
the new array and the memory pointer jumps to LOOP2.
• Similarly, in the 13th instruction [JNZ LOOP1;], if the counter is not zero, then
it’ll enter the LOOP1 to continue the process, else it’ll execute the next
instruction.
• Once the counter becomes zero, all the even numbers are stored in the new
array which begins with the address 4100H. Thus the result is obtained
successfully.
Result:
Let us add a series by using the above program.
4100H 4101H
5EH 8CH
5. Find the sum of series of given numbers
The length of the series is already stored in the memory location 4000H. The series
begins from the memory location 4001H. We assume the resultant sum to be a 16 bit
number, so we store the resultant sum at the memory locations 4100H and 4101H.
Algorithm:
1. Here we consider the register A and B as Sumlow and Sumhigh. So, first the number
will be added to accumulator, if there is a carry then it’ll be sent to register B.
2. Initially A and B are initialized as zero. Register C is used as a counter, in which the
length of the series (stored in 4000H) is stored.
3. The HL register pair (Pointer) is initialized with the address 4001H.
4. So, after adding one number to the accumulator, the pointer is incremented by one
and the counter is decremented by one.
5. Thus, when the counter reaches zero, the contents of A and B are stored in 4100H
and 4101H respectively.
Program:
LDA 4000H;
MOV C,A;
LXI H,4001H ;
SUB A ;
MOV B,A ;
LOOP1: ADD M ;
JNC LOOP2;
INR B;
LOOP2: INX H ;
DCR C ;
JNZ LOOP1 ;
STA 4100H ;
MOV A,B;
STA 4101H ;
HLT;
Program Size :
Instruction Number No. of Bytes
1 3
2 1
3 3
4 1
5 1
6 3
7 1
8 1
9 3
10 3
11 1
12 3
13 1
Observation:
• In the 7th instruction [JNC LOOP2;], if the there’s a carry then the next instruction
will be executed to increment the higher byte, else the it enters the LOOP2.
• Similarly, in the 11th instruction [JNZ LOOP1;], if the counter is not zero, then it’ll
enter the LOOP1 to continue the addition, else it’ll execute the next instruction.
• Once the counter becomes zero, the Lower and Higher Byte sum are stored in their
respective places. Thus the result is obtained successfully.
Result:
Let us add a series by using the above program.
4100H 4101H
85H 01H
6. Find the factorial of ‘N’
Algorithm:
1. Let’s use function to find the factorial
2. Load the register A with the value. Here input is at 2200H.
3. Compare it with 02H, to find out if it is less than 2.
4. If it is less than 2, directly display the number and end it
5. If not, we call function to find the factorial
6. Output is stored in 2201H.
Program:
LDA 2200H;
CPI 02H;
JC LAST;
MVI D,00H;
MOV E,A;
DCR A;
MOV C,A;
CALL FACTO;
XCHG;
SHLD 2201H;
JMP END;
LAST: LXI H,0001H;
SHLD 2201H;
END: HLT;
FACTO: LXI H,0000H;
MOV B,C;
BACK: DAD D;
DCR B;
JNZ BACK;
XCHG;
DCR C;
CNZ FACTO;
RET;
Program Size :
Instruction Number Number of Bytes
1 3
2 1
3 3
4 2
5 1
6 1
7 1
8 3
9 1
10 3
11 3
12 3
13 3
14 1
15 3
16 1
17 8
18 1
19 3
20 1
21 1
22 3
23 1
Observation:
• Here we initially compare the number with to find out it is less than 2 or not,
because if it is less than 2, the number itself is the answer.
• If the number is greater than 2, we take separately N and N-1 and call the
function
• FACTO and recursively call the function again and again until we reach the
minimum
• number and add all the results will give us the final value in 2201H.
Result:
Let the input be
2200H
3
2201H
6