ROM Hacking Need a helping hand? You can get help on any ROM Hacking-related problems or questions you have here.

#1
May 14th, 2018 (5:42 AM).
 Lance Koijer 2.0 Lance Koijer Join Date: Apr 2018 Location: Criscanto Town Gender: Male Nature: Adamant Posts: 58
Hello guys..

I am trying to code an ASM that checks if the current HP is lower or equal to the Half of the 1st Party Pokémon's Total HP.

I have thought how to get the half in an ASM but the problem I have encountered is the byte indicator is not writing in the loaded address..

here's the code:
Code:
```.text
.align 2
.thumb
.thumb_func

main:
push {r0-r3, lr}
ldr r0, =(0x20242Dc) @loads the Total HP
ldrb r0, [r0]
ldr r1, =(0x20242Da) @loads the current hp
ldrb r1, [r1]
mov r2, #0x5 @multiplies to 5 to get the half
mul r0, r2, r0 @gets the half of Total HP
mov r3, #0xA @moves one decimal to current HP
mul r1, r3, r1 @gets the value of Current HP
cmp r1, r0 @compares if Current HP is higher than Half of the total HP
bhi push2
ldr r1, =(0x203c1b4) @freespace
mov r0, #0x1
strb r1, [r0]	 @stores byte 1 to indicate that the Current HP is lower than or equal the half of total HP
pop {r0-r3, pc}

push2:
ldr r1, =(0x203c1b4)
mov r0, #0x0
strb r1, [r0] @stores byte 0 to indicate that the Current HP is higher than the half of total HP
pop {r0-r3, pc}```
it feels to me that it's either the storing of the byte or the branch checking makes the code not functioning. If someone can help me fix this, it is much appreciated. Thanks.

#2
May 15th, 2018 (7:03 AM). Edited May 15th, 2018 by Koople.
 Koople Join Date: Nov 2016 Nature: Relaxed Posts: 69
Quote:
 Originally Posted by Lance Koijer 2.0 Hello guys.. I am trying to code an ASM that checks if the current HP is lower or equal to the Half of the 1st Party Pokémon's Total HP. I have thought how to get the half in an ASM but the problem I have encountered is the byte indicator is not writing in the loaded address.. here's the code: Code: ```.text .align 2 .thumb .thumb_func main: push {r0-r3, lr} ldr r0, =(0x20242Dc) @loads the Total HP ldrb r0, [r0] ldr r1, =(0x20242Da) @loads the current hp ldrb r1, [r1] mov r2, #0x5 @multiplies to 5 to get the half mul r0, r2, r0 @gets the half of Total HP mov r3, #0xA @moves one decimal to current HP mul r1, r3, r1 @gets the value of Current HP cmp r1, r0 @compares if Current HP is higher than Half of the total HP bhi push2 ldr r1, =(0x203c1b4) @freespace mov r0, #0x1 strb r1, [r0] @stores byte 1 to indicate that the Current HP is lower than or equal the half of total HP pop {r0-r3, pc} push2: ldr r1, =(0x203c1b4) mov r0, #0x0 strb r1, [r0] @stores byte 0 to indicate that the Current HP is higher than the half of total HP pop {r0-r3, pc}``` it feels to me that it's either the storing of the byte or the branch checking makes the code not functioning. If someone can help me fix this, it is much appreciated. Thanks.
A couple notes:
- The current/total HP are halfwords (2 bytes), so you should be using ldrh
- you should use a right shift to divide by 2 (lsr rX, rX, #0x1)
- you're storing r1 into r0. So you are storing the address into address 0x0 or 0x1. You should do strb r0, [r1] to store the value of r0 into the address of r1
- EDIT: and rather than using some free ram, you might want to store into a temporary variable such as the lastresult variable at 0x020370d0

Hope this helps :)
#3
May 15th, 2018 (5:01 PM).
 Lance Koijer 2.0 Lance Koijer Join Date: Apr 2018 Location: Criscanto Town Gender: Male Nature: Adamant Posts: 58
Quote:
 Originally Posted by Koople A couple notes: - The current/total HP are halfwords (2 bytes), so you should be using ldrh - you should use a right shift to divide by 2 (lsr rX, rX, #0x1) - you're storing r1 into r0. So you are storing the address into address 0x0 or 0x1. You should do strb r0, [r1] to store the value of r0 into the address of r1 - EDIT: and rather than using some free ram, you might want to store into a temporary variable such as the lastresult variable at 0x020370d0 Hope this helps :)
Thanks for the response. Very.

I'll gonna try that.
#4
May 16th, 2018 (7:27 AM).
 FBI Free supporter Join Date: Jan 2013 Location: Unknown Island Gender: Male Posts: 1,865
Well, that’s the most complicated solution to checking if a number is double. I’d optimize that portion of it, and also generalize this into an easy to call function.

Quote:
 push {lr} mov r3, #100 mul r3, r3, r0 ldr r0, totalhp add r0, r0, r3 ldrh r1, [r0, #2] ldrh r0, [r0] do calc
This would require a small wrapper to call it from a script properly, but it’d be worth it to get rid of the hard coded portions.
__________________
...
#5
May 17th, 2018 (7:48 PM).
 Lance Koijer 2.0 Lance Koijer Join Date: Apr 2018 Location: Criscanto Town Gender: Male Nature: Adamant Posts: 58
Quote:
 Originally Posted by FBI Well, that’s the most complicated solution to checking if a number is double. I’d optimize that portion of it, and also generalize this into an easy to call function. This would require a small wrapper to call it from a script properly, but it’d be worth it to get rid of the hard coded portions.
Sweet. Thanks for this.

# Join the conversation!

Create an account to post a reply in this thread, participate in other discussions, and more!