How to get an argument from stack in x64 assembly?

旧城冷巷雨未停 提交于 2020-12-15 06:22:05

问题


I'm trying to write a procedure in x64 assembly.

I'm calling it in a main program that is written in 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. What's more, I read that before taking 5th argument from the stack, I should substract 40 from RSP. And at the begining it worked. Later I needed to check the address of sth so I did it by: cout and &. But then, taking 5th argument from stack didn't work and I have no idea what whould I do.

fragment of C++ code:

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:

.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. When I didn't check address by cout before calling this function, it worked.

Does anyone know what is the problem here?


回答1:


Thanks to Jester I know what is wrong in my code. I must have misunderstood sth when I read about x64 assembly. Substracting from RSP - I shouldn't do it.

Instead of that, getting arguments from stack works when I write: MOV RAX, QWORD PTR [RSP+40] MOV RAX, QWORD PTR [RSP+48] etc.

Thank you Jester again!



来源:https://stackoverflow.com/questions/59538388/how-to-get-an-argument-from-stack-in-x64-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!