0

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.

FlatAssembler
  • 667
  • 7
  • 30
  • What inputs are you testing with? What actual results do you get? That's an important part of a [mcve]. – Peter Cordes Nov 12 '20 at 12:15
  • @PeterCordes OK, I've included an example. – FlatAssembler Nov 12 '20 at 12:20
  • 1
    `3 ^ (3 << 1)` is 5, (`0b011 ^ 0b110 = 0b101`) so your output matches your instructions (if I'm guessing the mnemonic meanings correctly). Apparently your formula for gray -> binary is wrong, nothing to do with asm, just the algorithm. Google and find a correct algorithm. – Peter Cordes Nov 12 '20 at 12:32

1 Answers1

0

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
FlatAssembler
  • 667
  • 7
  • 30