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

Class Point:: It holds co-ordinates of a point in format (x,y)

A class Named "Point" which stores the Co-ordinates of a point in (x,y) Format. This Class Contains:: O Constructor with default arguments O Copy Constructor O Function to display which Co-ordinate a Point belongs to O Function to get value of x of a point O Function to get value of y of a point O Function to get the Radious Vector (r) of a point O Function to get the Theta of a point O Overloaded Operator - to find out distance between two points O Overloaded Operator >> to take input O Overloaded operator O Destructor #include<iostream.h> #include<math.h> class point{ //Data Members int x,y; //They hold the value of X and Y co-ordinate of a Point. public: //Member Functions point(int t1=0, int t2=0) //Constructor with default arguments. { x=t1; y=t2; } point(point &t) //Copy Constructor { x=t.x; y=t.y; } int coordinate(void); //It returns the point's Co-ordinate, e.g., 1, 2, 3, or 4th. int getx(...

0s and 1s Count in 1 Byte Memory Word

This program will count all the 1s and 0s individualy presents in a 1 byte word. And store the result in individual places in Memory. Suppose, argument byte is stored in memory location 2050H and store the result of 1's count at 2060H memory location and 0's at 2061H Memory Location Hex Code Label Mnemonics Comment 3050 3051 3052 3A 50 20 START: LDA 2050H ;Load Accumulator with the number stored at location 2050H 3053 3054 06 00 MVI B, 00H ;Initiate 0's Counter 3055 3056 0E 00 MVI C, 00H ;Initiate 1's Counter 3057 3058 16 08 MVI D, 08H ;Initiate Bit Counter 3059 07 LOOP: RLC ;Rotate Accumulator Left with Carry 305A 305B 305C DA 61 30 JC ONE ;If 1 is found count 1 305D 04 INR B ;If 1 is not found it must be 0. So increase 0's Counter 305E 305F 3060 D2 62 30 JNC NEXT ;Go to next Bit 3061 0C ONE: INR C ;Increase 1's Counter 3062 15 NEXT: DCR D ;Decrease the Bit Counter 3063 3064 3065 C2 ...