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 


What is the difference between adding -1 and subtracting 1?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
PrometheusPB
How do I cheat?
Reputation: 0

Joined: 01 Apr 2018
Posts: 4

PostPosted: Tue Apr 03, 2018 10:47 pm    Post subject: What is the difference between adding -1 and subtracting 1? Reply with quote

(I hope I posted this in the right place, if not, sorry moderator(s))

So I am hacking a program and I find the instruction I was looking for, but the code was a bit odd. I haven't run into this before, so it got me to thinking...

As follows:

00534083 - 83 00 FF - add dword ptr [eax],-01

Would that not literally be equivalent to:

00534083 - 83 00 01 - sub dword ptr [eax],01

It is obviously a signed byte (as I have tried changing it to -80 [hex] and it did not fit, but -7F [hex] did), but what is the difference between the two? Is there some benefit to add -1 as opposed to subtracting 1? Is that not the same instruction with a different operand? Anyone know why this was coded to add -1 instead of simply subtracting 1?

Is this some sort of tin-foil hat conspiracy, or is it just wacky coding that is as wacky as the game itself? Developer trolling? I tried it both ways and it works the same, so why do it that way? Any ideas, because maybe I missed something here?

I'm a bit new to ASM, so I don't understand the relevance of one over the other. This code came from the compiled result of the Torque Game Engine (I can't find it either), if that means anything to anyone.

Just curious if there was some reason for this method.

Anyone?
Back to top
View user's profile Send private message
ParkourPenguin
Grandmaster Cheater Supreme
Reputation: 63

Joined: 06 Jul 2014
Posts: 1944
Location: Arcadian Suburbia

PostPosted: Wed Apr 04, 2018 9:14 am    Post subject: Reply with quote

There's not much difference between adding -1 and subtracting 1. Given the same input, some flags are different between the two (i.e. CF / AF). There might be negligible performance differences between the two.

It was probably either explicitly written that way in the source code or it was compiled that way just because.

PrometheusPB wrote:
00534083 - 83 00 01 - sub dword ptr [eax],01
That disassembly is incorrect for that machine code. The correct disassembly for the bytes 83 00 01 would be "add dword ptr [eax],01", and the correct bytes for the instruction "sub dword ptr [eax],01" would be 83 28 01.
PrometheusPB wrote:
It is obviously a signed byte (as I have tried changing it to -80 [hex] and it did not fit, but -7F [hex] did)...
You're misunderstanding something here. -0x80 technically falls within the range of a two's compliment signed byte. 0x80 (-128) is the minimum value in the range of a signed byte, and the two's compliment of it is itself (-128).

There is no way for you to tell if an integral type is signed or unsigned by only looking at the data. You'd need to look at what operations the program is performing on the data. Addition and subtraction don't say anything definitive due to how two's compliment works, but other instructions can (e.g. ja / jg, mul / imul, div / idiv).
PrometheusPB wrote:
Is that not the same instruction with a different operand?
No. "add" adds two values together and "sub" subtracts one value from another. In this case, they're both doing the same thing (more or less), but they are not the same instruction.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
PrometheusPB
How do I cheat?
Reputation: 0

Joined: 01 Apr 2018
Posts: 4

PostPosted: Wed Apr 04, 2018 1:10 pm    Post subject: Reply with quote

Thanks for the input...That is much food for thought, I'll have to hit the books a little more. I might have mixed up the ASM bytes a bit, but I follow where you are going. I still get a bit confused on syntax sometimes, like when I see one register following another ([eax,esi] for a crude example... "is it push eax into esi?, no it's push esi to eax...gaah! it's too early in the morning, where's my beer? rofl). I'm working on it, eventually.

I clearly still have much to learn, which is sometimes difficult for me after my compact car was converted into a sub-compact that didn't drive too well afterwards Lame. Unfortunately, I was in the car when the "conversion" took place Lame Lame, so bear with me a little. But I love to hack, and CE is my favorite tool for debuggery (debauchery?) Razz

I *devastate* on game mods (I am a feared admin on Freelancer), but admittedly my hacks are pretty weak right now. I am at least clever enough to look for instructions adjacent to the one the debugger found. I try to examine the code block as best as I can, instead of just NOP'ing the initial "hit". I prefer to alter the instruction rather than just remove it.

Sorry, I'm running-on...Okay, back to code syntax and flow again for me. Thanks again.
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