Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assignment 1, 8085 Microprocessor

Problem: ​Find the sum of all the 32 bit odd numbers in an array using the 8085 assembly
language.
Algorithm for Checking an Odd number
Any decimal number may be represented in its equivalent binary number, the number may be
decomposed as the sum of the powers of 2 the presence of an exponent depending on where the
corresponding bit is a 1 or a 0, as 2 is even and all its powers 2, 4, 8, 16, 32 and so on are also
even except 2​0 which is odd and will contribute only if the last bit (LSB) is 1. If the LSB is 0
then we have a sum of even powers of 2 which because the sum of two even numbers is always
even will be even, hence our number is even. And if the LSB is 1 then we have a sum of even
powers of 2 (the sum of which will be even) and a 1 which must also be added, now since 1 is
odd and the sum of the other contributing terms is even therefore the sum will be odd and hence
our number will be odd.
Some examples are
156 = (0000 0000 1001 1100)​2​ Last bit is 0 therefore even
157 = (0000 0000 1001 1101)​2​ Last bit is 1 therefore odd
158 = (0000 0000 1001 1110)​2​ Last bit is 0 therefore even
159 = (0000 0000 1001 1111)​2​ Last bit is 1 therefore odd
To check whether the last bit is 1 or 0 we can do bitwise AND of the number with (0000 0000
0000 0001)​2 and check if the result is 1 then the number is odd and if the result is 0 then the
number is even.
An example
5 AND 1 = 101 AND 001
= 001 (the result is 1 as 5 is odd)
4 AND 1 = 100 AND 001
= 000 (the result is 0 as 4 is even)
Mnemonics
HL stores the starting address of the array starting from 5000H, each 32 bit number takes up 4
bytes of data and E register is used to store the number of bytes taken by each element of the
array.

5000H 5001H 5002H 5003H

From 5000H to 5003H one number is stored then from 5004H to 5007H another number is
stored and so on.
D register stores the number of elements in the array.
BC pair stores the starting address of the memory starting from 50A0H, where the answer will be
stored after executing the program.
The memory at the address 5050H is used to store the number of odd numbers present in the
array.
Mnemonics
1. LXI H,5000H
2. MVI D,05H
3. MVI E,04H
4. LXI B,50A0H
5. MVI A,00H
6. STA 5050H
7.
8. L1: MOV A,M
9. ANI 01H
10. JZ L2
11. LDA 5050H
12. INR A
13. STA 5050H
14. LDAX B
15. ADD M
16. STAX B
17. INX H
18. INX B
19. LDAX B
20. ADC M
21. STAX B
22. INX H
23. INX B
24. LDAX B
25. ADC M
26. STAX B
27. INX H
28. INX B
29. LDAX B
30. ADC M
31. STAX B
32. INX H
33. INX B
34. JNC L4
35. MVI A,01H
36. STAX B
37. JMP L4
38.
39. L2: MOV A,L
40. ADD E
41. JNC L3
42. INR H
43. L3: MOV L,A
44.
45. L4: LXI B,50A0H
46. MVI E,04H
47. DCR D
48. JNZ L1
49.
50. HLT
The output for the inputs

1 2 3 4 5

10D87H 269EH B279H 3FFH 7FFFFFFFH

68999D 9886D 45689D 1023D 2147483647D

There are 4 odd numbers and the result at 5050H address is also 4, the sum of the odd numbers is
8001C3FEH which is also the answer at the address from 50A0H.

You might also like