# byte mirror

[ input = 1 byte ; value to be mirrored
result = 1 byte ; mirrored byte ]

org &hc000
xor a : ld (result),a
ld c,1 : ld d,128 : ld b,8
start ld a,(input)
and c
jr z,after
ld a,(result) : add a,d : ld (result),a
after sla c : srl d :djnz start
ret

Hallo ,

I coded this routine as it come up to my mind, but

I need a simplier and faster routine to mirror a byte.

when I say Mirror i mean for example

input = &b11000000 result = &b00000011
input = &b00000001 result = &B10000000

thanks

Entrar ou registrar-se para comentar

Just rotate?

Try this one:

```
; INPUT: A=byte to be rotated
; OUPUT: C=byte rotated

ld b,8
rot:	rrca
rl c
djnz rot
ret
```

got it! ,This routine is professional

Well, you know. Only pros here!

In that case, I'll optimise it somewhat for speed!

```
; INPUT: A=byte to be rotated
; OUPUT: C=byte rotated

ld b,4
rot:	rrca
rl c
rrca
rl c
djnz rot
ret
```

It is a miracle!

No need to use the b register here:

```; INPUT: A=byte to be mirrored
; OUTPUT: C=mirrored byte

scf
loop:
rr c
rr c
jp nz, loop
ret
```

Of course one can also unroll the loop even further, left as exercise for the reader.

If you have too much memory on your hands you could usea look-up table:

```; INPUT: C=byte to be mirrored
; OUTPUT: A=mirrored byte

ld b,mirrortable/256
ld a,(bc)
ret
.balign 256 ; align to 256-byte boundary
mirrortable:
defb #00, #80, #40, #C0 ; etc.
```

You'll want to generate the data with one of the other methods. The actual routine is now as long as a call, so you should make it a macro rather than a call.

My version:

```; INPUT: A=byte to be rotated
; OUPUT: C=byte rotated

ld c,1
rot:	rrca
rl c
jr nc,rot
ret
```