Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How to 16 byte align rsp

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
k4sh
Cheater
Reputation: 0

Joined: 01 Mar 2016
Posts: 28

PostPosted: Wed Mar 30, 2016 3:54 pm    Post subject: How to 16 byte align rsp Reply with quote

I need to call a lua function in a code injected.
The call CE lua function template says that rsp has to be 16 byte aligned prior to call my function.

Here is a portion of my actual code :
Code:

  ...
  mov rcx, setnewxyratios
  sub rsp,20
  call LuaFunctionCall
  add rsp,20
  ...

  ...
  setnewxyratios:
    db 'setNewXYRatios',0


The only ending is a game crash after LuaFunctionCall has been called.
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 222

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Wed Mar 30, 2016 4:12 pm    Post subject: Reply with quote

Code:
  push rbp
  mov rbp,rsp

  sub rsp,20
  and rsp,-10 //  aligning the stack with the next lowest 16-byte boundary

  mov rcx, setnewxyratios
  call LuaFunctionCall

  mov rsp,rbp
  pop rbp
  ...

  ...
  setnewxyratios:
    db 'setNewXYRatios',0



and rsp,-10 instruction zeroes the low 4 bits of rsp, which may decrease it (it might reserve an additional 8 bytes if needed)

_________________
Back to top
View user's profile Send private message MSN Messenger
k4sh
Cheater
Reputation: 0

Joined: 01 Mar 2016
Posts: 28

PostPosted: Fri Apr 01, 2016 12:14 pm    Post subject: Reply with quote

Again big thank mgr.inz.Player
Now with this code, my game is not crashing anymore :
Code:

  ...
  {$lua}
    function test()
      print('triggered')
    end
  {$asm}
  ...
  mov rcx,[rax+rdi*8] <== original game code
  mov rax,[rcx]    <== original game code

  push rcx
  push rax
  push rbp
  mov rbp,rsp
  sub rsp,20
  and rsp,-10 //  aligning the stack with the next lowest 16-byte boundary

  mov rcx, setnewxyratios
  call LuaFunctionCall

  mov rsp,rbp
  pop rbp
  pop rax
  pop rcx

  ...
  setnewxyratios:
  db 'test',0


However, my function test to print the string triggered is never executed as i don't get any lua output.

What could i have done wrong ?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites