12th Computer Science: Maharashtra Board
12th Computer Science: Maharashtra Board
Solution :
Mnemonics Comments
MVI A, 12 H A=12 H
ADI 7A H A=A + 7A H => 12 H + 7AH
STA C000 H C000H= 8C H
HTL. STOP MPU
Write an ALP to add two 16 bit numbers. The first number stored at memory
location C030 H and C031 H and the second number stored at C032 H and
C033 H. Store 16 bit result at memory location C034 H and C035 H
C030 H 10 H
Solution :
C031 H 20 H
Mnemonics Comments C032 H 20 H
LHLD C030 H HL = 1020 H
C033 H 10 H
XCHG DE = 1020 H
LHLD C032 H HL = 2010 H C034 H 30 H
DAD D HL = HL + DE => 2010 H + 1020 H = 3030 H C035 H 30 H
SHLD C034 H [C034 H] = L(30) , [C035 H] = H (30)
HLT Stop MPU
Write an ALP to find 1’s complement of 8 bit number stored in
memory location C000 H. Store result at memory location C001 H
Solution :
Mnemonics Comments
LDA C000 H. [C000 H]= 02 H , A=02 H
CMA. [A]=FD H
STA C001 H. [C001] =A (FD H)
HLT. Stop MPU
Write an ALP to find 1’s complement of 16 bit number stored at
memory location C000 H and C001 H and stored result at memory
location C002 H and C003 H.
Solution : C000 H 01 H
C001 H 02 H
Mnemonics Comments
LHLD C000 H. [HL]=0102 H C002 H FE H
MOV A, H. [A]=01 H C003 H FD H
CMA [A] = FE H
MOV H, A [H]=FE H
MOV A, L [A]=02 H
CMA [A] = FD H
MOV L, A [L]= FD H
SHLD C002 H [C002 H] = FE FD H
HLT Stop MPU
Write an ALP to find 2’s complement of 8 bit number stored in
memory location C000 H. Store result at memory location C001 H.
Solution :
Mnemonics Comments
LDA C000 H [A]=02 H
CMA [A]=FD H
INR A [A] = FE H
STA C001 H [C001 H]=FE H
HLT [C001 H]=FE H
Write an ALP to find 2’s complement of 16 bit number stored at
memory location C000 H and C001 H and stored result at memory
location C002 H and C003 H.
Solution : C000 H 01 H
C001 H 02 H
Mnemonics Comments
LHLD C000 H [HL]=0102 H C002 H FE H
MOV A, H [A]=01 H
CMA [A] = FE H C003 H FE H
MOV H, A H=A => [H]=FE H
MOV A, L [A]=02 H
CMA [A] = FD H
MOV L, A [L]=FD H
INX H HL=HL+1 = FEFD H + 1 = FEFE H
SHLD C002 H [C002 H]=H (FE H) , [C003 H]=L (FE H)
HLT Stop MPU
Interchange Digits
[A]=01 H
01 H => 00000001
RRC => 10000000
RRC => 01000000
RRC => 00100000
RRC => 00010000
[A]=10 H
A hex number is stored at location AB00 H. Write an ALP to interchange its
digits. And the new number is to be stored at AB01 H. Add original number with
new number and store result at location ABCD H.
Solution : AB00 H 01 H
AB01 H 10 H
Mnemonics Comments
LXI H, AB00 H [HL]=AB00 H , [AB00 H]=01 H AB02 H ------
MOV A, M [A]=01 H
RRC Rotate Right ABCD H 11 H
RRC Rotate Right
RRC Rotate Right
RRC Rotate Right, [A] = 10 H
INX H [HL]=AB01 H
MOV M, A [[HL]]=10 H => [AB01 H]=10 H
DCX H [HL]=AB00 H => [AB00 H] => 01 H
ADD M A=A+M=> 10 H + 01 H = 11 H
STA ABCD H ABCD H =A => [ABCD H ] =11 H
HLT STOP MPU
Separate Nibble of a number :
12 H = > After Nibble separation result should be : 01 H and 02H
12 H. = 0001 0010
^ 0F H. = 0000 1111
---------------------
02 H = 0000 0010
12 H. = 0001 0010
^ F0 H. = 1111 0000
---------------------
10 H = 0001 0000
01 H
Write an ALP to separate Nibble of a number stored at memory locations 2000 H.
add separated nibble and store result at 2001 H.
Solution :
Mnemonics Comments
LXI H, 2000 H [HL]=2000 H, [2000 H] = H
MOV A, M [A]=12 H
ANI 0F H A=A+0F H => 02 H
MOV B, A [B]=02 H, [A]=02H
MOV A, M. [A]=12 H
ANI F0 H A=A+F0H => 10 H
RRC Rotate Right
RRC Rotate Right
RRC Rotate Right
RRC Rotate Right , [A]=01 H
ADD B A=A+B => 03 H
INX H [HL]=2001 H
MOV M, A [HL]=2001H , [2000 H] =03 H
HLT STOP MPU
Write an ALP to add two 8 bits hex numbers stored at memory locations
C000 H and C001 H. Store the two byte result from memory locations C002 H
onwards.
Solution 1:
Mnemonics Comments
MVI C, 00 H [C]= 00H
LXI H, C000 H. [HL] = C000 H, [C000 H]=0
MOV A, M [A] = 04 H
INX H [HL]=C001 H, [C001 H]=02 H
ADD M A=A+M= 01+02 =06 H
JNC SKIP Cy=0 then jump to SKIP
INR C [C]=01 H
SKIP:INX H [HL]=C002 H
MOV M, A [[HL]](M)=06H
INX H [HL]=C003 H
MOV M, C [[HL]](M)=00H
HLT STOP MPU
2. Arithmetic Group :
20. DAA : [DECIMAL ADJUST ACCUMULATOR]
Comment:
The eight bit number in the accumulator is adjusted to form two four-bit Binary coded Decimal digits by this
instruction. It can be done by following process:
1) If the value of the LSB of the A (A3 - A0) is > 9 or if the AC flag is set =1, 6 (06) is added to LSB of A
2) If the value of MSB of the A (A7 - A4) is > 9 or if the Cy =1, 6 (60) is added to the MSB of A
3) If both 4 LSBs and 4 MSBs of A are > 9 or flags Ac=1 and Cy=1 are set respectively then 66 add to
the accumulator content.
1. LSB > 9 or AC =1 then 6 are added to LSB
2. MSB > 9 or Cy =1 then 6 are added to MSB
3. LSB > 9 and MSB >9 and AC=1 and Cy=1 then 66 to A
2. Arithmetic Group :
[ Note : This instruction must always follow an addition instruction for two BCD numbers. It
can not be used to adjust results after subtraction.]
Example:
4B = 0 1 0 0 1 0 1 1
+06 = 0 0 0 0 0 1 1 0
--------------------------------
51 = 0 1 0 1 0 0 0 1
Solution :
Mnemonics Comments
MVI C, 00 H. [C]=00 H
LXI H, AB00 H [HL]=AB00 H, [AB00 H]=39 (BCD)
MOV A, M [A]=39 (BCD)
INX H [HL]=AB01 H, [AB01 H]=12 (BCD)
ADD M A=A+M=39+12 =4B H
DAA A=4B+06 = 51(BCD)
JNC SKIP Jump if not carry to SKIP
INR C [C]=01 H
SKIP : INX H. [HL]=AB02 H
MOV M, A M=51 (BCD) => [AB02 H]=51 (BCD)
INX H [HL]=AB03 H
MOV M, C M=00H (BCD) => [AB02 H]=00h (BCD)
HLT
Write an ALP to rotate the content of memory location D000 H towards left
by one bit position and add original content with rotated number and store
the result from D001 H onwards.
Solution :
[A]=01 H => 0000 0001
Mnemonics Comments RLC => 000 00010
MVI C, 00 H [C]=00H [A] =>02 H
LXI H, D000 H. [HL]=D000 H,[D000 H]= 01 H => M=01 H
MOV A, M. [A]=01 H
RLC Rotate left by 1 bit , [A]=02 H
ADD M A=A+M => 02+ 01 = 03 H, Cy=00 H
JNC SKIP Jump if not carry to SKIP
INR C [C] = 01 H
SKIP : INX H [HL]=D001 H
MOV M, A M=03 H =>[HL] =D001 H, [D001 H]=>03 H
INX H [HL]=D002 H
MOV M, C M=00 H =>[HL] =D002 H, [D002 H]=>00 H
HLT Stop MPU
Check if hex number is even or odd :
● If the last digit of a binary number is 1, the number is odd
● If the last digit of a binary number is 0, the number is even.
● Eg.1 45 H => 0100 0101 ---> Odd number
● Eg.2 32 H => 0011 0010 ---> Even number
Solution :
Mnemonics Comments
MVI E, FF H [E]=FF H
LXI H, C000 H [HL]=C000 H,[C000 H]=45 H
MOV A, M [A]=45 H
RRC Rotate right by 1 bit , [A]=A2 H
JNC SKIP Jump if no carry, Cy=00 H
MVI E, 00 H [E]=00 H
SKIP:INX H [HL]=C001 H
MOV M, E M=FF H =>[HL]=C001 H, [C001 H] = FF H
HLT Stop MPU
What is Palindrome number ?
● A palindrome number is a number that remains the same when digits are
reversed.
● For example, the number 12321 is a palindrome number, but 1451 is not
a palindrome number.
● Number 12321 , after reverse is 12321 => After reverse it's same that's
why it's palindrome
Write an ALP to clear register B if the number at memory 20F9 H is
palindrome otherwise store FF H in register B.
Solution :
Mnemonics Comments
MVI B, 00 H [B]=00 H [Consider number is palindrome that's why register B cleared ]
LXI H, 20F9 H [HL]=20F9 H,[20F9 H]=33 H
MOV A, M [A]=33 H
RRC Rotate right by 1 bit
RRC Rotate right by 1 bit
RRC Rotate right by 1 bit
RRC Rotate right by 1 bit , [A]=33 H
CMP M A and M are equal then Z=1
JZ SKIP Jump if Z flag set
MVI B, FF H [B]= FF H. if Z flag is not set
SKIP:HLT Stop MPU
Write an ALP to subtract the number stored in memory location 3601 H from
the number stored in memory location 3600 H. Store the positive result /
absolute difference at location 3602 H.
Solution :
Mnemonics Comments
LXI H, 3600 H [HL] = 3600 H, [3600 H] = 32 H
MOV A, M. [A] = 32 H
INX H. [HL]=3601 H,[3601 H] = 33 H
SUB M A=A-M= 32-33=01 H , Sign Flag=1
JP SKIP Jump if Positive to SKIP
MOV A, M [A]=33 H
DCX H [HL]=3600 H,[3601 H] = 32 H
SUB M A=A-M= 33-32=01 H
SKIP:STA 3602 H. [3602 H]=01 H
HLT Stop MPU
Write an ALP to fill the memory location 4500 H to 4504 with
Hexadecimal numbers 09 H to 0D H respectively.
Solution :
Mnemonics Comments
MVI B, 05 H. [B]=05 H
MVI A, 09 H [A]=09 H
LXI H, 4500 H [HL]=4500 H
UP:MOV M, A M=A => [HL]=4500 H,[4500 H]=09 H
INR A [A]=0A H
INX H [HL]=4501 H
DCR B [B]=04 H , Z=0
JNZ UP Jump if not zero to UP
HLT Stop MPU
Write an ALP to fill 20 consecutive memory location starting from
2501 H onwards with data AA H.
Solution :
Mnemonics Comments
MVI B, 14 H. [B]= 14 H
MVI A, AA H [A]=AA H
LXI H, 2501 H [HL]=2501 H
UP:MOV M, A M=AA H => [HL]=2501 H, [2501 H]= AA H
INX H [HL]=2502 H
DCR B [B]=13 H
JNZ UP Jump to UP if B is not Zero
HLT Stop MPU
20. Write an ALP to fill the memory locations from AB00 H an
onwards with hexadecimal number 00 H to 0F H.
Solution :
Mnemonics Comments
MVI B, 10 H [B]= 10 H
MVI A, 00 H [A]= 00 H
LXI H, AB00 H [HL]=AB00 H
UP:MOV M, A M= 00 H= > [HL]=AB00 H, [AB00 H]=00H
INR A [A]=01 H
INX H [HL]=AB01 H
DCR B [B]=0F H
JNZ UP Jump to UP if B is not Zero
HLT Stop MPU
22. Write an ALP to fill memory block from 2000 H to 2009 with data
BB H and 44 H alternately.
Solution 1: Note :
[A] = BB H
Mnemonics Comments BB H => 1011 1011
MVI B, 0A H [B]=0A H After CMA=> 0100 0100
MVI A, BB H [A] = BB H [A]= 44 H
LXI H, 2000 H [HL]=2000 H
UP :MOV M, A. M=BBH Complement of BB H is 44H
CMA [A]=44 H and complement of 44 H is BB
INX H [HL]=2001 H H
DCR B [B]=09 H
JNZ UP Jump to UP if B is not Zero
HLT Stop MPU
There is a block of memory from 2501 H to 250A H. Write an ALP
replace odd numbers with data FF H in given block.
Solution : Note :
[A]=01 H
Mnemonics Comments 01 H => 0000 0001
MVI B, 0A H [B]=0A H RRC => 10000 000 and CY=1
LXI H, 2501 H. [HL]=2501 H ,let [2501 H]=01 H After rotation Cy is set
UP :MOV A, M [A]=01 H
RRC Rotate right by one bit, Cy=1
JNC SKIP Jump if carry is not set to SKIP
MVI M, FF H M=FF H => [HL]=2501 H ,[2501 H]=FF H
SKIP : INX H [HL]=2502 H, Let [2502 H]=02 H
DCR B [B]=09 H
JNZ UP Jump to UP if B is not Zero
HLT Stop MPU
24. There is a block of memory from 2501 H to 250A. Write an ALP
replace even numbers with data FF H in given block.
Solution :
Mnemonics Comments
MVI B, 0A H [B]=0A H
LXI H, 2501 H. [HL]=2501 H ,let [2501 H]=01 H
UP :MOV A, M [A]=01 H
RRC Rotate right by one bit, Cy=1
JC SKIP Jump if carry is set to SKIP
MVI M, FF H M=FF H => [HL]=2501 H ,[2501 H]=FF H
SKIP : INX H [HL]=2502 H, Let [2502 H]=02 H
DCR B [B]=09 H
JNZ UP Jump to UP if B is not Zero
HLT Stop MPU
A 8 bit hexadecimal number stored at memory location C000 H. Write
an ALP to count numbers of zeros in it and store count in memory
location C001 H.
Solution :
Mnemonics Comments
MVI B, 08 H [B]=08 H
MVI C, 00 H. [C]=00 H
LXI H, C000 H. [HL]=C000 H,[C000 H]=01 H
MOV A, M [A]=01 H
UP : RRC Rotate right by 1 bit
JC SKIP. Jump to SKIP if Carry is set
INR C [C]=01 H
SKIP:DCR B [B]=07 H
JNZ UP Jump to UP if register B is not zero
INX H [HL]=C001 H
MOV M, C M=07 H,
HLT Stop MPU
Write an ALP to increment the content of alternate memory location each by
2 from 1051 H to 1060 H
1051 H 01 H
1052 H 02 H
Solution : 1053 H 03 H
1054 H 04 H
1055 H 05 H
Mnemonics Comments
1056 H 06 H
MVI B, 10 H [B]=10 H
1057 H 07 H
LXI H, 1051 H [HL]=1051 H,[1051 H]=01 H 1058 H 08 H
UP: INR M M= 02 H 1059 H 09 H
INR M M= 03 H 105A H 0A H
INX H [HL]=1052 H,[1053 H]=02 H 105B H 0B H