byte mirror

By monant

Resident (48)

monant's picture

10-04-2008, 22:26

[ 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

Login or register to post comments

By jltursan

Prophet (2619)

jltursan's picture

10-04-2008, 23:15

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

By monant

Resident (48)

monant's picture

10-04-2008, 23:24

got it! ,This routine is professionalHannibal

By ro

Scribe (4963)

ro's picture

11-04-2008, 19:26

Well, you know. Only pros here! Tongue

By Edwin

Paragon (1182)

Edwin's picture

11-04-2008, 21:54

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! Tongue

By arnold_m

Master (173)

arnold_m's picture

11-04-2008, 22:48

No need to use the b register here:

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

  scf
  adc a,a
loop:
  rr c
  add a,a
  rr c
  add a,a
  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.

By Metalbrain

Expert (67)

Metalbrain's picture

13-04-2008, 16:22

My version:

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

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