Gray code and optimization software
Lord Vader
I. What is it
Gray code commonly used in those cases
cases in which the measured parameter changes
by 1 sequentially in time, the NAM
Example, a mechanical valkoderah, some
ADC, etc. A classic example of the use
of Gray code - the signals from the optocouplers mechanical
cally mouse, having the form
X: _/~~~___/~~~_
Xq: ___/~~~___/~~~
and is a 2-bit Gray code.
The main property of the Gray code - in the transition
progress to the next state in the N-bit ve
The quantity in the Gray code changes only one bit.
For example, for a 4-bit value sequence
sequence of Gray codes is as follows:
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Hence it is clear the rules for codes
Gray: at each step you have to change the most
LSB, which will not repeat
Niya code.
It is also possible, and directly re
Led the usual binary code in the Gray code and
ago. Translated from the usual code in Gray code
(Examples in Assembler Z80) for 8-bit
quantity:
Bin2Gray
; IN: A - in binary code
; OUT: A - in Gray code
LD C, A
SRL C
XOR C
RET
Translated back:
Gray2Bin
; IN: A - in Gray code
; OUT: A - in binary code
LD C, A
XOR A
G2B_L
XOR C
SRL C
JR NZ, G2B_L
RET
More details about the Gray code may be other
a thief in [1] and [2].
II. Examples of optimization
Imagine that you want napecha
thief character 8x8 in a linear screen buffer
(Where the lines go to each other and for ne
transition to the next should be added to the address
32).
Code is received as follows:
DUP 8
LD A, (DE)
INC E
LD (HL), A
ADD HL, BC; BC = 32
EDUP
For the addition of 32 required chains already
register a pair of barking. You can do this:
LD A, L
ADD A, 32
LD L, A
But either it is quite slow, or triangle
requires additional registers to store
number 32.
Now try to sort out the address in
Gray code:
LD A, (DE)
INC E; SET 0, E
LD (HL), A
SET 5, L; 000> 001
LD A, (DE)
SET 1, E
LD (HL), A
SET 6, L; 001 011
LD A, (DE)
DEC E; RES 0, E
LD (HL), A
RES 5, L; 011 010
LD A, (DE)
SET 2, E
LD (HL), A
SET 7, L; 010> 110
LD A, (DE)
INC E
LD (HL), A
SET 5, L; 110 111
LD A, (DE)
RES 1, E
LD (HL), A
RES 6, L; 111 101
LD A, (DE)
DEC E
LD (HL), A
RES 5, L; 101> 100
LD A, (DE)
LD (HL), A; 100
Thus, we at least got rid of
the use of additional registers
adding nearly beat to every copy
(Instead of INC E: ADD HL, BC - 2 SET / RES 'a).
If you also change the font according Baitik
tstvuyuschim way, you can always write
INC E - winning is obvious.
A similar technique can be used
in other cases (no - he think:).
For the convenience of writing we can introduce two maximal
dew (example for the Arab assembler aka
Al-asma;).
MACRO GRAY
INC E
SET 5, L
SET 1, E
SET 6, L
DEC E
RES 5, L
SET 2, E
SET 7, L
INC E
SET 5, L
RES 1, E
RES 6, L
DEC E
RES 5, L
ENDM
MACRO DOIT
LD A, (DE)
LD (HL), A
ENDM
And further:
GRAY DOIT
lvd ^ mHm lvd@dgap.mipt.ru
Literature:
1. P. Horowitz, W. Hill.
"The Art of circuitry," Chapter 8.
2. http://en.wikipedia.org/wiki/Gray_code