Skip to content

Commit

Permalink
Some improvement to the caller
Browse files Browse the repository at this point in the history
  • Loading branch information
MockbaTheBorg committed Aug 8, 2019
1 parent 1e86414 commit 60d8256
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
8 changes: 8 additions & 0 deletions Source/DynCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ int main(void) {
ctx.Push(123.456f);
ctx.Push(999);
printf("6th is: %d.\n", ctx.Call<int>());
//
nioCall<void>(&printf, "0x%08x %s\n", 12947765, "TEST");
//
ctx.Init(printf);
ctx.Push("0x%08x %s\n");
ctx.Push(12947765);
ctx.Push("TEST");
ctx.Call<void>();
}
8 changes: 7 additions & 1 deletion Source/DynCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ extern "C" void* _DynCall(uint64_t);
// 22222222 - Arg2
// ........ - And so on
//

template <typename TRet = void, typename... TArgs, typename TAddr>
constexpr inline TRet nioCall(TAddr address, TArgs... args) {
return reinterpret_cast<TRet(*)(TArgs...)>(address)(args ...);
}

#define MAX_ARGS 32
class DynCallContext {
// Data fields
Expand All @@ -23,7 +29,7 @@ class DynCallContext {
nArgs = 0;
Address = (uint64_t)address;
for(int i = 0; i < MAX_ARGS; i++)
Args[i] = 0xCCCCCCCCCCCCCCCC;
Args[i] = 0x0;
}
// Push a value onto the context
template<typename T>
Expand Down
29 changes: 15 additions & 14 deletions Source/caller.asm
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ _DynCall PROC

push rsi ; Save some nonvolatile registers
push rdi ;
push rbp ;
push r12 ;

sub rsp, 10h ; Give the stack some slack
mov r12, rsp ; and save it

mov rax, [rcx] ; Gets the number of arguments onto RAX

add rcx, 8 ; Gets the address of the called function
mov r10, [rcx] ; onto R10

add rcx, 8 ; Points RCX to the first argument

mov r12, rsp ; Saves the RSP onto R12

test eax, eax ; Do we have any args?
test eax, eax ; Do we have any arguments?
jz noargs ;

sub rsp, 108h ; Open enough space in the stack for 32 args
shl eax,3 ; Open enough space in the stack for the arguments
sub rsp, rax ;
shr eax,3 ;

mov rsi, rcx ; Copies all the parameters to the stack
add rcx, 8 ; Points RCX to the first argument

mov rsi, rcx ; Copies all the arguments to the stack
mov rcx, rax ;
mov rdi, rsp ;
rep movsq ;
Expand All @@ -47,20 +49,19 @@ _DynCall PROC
movq xmm3, r9 ;

noargs:
mov rax, return ; Pushes the return address onto the stack
push rax
jmp r10 ; Executes the called function
call r10 ; Executes the called function
; Which will probably crash ...
; ... or maybe not! :)

return:
mov rsp, r12 ; Gets the rsp back to where it was before
mov rsp, r12 ; Restore the stack and...
add rsp, 10h ; get the stack slack back

pop r12 ; Restore nonvolatile registers
pop rbp ;
pop rdi ;
pop rsi ;

ret ; Back to the caller
ret ; Back to the original caller

_DynCall ENDP

Expand Down
Binary file modified premake5.exe
Binary file not shown.
8 changes: 4 additions & 4 deletions premake5.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
workspace "DynCall"
location "Build"
location "VStudio"
configurations { "Debug", "Release" }
platforms { "X64" }
startproject "DynCall"
pic "On"
systemversion "10.0.15063.0"
systemversion "10.0.17763.0"
characterset "ASCII"

project "DynCall"
Expand All @@ -14,7 +14,7 @@ project "DynCall"
targetdir "Binary"
includedirs { }

files { "**.hpp", "**.h", "**.cpp", "**.c", "**.asm", "**.inc", "**.def"}
files { "Source/**"}
removefiles { }

vpaths {
Expand All @@ -34,5 +34,5 @@ project "DynCall"
-- Cleanup
if _ACTION == "clean" then
os.rmdir("Binary");
os.rmdir("Build");
os.rmdir("VStudio");
end
2 changes: 1 addition & 1 deletion setup.cmd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
premake5 vs2017
premake5 vs2019

0 comments on commit 60d8256

Please sign in to comment.