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 


Need help with assembly code

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

Joined: 28 Mar 2014
Posts: 33

PostPosted: Thu May 18, 2017 9:41 am    Post subject: Need help with assembly code Reply with quote

Disclaimer: I have a very minimalistic knowledge of assembly

Hey guys so I've been trying to patch Aliens vs Predator 2010's weapon damage. The way I've went about it is I've found the function that modifies health, which goes like movss [esi+34],xmm0. xmm0 stores my new health. What I would like to do is subtract the value of our new health(stored in xmm0) from my current one to find out how much damage I took. Then I compare that damage to the damage done by the gun I would like to nerf/buff. Normally this wouldn't work for most games because multiple guns share the same damage value, but specifically for this one every single weapon does a unique amount of damage. So this is sort of what I'm trying to do:

Subtract new hp from current hp to find out damage // what I mean here is like copy our current hp to some other place and subtract it there, not the actual hp address, because that would decrement our health and we are just using this as a comparison.
If our dmg is equal to x we run this:
sub [esi+34],(float)my custom dmg value (don't know if this is correct or not not :d)
if not we run the regular code
movss [esi+34]

Also can I stack this for every gun in one assembly script, or do I have to write a c++ program and use c++ code in combination with code caving. What I mean is:

Subtract our new hp's value out of current one's //like before

Now I want to go through the damage values I'm looking for and change accordingly, sort of like a c++ switch in assembly:

is it 50?
sub [esi+34],(float)someothervalue

is it 80?
sub [esi+34],(float)someothervalue

is it 10?
sub [esi+34],(float)someothervalue

Is it none of the above? Execute the regular code.
movss [esi+34],xmm0


If you have trouble understanding what I am trying to do feel free to ask and I'll clarify. If you feel like the way I'm going about this is completely wrong feel free to suggest something else/tell me why. I wasn't sure if this is the right forum to post on, maybe I should've posted this on game hacking.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Thu May 18, 2017 12:21 pm    Post subject: Reply with quote

Switches in C++ get compiled down to either a jump table or a cmp/je style block of things.

For example, if you have code like this and compile it with optimizations turned of, you get a jmp table result:

Code:
#include <Windows.h>
#include <iostream>

int __cdecl main(int argc, char* argv[])
{
    // Create a random seed..
    ::srand(::GetTickCount());

    // Create a random number between 0 and 5..
    auto health = 100;
    auto value = ::rand() % 5;

    // Adjust the health based on the random number..
    switch (value)
    {
    case 0:
        health -= 10;
        break;
    case 1:
        health -= 20;
        break;
    case 2:
        health -= 30;
        break;
    case 3:
        health -= 40;
        break;
    case 4:
        health -= 50;
        break;
    case 5:
        health -= 60;
        break;
    }

    std::cout << "Health is now: " << health << std::endl;
    return 0;
}


Code:
003B1090 | 55                               | push ebp                                                          | main.cpp:6
003B1091 | 8B EC                            | mov ebp,esp                                                       |
003B1093 | 83 EC 0C                         | sub esp,C                                                         |
003B1096 | FF 15 00 30 3B 00                | call dword ptr ds:[<&GetTickCount>]                               | main.cpp:8
003B109C | 50                               | push eax                                                          |
003B109D | FF 15 08 31 3B 00                | call dword ptr ds:[<&srand>]                                      |
003B10A3 | 83 C4 04                         | add esp,4                                                         |
003B10A6 | C7 45 FC 64 00 00 00             | mov dword ptr ss:[ebp-4],64                                       | main.cpp:11
003B10AD | FF 15 0C 31 3B 00                | call dword ptr ds:[<&rand>]                                       | main.cpp:12
003B10B3 | 99                               | cdq                                                               |
003B10B4 | B9 05 00 00 00                   | mov ecx,5                                                         |
003B10B9 | F7 F9                            | idiv ecx                                                          |
003B10BB | 89 55 F4                         | mov dword ptr ss:[ebp-C],edx                                      |
003B10BE | 8B 55 F4                         | mov edx,dword ptr ss:[ebp-C]                                      | main.cpp:15
003B10C1 | 89 55 F8                         | mov dword ptr ss:[ebp-8],edx                                      |
003B10C4 | 83 7D F8 05                      | cmp dword ptr ss:[ebp-8],5                                        |
003B10C8 | 77 4A                            | ja switchexamplecpp.3B1114                                        |
003B10CA | 8B 45 F8                         | mov eax,dword ptr ss:[ebp-8]                                      |
003B10CD | FF 24 85 48 11 3B 00             | jmp dword ptr ds:[eax*4+3B1148]                                   |
003B10D4 | 8B 4D FC                         | mov ecx,dword ptr ss:[ebp-4]                                      | main.cpp:18
003B10D7 | 83 E9 0A                         | sub ecx,A                                                         |
003B10DA | 89 4D FC                         | mov dword ptr ss:[ebp-4],ecx                                      |
003B10DD | EB 35                            | jmp switchexamplecpp.3B1114                                       | main.cpp:19
003B10DF | 8B 55 FC                         | mov edx,dword ptr ss:[ebp-4]                                      | main.cpp:21
003B10E2 | 83 EA 14                         | sub edx,14                                                        |
003B10E5 | 89 55 FC                         | mov dword ptr ss:[ebp-4],edx                                      |
003B10E8 | EB 2A                            | jmp switchexamplecpp.3B1114                                       | main.cpp:22
003B10EA | 8B 45 FC                         | mov eax,dword ptr ss:[ebp-4]                                      | main.cpp:24
003B10ED | 83 E8 1E                         | sub eax,1E                                                        |
003B10F0 | 89 45 FC                         | mov dword ptr ss:[ebp-4],eax                                      |
003B10F3 | EB 1F                            | jmp switchexamplecpp.3B1114                                       | main.cpp:25
003B10F5 | 8B 4D FC                         | mov ecx,dword ptr ss:[ebp-4]                                      | main.cpp:27
003B10F8 | 83 E9 28                         | sub ecx,28                                                        |
003B10FB | 89 4D FC                         | mov dword ptr ss:[ebp-4],ecx                                      |
003B10FE | EB 14                            | jmp switchexamplecpp.3B1114                                       | main.cpp:28
003B1100 | 8B 55 FC                         | mov edx,dword ptr ss:[ebp-4]                                      | main.cpp:30
003B1103 | 83 EA 32                         | sub edx,32                                                        |
003B1106 | 89 55 FC                         | mov dword ptr ss:[ebp-4],edx                                      |
003B1109 | EB 09                            | jmp switchexamplecpp.3B1114                                       | main.cpp:31
003B110B | 8B 45 FC                         | mov eax,dword ptr ss:[ebp-4]                                      | main.cpp:33
003B110E | 83 E8 3C                         | sub eax,3C                                                        |
003B1111 | 89 45 FC                         | mov dword ptr ss:[ebp-4],eax                                      |
003B1114 | 68 D0 16 3B 00                   | push <switchexamplecpp.std::endl<char,std::char_traits<char> >>   | main.cpp:37
003B1119 | 8B 4D FC                         | mov ecx,dword ptr ss:[ebp-4]                                      |
003B111C | 51                               | push ecx                                                          |
003B111D | 68 58 31 3B 00                   | push switchexamplecpp.3B3158                                      | 3B3158:"Health is now: "
003B1122 | 8B 15 50 30 3B 00                | mov edx,dword ptr ds:[<&class std::basic_ostream<char,struct std: |
003B1128 | 52                               | push edx                                                          |
003B1129 | E8 62 02 00 00                   | call <switchexamplecpp.std::operator<<<std::char_traits<char> >>  |
003B112E | 83 C4 08                         | add esp,8                                                         |
003B1131 | 8B C8                            | mov ecx,eax                                                       |
003B1133 | FF 15 38 30 3B 00                | call dword ptr ds:[<&public: class std::basic_ostream<char,struct |
003B1139 | 8B C8                            | mov ecx,eax                                                       |
003B113B | FF 15 4C 30 3B 00                | call dword ptr ds:[<&public: class std::basic_istream<unsigned sh |
003B1141 | 33 C0                            | xor eax,eax                                                       | main.cpp:38
003B1143 | 8B E5                            | mov esp,ebp                                                       | main.cpp:39
003B1145 | 5D                               | pop ebp                                                           |
003B1146 | C3                               | ret                                                               |


Which shows how you can use a block of predefined locations to handle the calculations based on the current value. Or you can do a list of compares and jumps based on that value. This can be mimic'd if you use an if / else if table in C++ like this:
Code:
#include <Windows.h>
#include <iostream>

int __cdecl main(int argc, char* argv[])
{
    // Create a random seed..
    ::srand(::GetTickCount());

    // Create a random number between 0 and 5..
    auto health = 100;
    auto value = ::rand() % 5;

    // Adjust the health based on the random number..
    if (value == 0)
        health -= 10;
    else if (value == 1)
        health -= 20;
    else if (value == 2)
        health -= 30;
    else if (value == 3)
        health -= 40;
    else if (value == 4)
        health -= 50;
    else if (value == 5)
        health -= 60;

    std::cout << "Health is now: " << health << std::endl;
    return 0;
}


Which yields:

Code:
01311090 | 55                               | push ebp                                                          | main.cpp:6
01311091 | 8B EC                            | mov ebp,esp                                                       |
01311093 | 83 EC 08                         | sub esp,8                                                         |
01311096 | FF 15 00 30 31 01                | call dword ptr ds:[<&GetTickCount>]                               | main.cpp:8
0131109C | 50                               | push eax                                                          |
0131109D | FF 15 08 31 31 01                | call dword ptr ds:[<&srand>]                                      |
013110A3 | 83 C4 04                         | add esp,4                                                         |
013110A6 | C7 45 FC 64 00 00 00             | mov dword ptr ss:[ebp-4],64                                       | main.cpp:11
013110AD | FF 15 0C 31 31 01                | call dword ptr ds:[<&rand>]                                       | main.cpp:12
013110B3 | 99                               | cdq                                                               |
013110B4 | B9 05 00 00 00                   | mov ecx,5                                                         |
013110B9 | F7 F9                            | idiv ecx                                                          |
013110BB | 89 55 F8                         | mov dword ptr ss:[ebp-8],edx                                      |
013110BE | 83 7D F8 00                      | cmp dword ptr ss:[ebp-8],0                                        | main.cpp:15
013110C2 | 75 0B                            | jne switchexamplecpp.13110CF                                      |
013110C4 | 8B 55 FC                         | mov edx,dword ptr ss:[ebp-4]                                      | main.cpp:16
013110C7 | 83 EA 0A                         | sub edx,A                                                         |
013110CA | 89 55 FC                         | mov dword ptr ss:[ebp-4],edx                                      |
013110CD | EB 53                            | jmp switchexamplecpp.1311122                                      |
013110CF | 83 7D F8 01                      | cmp dword ptr ss:[ebp-8],1                                        | main.cpp:17
013110D3 | 75 0B                            | jne switchexamplecpp.13110E0                                      |
013110D5 | 8B 45 FC                         | mov eax,dword ptr ss:[ebp-4]                                      | main.cpp:18
013110D8 | 83 E8 14                         | sub eax,14                                                        |
013110DB | 89 45 FC                         | mov dword ptr ss:[ebp-4],eax                                      |
013110DE | EB 42                            | jmp switchexamplecpp.1311122                                      |
013110E0 | 83 7D F8 02                      | cmp dword ptr ss:[ebp-8],2                                        | main.cpp:19
013110E4 | 75 0B                            | jne switchexamplecpp.13110F1                                      |
013110E6 | 8B 4D FC                         | mov ecx,dword ptr ss:[ebp-4]                                      | main.cpp:20
013110E9 | 83 E9 1E                         | sub ecx,1E                                                        |
013110EC | 89 4D FC                         | mov dword ptr ss:[ebp-4],ecx                                      |
013110EF | EB 31                            | jmp switchexamplecpp.1311122                                      |
013110F1 | 83 7D F8 03                      | cmp dword ptr ss:[ebp-8],3                                        | main.cpp:21
013110F5 | 75 0B                            | jne switchexamplecpp.1311102                                      |
013110F7 | 8B 55 FC                         | mov edx,dword ptr ss:[ebp-4]                                      | main.cpp:22
013110FA | 83 EA 28                         | sub edx,28                                                        |
013110FD | 89 55 FC                         | mov dword ptr ss:[ebp-4],edx                                      |
01311100 | EB 20                            | jmp switchexamplecpp.1311122                                      |
01311102 | 83 7D F8 04                      | cmp dword ptr ss:[ebp-8],4                                        | main.cpp:23
01311106 | 75 0B                            | jne switchexamplecpp.1311113                                      |
01311108 | 8B 45 FC                         | mov eax,dword ptr ss:[ebp-4]                                      | main.cpp:24
0131110B | 83 E8 32                         | sub eax,32                                                        |
0131110E | 89 45 FC                         | mov dword ptr ss:[ebp-4],eax                                      |
01311111 | EB 0F                            | jmp switchexamplecpp.1311122                                      |
01311113 | 83 7D F8 05                      | cmp dword ptr ss:[ebp-8],5                                        | main.cpp:25
01311117 | 75 09                            | jne switchexamplecpp.1311122                                      |
01311119 | 8B 4D FC                         | mov ecx,dword ptr ss:[ebp-4]                                      | main.cpp:26
0131111C | 83 E9 3C                         | sub ecx,3C                                                        |
0131111F | 89 4D FC                         | mov dword ptr ss:[ebp-4],ecx                                      |
01311122 | 68 D0 16 31 01                   | push <switchexamplecpp.std::endl<char,std::char_traits<char> >>   | main.cpp:28
01311127 | 8B 55 FC                         | mov edx,dword ptr ss:[ebp-4]                                      |
0131112A | 52                               | push edx                                                          |
0131112B | 68 58 31 31 01                   | push switchexamplecpp.1313158                                     | 1313158:"Health is now: "
01311130 | A1 50 30 31 01                   | mov eax,dword ptr ds:[<&class std::basic_ostream<char,struct std: |
01311135 | 50                               | push eax                                                          |
01311136 | E8 55 02 00 00                   | call <switchexamplecpp.std::operator<<<std::char_traits<char> >>  |
0131113B | 83 C4 08                         | add esp,8                                                         |
0131113E | 8B C8                            | mov ecx,eax                                                       |
01311140 | FF 15 38 30 31 01                | call dword ptr ds:[<&public: class std::basic_ostream<char,struct |
01311146 | 8B C8                            | mov ecx,eax                                                       |
01311148 | FF 15 4C 30 31 01                | call dword ptr ds:[<&public: class std::basic_istream<unsigned sh |
0131114E | 33 C0                            | xor eax,eax                                                       | main.cpp:29
01311150 | 8B E5                            | mov esp,ebp                                                       | main.cpp:30
01311152 | 5D                               | pop ebp                                                           |
01311153 | C3                               | ret                                                               |


There are two methods of setting up your code to get it to be how you want in a single script rather t han multiple things. Or you can just code cave it in C++ to do it.

Another thing you can do is reverse the file formats to find the gun information and edit the files themselves rather than doing a memory based hack to do it.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming 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