People ignore design when design ignores them

Tag Archives: optimisation


I have recently been working on a data decryption program in x86 Assembler language. This being my first project involving assembler language has really taught me the value efficiency.

The original program took in a standard cstring and applied an encryption routine to it, I was then asked to write an assembler routine that would decrypt the new string and return it to its former self, only all lower-case.  I found the lessons learnt while messing around with the assembler code invaluable, as they lead to me learning some great optimisation techniques which could make all the difference when applied to a large complex program. The routine I was working on was not large enough to see massive gains in performance from the optimisation I was implementing, however knowing how to speed up code that may have to be called thousands of times a second is a vital skill to learn in game development. All though the code may be fairly elementary, I’ve found a real passion for this type of programming and would definitely like to develop my knowledge in this area.

void decrypt_chars (const int ARRAY_LENGTH, char EKey)
{  
	_asm{
		////////////// For Loop - Setup /////////////////////

		push	eax					;//Save Registers
		push	ecx					;//...
		mov		eax, 0				;//Initialise loop counter

		//////////// For Loop - Jump //////////////////////////

forloop:							;//Label
		cmp		eax, ARRAY_LENGTH	;
		jg		endLoop				;//(EAX > ARRAY_LENGTH)
		movsx	ecx, EChars[eax]	;//Get and store encrypted character

		//////////////// Setup Parameters/Stack ///////////////

		push   eax					;//Save regsiter values to the stack
		push   ecx					;//...
		movsx  eax, EKey			;//Store 'EKey' in EAX register


		///////// Modify Encryption Key /////////////////////

		and		eax,0x3C			;//Bitwise AND 'EKey' with '0x3C'
		ror		eax,1				;//Rotate right EAX, Halving 'EKey'
		ror		eax,1				;//Rotate right EAX, Halving 'EKey'
		add		eax,0x01			;//Increment 'EKey'
		mov		edx,eax				;//Store modified EKey in EDX register
		pop		eax					;//Restore character to be encrypted in EAX

		///////////// Apply Decryption  /////////////////////

		rol		al,1				;//Undo ror al,1
		xor		eax,edx				;//Undo xor eax,edx
		sub		eax,edx				;//Undo add eax,edx

		//////////////// To Lowercase //////////////////////

		cmp		al, 0x41			;//ASCII values 0x41 - 0x5A represent Captial Letters, Upper Bound
		jl		done				;//Not in range
		cmp		al, 0x5A			;//Lower Bound
		jge		done				;//Not in range
		add		al, 0x20			;//Lowercase equivalent is + 0x20 in ASCII table
done:								;//Label
		
		mov	   bl,al;				;//Store encrypted character in bl
		pop    eax					;//...

		/////////////// End For Loop  - Setdown //////////////

		movsx  cl, bl				;//Store decrypted character in CL
		mov    DChars[eax], cl		;//Move decrypted character into DChars[index] array
		add    eax, 1				;//Incremenet loop counter

		//////////// For Loop - Jump //////////////////////////

		cmp    eax, ARRAY_LENGTH	;
		jle    forloop				;//( EAX <= ARRAY_LENGTH )
endLoop:							;//Label

		///////////////// For Loop - Exit //////////////////////

		pop   ecx					;//Restore Registers
		pop   eax					;
	}
	return;
}