RISC-V Assembly Language - Gray Code

Use 32-bit or 64-bit architecture.

Definition: Gray Code

Gray code, or reflected binary code, is a binary numbering system where two consecutive values differ in only one bit. Named after researcher Frank Gray, it stops digital and mechanical systems from reading wrong intermediate values when numbers change.

In digital electronics, a Gray code counter is a specialized circuit where only one bit changes state with every clock pulse. Unlike standard binary counters—where multiple bits can flip simultaneously (e.g., 011 to 100 flips three bits)—a Gray code counter eliminates temporary glitches, race conditions, and high dynamic power consumption during state transitions.

Key Properties:

Project 14a

What is a gray code and how (and why) is it used? (In your own words.)

Project #14b

Develop and test three functions:

  1. binary to gray code converter
  2. gray code to binary converter
  3. gray code counter

Create a reasonable amount of test test data.

Print the input and output and make it "pretty".

links

Gray code (Wikipedia)

How To Convert Gray Code to Binary and Binary to Gray Code (YouTube)

Gray Code (Geeks for Geeks)

Decimal, Binary, Gray

DecimalBinaryGray DecimalBinaryGray
000000000 810001100
100010001 910011101
2001000111010101111
3001100101110111110
4010001101211001010
5010101111311011011
6011001011411101001
7011101001511111000

Note: For decimal 15 the code rolls over to decimal 0 with only one switch change. This is called the cyclic or adjacency property of the graycode. (Wikipedia)