Z80 Routines:Optimized:reverseA

From WikiTI
Jump to: navigation, search

This is a faster/smaller (and also obfuscated) replacement for the normal reverse a.

Typical routine:

;input: b
;output: a
;25 bytes, 96 clock cycles
typicalreversea:
	rr b
	rla
	rr b
	rla
	rr b
	rla
	rr b
	rla
	rr b
	rla
	rr b
	rla
	rr b
	rla
	rr b
	rla
	ret

Better routine:

;- Reverse a
;input:	byte in A
;output: Reversed byte in A
;destroys B
;17 bytes and 66 clock cycles
;author: calcmaniac84
ReverseA:
	ld b,a 		;b=ABCDEFGH
	rrca 		;a=HABCDEFG
	rrca 		;a=GHABCDEF
	xor b \ and %10101010 \ xor b 	;a=GBADCFEH
	ld b,a 		;b=GBADCFEH
	rrca 		;a=HGBADCFE
	rrca 		;a=EHGBADCF
	rrca 		;a=FEHGBADC
	rrca 		;a=CFEHGBAD
	xor b \ and %01100110 \ xor b 	;a=GFEDCBAH
	rrca 		;a=HGFEDCBA