ALP Programs
ALP Programs
channel link :
https://www.youtube.com/c/ComputerScienceAcademy7
Assembly Language Programs (ALP)
• The assembly language program has the following format:
Memory address Label Mnemonics OP code (hex) Comments
• Label is name of line. It is used in jump instruction.
• Mnemonics consist of instruction and operands.
For example: MOV A, B.
• OP code (hex) is a corresponding hexadecimal code for the instruction.
Eg. MOV A, B (Op code = 78 H)
• Comments are useful for understanding purpose of instructions.
• Memory address is location where OP code / immediate data / address
are stored.
• Label and comment fields are optional.
Assembly Language Programs (ALP)
1. Write Assembly Language Program (ALP) to add two hexadecimal
numbers 12 H and 7A H. Store sum to memory location C000 H.
Solution 1: 12 H and 7A H
Mnemonics Comments 12 H 4 bit bin 00010010
MVI A, 12 H A = 12 H + 7A H + 01111010
8C H 1000 110 0
ADI 7A H A = A + 7A H
STA C000 H [C000 H] = (A) 8 C
HLT Stop MPU ADD r
ADD M x A = A + data
ADI data 12 H 7A H
ADI 7A H C000 H 8C H
Total Length of program = 8 byte
A = 8C H
Assembly Language Programs (ALP)
1. Write Assembly Language Program (ALP) to add two hexadecimal
numbers 12 H and 7A H. Store sum to memory location C000 H.
Solution 2: 12 H and 7A H
Mnemonics Comments 12 H 4 bit bin 00010010
MVI A, 12 H A = 12 H + 7A H + 01111010
8C H 1000 110 0
MVI B, 7A H B = 7A H
ADD B A=A+B 8 C
STA C000 H [C000 H] = (A) ADD r
HLT Stop MPU ADD M x A=A+B
ADI data 12 H 7A H
ADD B C000 H 8C H
Total Length of program = 9 byte
A = 8C H
Assembly Language Programs (ALP)
2. Write an ALP to add the content of memory location C000 H with the
content of memory location D000 H and store the 8 bit result in E000 H.
Solution 1:
Mnemonics Comments C000 H 03 H D000 H 02 H
LDA C000 H A = 03 H
E000 H 05 H
MOV B, A B = 03 H
LDA D000 H A = 02 H [C000 H] 03 H 4 bit bin 00000011
ADD B + [D000 H] + 02 H + 00000010
A=A+B
STA E000 H [E000 H ] = (A) 05 H 0 000 010 1
HLT Stop MPU 0 5
ADD r
ADD M A=A+ B
ADI data x 03 H 02 H
Total Length of program = 12 byte ADD B A = 05 H
Assembly Language Programs (ALP)
2. Write an ALP to add the content of memory location C000 H with the
content of memory location D000 H and store the 8 bit result in E000 H.
Solution 1: H D0 00 L
LDA C000 H A = 03 H
E000 H 05 H
LXI H, D000 H (HL) = D000H
ADD M A = A + [(HL)] [C000 H] 03 H 4 bit bin 00000011
+ [D000 H] + 02 H + 00000010
STA E000 H [E000 H ] = (A)
HLT 05 H 0 000 010 1
Stop MPU
0 5
ADD r
ADD M A=A+ M
ADI data x 03 H [(HL)]
Total Length of program = 11 byte ADD M A = 05 H
Assembly Language Programs (ALP)
3. Write an ALP to add the content of memory location C000 H with the
content of memory location C001 H and store the 8 bit result in C002 H.
Solution 1: C000 H 03 H M
Mnemonics Comments
C001 H 02 H M
LXI H, C000 H (HL) = C000H C0 00
02
01 C002 H 05 H M
MOV A, M A = [(HL)] A = 03 H H L
INX H (HL) = (HL) + 1 M = 02
ADD M A = A + [(HL)] A = 05 H ADD M A = A + M
INX H (HL) = (HL) + 1 A = A + [(HL)]
MOV M, A [(HL)] = A A = 03 H + 02 H
HLT Stop MPU A = 05 H
channel link :
https://www.youtube.com/c/ComputerScienceAcademy7
28. Write an ALP to double (multiply by 2) the content of block from D001 H to D00A H and
store the double content at same memory location.
Solution 1 : D001 H 02 H D001 H 04 H
MVI B, 0A H B = 0A H D002 H 05 H D002 H 0A H
LXI H, D001 H M = 02 H D003 H 04 H D003 H 08 H
UP: MOV A, M A = 02 H A = 05 H M = 05 H . .
ADD M A = 04 H A = 0A H . .
MOV M, A M = 04 H M = 0A H D00A H 49 H D00A H 92 H
INX H M = 05 H M = 04 H
DCR B B = 09 H B = 08 H
JNZ UP Solution 2 : 1. Counter reg. B initial value : B = 0A H
HLT MVI B, 0A H DCR B JNZ
LXI H, D001 H
UP: MOV A, M A = 02 H A = 02 H 0 0 0 0 0 0 1 0 RLC
RLC A = 04 H 00000100 A = 04 H
MOV M, A A = 05 H 0 0 0 0 0 1 0 1 RLC
INX H 00001010 A = 0A H 01001001
DCR B + 01001001
A = 49 H 0 1 0 0 1 0 0 1 RLC
JNZ UP 1 0 0 1 0 0 1 0 A = 92 H -----------------------
100 100 1 0
HLT
29. Write an ALP to half (divide by 2) the content of block from D001 H to D00A H and store
the half content at same memory location.
Solution : D001 H 04 H D001 H 02 H
MVI B, 0A H B = 0A H D002 H 0A H D002 H 05 H
LXI H, D001 H M = 04 H D003 H 10 H D003 H 08 H
UP: MOV A, M A = 04 H A = 0A H . .
RRC A = 02 H A = 05 H . .
MOV M, A M = 02 H M = 05 H D00A H D00A H
INX H M = 0A H M = 10 H
DCR B B = 09 H B = 08 H
JNZ UP 1. Counter reg. B initial value : B = 0A H
HLT DCR B JNZ
A = 04 H 0 0 0 0 0 1 0 0 RRC
00000010 A = 02 H
A = 0A H 0 0 0 0 1 0 1 0 RRC
00000101 A = 05 H
A = 10 H 0 0 0 1 0 0 0 0 RRC
0 0 0 0 1 0 0 0 A = 08 H
30. Write an ALP to exchange the Nibble of each memory location, content of block which
begins from 2501 H. The length of block is at 2500 H. Store the result at same memory
location.
Solution : Exchange the Nibble ?
2500 H 03 H M
LXI H, 2500 H M = 03 H Reverse Number 2501 H 25 H 52 H M
MOV B, M B = 03 H Interchange digits 2502 H AE H EA H M
UP: INX H M = 25 H M = AE H A = 25 H 2503 H 95 H
MOV A, M A = 25 H A = AE H RRC
RRC RRC Initial value of counter
RRC RRC (Length of block / series of
RRC RRC A = 52 H memory) is given at 2500 H
RRC
MOV M, A
A = 52 H A = EA H
M= 52 H M = EA H
X
MVI B, 03 H
channel link :
https://www.youtube.com/c/ComputerScienceAcademy7
52. Write an ALP to find the position of data 05 H in a block of memory D001 H to D005 H.
If data is found then store the position of data at memory location D100 H else store 00 H
at the same memory location. (Note : It is assume that 05 H may present only once )
Solution:
MVI B, 05 H Position (Reg. C)
MVI C, 01 H C = 01 H D001 H 05
12 H M
C = 02 H D002 H 05 HH
45 M
LXI H, D001 H D003 H 6B H
C = 03 H
UP: MOV A, M A = 0512
10HH
05 C = 04 H D004 H A9 H
CPI 05 H Z = 1010 C = 05 H D005 H 10 H M
JZ SKIP
INX H
INR C C = 02
06HH D100 H 02
01 HH
00
DCR B
JNZ UP B = 00 H
Here B became zero i.e. it indicate that 05 H
MVI C, 00 H C = 00 H is not found anywhere in the block.
SKIP: LXI H, D100 H
MOV M, C
HLT
53. There are two block of memory, one is from 2501 H to 2505 H, another is from 3501 H to
3505 H. Write an ALP to check whether content of this two block are exactly same or not.
If the contents are same then memory location D100 H should contain 00 H else FF H.
Solution:
MVI B, 05 H
MVI C, 00 H 2501 H 10 H M DE 3501 H 10
20 H
H
LXI H, 2501 H M = 10 H 2502 H 30 H M DE 3502 H 30
40 H
LXI D, 3501 H 2503 H 50 H 3503 H 50 H
UP: LDAX D A = 20 10 H
90
40 H 2504 H 70 H 3504 H 70 H
CMP M Z = 0011 2505 H 90 H M DE 3505 H 90 H
JNZ SKIP
INX H M = 30
90 HH
INX D D100 H 00HHH
FF
FF
DCR B B = 04
00 HH
JNZ UP
JMP NXT Here B became zero i.e. it indicate that two
SKIP: MVI C, FF H
blocks are exactly same.
NXT : LXI H, D100 H
MOV M, C
HLT
Multiplication of two 8 bit Hexadecimal numbers :
Decimal Multiplication :
Eg. 15 x 03 = 45
• Repeated addition is adding equal groups together. It is also known as multiplication.
• If the same number is repeated then, we can write that in the form of multiplication.
15 15 00 Sum = 00 Counter = 03
+ 15 + 15 + 15 Sum = Sum + 15
+ 15 ------- ------- Sum = 00 + 15
------- 30 15 Sum = 15 Decrement counter Counter = 02
45 + 15 + 15
------- ------- Sum = Sum + 15
45 30 Sum = 15 + 15
+ 15 Sum = 30 Decrement counter Counter = 01
-------
Sum = 45 Sum = Sum + 15
Sum = 30 + 15
Sum = 45 Decrement counter Counter = 00
Multiplication of two 8 bit Hexadecimal numbers :
Logic of our Program ?
A = 00 Counter
Sum B = 03
1st Hex Number (multiplier) C = 00
Sum = Sum + 15
ADD M
Sum = 00 + 15 [M] = 2nd Hex Number (multiplicand)
Sum = 15 Decrement counter Counter = 02
If CY = 1 then INR C
Otherwise SKIP
Repeat
Sum = Sumwhile
+ 15 B != 00
Sum = 15 + 15
Store
Sum = 3016 bit result
Decrement counter Counter = 01
A (Lower byte)
Sum = Sum + byte)
C (Higher 15
Sum = 30 + 15
Sum = 45 Decrement counter Counter = 00
channel link :
https://www.youtube.com/c/ComputerScienceAcademy7
54. Write an ALP to multiply two 8 bit data where multiplier is stored at D000 H and
multiplicand stored at D001 H. Store the 16 bit product from memory location D002 H.
Solution:
MVI A, 00 H A = 00 H
D000 H 03 H M
MVI C, 00 H C = 00 H D001 H 8A H M
D002 H 9E H M
LXI H , D000 H B = 03 H
D003 H 01 H M
MOV B, M
INX H M= 8A H A = 00 H 00000000
UP : ADD M + M = 8A H + 10001010
JNC SKIP ------------- --------------------
A = 8A H 1000 10 10 CY = 0
INR C 01 HH
C = 00
C = 00
SKIP: DCR B + M = 8A H + 10001010
00HH
B = 02
01 -------------
JNZ UP --------------------
Jump if B != 00 H A = 14 H 1 000 101 0 0 CY = 1
INX H + M = 8A H + 10001010 C = 01
MOV M, A ------------- --------------------
INX H A = 9E H 100 1 11 1 0 CY = 0
MOV M, C C = 01
HLT
55. Write an ALP to to exchange position of digit of number stored at C040 H. multiply
original number with the exchanged number, the result to be stored at memory
location from C041 H onwards.
Solution: C040 H 40 H M
MVI C, 00 H
C041 H Reg. A M
LXI H, C040 H M = 40 H
C042 H Reg. C M
MOV A, M
RRC A = 40 H
RRC RRC
RRC RRC
RRC RRC
MOV B, A B = 04 H RRC
MVI A, 00 H A = 04 H MOV B, A B = Reverse number
UP : ADD M (counter)
JNC SKIP
INR C
SKIP : DCR B
JNZ UP INX H
INX H MOV M, C
MOV M, A HLT
56. Write an ALP to separate nibbles of a number stored at memory location 2000 H.
Multiply separated nibbles and store result at 2001 H.
Solution: 2000 H 23 H M
LXI H, 2000 H
2001 H 06 H M
MOV A, M
ANI 0F H
MOV B, A A = 23 H
MOV A, M A ^ 0F H = 03 H MOV B, A B = 2nd Nibble
RRC A = 23 H (multiplier /counter)
RRC Four time RRC
RRC A = 32 H
RRC A ^ 0F H = 02 H MOV C, A C = 1st Nibble
ANI 0F H (multiplicand)
MOV C, A
MVI A, 00 H
UP: ADD C INX H
DCR B MOV M, A
JNZ UP HLT
57. Write an ALP that separate the two nibble of an 8 bit hex number stored in memory
location D000 H. Store the same in memory location D001 H and D002 H. The program
must also multiply the two nibble and store the product in memory location D003 H.
Solution: LXI H, D000 H
MOV A, M A = 23 H D000 H 23 H M
MOV C, A C = 23 H D001 H 03 H M
ANI 0F H A = 03 H D002 H 02 H M
MOV B, A B = 03 H
INX H D003 H 06 H M
MOV M, A
MOV A, C A = 23 H
RRC
RRC
RRC
RRC A = 32 H
ANI 0F H A = 02 H
INX H
MOV M, A M = 02 H
MVI A, 00 H INX H
UP: ADD M MOV M, A
DCR B HLT
JNZ UP
58. Write an ALP that multiplies the original number stored at C030 H with its lower
nibble. Store the result starting from C031 H onwards.
Solution:
MVI C, 00 H
LXI H, C030 H F2 H C030 H F2 H M
MOV A, M F 2 C031 H Reg. A M
ANI 0F H Lower Nibble C032 H Reg. C M
A = 02 H
MOV B, A B = 02 H (02 H)
MVI A, 00 H A = 00 H
UP: ADD M
JNC SKIP
INR C
SKIP: DCR B
JNZ UP
INX H
MOV M, A
INX H
MOV M, C
HLT
59. Write an ALP to multiply number stored at 8085 H by 09 H and store result at 8086 H
and 8087 H with lower byte 8086 H.
Solution: (Multiplier) Reg B = 09H
MVI C, 00 H M 8085 H 4B H X 09 H
MVI A, 00 H M 8086 H Reg. A
MVI B, 09 H M 8087 H Reg. C
LXI H, 8085 H
UP: ADD M
JNC SKIP
INR C
SKIP: DCR B
JNZ UP
INX H
MOV M, A
INX H
MOV M, C
HLT
60. Write an ALP to multiply 16 bit number stored at memory location C000 H and C001 H
by 8 bit number stored at memory location C002 H and store product in memory
location from C003 H.
Solution:
MVI C, 00 H 7A93 H C000 H 93 H
LHLD C000 H HL = 7A93 H X 03 H C001 H 7A H
XCHG DE = 7A93 H -------------- C002 H 03 H
C003 H B9 H
LDA C002 H A = 03 H
C004 H 6F H
LXI H, 0000 H HL = 0000 H A = =? 0000 H
HL C005 H 01 H M
UP : DAD D HL = HL + DE
JNC SKIP F526 HH + 7A93 H
7A93
HL = 0000 Carry may generate, C = 00 H
INR C F526 H
HL = 7A93
6FB9 H CY = 01 Get Multiplicand in Reg. Pair DE
SKIP : DCR A A = 02
01 H
00 H C = 00
01 H
H Get Multiplier in Reg. B
A (counter)
JNZ UP Jump if B != 00 H Add DE with HL DAD D Until A B become zero
SHLD C003 H Store 3 byte result
LXI H, C005 H
MOV M, C
HLT
Division of two 8 bit Hexadecimal numbers :
Decimal Division :
Quotient = 3 Remainder = 2
Eg. 11 / 03 = 3.67
• Repeated subtraction is a method of subtracting the equal number of items from a
larger group. It is also known as division.
• If the same number is repeatedly subtracted from another larger number until the
remainder is zero or a number smaller than the number being subtracted, we can
write that in the form of division.
Quotient = 0 (it’s the
(Reg. C =count
00 H)of How many times 2nd number is subtracted from 1st number)
Compare ( CMP M ) if Cy = 0 then continue
Is 11 >= 3 ? If yes, then Is 08 >= 3 ? Is 05 >= 3 ? Is 2 >= 3 ? (CY = 1 )
11 (A) SUB M 08 (A) 05 (A) If No then stop subtraction
- 03 (M) - 03 (M) - 03 (M) Result = ?
-------- -------- -------- Quotient = 3 ( C = 03 H )
08 (A) 05 (A) 02 (A) Remainder = 2 ( A = 02 H )
Quotient = 1 JMP Quotient = 2 JMP Quotient = 3 JMP
INR C C = 01 H INR C C = 02 H INR C C = 03 H
61. Write an ALP that divides two 1 byte Hex number where dividend is store in memory
location C000 H and divisor is stored in memory location C001 H. Store quotient and
remainder in memory location C002 H and C003 H respectively.
Solution: MVI C, 00 H C = 00 H (Quotient ) C000 H 0B H M
LXI H, C000 H C001 H 03 H M
MOV A, M A = 0B H (Dividend) C002 H 03 H M
INX H M = 03 H (Divisor) C003 H 02 H M
UP: CMP M A - M 02 05 - 03 CY = 01
0B
08
JC SKIP
SUB M A = A - M A = 08 05H(Remainder)
02
INR C 02 H
C = 01
03
JMP UP Unconditional Jump
SKIP: INX H
MOV M, C
INX H
MOV M, A
HLT
channel link :
https://www.youtube.com/c/ComputerScienceAcademy7
62. Write an ALP to interchange (swap) the content of memory location 1201 H and 1202 H.
Solution: