Search code examples
c++assemblystackx86-64calling-convention

How to get an argument from stack in x64 assembly?


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?


Solution

  • 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!