Problem – Write an assembly language program to count the number of ones in contents of register B and store the result at memory location 3050.
Example –
Algorithm –
- Convert the decimal number in Accumulator to its binary equivalent
- Rotate the digits of the binary number right without carry
- Apply a loop till count is not zero to change the values of D register and count
- Copy the value of D register to accumulator and store the result
Program –
MEMORY ADDRESS | MNEMONICS | COMMENTS |
---|---|---|
2000 | MVI B 75 | B ← 75 |
2002 | MVI C 08 | C ← 75 |
2004 | MVI D 00 | D ← 00 |
2006 | MOV A, B | A ← B |
2007 | RRC | Rotate right without carry |
2008 | JNC 200C | Jump if Not Carry |
200B | INR D | D ← D+1 |
200C | DCR C | C ← C-1 |
200D | JNZ 2007 | Jump if Not Zero |
2010 | MOV A, D | A ← D |
2011 | STA 3050 | A → 3050 |
2014 | HLT | Stops execution |
Explanation –
- MVI B 75 moves 75 decimal number into B register
- MVI C 08 moves 08 decimal number into C register, which is taken as counter as the number is of 8 bites
- MVI D 00 moves 00 number into d register
- MOV A, B moves the contents of B register into A (accumulator) register
- RRC rotates the contents of A (which is 75 with binary equivalent 01110101) right
- JNC 200C jumps to 200C address and perform the instructions written there if the carry flag is not zero
- INR D increases the value of D register by adding one to its contents
- DCR C decreases the value of C register by subtracting one from its contents
- JNZ 2007 jumps to 2007 address and perform the instructions written there if the zero flag is not zero
- MOV A, D moves the contents of B register into A register
- STA 3050 store the contents of A at 3050 memory location
- HLT stops execution
leave a comment
0 Comments