I've been trying to implement a loop into my program however I'm getting confused as to how I would be able to achieve the desired outcome.
I've been practising creating loops like this
(just an example of)
void f()
{
int a, b ;
for(a = 10, b = 1; a != 0; --a)
{
b = b << 2 ;
}
}
I used the following question to practice with for loops in assembly: For loop in x86 assembly and optimising code?
_f:
push ebp
mov ebp, esp
sub esp, 8 ; int a,b
initialize: ; for
mov dword ptr [ebp-4], 10 ; a = 10,
mov dword ptr [ebp-8], 1 ; b = 1
mov eax, [ebp-4]
condition:
test eax, eax ; tests if a == 0
je exit
runCode:
mov eax, [ebp-8]
shl eax, 2 ; b = b << 2
mov dword ptr [ebp-8], eax
modify:
mov eax, [ebp-4]
sub eax, 1 ; --a
mov dword ptr [ebp-4], eax
jmp condition
exit:
mov esp, ebp
pop ebp
ret
Now, in my program, I have this for loop:
void encrypt_chars(int length, char EKey)
{
char temp_char; // char temporary store
for (int i = 0; i < length; i++) // encrypt characters one at a time
{
temp_char = OChars[i]; // temp_char now contains the address values of the individual character
__asm
{
// Assembly code for encryption
}
EChars[i] = temp_char; // Store encrypted char in the encrypted chars array
}
return;
I don't fully understand how to take the parameters from the function and implement them into the loop in assembly. Would anyone be able to show me an example of that please?
full code: http://pastebin.com/x14L7e3x
Would anyone be able to show me how to convert the above loop into assembly? How do I write OChars and EChars in assembly also?
thanks.
EDIT: I've had a go at implementing it but I get a few errors when I run my program:
Here's the code:
void encrypt_chars(int length, char EKey)
{
char temp_char;
__asm
{
mov dword ptr[ebp - 14h], 0
jmp encrypt_chars + 30h
mov eax, dword ptr[ebp - 14h]
add eax, 1
mov dword ptr[ebp - 14h], eax
mov eax, dword ptr[ebp - 14h]
cmp eax, dword ptr[length] // error here
jge encrypt_chars + 6Ah
mov eax, dword ptr[ebp - 14h] // the temp_char = OChar[i]
mov cl, byte ptr
mov byte ptr[temp_char], cl
push eax
push ecx
movzx ecx, byte ptr[temp_char]
push ecx
lea eax, [EKey]
push eax
call encrypt4
add esp, 8
mov byte ptr[temp_char], al
pop ecx
pop eax
mov eax, dword ptr[ebp - 14h]
mov cl, byte ptr[temp_char]
byte ptr[temp_char], cl // error here
jmp encrypt_chars + 27h
}
return;
__asm
{
encrypt4:
push ebp // Set stack
mov ebp, esp // Set up the base pointer
mov eax, [ebp + 8] // Move value of parameter 1 into EAX
mov ecx, [ebp + 12] // Move value of parameter 2 into ECX
push edi // Used for string and memory array copying
push ecx // Loop counter for pushing character onto stack
not byte ptr[eax] // Negation
add byte ptr[eax], 0x04 // Adds hex 4 to EKey
movzx edi, byte ptr[eax] // Moves value of EKey into EDI using zeroes
pop eax // Pop the character value from stack
xor eax, edi // XOR character to give encrypted value of source
pop edi // Pop original address of EDI from the stack
rol al, 1 // Rotates the encrypted value of source by 1 bit (left)
rol al, 1 // Rotates the encrypted value of source by 1 bit (left) again
add al, 0x04 // Adds hex 4 to encrypted value of source
mov esp, ebp // Deallocate values
pop ebp // Restore the base pointer
ret
}
//--- End of Assembly code
}
// end of encrypt_chars function