简体   繁体   English

如何从 x64 程序集中的堆栈中获取参数?

[英]How to get an argument from stack in x64 assembly?

I'm trying to write a procedure in x64 assembly.我正在尝试在 x64 程序集中编写一个过程。

I'm calling it in a main program that is written in C++.我在用 C++ 编写的主程序中调用它。 I'm passing several parameters.我正在传递几个参数。 I know that first 4 will be in specific registers and the rest of them (should be) on stack.我知道前 4 个将在特定寄存器中,其余的(应该)在堆栈中。 What's more, I read that before taking 5th argument from the stack, I should substract 40 from RSP.更重要的是,我在从堆栈中取出第 5 个参数之前读到,我应该从 RSP 中减去 40。 And at the begining it worked.一开始它奏效了。 Later I needed to check the address of sth so I did it by: cout and &.后来我需要检查某事的地址,所以我通过:cout和&来完成。 But then, taking 5th argument from stack didn't work and I have no idea what whould I do.但是,从堆栈中取出第 5 个参数不起作用,我不知道我该怎么办。

fragment of C++ code: C++代码片段:

std::cout << xOld << '\t' << &xOld << std::endl;
std::cout << xOld[0] << '\t' << &xOld[0] << std::endl;

SthInAsm(A, B, alfa, beta, n, xOld, xNew, lowerBound, upperBound, condition, isReady, precision, maxIterations);

fragment of Asm code: Asm 代码片段:

.data
    Aaddr DQ 0
    Baddr DQ 0
    alfa DQ 0
    beta DQ 0
    n DQ 0
    xOld DQ 0
.
.
.

.code

SthInAsm PROC   
    MOV Aaddr, RCX
    MOV Baddr, RDX
    MOV alfa, R8
    MOV beta, R9

    SUB RSP, 40

    XOR RAX, RAX
    POP n
    MOV RAX, n
.
.
.

After 'MOV RAX, n' RAX doesn't contain value of n.在 'MOV RAX, n' RAX 之后不包含 n 的值。 When I didn't check address by cout before calling this function, it worked.当我在调用此函数之前没有通过 cout 检查地址时,它起作用了。

Does anyone know what is the problem here?有谁知道这里有什么问题?

Thanks to Jester I know what is wrong in my code.感谢 Jester,我知道我的代码有什么问题。 I must have misunderstood sth when I read about x64 assembly.当我阅读有关 x64 程序集的内容时,我一定误解了某事。 Substracting from RSP - I shouldn't do it.从 RSP 中减去 - 我不应该这样做。

Instead of that, getting arguments from stack works when I write: MOV RAX, QWORD PTR [RSP+40] MOV RAX, QWORD PTR [RSP+48] etc.取而代之的是,当我写:MOV RAX、QWORD PTR [RSP+40] MOV RAX、QWORD PTR [RSP+48] 等时,从堆栈中获取参数是有效的。

Thank you Jester again!再次感谢小丑!

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

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