assemblygray-codepicoblaze

Converting from binary to gray works in assembly, but not vice versa


I am trying to build a program for PicoBlaze which will translate from Gray code to binary and vice versa. Here is what I've made thus far:

address 0
start: ;Infinite loop...
;Converting from binary to gray...
constant binary_input,0
constant gray_output,0
input s0,binary_input
load s1,s0
sr0 s1
xor s1,s0
output s1,gray_output ;Seems to work.
;Converting from gray to binary...
constant gray_input,1
constant binary_output,1
input s0,gray_input
load s1,s0
sl0 s1
xor s1,s0
output s1,binary_output ;Does not work.
jump start

So, the conversion from binary to Gray seems to work. However, the conversion from Gray to binary doesn't work. For example, for the input:

2
3

I expect the output:

3
2

However, I get the output:

3
5

What is going on here? I am testing my program in PicoBlaze Simulator.


Solution

  • OK, this seems to work:

    address 0
    start: ;Infinite loop...
      ;Converting from binary to gray...
      constant binary_input,0
      constant gray_output,0
      input s0,binary_input
      load s1,s0
      sr0 s1
      xor s1,s0
      output s1,gray_output
      ;Converting from gray to binary...
      constant gray_input,1
      constant binary_output,1
      input s0,gray_input
      load s1,s0
      convert_to_binary_loop:
        sr0 s1
        xor s0,s1
        compare s1,0
      jump nz,convert_to_binary_loop
      output s0,binary_output
    jump start