; =============================================================================
;
;                         Litos8 integer number operations
;
; =============================================================================

; -----------------------------------------------------------------------------
; Exported user functions (3):
;	EndianByte - change endian of Byte
;	EndianWord - change endian of Word
;	EndianDWord - change endian of DWord
; -----------------------------------------------------------------------------

		CODE_SECTION

; -----------------------------------------------------------------------------
;                          Change endian of DWord
; -----------------------------------------------------------------------------
; INPUT:	DX:AX = number in big/little endian
; OUTPUT:	DX:AX = number in little/big endian
; Notes:	x86 CPU uses little-endian, least significant byte is lowest.
; -----------------------------------------------------------------------------

EndianDWord:	call	EndianWord	; endian LOW word
		xchg	ax,dx		; exchange LOW and HIGH word

; === EndianWord must follow !

; -----------------------------------------------------------------------------
;                          Change endian of Word
; -----------------------------------------------------------------------------
; INPUT:	AX = number in big/little endian
; OUTPUT:	AX = number in little/big endian
; Notes:	x86 CPU uses little-endian, least significant byte is lowest.
; -----------------------------------------------------------------------------

EndianWord:	call	EndianByte	; endian LOW byte
		xchg	al,ah		; exchange LOW and HIGH byte

; === EndianByte must follow !

; -----------------------------------------------------------------------------
;                          Change endian of Byte
; -----------------------------------------------------------------------------
; INPUT:	AL = number in big/little endian
; OUTPUT:	AL = number in little/big endian
; Notes:	x86 CPU uses little-endian, least significant byte is lowest.
; -----------------------------------------------------------------------------

; ------------- push registers

EndianByte:	push	dx		; push DX

; ------------- swap bits

		ror	al,1		; export bit 0
		rcl	dl,1		; import bit 0
		ror	al,1		; export bit 1
		rcl	dl,1		; import bit 1
		ror	al,1		; export bit 2
		rcl	dl,1		; import bit 2
		ror	al,1		; export bit 3
		rcl	dl,1		; import bit 3
		ror	al,1		; export bit 4
		rcl	dl,1		; import bit 4
		ror	al,1		; export bit 5
		rcl	dl,1		; import bit 5
		ror	al,1		; export bit 6
		rcl	dl,1		; import bit 6
		ror	al,1		; export bit 7
		rcl	dl,1		; import bit 7
		mov	al,dl		; AL <- result

; ------------- pop registers

		pop	dx		; pop DX
		ret
