0

In my main function in C++ I pass 6 int arguments to a subroutine written in assembly x64. The first four arguments are in registers but I'm struggling with getting the last two from stack. Here's my code:

.code
myProc1 proc a:DWORD, b:DWORD, c:DWORD, d:DWORD, e:DWORD, f:DWORD
 push rbp
 mov rbp, rsp
 sub rsp, 32  ; allocate shadow space 'padding'
 sub rsp, 16  ; allocate space for fifth and sixth argument
 mov DWORD PTR [rsp + 20h], e  ; push fifth argument
 mov DWORD PTR [rsp + 24h], f  ; push sixth argument
 mov rsp, rbp
 pop rbp
 ret
myProc1 endp
end

When I try to push e i f on stack I'm getting "invalid instruction operands". And I just don't know how else to try to push dword on stack.

I've tried using push and pop on qwords but then e and f value isn't as it should be. I'm guessing it's because int and qword are different sizes.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • First of all, stack slots for arg passing are always 8 bytes wide, even if you only use the low 4 bytes of each one. Second, you can't copy memory to memory in one `mov` instruction. If you used `push` in the right place in your function (before allocating shadow space), it could work, or just load your incoming args into registers before using `mov` to store them. (e.g. eax and r10 are call-clobbered and not holding any incoming args.) – Peter Cordes Apr 05 '23 at 19:02
  • In this particular case, you could replace the whole function with an optimized tailcall, `jmp somefunc`. Except your code here doesn't do a `call` after you set up to pass all 6 args to a new one. – Peter Cordes Apr 05 '23 at 19:07

0 Answers0