简体   繁体   English

访问在ASM函数中在堆栈上传递的参数

[英]Accessing parameters passed on the stack in an ASM function

I am writing an assembly function to be called from C that will call the sidt machine instruction with a memory address passed to the C function. 我正在编写一个要从C调用的汇编函数,该函数将调用sidt机器指令,并将内存地址传递给C函数。 From my analysis of the code produced by MSVC10, I have constructed the following code (YASM syntax): 通过对MSVC10生成的代码的分析,我构造了以下代码(YASM语法):

SECTION    .data
SECTION    .text
GLOBAL _sidtLoad
_sidtLoad:

push        ebp  
mov         ebp,esp  
sub         esp,0C0h  
push        ebx  
push        esi  
push        edi  
lea         edi,[ebp-0C0h]  
mov         ecx,30h  
mov         eax,0CCCCCCCCh  

sidt [ebp+8]

pop         edi  
pop         esi  
pop         ebx  
add         esp,0C0h  
cmp         ebp,esp    
mov         esp,ebp  
pop         ebp  
ret  

Here is the C function signature: 这是C函数签名:

void sidtLoad (void* mem);

As far as I can see everything should work, I have even checked the memory address passed to the function and have seen that it matches the address stored at [ebp+8] (the bytes are reversed, which I presume is a result of endianness and should be handled by the machine). 据我所知一切正常,我什至检查了传递给该函数的内存地址,并发现它与[ebp + 8]中存储的地址匹配(字节反转,我认为这是字节序的结果)并应由机器处理)。 I have tried other arguments for the sidt instruction, such as [ebp+12], [ebp+4], [ebp-8] etc but no luck. 我已经为sidt指令尝试了其他参数,例如[ebp + 12],[ebp + 4],[ebp-8]等,但是没有运气。

PS I am writing this in an external assembly module in order to get around the lack of inline assembly when targeting x64 using MSVC10. PS我正在外部组装模块中编写此代码,以解决使用MSVC10定位x64时缺少内联汇编的问题。 However, this particular assembly function/program is being run as x86, just so I can get to grips with the whole process. 但是,这个特定的汇编功能/程序是作为x86运行的,所以我可以掌握整个过程。

PPS I am not using the __sidt intrinsic as the Windows Driver Kit (WDK) doesn't seem to support it. PPS我没有使用__sidt内在函数,因为Windows驱动程序工具包(WDK)似乎不支持它。 Or at least I can't get it to work with it! 或至少我无法使用它!

Your code will write the IDT into the memory address ebp+8 , which is not what you want. 您的代码会将IDT写入到您不需要的内存地址ebp+8 Instead, you need something like: 相反,您需要以下内容:

mov eax, [ebp+8]
sidt [eax]

compile: 编译:

int sidLoad_fake(void * mem) {
    return (int)mem;
}

to assembly, and then look at where it pulls the value from to put in the return register. 组装,然后查看将值从何处提取到返回寄存器中。

I think that the first few arguments on x86_64 are passed in registers. 我认为x86_64上的前几个参数在寄存器中传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM