Binary <-> Gray Conversion in 8085 Assembly

Binary <-> Gray Conversion in 8085 Assembly

Published by Arun Isaac on

Tags: 8085, assembly

…a truly awesome implementation of gray to binary conversion in 8085 assembly language

This is regarding a truly awesome implementation of gray to binary conversion in 8085 assembly language. I had Microprocessor and Microcontroller Laboratory the next day and was supposed to be implementing several code conversion programs one of which was binary to gray and gray to binary conversion. Owing to lack of sleep and a general attachment towards elegant code, I was totally pissed off with the long, complicated and messed up codes in circulation in the class. And like I always do when I am literally pulling my hair out on some problem, I went to my friend Sudhakar who came up with this miracle. All credit to him. Though this algorithm may already be in existence, I thought it was worth putting up.

So, as a preamble, here is the binary to gray conversion code. As can be seen, this is more or less straight forward.

MVI B, 5Fh      ;Move input binary data to B
MOV A, B        ;Copy data to Accumulator
RRC             ;Rotate right 
ANI 7Fh         ;Reset MSB to preserve MSB in exorred result
XRA B           ;EXOR rotated data with unrotated data
STA 4300h       ;Store result
HLT

And now, the gray to binary conversion code. On observation, you may notice that it is a very elegant digital implementation of a long division algorithm.

        MVI C, 07h      ;Set counter C to no of bits-1 (8-1=7)
        MVI B, 80h      ;Load gray data to B
        MOV A, B        ;Copy data to Accumulator
        ANI 80h         ;Copy MSB alone
loop:   RRC             ;Rotate right
        ANI 7Fh         ;Reset MSB to preserve MSB in exorred result
        XRA B           ;EXOR rotated data with unrotated data
        DCR C           ;Decrement counter
        JNZ loop        ;loop
        STA 4300h       ;Store result
        HLT