byte mirror

Door monant

Resident (48)

afbeelding van monant

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

Aangemeld of registreer om reacties te plaatsen

Van jltursan

Prophet (2313)

afbeelding van jltursan

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

Van monant

Resident (48)

afbeelding van monant

10-04-2008, 23:24

got it! ,This routine is professionalHannibal

Van ro

Guardian (4188)

afbeelding van ro

11-04-2008, 19:26

Well, you know. Only pros here! Tongue

Van Edwin

Paragon (1182)

afbeelding van Edwin

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

Van arnold_m

Master (173)

afbeelding van arnold_m

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.

Van Metalbrain

Expert (67)

afbeelding van Metalbrain

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