i learning assembly programming part of 1 of university modules. have program written in c++ inline x86 assembly takes string of 6 characters , encrypts them based on encryption key.
here's full program: https://gist.github.com/anonymous/1bb0c3be77566d9b791d
my code fo encrypt_chars
function:
void encrypt_chars (int length, char ekey) { char temp_char; // char temporary store (int = 0; < length; i++) // encrypt characters 1 @ time { temp_char = ochars [i]; // temp_char contains address values of individual character __asm { push eax // save values contained within register stack push ecx movzx ecx, temp_char push ecx // push argument #2 lea eax, ekey push eax // push argument #1 call encrypt add esp, 8 // clean parameters of stack mov temp_char, al // move temp character register pop ecx pop eax } echars [i] = temp_char; // store encrypted char in encrypted chars array } return; // inputs: register eax = 32-bit address of ekey, // ecx = character encrypted (in low 8-bit field, cl). // output: register eax = encrypted value of source character (in low 8-bit field, al). __asm { encrypt: push ebp // set stack mov ebp, esp // set base pointer mov eax, [ebp + 8] // move value of parameter 1 eax mov ecx, [ebp + 12] // move value of parameter 2 ecx push edi // used string , memory array copying push ecx // loop counter pushing character onto stack not byte ptr[eax] // negation add byte ptr[eax], 0x04 // adds hex 4 ekey movzx edi, byte ptr[eax] // moves value of ekey edi using zeroes pop eax // pop character value stack xor eax, edi // xor character give encrypted value of source pop edi // pop original address of edi stack rol al, 1 // rotates encrypted value of source 1 bit (left) rol al, 1 // rotates encrypted value of source 1 bit (left) again add al, 0x04 // adds hex 4 encrypted value of source mov esp, ebp // deallocate values pop ebp // restore base pointer ret } //--- end of assembly code }
my questions are:
- what best/ efficient way convert
for loop
assembly? - is there way remove call
encrypt
, place code directly in place? - how can optimise/minimise use of registers , instructions make code smaller , potentially faster?
- is there way me convert
ochars
,echars
arrays assembly?
if possible, able provide me explanation of how solution works eager learn.
i can't optimization or cryptography can show way go making loop, if @ loop in function:
void f() { int a, b ; for(a = 10, b = 1; != 0; --a) { b = b << 2 ; } }
the loop essentially:
for(/*initialize*/; /*condition*/; /*modify*/) { // run code }
so function in assembly along these lines:
_f: push ebp mov ebp, esp sub esp, 8 ; int a,b initialize: ; mov dword ptr [ebp-4], 10 ; = 10, mov dword ptr [ebp-8], 1 ; b = 1 mov eax, [ebp-4] condition: test eax, eax ; tests if == 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
plus show in source how make local variables;
- subtract space stack pointer.
- and access them through base pointer.
i tried make source generic intel x86 assembly syntax apologies if needs changing specific environment more aiming give general idea how construct loop in assembly giving can copy, paste , run.