Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 高级到ASM转换_C++_Assembly_Visual Studio 2013_X86_Inline Assembly - Fatal编程技术网

C++ 高级到ASM转换

C++ 高级到ASM转换,c++,assembly,visual-studio-2013,x86,inline-assembly,C++,Assembly,Visual Studio 2013,X86,Inline Assembly,我正在学习汇编编程,我的任务是在我的程序中将for循环(以及任何数组使用)转换为汇编 该程序只需获取一个加密密钥(EKey),并使用它来加密字母数组(hello) 这是C++中的循环: void encrypt_chars(int length, char EKey) { char temp_char; // char temporary store for (int i = 0; i < length; i++)

我正在学习汇编编程,我的任务是在我的程序中将for循环(以及任何数组使用)转换为汇编

该程序只需获取一个加密密钥(
EKey
),并使用它来加密字母数组(
hello

这是C++中的循环

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
}
void encrypt_chars(int length, char EKey)
{
    char temp_char;

    __asm
    {
        mov     DWORD PTR[rbp - 4], 0
        jmp     L2

    L3:
        mov     eax, DWORD PTR[rbp - 4]
        cdqe
        cltq   

// ERROR HERE ^ above: error C2400: inline assembler syntax error in 'opcode'; found 'newline'  
        movzx   eax, BYTE PTR EChars[rax] 

// ERROR HERE ^ above: error C2400: inline assembler syntax error in 'opcode'; found 'newline'  
// another error ^ above: error C2424: '[' : improper expression in 'second operand'

        mov     BYTE PTR[rbp - 5], al

        // OG code
        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    encrypt4
        add     esp, 8              // Clean parameters of stack
        mov     temp_char, al       // Move the temp character into a register    

        pop     ecx
        pop     eax

        // end of OG code

        mov     eax, DWORD PTR[rbp - 4]
        cdqe
        movzx   edx, BYTE PTR[rbp - 5] // ERROR HERE: error C2400: inline assembler syntax error in 'opcode'; found 'newline'   
        mov     BYTE PTR DChars[rax], dl // ERROR HERE: error C2424: '[' : improper expression in 'first operand'

    L2:
        mov     eax, DWORD PTR[rbp - 4]
        cmp     eax, DWORD PTR[rbp - 20]
        jl      L3

    }
    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
}
有人能指出我哪里出了问题吗?我发现这相对来说比较困难,所以一步一步的解释是最受欢迎的。请告诉我哪里出了问题。谢谢x

编辑:

我的完整代码:

试试这个:

void encrypt_chars(int length, char EKey, char *Msg)
{
    int InLength = length;
    int counter;

    __asm
    {
        push eax        // Counter
        mov eax, 0      // Zero counter
        push ebx        // Value
        mov ebx, InLength
        jmp     L2

L3:
        mov counter, EAX
        call    encrypt4
        inc eax         // Increment counter.

    L2:
        cmp     eax, ebx
        jl      L3      // Jump if we haven't reached our count.
        pop ebx
        pop eax
    }
    return;

    __asm
    {
    encrypt4:
            push    eax
            push    ebx
            push    edi

            mov     ebx, counter
            add     ebx, Msg
            mov     al, [ebx]      // Move character into al
            CBW                         // Make word.
            CWDE                        // Make Dword.


            not     byte ptr[EKey]       // Negation
            add     byte ptr[EKey], 0x04 // Adds hex 4 to EKey
            movzx   edi, byte ptr[EKey]  // Moves value of EKey into EDI using zeroes

            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 [ebx], al
            pop     ebx
            pop     eax
            ret
    }

    //--- End of Assembly code
}
我用这个片段来称呼它:

EKey = 'i';

sprintf(OChars, "hello");

printf("%s\n", OChars);
encrypt_chars(sizeof(OChars), EKey, OChars);
printf("%s\n", OChars);
我的输出如下所示: 你好

╧4.▀↑█⌐

这与您的输出相匹配,但除第一个字符外,其他所有字符都匹配。我确信这个算法看起来是正确的,我不能再花时间了。我相信这可能是字符集的差异。试试看


祝你好运

请编辑您的帖子并将错误消息包含在其中,而不是链接到它们。确保发布消息的文本,而不是图片。特别是由于某种原因,链接无法从这里工作。无论如何,我能看到的是,你肯定应该使用标签,而不是像
jge encrypt_chars+6Ah
@CaptainObvlious嗨,我不知道。道歉。错误请参见编辑。@Jester你能给我举个例子吗?请分别删除
cdqe
cltq
,使用
eax
ebp
而不是
rax
rbp
。我回去验证了十六进制。这与你的照片相符。@Ellen帮了忙吗?