Gray code: Difference between revisions

Added solution for EDSAC
(Add BASIC)
(Added solution for EDSAC)
Line 2,273:
[i, IntToBin(i, 5), IntToBin(g, 5), IntToBin(d, 5), d]));
end;</syntaxhighlight>
 
=={{header|EDSAC order code}}==
The only logical operation on EDSAC was AND, or "collate" as it was called, but it's possible to calculate XOR from AND together with arithmetical operations. For converting Gray code to binary on EDSAC, I couldn't think up any shorter method than the one below.
<syntaxhighlight lang="edsac">
[Gray code task for Rosetta Code.
EDSAC program, Initial Orders 2.]
 
[Library subroutine M3. Prints header at load time,
then M3 and header are overwritten.]
PFGKIFAFRDLFUFOFE@A6FG@E8FEZPF
*BINARY!!GRAY!!!!ROUND!TRIP@&
..PK [after header, blank tape and PK (WWG, 1951, p. 91)]
 
T64K [load at location 64 (arbitrary choice)]
GK [set @ (theta) parameter]
[Subroutine to print 5-bit number in binary.
Input: 1F = number (preserved) in low 5 bits.
Workspace: 0F, 4F.]
[0] A3F T17@ [plant return link as usual]
H19@ [mult reg := mask to remove top 4 bits]
A1F [acc := code in low 5 bits]
L32F [shift 7 left]
TF [store in workspace]
S18@ [initialize negative count of digits]
[7] T4F [update negative count]
AF LD TF [shift workspace 1 left]
CF [remove top 4 bits]
TF [store result]
OF [print character '0' or '1' in top 5 bits]
A4F A2F G7@ [inc count, loop if not yet 0]
[17] ZF [{planted} jump back to caller]
[18] P5F [addres field = number of bits]
[19] Q2047D [00001111111111111 binary]
 
[Subroutine to convert binary code to Gray code.
Input: 1F = binary code (preserved).
Output: 0F = Gray code.]
[20] A3F T33@ [plant return link as usual]
A1F RD TF [0F := binary shifted 1 right]
[One way to get p XOR q on EDSAC: Let r = p AND q.
Then p XOR q = (p - r) + (q - r) = -(2r - p - q).]
HF [mult reg := 0F]
C1F [acc := 0F AND 1F]
LD [times 2]
SF S1F [subtract 0F and 1F]
TF SF TF [return result negated]
[33] ZF [{planted} jump back to caller]
 
[Subroutine to convert 5-digit Gray code to binary.
Uses a chain of XORs.
If bits in Gray code are ghijk then bits in binary are
g, g.h, g.h.i, g.h.i.j, g.h.i.j.k where dot means XOR.
Input: 1F = Gray code (preserved).
Output: 0F = binary code.
Workspace: 4F, 5F.]
[34] A3F T55@ [plant return link as usual]
A1F UF [initialize result to Gray code]
T5F [5F = shifted Gray code, shift = 0 initialiy]
S56@ [initialize negative count]
[40] T4F [update negative count]
HF [mult reg := partial result]
A5F RD T5F [shift Gray code 1 right]
[Form 5F XOR 0F as in the previous subroutine]
C5F LD SF S5F TF SF
TF [update partial result]
A4F A2F G40@ [inc count, loop back if not yet 0]
[55] ZF [{planted} jump back to caller]
[56] P4F [address field = 1 less than number of bits]
 
[Main routine]
[Variable]
[57] PF [binary code is in low 5 bits]
[Constants]
[58] P16F [exclusive maximum code, 100000 binary]
[59] PD [17-bit 1]
[60] #F [teleprinter figures mode]
[61] !F [space]
[62] @F [carriage return]
[63] &F [line feed]
[Enter with acc = 0]
[64] O60@ [set teleprinter to figures]
S58@ [to make acc = 0 after next instruction]
[66] A58@ [loop: restore acc after test below]
U57@ T1F [save binary code, and pass it to print soubroutine]
[69] A69@ G@ [print binary code]
O61@ O61@ O61@ [print 3 spaces]
[74] A74@ G20@ [convert binary (still in 1F) to Gray]
AF T1F [pass Gray code to print subroutine]
[78] A78@ G@ [print Gray code]
O61@ O61@ O61@ [print 3 spaces]
[83] A83@ G34@ [convert Gray (still in 1F) back to binary]
AF T1F [pass binary code to print subroutine]
[87] A87@ G@ [print binary]
O62@ O63@ [print CR, LF]
A57@ A59@ [inc binary]
S58@ [test for all done]
G66@ [loop back if not]
O60@ [dummy character to flush teleprinter buffer]
ZF [stop]
E64Z [define entry point]
PF [acc = 0 on entry]
[end]
</syntaxhighlight>
{{out}}
<pre>
BINARY GRAY ROUND TRIP
00000 00000 00000
00001 00001 00001
00010 00011 00010
00011 00010 00011
00100 00110 00100
00101 00111 00101
00110 00101 00110
00111 00100 00111
01000 01100 01000
01001 01101 01001
01010 01111 01010
01011 01110 01011
01100 01010 01100
01101 01011 01101
01110 01001 01110
01111 01000 01111
10000 11000 10000
10001 11001 10001
10010 11011 10010
10011 11010 10011
10100 11110 10100
10101 11111 10101
10110 11101 10110
10111 11100 10111
11000 10100 11000
11001 10101 11001
11010 10111 11010
11011 10110 11011
11100 10010 11100
11101 10011 11101
11110 10001 11110
11111 10000 11111
</pre>
 
=={{header|Elixir}}==
113

edits