从二进制转换为灰色在汇编中有效,但反之则不然

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

我正在尝试为 PicoBlaze 构建一个程序,它将格雷码转换为二进制,反之亦然。这是我到目前为止所做的:

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

因此,从二进制到格雷的转换似乎可行。但是,从 Gray 到 binary 的转换不起作用。例如,对于输入:

2
3

我期望输出:

3
2

但是,我得到了输出:

3
5

这是怎么回事?我正在 PicoBlaze Simulator.

测试我的程序

好的,这似乎有效:

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