对于x86汇编循环和优化code? [英] For loop in x86 assembly and optimising code?

查看:153
本文介绍了对于x86汇编循环和优化code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在学习汇编编程为我的大学模块中的一个组成部分。我已经写在C ++内联x86汇编这需要6个字符的字符串,并根据加密密钥进行加密的程序。

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.

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

我的code FO的 encrypt_chars 功能:

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
}

我的问题是:


  • 什么是转换这一点的最好/最有效的方式循环成汇编?

  • 有没有办法到c直接在其位置取出呼吁加密,并将$ C $?

  • 如何优化/尽量减少使用寄存器和指令,使code较小的和潜在的更快?

  • 有我转换一个方式 OChars EChars 阵列成汇编?

  • 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.

推荐答案

我不能优化或密码帮助,但我可以告诉你一个方法去制作一个循环,如果你在这个函数看环

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 ;            
        }       
}

循环本质上是:

        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;


  • 减去堆栈指针的空间。

  • ,并通过基指针访问它们。

我试图使源作为通用的英特尔x86汇编语法,我可以让我的道歉,如果有什么需要改变特定环境我更旨在让有关如何在装配构造一个循环的总体思路,然后给你的东西您可以复制,粘贴和运行。

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.

这篇关于对于x86汇编循环和优化code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆