I have a general question when switching modes. I would like to place my hack in arm9 to run my code that is placed somewhere in memory (which is in arm mode). Then I would like to end it and jump back into thumb mode. I read that using "bx r#" allows you to switch between modes, but I have yet to see this work. I use emulator (no$gba) and the code would jump from the arm9 to my code and back and continue to process the game. When I load the nds game into the flash card, it does not and freezes. I believe that it is jumping to my code but in the wrong mode which causes it to crash.
I looked at cracker's hooking tutorial (http://crackerscrap.com/index.php?p=docs) but it doesnt work for me (tried something similar).
Here is the part where I followed:
Code:
@37FFB20 <---- thumb
ldr r0,[r1]
cmp r0,#0x0
blt 0x37FFB3C
mov r2,#0x5
ldr r4,[r15,#-0x2]
bx r4
.long 0x23FE000
mov r4,#0x8
Notice that r4 is being loaded with a new value right after our hook so we know it is safe to use it for our jump.
@23FE000 <--- arm
stmdb r13!,{r0-r12,r14} ;@ Save registers to stack
;@ Start of your function
...
;@ End of your function
ldmia r13!,{r0-r12,r14} ;@ Restore registers from stack
sub r0,r2 ;@ Patch in overwritten opcodes
str r0,[r1]
add r1,#0xFC
ldr r0,[r1]
ldr r4,[r15]
bx r4 ;@ jump to IRQ handler
.long 0x37FFB31 ;@ return address ORed with 1 to switch back to Thumb mode
ldr r0,[r1]
cmp r0,#0x0
blt 0x37FFB3C
mov r2,#0x5
ldr r4,[r15,#-0x2]
bx r4
.long 0x23FE000
mov r4,#0x8
Notice that r4 is being loaded with a new value right after our hook so we know it is safe to use it for our jump.
@23FE000 <--- arm
stmdb r13!,{r0-r12,r14} ;@ Save registers to stack
;@ Start of your function
...
;@ End of your function
ldmia r13!,{r0-r12,r14} ;@ Restore registers from stack
sub r0,r2 ;@ Patch in overwritten opcodes
str r0,[r1]
add r1,#0xFC
ldr r0,[r1]
ldr r4,[r15]
bx r4 ;@ jump to IRQ handler
.long 0x37FFB31 ;@ return address ORed with 1 to switch back to Thumb mode
At the end I would like to return to thumb mode, so I tried to OR the address but it didnt work.
I am not sure how to do this correctly but if someone can provide an example that would help a lot.