繁体   English   中英

对于x86汇编中的循环和优化代码?

[英]For loop in x86 assembly and optimising code?

我目前正在学习汇编编程,这是我的大学模块之一。 我有一个用C ++用内联x86汇编语言编写的程序,该程序采用6个字符的字符串并根据加密密钥对它们进行加密。

这是完整的程序: https : //gist.github.com/anonymous/1bb0c3be77566d9b791d

我的代码来自encrypt_chars函数:

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
}

我的问题是:

  • for loop转换为汇编的最佳/最有效方法是什么?
  • 有没有一种方法可以消除对encrypt的调用,并将代码直接放在其位置?
  • 如何优化/最小化寄存器和指令的使用,以使代码更小且可能更快?
  • 我有办法将OCharsEChars数组转换为汇编吗?

如果可能的话,您能为我提供一个我渴望学习的解决方案工作原理的解释。

我建议研究编译器生成的汇编代码。 您可以稍后对其进行更改和优化。

如何从gcc中的C / C ++源获取汇编程序输出?

我不能帮助优化或加密,但是如果您查看此函数中的循环,我可以向您展示一种制作循环的方法:

void f()
{
        int a, b ;

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

循环本质上是:

        for(/*initialize*/; /*condition*/; /*modify*/)
        {
            // run 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

另外,我在源代码中展示了如何创建局部变量。

  • 从堆栈指针中减去空间。
  • 并通过基本指针访问它们。

我试图将源代码尽可能地设为通用的Intel x86汇编语法,因此,如果您需要针对特定​​环境进行任何更改,我深表歉意,我的目标是针对如何在汇编中构建循环提供一个总体思路,然后为您提供可以复制的内容,粘贴并运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM