FCAAssignment2 Thomas Sampson
-
Upload
tomtech999 -
Category
Documents
-
view
214 -
download
0
Transcript of FCAAssignment2 Thomas Sampson
-
8/14/2019 FCAAssignment2 Thomas Sampson
1/10
Computer ArchitectureAssignment 2
Encryption!
Thomas SampsonGames Software Development2Y - 2007
-
8/14/2019 FCAAssignment2 Thomas Sampson
2/10
-
8/14/2019 FCAAssignment2 Thomas Sampson
3/10
Implementing the Standard Call mechanism
Instead of using registers to pass parameters to my sub routine, below I have
implemented the standard call procedure which uses the stack to send parameters into
my sub routine.
NOTE: - At this point I am only implementing _stdcall and not optimising any of the code
Part 1
This part of the code is responsible for organising the stack / calling the sub routine,
and resides inside the while loop.
__asm {
push ecx //push or 'save' the value of ecx onto the stackpush edx //push or 'save' the value of edx onto the stack
push EKey; //push parameter 1 (the encryption key) onto the stackpush s_char; //push parameter 2 (the source char) onto the stack
call encrypt14; //call sub routine with result stored in EAX or AL to be precise
add esp,8; //push the stack pointer back (by 8 as we used 2*4byte paramaters)mov e_char, al; //move the encrypted character into variable 'e_char'
pop edx; //pop or 'restore' the old value of edx from the stack
pop ecx; //pop or 'restore' the old value of ecx from the stack
}
-
8/14/2019 FCAAssignment2 Thomas Sampson
4/10
-
8/14/2019 FCAAssignment2 Thomas Sampson
5/10
Encryption Routine
To work out a method of decrypting the character I first decided to draw a simple
flow chart of how the source character is encrypted, allowing me to reverse the
process.
Source Character CL
CLROR x 4CL pushed onto stack
Encryption Key AL
AL
Add 55h
ALCopied into DL
CL popped off of stackALNow = CL
XOR AL with DL
AL =Scrambled source charDL = Encryption Key + 55h
Result into AL
ALROR x 2
NOT AL
AL + 10h
Stack
Encrypted key is now in AL
-
8/14/2019 FCAAssignment2 Thomas Sampson
6/10
-
8/14/2019 FCAAssignment2 Thomas Sampson
7/10
-
8/14/2019 FCAAssignment2 Thomas Sampson
8/10
Optimising my code
I will now attempt to optimise the assembly code used in both the encryption and
decryption routines to increase efficiency and reduce the code footprint.
Encryption Routine Optimisation
__asm {push ecx //push or 'save' the value of ecx onto the stackpush EKey; //push parameter 1 (the encryption key) onto the stackpush s_char; //push parameter 2 (the source char) onto the stack
call encrypt14; //call sub routine with result stored in EAX or AL to be precise
add esp,8; //push the stack pointer back (by 8 as we used 2*4byte paramaters)mov e_char, al; //move the encrypted character into variable 'e_char'
pop ecx; //pop or 'restore' the old value of ecx from the stack}
In the main encrypt14 routine I removed the use of the register edx, thereforeremoved the process of pushing and popping it off the stack when calling the subroutine. There is no point in saving the value of EDX on the stack and retrieveing itafter the function call, as I am confident that the value of EDX will not be changedduring the sub routine.
__asm {encrypt14:push ebp; //push old base pointer value onto stack
mov ebp, esp; //move base pointer to current stack pointer
//----Main Encrytion Routine----//mov ecx, dword ptr [ebp+8]; //place the source character in ecxmov eax, dword ptr [ebp+0Ch]; //place the encryption key in eax
Here I used the second operand of ror to inform the instruction to do 4 rotationsto the right, instead of repeating the operation 4 timesror cl, 4; //rotate the source character bits right 4 timesHere I removed the process of pushing ECX onto the stack as it served no relevantpurposeadd al,0x55Here I removed the process of copying EAX into EDX as it was un-necessary
As a consequence of not putting ECX on the stack, and not using EDX, I can now usethe XOR function on EAX and ECX (this has exactly the same effect as the previousconvoluted method)xor al,clHere I used the second operand of ror to inform the instruction to do 2 rotationsto the right, instead of repeating the operation twiceror al,2not aladd al,0x10//--End Main Encryption Routine--//
mov esp, ebp; //set stack pointer back to base pointerpop ebp; //restore the old base pointer from stackret; //end sub routine, also pops return address off the stack
}
Note: - For all manipulations of EAX and ECX I changed all the references to AL andCL (the latter byte of the registers). This should make arithmentic and XOR functionsmore efficient as they do not have to work with 32 bits of data, only 8.
-
8/14/2019 FCAAssignment2 Thomas Sampson
9/10
-
8/14/2019 FCAAssignment2 Thomas Sampson
10/10
Lower Case Conversion
The c++ code is responsible for checking that the string we enter contains ONLY
alpha numeric characters. To do lower case conversion (during decryption) we must
isolate any uppercase characters and convert them.
Uppercase characters lie within the ASCII code region of 0x41 0x5A. In the
assembly code I do two checks to ensure the character lies within these bounds, only
if it meets these conditions will it be converted to lowercase, otherwise the IP will
jump to the end of the decryption routine, bypassing any case conversion.
Lowercase conversion is easy as there is a direct link between lower and uppercase
ASCII codes. Any uppercase ASCII code + 0x20 gives its lowercase equivalent,
(a simple addition to the AL region of the EAX register).
add ecx, 55h;xor eax, ecx;rol al, 4;
cmp al, 41h; // the decrypted characater with 41hjl fail; //if decrypted char is 5Ah then jump to failadd al, 20h; //+20h to decrypted char (shift to lower case)
fail: //The decrypted character is NOT a captial
mov esp, ebp;pop ebp;ret;
End of decryption logic
End of decryption sub routine