【问题标题】:memory corrupt using assembler procedure使用汇编程序损坏内存
【发布时间】:2013-11-14 12:39:59
【问题描述】:

我已经用 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 应用程序的外部程序。我可以毫无问题地打印加密文本,但是在 main 函数中返回 0 时,会弹出内存损坏错误。

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

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

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

【问题讨论】:

  • 你覆盖了一些内存缓冲区?
  • 调试器。一小步。注册窗口。查找缓冲区写入。
  • @Joachim Pileborg 可能是的,但我找不到原因。正如我所写,我在过程结束时将每个值都弹出。
  • 这不是关于寄存器,而是你在代码中写入的内存。检查循环条件以确保一切正常,正如 mah 所说,在检查寄存器和变量以及其他所有内容的同时单步执行代码。
  • 您应该关注算法的边界以及edx+edi 等指令。也许您的索引超出范围。

标签: c assembly x86


【解决方案1】:

这里有一位可能的候选人:

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

【讨论】:

  • 我应该做 mov esi,0 还是什么?
  • 不,因为这将是一个 NULL 指针。 YOui 必须用一些合适的值来初始化它。 IE。可能是edi 的目标缓冲区和esi 的源我假设。我没有研究算法及其工作原理,只是看看我是否能发现一些明显的问题。
  • 我会在esi 中初始化plaintext,在ediebx 中初始化encryptedTextcipherArray。从您的实现的外观来看,这已经足够了,因此您可以免费使用eax, ecx and edx,并且您可以在算法期间取消加载它。应该更清楚地理解和更高效,因为您似乎没有更改它,但似乎一次又一次地使用相同的值加载 ebx,这只是代码中的噪音。
【解决方案2】:

我找到的人!错误的原因是......'movsb'。我在我的算法的早期版本中使用它,我忘记删除它......我的其余代码工作正常。感谢您提供所有答案并愿意帮助 m ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 2014-11-11
    相关资源
    最近更新 更多