ESBExchange Procedure
Swaps the data for the specified number of bytes between the two structures.

Unit
QESBPCSSystem

Declaration
Procedure ESBExchange(var Obj1, Obj2; const Size: LongWord);

Description
Uses fast dword moves. BASM.

Parameters
Obj1 First Object.
Obj2 Second Object.
Size Number of bytes to swap starting from the beginning of each object.

Category
Memory Operations

Implementation

procedure ESBExchange (var Obj1, Obj2; const Size: LongWord); register;
asm
	push 	edi			// Preserve EDI
	push 	ebx			// Preserve EBX
	push 	ecx            // Preserve ECX (which currently has Size)
	shr  	ecx, 2         // Integer divide by 4 to see if less than 4 Byte
						// ECX now contains the number of DWords to Swap
	jz   	@@LessThanFour	// If so then jump

@@AgainDWords:
						// Swap DWords
	mov  	ebx, [eax]	// Copy a DWord from Obj1 into EBX
	mov  	edi, [edx]	// Copy a DWord from Obj2 into EDI
	mov  	[edx], ebx	// Copy a Dword from EBX into Obj2
	mov  	[eax], edi	// Copy a Dword from EDI into Obj1

	add  	eax, 4		// Move to the next DWord in Obj1
	add  	edx, 4		// Move to the next DWord in Obj2
	dec  	ecx			// Decrement count
	jnz  	@@AgainDWords	// Repeat until ECX is Zero

@@LessThanFour:
	pop  	ecx  		// Restore original ECX (i.e Size)
	and  	ecx, $3		// Get just the remainder after division by 4
	jz   	@@Done		// If Zero nothing to do
	mov  	bl, [eax]      // Otherwose swap a byte
	mov  	bh, [edx]
	mov  	[edx], bl
	mov  	[eax], bh
	inc  	eax      		// And move one byte
	inc  	edx
	dec  	ecx
	jz   	@@Done

	mov  	bl, [eax]		// Repeat for remainding bytes...
	mov  	bh, [edx]
	mov  	[edx], bl
	mov  	[eax], bh
	inc  	eax
	inc  	edx
	dec  	ecx
	jz   	@@Done

	mov  	bl, [eax]
	mov  	bh, [edx]
	mov  	[edx], bl
	mov  	[eax], bh

@@Done:
	pop  	ebx            // Restore EBX
	pop  	edi			// Restore EDI
End;


HTML generated by Time2HELP
http://www.time2help.com