C - 使用汇编程序过程损坏内存



我在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

它实现了铁路围栏密码算法(最后变量加密文本包含加密的明文)。它工作正常,我的意思是它可以很好地进行加密,但毕竟我遇到了内存损坏错误......我将此过程称为来自 C 应用程序的外部过程。我可以毫无问题地打印加密文本,但是在main函数中返回0时,会弹出内存损坏错误。

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

错误消息: Unhandled exception at 0x72676F74 in ConsoleApplication12.exe: 0xC0000005: Access violation executing location 0x72676F74.

我会很高兴得到任何提示。

一个可能的候选者可能在这里:

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

esi是从调用方推送的,但它在哪里初始化?似乎它使用了调用者的任何内容。edi也一样,那么movsb将它存储在哪里?

更新

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

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

我找到了的人!错误的原因是..."莫夫斯布"。我在以前版本的算法中使用它,但我忘记删除它......我的其余代码工作正常。感谢您的所有答案并愿意帮助我;)

最新更新