Alpprograming
Alpprograming
Flowchart:
Start
Complement accumulator.
Stop
Program:
Address Mnemonics Operand Opcode Remarks
2000 LDA 3000H 3A Load H-L pair with data from 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 CMA 2F Complement accumulator.
2004 STA 3001H 32 Store the result at memory location 3001H.
2005 01 Lower-order of 3001H.
2006 30 Higher-order of 3001H.
2007 HLT 76 Halt.
Explanation:
This program finds the 1’s complement of an 8-bit number stored in memory location 3000H.
Let us assume that the operand stored at memory location 3000H is 85H.
The operand is moved to accumulator from memory location 3000H.
Then, its complement is found by using CMA instruction.
The result is stored at memory location 3001H.
Output:
Before Execution:
3000H: 85H
After Execution:
3001H: 7AH
Flowchart:
Start
Complement accumulator.
Increment accumulator.
Stop
Program:
Address Mnemonics Operand Opcode Remarks
2000 LDA 3000H 3A Load H-L pair with data from 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 CMA 2F Complement accumulator.
2004 INR A 2C Increment accumulator.
2005 STA 3001H 32 Store the result at memory location 3001H.
2006 01 Lower-order of 3001H.
2007 30 Higher-order of 3001H.
2008 HLT 76 Halt.
Explanation:
This program finds the 2’s complement of an 8-bit number stored in memory location 3000H.
Let us assume that the operand stored at memory location 3000H is 85H.
The operand is moved to accumulator from memory location 3000H.
Then, its complement is found by using CMA instruction.
One is added to accumulator by incrementing it to find its 2’s complement.
The result is stored at memory location 3001H.
Output:
Before Execution:
3000H: 85H
After Execution:
3001H: 7BH
Program 10: Add two 8-bit numbers along with considering the carry.
Flowchart:
Start
Add B with A.
If
No
Carry?
Yes
Increment register C.
Program:
Address Mnemonics Operand Opcode Remarks
2000 LXI H, 3000H 21 Load H-L pair with address 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 MOV A, M 7E Move the 1st operand from memory to reg. A.
2004 INX H 23 Increment H-L pair.
2005 MOV B, M 46 Move the 2nd operand from memory to reg. B.
2006 MVI C, 00H 0E Initialize reg. C with 00H.
2007 00 Immediate value 00H.
2008 ADD B 80 Add B with A.
2009 JNC 200D D2 Jump to address 200DH if there is no carry.
200A 0D Lower-order of 200DH.
200B 20 Higher-order of 200DH.
200C INR C 0C Increment reg. C.
200D INX H 23 Increment H-L pair.
200E MOV M, A 77 Move the result from reg. A to memory.
200F INX H 23 Increment H-L pair.
2010 MOV M, C 71 Move carry from reg. C to memory.
2011 HLT 76 Halt.
Explanation:
This program adds two operands stored in memory location 3000H and 3001H, along with
considering the carry produced (if any).
Let us assume that the operands stored at memory location 3000H is FAH and 3001H is 28H.
Initially, H-L pair is loaded with the address of first memory location.
The first operand is moved to accumulator from memory location 3000H and H-L pair is
incremented to point to next memory location.
The second operand is moved to register B from memory location 3001H.
Register C is initialized to 00H. It stores the carry (if any).
The two operands stored in register A and B are added and the result is stored in accumulator.
Then, carry flag is checked for carry. If there is a carry, C register is incremented.
H-L pair is incremented and the result is moved from accumulator to memory 3002H.
H-L pair is again incremented and carry (either 0 or 1) is moved from register C to memory
location 3003H.
Output:
Before Execution:
3000H: FAH
3001H: 28H
After Execution:
3002H: 22H
3003H: 01H
Flowchart:
Start
Add B with A.
Stop
Program:
Address Mnemonics Operand Opcode Remarks
2000 LXI H, 3000H 21 Load H-L pair with address 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 MOV A, M 7E Move the 1st operand from memory to reg. A.
2004 INX H 23 Increment H-L pair.
2005 MOV B, M 46 Move the 2nd operand from memory to reg. B.
2006 ADD B 80 Add B with A.
2007 INX H 23 Increment H-L pair.
2008 MOV M, A 77 Move the result from reg. A to memory.
2009 HLT 76 Halt.
Explanation:
This program adds two operands stored in memory location 3000H and 3001H, without
considering the carry produced (if any).
Let us assume that the operands stored at memory location 3000H is 04H and 3001H is 02H.
Initially, H-L pair is loaded with the address of first memory location.
The first operand is moved to accumulator from memory location 3000H and H-L pair is
incremented to point to next memory location.
The second operand is moved to register B from memory location 3001H.
The two operands are added and the result is stored in accumulator.
H-L pair is again incremented and the result is moved from accumulator to memory location
3002H.
Output:
Before Execution:
3000H: 04H
3001H: 02H
After Execution:
3002H: 06H
Before Execution:
3000: 16 H
3001: 5A H
3002: 9A H
3003: 7C H
After Execution:
3004: B0 H
3005: 76 H
3006: 01 H
8085 Programs
Program 13: Add two 16-bit numbers without considering the carry.
Flowchart:
Start
Stop
Program:
Address Mnemonics Operand Opcode Remarks
2000 LHLD 3000H 2A Load H-L pair with 1st operand from 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 XCHG EB Exchange H-L pair with D-E pair.
2004 LHLD 3002H 2A Load H-L pair with 2nd operand from 3002H.
2005 02 Lower-order of 3002H.
2006 30 Higher-order of 3002H.
2007 DAD D 19 Add D-E pair with H-L pair.
2008 SHLD 3004H 22 Store the result at address 3004H.
2009 04 Lower-order of 3004H.
200A 30 Higher-order of 3004H.
200B HLT 76 Halt.
Explanation:
This program adds two 16-bit operands stored in memory locations 3000H-3001H and
3002H-3003H, without considering the carry produced (if any).
Let us assume that the operands stored at memory locations 3000H-3001H is 02H-04H and
3002H-3003H is 04H-03H.
The H-L pair is loaded with the first 16-bit operand 0204H from memory locations 3000H-
3001H.
Then, the first 16-bit operand is moved to D-E pair.
The second 16-bit operand 0403H is loaded to H-L pair from memory locations 3002H-
3003H.
The two operands are added and the result is stored in H-L pair.
The result is stored from H-L pair to memory locations 3004H-3005H.
Output:
Before Execution: After Execution:
3000H: 02H 3004H: 06H
3001H: 04H 3005H: 07H
3002H: 04H
3003H: 03H
Flowchart:
Start
Load H-L pair with address Increment H-L pair to point to next
of counter’s memory number’s memory location.
location.
Compare B with A.
Increment H-L pair to point to
first number’s memory location.
Decrement counter C.
If No
Counter = 0?
Yes
Stop
Program:
Address Mnemonics Operand Opcode Remarks
2000 LXI H, 3000H 21 Load H-L pair with address 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 MOV C, M 4E Move counter from memory to reg. C.
2004 INX H 23 Increment H-L pair.
2005 MOV A, M 7E Move the 1st number from memory to reg. A.
2006 DCR C 0D Decrement counter.
2007 INX H 23 Increment H-L pair.
2008 MOV B, M 46 Move the next number from memory to reg. B.
2009 CMP B B8 Compare B with A.
200A JNC 200EH D2 Jump to address 200EH if there is no carry.
200B 0E Lower-order of 200EH.
200C 20 Higher-order of 200EH.
200D MOV A, B 78 Move largest from reg. B to reg. A.
200E DCR C 0D Decrement counter.
200F JNZ 2007H C2 Jump to address 2007H if counter is not zero.
2010 07 Lower-order of 2007H.
2011 20 Higher-order of 2007H.
2012 INX H 23 Increment H-L pair.
2013 MOV M, A 77 Move the result from reg. A to memory.
2014 HLT 76 Halt.
Explanation:
This program finds the largest number in an array.
Initially, the counter is initialized with the size of an array.
Then, two numbers are moved to registers A and B, and compared.
After comparison, the largest of two must be in accumulator. If it is already in accumulator,
then its fine, otherwise it is moved to accumulator.
Counter is decremented and checked whether it has reached zero. If it has, the loop
terminates otherwise, the next number is moved to register and compared.
Let us assume that the memory location 3000H stores the counter. The next memory
locations store the array.
Initially, H-L pair is loaded with the address of the counter and is moved to register C.
Then, H-L pair is incremented to point to the first number in the array.
The first number is moved from memory to accumulator and counter is decremented by one.
H-L pair is again incremented and second number is moved to register B.
The two numbers are compared.
After comparison, if A > B, then CF = 0, and if A < B, then CF = 1.
Carry flag is checked for carry. If there is a carry, it means B is greater than A and it is
moved to accumulator.
Counter is decremented and checked whether it has become zero.
If it hasn’t become zero, it means there are numbers left in the array. In this case, the control
jumps back to increment the H-L pair and moves the next number to register B.
This process continues until counter becomes zero, i.e. all the numbers in the array are
compared.
At last, H-L pair is incremented and the largest number is moved from accumulator to
memory.
Output:
Before Execution:
3000H: 05H (Counter)
3001H: 15H
3002H: 01H
3003H: 65H
3004H: E2H
3005H: 83H
After Execution:
3006H: E2H
Flowchart:
Start
Stop
Move the first operand from
memory to accumulator.
Compare B with A.
If
No
Carry?
Yes
Program:
Address Mnemonics Operand Opcode Remarks
2000 LXI H, 3000H 21 Load H-L pair with address 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 MOV A, M 7E Move the 1st operand from memory to reg. A.
2004 INX H 23 Increment H-L pair.
2005 MOV B, M 46 Move the 2nd operand from memory to reg. B.
2006 CMP B B8 Compare B with A.
2007 JNC 200BH D2 Jump to address 200BH if there is no carry.
2008 0B Lower-order of 200BH.
2009 20 Higher-order of 200BH.
200A MOV A, B 78 Move largest from reg. B to reg. A.
200B INX H 23 Increment H-L pair.
200C MOV M, A 77 Move the result from reg. A to memory.
200D HLT 76 Halt.
Explanation:
This program compares the two operands to find the largest out of them.
After comparison, the largest of two must be in accumulator. If it is already in accumulator,
then it is moved to memory.
If it is not in accumulator, then first it is moved to accumulator and then from there, it is
moved to memory.
Let us assume that the operands stored at memory location 3000H is 25H and 3001H is 15H.
Initially, H-L pair is loaded with the address of first memory location.
The first operand is moved to accumulator from memory location 3000H and H-L pair is
incremented to point to next memory location.
The second operand is moved to register B from memory location 3001H.
The two operands are compared.
After comparison, if A > B, then CF = 0, and if A < B, then CF = 1.
Carry flag is checked for carry. If there is a carry, it means B is greater than A and it is
moved to accumulator.
At last, H-L pair is incremented and the largest number is moved from accumulator to
memory location 3002H.
Output:
Before Execution:
3000H: 25H
3001H: 15H
After Execution:
3002H: 25H
Flowchart:
Start
No Is
Move the number from memory counter-1 = 0?
to accumulator.
Yes
Increment H-L pair to point to
next number’s memory location. Stop
Is
No A > M?
Yes
Interchange the numbers.
Decrement counter-2.
No Is
counter-2 = 0?
Yes
Program:
Address Mnemonics Operand Opcode Remarks
2000 MVI B, 05H 06 Initialize counter-1.
2001 05 Immediate value 05H.
2002 MVI C, 05H 0E Initialize counter-2.
2003 05 Immediate value 05H.
2004 LXI H, 3000H 21 Load H-L pair with address 3000H.
2005 00 Lower-order of 3000H.
2006 30 Higher-order of 3000H.
2007 MOV A, M 7E Move the number from memory to reg. A.
2008 INX H 23 Increment H-L pair.
2009 CMP M BD Compare the number with next number.
200A JC 2015H DA Don’t interchange if number < next number.
200B 15 Lower-order of 2015H.
200C 20 Higher-order of 2015H.
200D JZ 2015H CA Don’t interchange if number = next number.
200E 15 Lower-order of 2015H.
200F 20 Higher-order of 2015H.
Otherwise, swap the numbers. Move next
2010 MOV D, M 56
number from memory to D.
2011 MOV M, A 77 Move first number from A to memory.
2012 DCX H 2B Decrement H-L pair.
2013 MOV M, D 72 Move next number from D to memory.
2014 INX H 23 Increment H-L pair.
2015 DCR C 0D Decrement counter 2.
2016 JNZ 2007H C2 If counter-2 ≠ 0, repeat.
2017 07 Lower-order of 2007H.
2018 20 Higher-order of 2007H.
2019 DCR B 05 Decrement counter-1.
201A JNZ 2002 C2 If counter-1 ≠ 0, repeat.
201B 02 Lower-order of 2002H.
201C 20 Higher-order of 2002H.
201D HLT 76 Halt.
Explanation:
This program sorts an array in ascending order.
Let us assume that there are five numbers in the array and its starting address is 3000H.
Initially, counter-1 and counter-2 are initialized with the size of the array.
H-L pair is pointed to the starting address of the array.
In the first iteration, first number is compared with the second number.
If first number < second number, then do not interchange them. Otherwise, if first number >
second number, then swap them.
In the next iteration, first number is compared with the third number.
If first number < third number, then do not interchange them. Otherwise, if first number >
third number, then swap them.
In the next iteration, first number is compared with the fourth number and the process
continues until counter-2 becomes zero.
When counter-2 becomes zero, counter-1 is decremented and the process continues until all
the numbers are arranged in ascending order.
Output:
Before Execution: After Execution:
3000H: 05H 3000H: 01H
3001H: 15H 3001H: 05H
3002H: 01H 3002H: 15H
3003H: 65H 3003H: 32H
3004H: 32H 3004H: 65H
Flowchart:
Start
No Is
Move the number from memory counter-1 = 0?
to accumulator.
Yes
Increment H-L pair to point to
next number’s memory location. Stop
Yes Is
A > M?
No
Interchange the numbers.
Decrement counter-2.
No Is
counter-2 = 0?
Yes
Program:
Address Mnemonics Operand Opcode Remarks
2000 MVI B, 05H 06 Initialize counter-1.
2001 05 Immediate value 05H.
2002 MVI C, 05H 0E Initialize counter-2.
2003 05 Immediate value 05H.
2004 LXI H, 3000H 21 Load H-L pair with address 3000H.
2005 00 Lower-order of 3000H.
2006 30 Higher-order of 3000H.
2007 MOV A, M 7E Move the number from memory to reg. A.
2008 INX H 23 Increment H-L pair.
2009 CMP M BD Compare the number with next number.
200A JNC 2015H D2 Don’t interchange if number > next number.
200B 15 Lower-order of 2015H.
200C 20 Higher-order of 2015H.
200D JZ 2015H CA Don’t interchange if number = next number.
200E 15 Lower-order of 2015H.
200F 20 Higher-order of 2015H.
Otherwise, swap the numbers. Move next
2010 MOV D, M 56
number from memory to D.
2011 MOV M, A 77 Move first number from A to memory.
2012 DCX H 2B Decrement H-L pair.
2013 MOV M, D 72 Move next number from D to memory.
2014 INX H 23 Increment H-L pair.
2015 DCR C 0D Decrement counter 2.
2016 JNZ 2007H C2 If counter-2 ≠ 0, repeat.
2017 07 Lower-order of 2007H.
2018 20 Higher-order of 2007H.
2019 DCR B 05 Decrement counter-1.
201A JNZ 2002 C2 If counter-1 ≠ 0, repeat.
201B 02 Lower-order of 2002H.
201C 20 Higher-order of 2002H.
201D HLT 76 Halt.
Explanation:
This program sorts an array in descending order.
Let us assume that there are five numbers in the array and its starting address is 3000H.
Initially, counter-1 and counter-2 are initialized with the size of the array.
H-L pair is pointed to the starting address of the array.
In the first iteration, first number is compared with the second number.
If first number > second number, then do not interchange them. Otherwise, if first number <
second number, then swap them.
In the next iteration, first number is compared with the third number.
If first number > third number, then do not interchange them. Otherwise, if first number <
third number, then swap them.
In the next iteration, first number is compared with the fourth number and the process
continues until counter-2 becomes zero.
When counter-2 becomes zero, counter-1 is decremented and the process continues until all
the numbers are arranged in descending order.
Output:
Before Execution: After Execution:
3000H: 05H 3000H: 65H
3001H: 15H 3001H: 32H
3002H: 01H 3002H: 15H
3003H: 65H 3003H: 05H
3004H: 32H 3004H: 01H
Flowchart:
Start
Add B with A.
Decrement register C.
No Is
C = 0?
Yes
Increment H-L pair.
Program:
Address Mnemonics Operand Opcode Remarks
2000 LXI H, 3000H 21 Load H-L pair with address 3000H.
2001 00 Lower-order of 3000H.
2002 30 Higher-order of 3000H.
2003 MOV B, M 46 Move the 1st operand from memory to reg. B.
2004 INX H 23 Increment H-L pair.
2005 MOV C, M 4E Move the 2nd operand from memory to reg. C.
2006 MVI A, 00H 3E Initialize accumulator with 00H.
2007 00 Immediate value 00H.
2008 ADD B 80 Add B with A.
2009 DCR C 0D Decrement reg. C (counter).
200A JNZ 2008H C2 Jump back to address 2008H if C ≠ 0.
200B 08 Lower-order of 2008H.
200C 20 Higher-order of 2008H.
200D INX H 23 Increment H-L pair.
200E MOV M, A 77 Move the result from accumulator to memory.
200F HLT 76 Halt.
Explanation:
This program multiplies two operands stored in memory location 3000H and 3001H, using
successive addition method.
In successive addition method, the second operand is considered as counter, and the first
number is added with itself until counter decrements to zero.
Let us assume that the operands stored at memory location 3000H is 02H and 3001H is 05H.
Then, by using successive addition method, we get 02H + 02H + 02H + 02H + 02H = 0AH.
Initially, H-L pair is loaded with the address of first memory location.
The first operand is moved to register B from memory location 3000H and H-L pair is
incremented to point to next memory location.
The second operand is moved to register C from memory location 3001H to act as counter.
Accumulator is initialized to 00H.
Register B is added with accumulator and the result is stored in accumulator.
Register C (counter) is decremented by 1.
Then, counter is checked for zero. If it hasn’t become zero yet, then register B is again added
with accumulator, and counter is again checked for zero.
If counter becomes zero, then H-L pair is incremented and the result is moved from
accumulator to memory location 3002H.
Output:
Before Execution:
3000H: 02H
3001H: 05H
After Execution:
3002H: 0AH
Flowchart:
Start
Initialize counter C to
number of bytes.
Increment destination
pointer D-E pair.
Decrement counter C.
Is No
counter = 0?
Yes
Stop
Program:
Address Mnemonics Operand Opcode Remarks
2000 MVI C, 05H 0E Initialize reg. C to 05H, i.e. number of bytes.
2001 05 Immediate value 05H
2002 LXI H, 3000H 21 Initialize H-L pair to source memory location.
2003 00 Lower-order of 3000H.
2004 30 Higher-order of 3000H.
Initialize D-E pair to destination memory
2005 LXI D, 3500H 11
location.
2006 00 Lower-order of 3500H.
2007 35 Higher-order of 3500H.
2008 MOV A, M 7E Move the byte from source to accumulator.
2009 STAX D 12 Store the byte from accumulator to destination.
200A INX H 23 Increment the source pointer H-L pair.
200B INX D 13 Increment the destination pointer D-E pair.
200C DCR C 0D Decrement counter C.
200D JNZ 2008H C2 Jump to address 2008H if counter is not zero.
200E 08 Lower-order of 2008H.
200F 20 Higher-order of 2008H.
2010 HLT 76 Halt.
Explanation:
This program transfers block of N-bytes from source to destination. The source bytes start
from memory location 3000H and needs to be transferred to memory locations 3500H
onwards.
In order to transfer these bytes, first the counter must be initialized to the number of bytes to
transfer.
Then, H-L pair is initialized to point to the source memory location and D-E pair is initialized
to point to destination memory location.
The first byte is moved from source to accumulator and then from there to destination.
The H-L pair and D-E pair are incremented to point to the next respective memory locations.
The counter is decremented and checked whether it has become zero.
If it hasn’t become zero, it means that there are bytes remaining to be transferred. In this case,
the control jumps back to move the next byte from source to destination.
This process continues until counter becomes zero, i.e. all the bytes have been transferred.
Output:
Before Execution: After Execution:
3000H: 05H 3500H: 05H
3001H: 02H 3501H: 02H
3002H: 04H 3502H: 04H
3003H: 03H 3503H: 03H
3004H: 02H 3504H: 02H