Skip to main content

2's Complement of 2Byte Number

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

  1. Fetch the 2 Byte Number in a Register Pair
  2. Bring Least Significant Byte of the Number from last Register into A (Accumulator)
  3. Make Complement of A and add 1
  4. Store back the Updated LSByte into its source Register
  5. Bring MSByte of the Number from another Register into A
  6. Make Complement of A and add 1 if carry was genetrated at the time of LSByte opeartion
  7. Store back the Updated MSByte into its source Register
  8. Store the Result to a Memory location

Suppose, Argument Number is stored in Location 1000H and 1001H Memory Location and store the value in 2000H and 2001H Memory Locations.

Memory LocationHex CodeLabelMnemonicsComment
3050
3051
3052
2A
00
10
START:LHLD 1000H;Load the HL Register Pair with the content of Memory Location 1000H and 1001H
30537DMOV A, L;Move the Content of L to A (Accumulator)
30542FCMA;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
30576FMOV L, A;Send Back the Updated Data to L
30587CMOV A, H;Bring the MSByte of the Number in A
30592FCMA;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
305F67NOCARRY: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
306376HLT;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.

  1. Fetch the 2 Byte Number in a Register Pair
  2. Bring the Least Significant Byte into A
  3. Make Complement of A
  4. Store back to Register
  5. Bring the MSByte into A
  6. Make Complement of A
  7. Store Back to Register
  8. increase this Resgister pair using INX Rp
  9. Store the Result in Memory

Comments

Popular posts from this blog

Flip-Flop Conversion

Flip-Flop is a memory element. You can say a basic element. It is a circuit that can hold a binary state (0 or 1 as low or high volts) until an input to change that state. We have many flip-flops as SR Flip-Flop (Set Reset) D Flip-Flop (Data) JK Flip-Flop T Flip-Flop (Toggle) Those Flip-Flops are available in market in IC Package Form. Now You have a circuit that uses 1 JK and 1 D Flip-Flop and 3 NOT Gates. So you have to buy three ICs- one for JK, one for D and other for NOT Gate. Ultimately you will see that a flip flop in IC of JK, a flip-flop in D IC and 3 NOT gates are not used. Now, if you convert a JK flip flop into D flip flop then you didn’t have to pay for the D flip flop IC. So, your circuit will be made by 2 ICs in place of 3 and in cheap cost. This article is for that reason. How the Flip-Flops are converted from one to other. Procedure: 1. Write Qt and Qt+1 Table with 4 combination of 0 and 1. 2. Write columns for inputs of destination Flop-Fl op. 3. Write the ce

How to set Set-Top Box to Resume automatically to last Channel after switch on?

How to set customised automatic Resume Channel to last viewed in Set-Top Box?  Introduction In the era of Digitally fast growing life WiFi enabled smart devices and also voice controlled smart devices are trying to be companion of every human being. But, all human do not change their habit at the same time. It may be cause of mental attachment or financial limits or unwillingness or though as unnecessary. What ever the reason may be, there exists the old technology, like IR(Infrared), RF(Radio Frequency), etc. So, this article for them who are using "Un-smart" TV like me in this time of Smart World. I was searching for a solution to set my Set-top Box of Videocon DTH (controlled by a RF Remote) to resume the last viewed channel when I power on my TV anytime. Suppose, I was watching NAT-Geo and switch off my (Un-smart) TV, even switch off the plug power at board. Now, after some time / on the next day, when I switch on my TV it will directly tune to the last viewed channel, i.