Aim To write a program to convert an 8-bit binary number to equivalent gray code.

Algorithm
1. The MSB in the gray code is the same as the corresponding bit in a binary number.
2. Going from left to right, add each adjacent pair of binary digits to get the next gray code digit. Disregard carries.

Program

4100: 90 42 00              MOV DPTR,#4200H            ; Address of the number
4103: E0                         MOVX A,@DPTR                  ; Keep number in A
4104: C3                        CLR C                                     ; Clear carry for rotation
4105: 13                        RRC A                                    ; Rotate through carry
4106: F9                        MOV R1,A                              ; Rotated number in R1
4107: E0                        MOVX A,@DPTR                   ; Number again in A
4108: 69                        XRL A,R1             ; XOR rotated and original numbers
4109: A3                        INC DPTR
410A:F0                         MOVX @DPTR,A                   ; Store result in 4201
410B: 80 FE          HLT: SJMP HLT(410B)

Procedure
1. Enter the program in memory starting from 4100H.
2. Enter the number in memory location 4200H.
3. Execute the program and verify the result in 4201H.

Test data
Input: Output
4200: 90 4201: D8

0 comments