This program will work out the 2's Complement of a 2 Byte Number. As Microprocessor can perform 8 bits at a time in Accumulator, so we follow the foloowing Algo
Suppose, Argument Number is stored in Location 1000H and 1001H Memory Location and store the value in 2000H and 2001H Memory Locations. |
||||
Memory Location | Hex Code | Label | Mnemonics | Comment |
3050 3051 3052 | 2A 00 10 | START: | LHLD 1000H | ;Load the HL Register Pair with the content of Memory Location 1000H and 1001H |
3053 | 7D | MOV A, L | ;Move the Content of L to A (Accumulator) | |
3054 | 2F | CMA | ;Make Complement of Least Significant Byte of the Number | |
3055 3056 | C6> 01 | ADI 01H | ;Add 1 with LSByte of the number to make 2's complement | |
3057 | 6F | MOV L, A | ;Send Back the Updated Data to L | |
3058 | 7C | MOV A, H | ;Bring the MSByte of the Number in A | |
3059 | 2F | CMA | ;Make 1's Complement of MSByte of Number | |
305A 305B 305C | D2 5F 30 | JNC NOCARRY | ;If Carry was not generated in LSByte Operation, Jump into NOCARY Section | |
305D 305E | C6 01 | ADI 01H | ;Make 2's Complement of MSByte if carry was generated in LSByte operation | |
305F | 67 | NOCARRY: | MOV H, A | ;Send back the Updated MSByte to H |
3060 3061 3062 | 22 00 20 | SHLD 2000H | ;Store the Result from HL to the Memory Location 2000H | |
3063 | 76 | HLT | ;End of Program |
If you want to use INR A to add 1 with 1's Complement to make 2's Complement, you should use at the time of increamention of MSByte not at LSByte. Because INR do not update CY(Carry) Flag if carry is generated.
You can use the following algo also to do the same.
- Fetch the 2 Byte Number in a Register Pair
- Bring the Least Significant Byte into A
- Make Complement of A
- Store back to Register
- Bring the MSByte into A
- Make Complement of A
- Store Back to Register
- increase this Resgister pair using INX Rp
- Store the Result in Memory
Comments