Custom Code/Introduction to ASM Hacks: Difference between revisions

From Zenith
Jump to navigation Jump to search
Content added Content deleted
(Created page with "wip == Requirements == * NSMBU-Haxx/Setup NSMBU-Haxx == Guide == (blah blah introduction) === Finding our target function === stuff === Patching the function === doing...")
 
(Add information)
Line 5: Line 5:


== Guide ==
== Guide ==
(blah blah introduction) To introduce you to the basic ASM build system, we will be making a small patch to disable the music speedup after 100 seconds.
(blah blah introduction)


===Preparation===
=== Finding our target function ===
Before we start creating our patch, first we need to include the <code>asmsetup.S</code> file in our project. Simply download the file from [https://cdn.discordapp.com/attachments/814594642595151902/819005128429404201/asmsetup.S here], and place it in your /include/ directory in the <nowiki>[[NSMBU-Haxx]]</nowiki> project files.
stuff


=== Patching the function ===
===Finding our target function===
To find our target function, we can refer to the <nowiki>[[Symbol Map]]</nowiki> to see the address for the specific function. In our case, the symbol is <code>shouldHurryUp__11MusicPlayerFv</code> which corresponds to <code>0xF5783D8</code>. Let's make a hook to disable it!
doing nop or blr, as you can see it doesnt work and all that


=== Patching the function (the correct way)
===Patching the function===
To try and disable the function, we can try and insert a <code>nop</code> function that does nothing. To do this first we create a YAML file called <code>nohurryup.yaml</code>. Inside it, we can insert the following lines of code:
the correct way is this, and this is why: (blah blah)


<code>---</code>
==== Creating the assembly file ====
yes


<code>Files: []</code>
==== Creating the hook ====
also stuff


<code>Hooks:</code>
=== Conclusion and Review ===
also stuffX2


<code>- type: nop</code>
[[Category:Guides]]

<code>addr: F5783D8</code>

What this does is replace the instruction at address F5783D8 with a <code>nop</code> instruction which does nothing. This should work right? Well, let's test it out.

(insert video of crash)

It seems like this just crashes the game. Why is this so?

=== Patching the function (the correct way) ===
(insert technical reason of why this doesn't work)

The correct way of doing this, is to instead do a <code>blr</code> instruction, which will skip the function and return. The thing is, the function returns a bool (a 1 or a 0), but a blr would just return what is in register 3 (the return register). So what we need to do is set register 3 to zero, or <code>0x0</code> and ''then'' return with <code>blr</code>. Another issue we will run into is that a YAML hook cannot just do this by itself. For that we will need a <code>.S</code>, or '''assembly''' file.</code>
====Creating the assembly file====
Let's create a file in /source/ called <code>nohurryup.S</code>. The assembly we will be writing is quite simple. All it has to do is set register 3 to 0x0 and then return. Before we can do that though we need to add some setup lines.

<code>.text</code>


<code>.include "asmsetup.S"</code>


<code>.global NoHurryUp</code>

<code>NoHurryUp:</code>

<code>li r3, 0x0</code>

<code>blr</code>

Let's break down what this does. The .text indicates that the following is code to be executed. The .include will include the asmsetup.S file into our own file. The NoHurryUp label contains a load immediate (li) instruction that sets register 3 to 0x0, or 00 00 00 00, and then it has a blr instruction that returns back to the link register. The .global means that the NoHurryUp label will be accessible from anywhere. Don't forget to end the file in a newline!

What does this code do exactly?

====Creating the hook====
Now instead of having the code in the YAML, lets include the nohurryup.S file that we just created. In the YAML write the following code:


<code>---</code>

<code>Files: [source/nohurryup.S]</code>

<code>Hooks:</code>

<code>- type: branch</code>

<code>instr: b</code>

<code>func: NoHurryUp</code>

<code>addr: F5783D8</code>

What does this code do exactly? The <code>Files:</code> includes the nohurryup.S assembly file that we just created, from the path <code>source/</code>. The <code>Hooks:</code> section is a bit more complicated though. First we declare the type to be <code>branch</code>, because we will be branching to a different part of the code. The instruction is <code>b</code>, because we want it to branch unconditionally no matter what. The function (label) it will branch to is <code>NoHurryUp</code> as that is what we named our function to be in the assembly. Finally, the address is <code>F5783D8</code> because that is what the address for the target function was in the symbol map. Let's compile and test it out!

===Conclusion and Review===
(insert video of it working)

Revision as of 01:23, 10 March 2021

wip

Requirements

Guide

(blah blah introduction) To introduce you to the basic ASM build system, we will be making a small patch to disable the music speedup after 100 seconds.

Preparation

Before we start creating our patch, first we need to include the asmsetup.S file in our project. Simply download the file from here, and place it in your /include/ directory in the [[NSMBU-Haxx]] project files.

Finding our target function

To find our target function, we can refer to the [[Symbol Map]] to see the address for the specific function. In our case, the symbol is shouldHurryUp__11MusicPlayerFv which corresponds to 0xF5783D8. Let's make a hook to disable it!

Patching the function

To try and disable the function, we can try and insert a nop function that does nothing. To do this first we create a YAML file called nohurryup.yaml. Inside it, we can insert the following lines of code:

---

Files: []

Hooks:

- type: nop

addr: F5783D8

What this does is replace the instruction at address F5783D8 with a nop instruction which does nothing. This should work right? Well, let's test it out.

(insert video of crash)

It seems like this just crashes the game. Why is this so?

Patching the function (the correct way)

(insert technical reason of why this doesn't work)

The correct way of doing this, is to instead do a blr instruction, which will skip the function and return. The thing is, the function returns a bool (a 1 or a 0), but a blr would just return what is in register 3 (the return register). So what we need to do is set register 3 to zero, or 0x0 and then return with blr. Another issue we will run into is that a YAML hook cannot just do this by itself. For that we will need a .S, or assembly file.

Creating the assembly file

Let's create a file in /source/ called nohurryup.S. The assembly we will be writing is quite simple. All it has to do is set register 3 to 0x0 and then return. Before we can do that though we need to add some setup lines.

.text


.include "asmsetup.S"


.global NoHurryUp

NoHurryUp:

li r3, 0x0

blr

Let's break down what this does. The .text indicates that the following is code to be executed. The .include will include the asmsetup.S file into our own file. The NoHurryUp label contains a load immediate (li) instruction that sets register 3 to 0x0, or 00 00 00 00, and then it has a blr instruction that returns back to the link register. The .global means that the NoHurryUp label will be accessible from anywhere. Don't forget to end the file in a newline!

What does this code do exactly?

Creating the hook

Now instead of having the code in the YAML, lets include the nohurryup.S file that we just created. In the YAML write the following code:


---

Files: [source/nohurryup.S]

Hooks:

- type: branch

instr: b

func: NoHurryUp

addr: F5783D8

What does this code do exactly? The Files: includes the nohurryup.S assembly file that we just created, from the path source/. The Hooks: section is a bit more complicated though. First we declare the type to be branch, because we will be branching to a different part of the code. The instruction is b, because we want it to branch unconditionally no matter what. The function (label) it will branch to is NoHurryUp as that is what we named our function to be in the assembly. Finally, the address is F5783D8 because that is what the address for the target function was in the symbol map. Let's compile and test it out!

Conclusion and Review

(insert video of it working)