Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 04:29

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



c++ inline asm jnz & jz not working

Discussion on c++ inline asm jnz & jz not working within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
znoeen's Avatar
 
elite*gold: 0
Join Date: May 2014
Posts: 58
Received Thanks: 33
c++ inline asm jnz & jz not working

Hello, i have a very strange bug in visual studio.

PHP Code:
DWORD dwNoReload;
DWORD dwNoReloadJump;
DWORD dwNoReloadOffset;
DWORD dwNoReloadOG;

 
__declspec(nakedvoid __stdcall ASM_NoReloadFunction()
{
    
/*_:00518EB9                 test    byte ptr [edi+1047Ch], 10h
    ___:00518EC0                 jz      loc_519040
    ___:00518EC6                 mov     esi, [ebp+arg_4]*/

    
__asm
    
{
        
cmp byte ptr[NoReload], 0x00;                    //Compairs int noreload with 0
        
je disabled;                                    //If equal (so disabled) jump to disabled
        
jmp[dwNoReloadJump];                            //Jump over original code

    
disabled:
        
test byte ptr[edi dwNoReloadOffset], 0x10;    //original instruction
        
jz dwNoReloadOG;                                //original instruction (line 47)
        
jmp[dwNoReloadJump];                            //Jump back to original code
    
}

PHP Code:
Error    C2415    improper operand type    Weaponbinder    c:\Functions.h    47 
I tried with mulitple compiler settings. Also with /arch (msdn recommands this with this error). But still no result in a working code. Does anyone know why i can't use jz or jnz in c++?
znoeen is offline  
Old 11/07/2016, 18:48   #2
 
elite*gold: 100
Join Date: Apr 2008
Posts: 860
Received Thanks: 1,487
Not really a bug, more a beginners error

All conditional jump instructions are relative to their location.

Code:
0x000000 33 C0    xor eax, eax
0x000002 85 C0    test eax, eax
0x000004 74 FE    jz 0x4
This code will loop forever. It will always jump 0xFE bytes far, which is -2 bytes in decimal. So the jz will always land on the jz instruction.

Lets come back to your code: You're instructing the compiler to calculate a JMP instruction from a variable. On pure semantic, this is a valid idea since all assemblers accept JX <absolute address> or <label> rather than <relative offset>. But this only works, if you know where in the code your JMP instruction will be placed AND where the JMPs target instruction will be placed. You'll need the relative offsets aka. the distance between these two locations.

Having the target location in a variable is simply not assembleable.
florian0 is offline  
Old 12/08/2016, 01:29   #3




 
bloodx's Avatar
 
elite*gold: 55
Join Date: Mar 2006
Posts: 4,582
Received Thanks: 1,539
this works also.

DWORD dwNoReloadJump = 0x123456;
PHP Code:
jmp dwNoReloadJump
bloodx is offline  
Old 12/08/2016, 03:19   #4
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,137
Received Thanks: 573
I don't know if this fixes your problem, but you should never use DWORD type for pointer. On x64 architecture pointer have a size of 64 bit, DWORD holds only 32 bit. You could use the uintptr_t type, it's an unsigned integer of exactly the size of a pointer regardless of architecture.
warfley is offline  
Thanks
1 User
Old 12/08/2016, 06:38   #5
 
Dr. Coxxy's Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
you cant use inline asm @ 64bit with msvc anyway
Dr. Coxxy is offline  
Old 12/08/2016, 23:34   #6
 
elite*gold: 0
Join Date: Dec 2014
Posts: 442
Received Thanks: 211
why use inline assembler in the first place when you have the functionality to use C++ aka function pointers...?
_asm is offline  
Old 12/09/2016, 10:15   #7
 
elite*gold: 100
Join Date: Apr 2008
Posts: 860
Received Thanks: 1,487
Quote:
Originally Posted by _asm View Post
why use inline assembler in the first place when you have the functionality to use C++ aka function pointers...?
How would you solve that code snipped above with plain c++? (Really interested, not trolling).
florian0 is offline  
Old 12/09/2016, 16:53   #8
 
elite*gold: 0
Join Date: Dec 2014
Posts: 442
Received Thanks: 211
Quote:
Originally Posted by florian0 View Post
How would you solve that code snipped above with plain c++? (Really interested, not trolling).
From what I can tell by looking at the code, the function he is trying to hook doesn't require any function arguments. So instead of rebuilding the code section in inline assembler before it's hooked and modified, he could simply use a function pointer pointing to the function before it's hooked and return it in a custom callback with his desired modified variables or function calls (obviously with the same arguments)
e.g. Microsoft Detours, PolyHook, MologieDetours...

I have no idea how the function template might look like or what calling convention is used, but taking a look with IDA or similiar disassemblers should give you the answer.
_asm is offline  
Reply


Similar Threads Similar Threads
Inline-ASM| Was soll es tun?
10/17/2014 - General Coding - 3 Replies
Hallo, ich habe schon viele Source-Codes von Hacks gesehen, wo man Inline-ASM benutzt hat, und ich möchte es nun auch anwenden. Die Grundlegende Kenntnisse hab ich soweit, aber ein richtiges Programm alleine aus Assembler wäre etwas schwer. Aber zurück zu meiner Frage, wenn ich Inline-ASM in meinem Hack verwenden möchte, Wann sollte ich es benutzten? Was sollte der ASM Code bewirken? Okay das war soweit, vielen dank:)
Inline ASM
06/07/2010 - General Coding - 5 Replies
Huhu was mich schon länger beschäftigt ist die Frage wieso inline assembler unbedingt vermieden werden sollte. Es geht mir vorallem um windows tools/game hacks. Wäre nett wenn mich jemand mit ein paar schönen begründungen erleuchten könnte.
[Request] Some help with Inline asm in C++
09/08/2008 - CO2 Programming - 3 Replies
I'm trying to read a "string" (not necessarily human readable characters) that may also contain 0x00's using an injected dll. Is there a way to (efficiently) copy a string of a length that can be up to 400 bytes or so into a c++ variable? I have a pointer to the memory location inside EDI.



All times are GMT +1. The time now is 04:30.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.