呼叫未正确返回[X86_ASM]

时间:2012-04-13 12:38:30

标签: c++ inline-assembly

这是使用x86内联汇编的C ++ [Intel语法]

功能:

     DWORD *Call ( size_t lArgs, ... ){

    DWORD *_ret = new DWORD[lArgs];

    __asm {
        xor edx, edx
        xor esi, esi
        xor edi, edi
        inc edx
start:
        cmp edx, lArgs
        je end
        push eax
        push edx
        push esi
        mov esi, 0x04
        imul esi, edx
        mov ecx, esi
        add ecx, _ret
        push ecx
        call dword ptr[ebp+esi] //Doesn't return to the next instruction, returns to the caller of the parent function.
        pop ecx
        mov [ecx], eax
        pop eax
        pop edx
        pop esi
        inc edx
        jmp start
end:
        mov eax, _ret
        ret
    }
}

此功能的目的是调用多个函数/地址,而无需单独调用它们。

为什么我要你调试它? 我必须在那天开始上学,我需要在晚上完成学业。

非常感谢,iDomo

2 个答案:

答案 0 :(得分:3)

感谢您提供完整的可编译示例,它使解决问题变得更加容易。

根据您的Call函数签名,当设置堆栈帧时,lArgs位于ebp+8,指针从ebp+C开始。而你还有其他一些问题。这是一个更正版本,带有一些推送/弹出优化和清理,在MSVC 2010(16.00.40219.01)上测试:

DWORD *Call ( size_t lArgs, ... ) {

    DWORD *_ret = new DWORD[lArgs];

    __asm {
        xor edx, edx
        xor esi, esi
        xor edi, edi
        inc edx
        push esi
start:
        cmp edx, lArgs
        ; since you started counting at 1 instead of 0
        ; you need to stop *after* reaching lArgs
        ja end
        push edx
        ; you're trying to call [ebp+0xC+edx*4-4]
        ; a simpler way of expressing that - 4*edx + 8
        ; (4*edx is the same as edx << 2)
        mov esi, edx
        shl esi, 2
        add esi, 0x8
        call dword ptr[ebp+esi]
        ; and here you want to write the return value
        ; (which, btw, your printfs don't produce, so you'll get garbage)
        ; into _ret[edx*4-4] , which equals ret[esi - 0xC]
        add esi, _ret
        sub esi, 0xC
        mov [esi], eax
        pop edx
        inc edx
        jmp start
end:
        pop esi
        mov eax, _ret
        ; ret ; let the compiler clean up, because it created a stack frame and allocated space for the _ret pointer
    }
}

完成后不要忘记delete[]从此函数返回的内存。

答案 1 :(得分:1)

我注意到,在调用之前,您按下EAX,EDX,ESI,ECX(按顺序),但在返回后不按相反的顺序弹出。如果第一个CALL正确返回,但后续的CALL没有,则可能是问题。