Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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 使用汇编程序损坏内存_C_Assembly_X86 - Fatal编程技术网

C 使用汇编程序损坏内存

C 使用汇编程序损坏内存,c,assembly,x86,C,Assembly,X86,我在asm中编写了此过程: .586 .model flat, stdcall .xmm .data .code EncryptAsm proc plainText:ptr byte, heigth:DWORD, inputLength:DWORD, encryptedText:ptr byte, cipherArray:ptr byte local addRow:WORD local row:DWORD local column

我在asm中编写了此过程:

    .586
    .model flat, stdcall
    .xmm
    .data
    .code
    EncryptAsm proc plainText:ptr byte, heigth:DWORD, inputLength:DWORD,  encryptedText:ptr byte, cipherArray:ptr byte

local   addRow:WORD
local   row:DWORD 
local   column:DWORD 
local   iterator:DWORD 
local   forLoopIteratorI:DWORD
local   forLoopIteratorJ:DWORD




push    esi
push    edi
push    ebx
push    ecx
push    edx

mov     addRow,0
mov     row,0
mov     column,0
mov     iterator,0
mov     forLoopIteratorI,0
mov     forLoopIteratorJ,0


mov     ecx,heigth




FILL_CIPHER_ARRAY_LOOP: mov eax, inputLength
                        cmp iterator,eax
                        jge PREPARE_ITERATOR


                        push ecx ;pushing heigth value
                        mov ecx,row ;calculating index of cipher array   index=[row*inputLength+column]
                        imul ecx,inputLength
                        add ecx,column

                        mov eax,iterator



                        mov edx,plainText
                        mov al,[edx+eax]
                        mov [esi],al

                        mov ebx, cipherArray
                        mov [ebx+ecx],al

                        movsb



                        pop ecx;getting back heigth value


                        add column,1
                        cmp addRow,0
                        je INC_ROW
                        cmp addRow,0
                        jne DEC_ROW

                        INC_ROW:            add row,1
                                            jmp ROW_COMPARE

                        DEC_ROW:            sub row,1
                                            jmp ROW_COMPARE

                        ROW_COMPARE:        cmp row,ecx
                                            jge IF_STATEMENT_1
                                            cmp row,0
                                            jl IF_STATEMENT_2
                                            jmp INCREMENT_ITERATOR


                        IF_STATEMENT_1:     sub row,2
                                                mov addRow,1
                                            jmp INCREMENT_ITERATOR

                        IF_STATEMENT_2:     add row,2
                                            mov addRow,0
                                            jmp INCREMENT_ITERATOR
                        INCREMENT_ITERATOR: add iterator,1
                                            jmp FILL_CIPHER_ARRAY_LOOP


PREPARE_ITERATOR:       mov iterator,0


READ_CIPHER_ARRY_LOOP_I:cmp forLoopIteratorI,ecx
                        jge PREPARE_ITERATOR_2

READ_CIPHER_ARRY_LOOP_J:mov eax, inputLength
                        cmp forLoopIteratorJ,eax
                        jge PREPARE_I_AND_J
                        push ecx ;pushing heigth value
                        mov ecx,forLoopIteratorI ;calculating index of cipher array
                        imul ecx,inputLength
                        add ecx,forLoopIteratorJ
                        mov ebx,cipherArray
                        mov al,[ebx+ecx]
                        cmp al,'#'
                        jne COPY_VALUE
                        ITERATE:            add forLoopIteratorJ,1
                                            pop ecx
                                            jmp READ_CIPHER_ARRY_LOOP_J

PREPARE_I_AND_J:        mov forLoopIteratorJ,0
                        add forLoopIteratorI,1
                        jmp READ_CIPHER_ARRY_LOOP_I

COPY_VALUE:             push edi
                        mov edi,iterator
                        mov edx,encryptedText
                        mov [edx+edi],al
                        add iterator,1
                        pop edi
                        jmp ITERATE

PREPARE_ITERATOR_2:     mov iterator,0


FINISH:                 mov eax, encryptedText
                        pop     edx
                        pop     ecx
                        pop     ebx
                        pop     edi
                        pop     esi
                        ret

   EncryptAsm endp
    end
它实现了围栏密码算法(在变量encryptedText的末尾包含加密的明文)。它工作得很好,我的意思是它做加密很好,但毕竟我得到了内存损坏错误。。。我将此过程称为C应用程序中的外部过程。我可以毫无问题地打印加密文本,但在主函数中返回0时,会弹出内存损坏错误

我不知道是什么引起的。在asm过程的开始,我推送所有寄存器的值,并在整个操作之后弹出它们

错误消息:
控制台应用程序12.exe中0x72676F74处未处理的异常:0xC0000005:访问冲突执行位置0x72676F74。


如果有任何提示,我将不胜感激。

这里可能有一位候选人:

mov edx,plainText
mov al,[edx+eax]
mov [esi],al
esi
是从调用方推送的,但它在哪里初始化?它似乎使用了调用方提供的任何信息。对于
edi
也一样,那么
movsb
将在哪里存储它呢

更新

因为我不知道你的算法,也不知道它是如何使用的,所以我只能猜测。但我认为在循环之前,您应该执行以下操作:

mov esi, plainText
mov edi, encryptedText
mov ebx, cipherArray
由于不更改这些值,因此可以更改此代码:

COPY_VALUE:         push edi
                    mov edi,iterator
                    mov edx,encryptedText
                    mov [edx+edi],al
                    add iterator,1
                    pop edi
                    jmp ITERATE
为此:

COPY_VALUE:         mov edx,iterator
                    mov [edx+edi],al
                    inc iterator
                    jmp ITERATE

通常,您可以使用更短的
inc x
来代替使用
add x,1

我发现了它!错误的原因是……”移动某人。我在我的算法的前一个版本中使用了它,但我忘了删除它。。。我的其余代码工作正常。感谢所有的答案和愿意帮助m;)

你覆盖了一些内存缓冲区?调试器。单步走。注册窗口。查找缓冲区写入。@Joachim Pileborg可能是的,但我找不到原因。在我写的过程中,我在过程的末尾弹出每个值。这与寄存器无关,而是与代码中写入的内存有关。检查循环条件以确保正确无误,正如mah所说,在检查寄存器和变量以及其他所有内容的同时,单步执行代码。您应该关注算法的边界以及指令,如
edx+edi
和其他指令。也许你的索引超出了范围。我应该使用mov esi、0或其他什么吗?不,因为这将是一个空指针。你必须用一些合适的值初始化它。i、 E.我假设您的
edi
目标缓冲区和
esi
的源缓冲区。我没有研究该算法及其工作原理,只是想看看是否能发现一些明显的问题。我会为
esi
初始化
明文
encryptedText
edi
密码
ebx
中。从实现的外观来看,这就足够了,因此您可以自由使用
eax、ecx和edx
,并且可以在算法期间不加载它。应该更清楚地理解,也更有效,因为您似乎没有更改它,但似乎一次又一次地使用相同的值加载
ebx
,这只是代码中的噪音。