Gray code
Karnaugh maps
Create functions to encode a number to and decode a number from Gray code.

Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary).

There are many possible Gray codes. The following encodes what is called "binary reflected Gray code."

Encoding (MSB is bit 0, b is binary, g is Gray code):

if b[i-1] = 1
   g[i] = not b[i]
else
   g[i] = b[i]
Or:

g = b xor (b logically right shifted 1 time)
Decoding (MSB is bit 0, b is binary, g is Gray code):

b[0] = g[0]

for other bits:
b[i] = g[i] xor b[i-1]
Reference
 Converting Between Gray and Binary Codes. It includes step-by-step animations.
		org	100h
		xra	a	; set A=0
loop:		push	psw	; print number as decimal
		call	decout
		call	padding	; print padding
		pop	psw
		push	psw
		call	binout	; print number as binary
		call	padding
		pop	psw
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		mov	b,a	; gray encode
		ana	a	; clear carry
		rar		; shift right
		xra	b	; xor the original
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		push	psw
		call	binout	; print gray number as binary
		call	padding
		pop	psw
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		mov	b,a	; gray decode
decode:		ana	a	; clear carry
		jz	done	; when no more bits are left, stop
		rar		; shift right
		mov	c,a	; keep that value
		xra	b	; xor into output value
		mov	b,a	; that is the output value
		mov	a,c	; restore intermediate
		jmp	decode	; do next bit
done:		mov	a,b	; give output value
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		push	psw
		call	binout	; print decoded number as binary
		call	padding
		pop	psw
		push	psw
		call	decout	; print decoded number as decimal
		lxi	d,nl 
		call	strout
		pop	psw
		inr	a	; next number 
		ani	1fh	; are we there yet?
		jnz	loop	; if not, do next number
		ret
		;; Print A as two-digit number 
decout:		mvi	c,10
		call	dgtout
		mvi	c,1
dgtout:		mvi	e,'0' - 1
dgtloop:	inr	e
		sub	c
		jnc	dgtloop
		add	c
		push	psw
		mvi	c,2
		call	5
		pop	psw
		ret
		;; Print A as five-bit binary number
binout:		ani	1fh
		ral
		ral
		ral
		mvi	c,5
binloop:	ral
		push	psw
		push	b
		mvi	c,2 
		mvi	a,0
		aci	'0'
		mov	e,a
		call	5
		pop	b
		pop	psw
		dcr	c
		jnz	binloop
		ret
		;; Print padding
padding:	lxi	d,arrow
strout:		mvi	c,9
		jmp	5		
arrow:		db	' ==> $'
nl:		db	13,10,'$'