For loop in x86 assembly and optimising code?

浪尽此生 提交于 2019-12-02 23:21:00

问题


I am currently learning assembly programming as part of one of my university modules. I have a program written in C++ with inline x86 assembly which takes a string of 6 characters and encrypts them based on the encryption key.

Here's the full program: https://gist.github.com/anonymous/1bb0c3be77566d9b791d

My code fo the encrypt_chars function:

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
        {
            push    eax                 // Save values contained within register to 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 the temp character into a register    

            pop     ecx
            pop     eax
        }
        EChars [i] = temp_char;         // Store encrypted char in the encrypted chars array
    }
   return;

    // Inputs: register EAX = 32-bit address of Ekey,
    //                  ECX = the character to be encrypted (in the low 8-bit field, CL).
    // Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).

   __asm
   {
   encrypt:
       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
}

My questions are:

  • What is the best/ most efficient way to convert this for loop into assembly?
  • Is there a way to remove the call for encrypt and place the code directly in its place?
  • How can I optimise/minimise the use of registers and instructions to make the code smaller and potentially faster?
  • Is there a way for me to convert the OChars and EChars arrays into assembly?

If possible, would you be able to provide me with an explanation of how the solution works as I am eager to learn.


回答1:


I can't help with optimization or the cryptography but i can show you a way to go about making a loop, if you look at the loop in this function:

void f()
{
        int a, b ;

        for(a = 10, b = 1; a != 0; --a)
        {
            b = b << 2 ;            
        }       
}

The loop is essentially:

        for(/*initialize*/; /*condition*/; /*modify*/)
        {
            // run code
        }

So the function in assembly would look something along these lines:

_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

Plus I show in the source how you make local variables;

  • subtract the space from the stack pointer.
  • and access them through the base pointer.

I tried to make the source as generic intel x86 assembly syntax as i could so my apologies if anything needs changing for your specific environment i was more aiming to give a general idea about how to construct a loop in assembly then giving you something you can copy, paste and run.




回答2:


I would suggest to look into assembly code which is generated by compiler. You can change and optimize it later.

How do you get assembler output from C/C++ source in gcc?



来源:https://stackoverflow.com/questions/29792616/for-loop-in-x86-assembly-and-optimising-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!