Button and if codes in ASM [Enable/Dissable]

Started by Deathwolf, July 21, 2010, 07:27:14 PM

Previous topic - Next topic

Deathwolf

how to include a button activator in ASM?
I've seen all the time this:
94210000 9161YYYY

example:

C2056578 00000003
3DC04100 61CE0000
91DC01AC 80DC01AC
60000000 00000000

lis r14,0x4100
ori r14,r14,0x0000
stw r14,428(r28)
lwz r6,428(r28)


button activator:
2840A5E0 0000YYYY

and if I don't press the button, it should change it back.

I know the -HACKING GUIDE- ASM example, making moon jump codes but it doesn't work
for me.

lolz

Romaap

you could do something like:

if button not pressed
write original instruction (with a normal ramwrite)
endif, if button pressed
the C2 code


or you could read this tutorial, its about making a moonjump code in ASM but it also explains how to make the button activator in ASM

Deathwolf

thanks.

btw:

lis r0, 0x8049                //Loads first 2 bytes of Control address
lwz r1, 0x6AC0(r0)         //Loads the full value of the control address into r1
li r2, 0x00000200           //Loads the value for the button we want to be the activator into r2
lis r3, 0x80CC               //Loads first 2 bytes of co-ords
lwz r4, 0x4584(r3)         //Loads the full value of the z-co-ords into r4
li r5, 0x0000004C          //Loads the value we want to add to co-ords (jump speed) into r5
cmpw r1, r2               //Compares the BUTTON value and the CONTROLLER address (check if we are holding our activator or not)
bne +0x12                 //If we are NOT holding the button activator, jump to the end i.e cancel
add r6, r4, r5             // If we ARE, add 0x004C to our z-co-ord value (increase our height)
stw r6, 0x4584(r3)       //Store the modified co-ords back to their address!

lis r0, 0x8049   and  lwz r1, 0x6AC0(r0) load the button address?
li r2, 0x00000200 = store the button value 0200 into r2?
lis r3, 0x80CC  and  lwz r4, 0x4584(r3) load the address we wan to change into r4?
li r5, 0x0000004C = load the value 4C into r5?
cmpw r1, r2   = looks if it pressed and activate it if pressed?
bne +0x12   = looks if not pressed and cancel it?
add r6, r4, r5    =  add 4C?
stw r6, 0x4584(r3) = store it back?
lolz

Deathwolf

#3
so ok I've assembled one.

81236B24=jump address. write= 803CA34C:  987D06A0   stb   r3,1696(r29)
80756100= button address. write= 805484D0:  90030000   stw   r0,0(r3)

code:

lis r0, 0x8054
lwz r1, 0xFFFF849C(r0)       
li r2, 0x00000800       
lis r3, 0x803C           
lwz r4, 0xFFFFA34C(r3)     
li r5, 0x00000040   
cmpw r1, r2           
bne +0x12               
add r6, r4, r5       
stw r6, 0xFFFFA34C(r3)

assembly:

C2000000 00000006 <-- what address should I use?
3C008054 8020849C
38400800 3C60803C
8083A34C 38A00040
7C011000 40820010
7CC42A14 90C3A34C
60000000 00000000
lolz

dcx2

Quote from: Deathwolf on July 21, 2010, 07:27:14 PM
how to include a button activator in ASM?
example:

C2056578 00000003
3DC04100 61CE0000
91DC01AC 80DC01AC
60000000 00000000

lis r14,0x4100
ori r14,r14,0x0000
stw r14,428(r28)
lwz r6,428(r28)


button activator:
2840A5E0 0000YYYY

Romaap is right, one way to do it is to use normal WiiRD codes.

2840A5E0 0000YYYY   # if button activator
C2056578 00000003   # C2 hook
3DC04100 61CE0000
91DC01AC 80DC01AC
60000000 00000000
E2100000 00000000   # else
04056578 ZZZZZZZZ   # Z = original instruction at 80056578
E0000000 80008000

When the button is pressed, the C2 hook is run.  When the button is not pressed, Z is run instead.

---

You can also do it in pure ASM.

lis r12,0x8040      # r12 = controller pointer
ori r12,r12,0xA5E0
lhz r12,0(r12)      # r12 = controller value
cmpwi r12,YYYY      # compare r12 to YYYY
bne- 0x0C         # if Not Equal, branch
lis r12,0x4100      # if equal, write 41000000...
stw r12,428(r28)      # ...to 428(r28)
lwz r6,428(r28)      # bne- would take you here

I have a few comments about this.

1) What did I tell you about using r12?  Your original code uses r14.
2) lis writes 0 to the lower 16-bits automatically.  You only need to use ori if the lower 16-bits are not 0.
3) Be careful when calculating the branch distance.  In this case, it is 0x0C = 12.  4 would branch to the next instruction (i.e. skip 0 instructions), 8 skips 1 instruction, C skips 2 instructions.
4) Do you use PyiiASMH or Link's ASMWiiRD?  PyiiASMH supports "branch labels", which automatically calculate branch distance.  This is very, very helpful!
5) If you want to copy the Mask behavior of the 28 code type (i.e. 2840A5E0 FFFE0001), you can use andi. to do the masking.  Make sure you remember that there is a . after andi.!!  The . means "update the Condition Register"; the Condition Register is used when comparing values.
6) I assume that lwz r6,428(r28) is the original instruction.  Therefore I made sure it is *always* executed.  If you're pressing the button, we "fall through" after the stw.  If you are NOT pressing the button, we branch over the stw.

---

Quote from: Deathwolf on July 21, 2010, 08:14:34 PM
lis r0, 0x8049                //Loads first 2 bytes of Control address
lwz r1, 0x6AC0(r0)         //Loads the full value of the control address into r1
li r2, 0x00000200           //Loads the value for the button we want to be the activator into r2
lis r3, 0x80CC               //Loads first 2 bytes of co-ords
lwz r4, 0x4584(r3)         //Loads the full value of the z-co-ords into r4
li r5, 0x0000004C          //Loads the value we want to add to co-ords (jump speed) into r5
cmpw r1, r2               //Compares the BUTTON value and the CONTROLLER address (check if we are holding our activator or not)
bne +0x12                 //If we are NOT holding the button activator, jump to the end i.e cancel
add r6, r4, r5             // If we ARE, add 0x004C to our z-co-ord value (increase our height)
stw r6, 0x4584(r3)       //Store the modified co-ords back to their address!

lis r0, 0x8049   and  lwz r1, 0x6AC0(r0) load the button address?
li r2, 0x00000200 = store the button value 0200 into r2?
lis r3, 0x80CC  and  lwz r4, 0x4584(r3) load the address we wan to change into r4?
li r5, 0x0000004C = load the value 4C into r5?
cmpw r1, r2   = looks if it pressed and activate it if pressed?
bne +0x12   = looks if not pressed and cancel it?
add r6, r4, r5    =  add 4C?
stw r6, 0x4584(r3) = store it back?


Where did you get this?  It's very, very bad.

1) You should never, ever write to r1 or r2.
2) It's longer than necessary; you don't need to put your button value (0200) into r2, you can just use cmpwi.
3) It does a bne 0x12, which is not aligned (remember alignment?)
4) lwz is one of those odd instructions that uses (rA|0).  This means that if you use r0 as the address for lwz, it will ignore the value in r0 and use the value 0 instead.


Thomas83Lin

#6
Quote from: dcx2 on July 21, 2010, 09:11:02 PM

Where did you get this?  It's very, very bad.

1) You should never, ever write to r1 or r2.
2) It's longer than necessary; you don't need to put your button value (0200) into r2, you can just use cmpwi.
3) It does a bne 0x12, which is not aligned (remember alignment?)
4) lwz is one of those odd instructions that uses (rA|0).  This means that if you use r0 as the address for lwz, it will ignore the value in r0 and use the value 0 instead.
sorry for the hi-jack but i could shorten some of my codes up a bit with your help, using just the lwz without the ori and just using a cmpwi here is one of my examples of how i normally write them up
[spoiler]
Pac'n-Roll Moon Jump Press(1)
C209B7C8 00000009
9421FFE0 91610008
91810010 3D608041
616B18F0 816B0000
3D800000 618C0200
7C0B6000 40820010
3D603DAF 616B3D1A
91770234 81610008
81810010 38210020
C0170234 00000000

stwu r1,-32(r1)
stw r11,8(r1)
stw r12,16(r1)
lis r11,-32703
ori r11,r11,6384
lwz r11,0(r11)
lis r12,0
ori r12,r12,512
cmpw r11,r12
bne- 0x10
lis r11,15791
ori r11,r11,15642
stw r11,564(r23)
lwz r11,8(r1)
lwz r12,16(r1)
addi r1,r1,32
lfs f0,564(r23)

using your idea i get something like this

C209B7C8 00000007
9421FFF0 91610008
3D608041 816B18F0
2C0B0200 40820010
3D603DAF 616B3D1A
91770234 81610008
38210010 C0170234
60000000 00000000

stwu r1,-16(r1)
stw r11,8(r1)
lis r11,-32703
lwz r11,6384(r11)
cmpwi r11,512
bne- 0x10
lis r11,15791
ori r11,r11,15642
stw r11,564(r23)
lwz r11,8(r1)
addi r1,r1,16
lfs f0,564(r23)
[/spoiler]

Does this look ok, and do you have any more pointers to make it smaller? Thanks

dcx2

I see you're building a stack frame to store your registers for safety.  You don't actually need to do that.  r12 is almost always safe; I have only seen r12 used in one type of situation, which is loading the CTR register immediately before a bctr or bctrl.

Seeing as how you're down to one register, you can get rid of the stack frame altogether.

This reduces your code to

lis r12,-32703
lwz r12,6384(r12)
cmpwi r12,512
bne- THE_END
lis r12,15791
ori r12,r12,15642
stw r12,564(r23)

THE_END:
lfs f0,564(r23)

Note that I used a branch label.  You will need PyiiASMH to convert this to a C2 code.  But I like branch labels because they spare me counting instructions, and if you add something between the branch and its destination then you don't have to re-count everything.

---

Regarding a stack frame for safety, I do believe I once read that you can use negative offsets on the stack pointer to store data on the unallocated part of the stack.  I was concerned whether or not an interrupt service routine might eventually stomp on that part of the stack, but then I read somewhere that the PPC ISR's leave some room in case the current function is using unallocated stack...

Deathwolf

so no tut how to do?
no examples?
that's bad... :-[
lolz


Deathwolf

are you sure this will work?

2840A5E0 0000YYYY   # if button activator
C2056578 00000003   # C2 hook
3DC04100 61CE0000
91DC01AC 80DC01AC
60000000 00000000
E2100000 00000000   # else
04056578 ZZZZZZZZ   # Z = original instruction at 80056578
E0000000 80008000
lolz

dcx2

I can't be sure without having the game and testing it myself.  But the general template will work.

28-if-equal button activator
C2 hook
else
04 write original instruction
terminator

Deathwolf

#12
lol ok but thanks.

I'll put this instrucion into ASM to WiiRd ;D

code:

28-if-equal button activator
C2 hook
else
04 write original instruction
terminator

assembly should be :

2840A5E0 0000YYYY
C2056578 00000003   
3DC04100 61CE0000
91DC01AC 80DC01AC
60000000 00000000
E2100000 00000000   
04056578 ZZZZZZZZ
E0000000 80008000
lolz

dcx2

Two little things bug me, though.

1) button activators are usually a multiple of 2.  Are you sure it shouldn't be 2840A5E2?

2) Make sure you replace Z with the original instruction that the hook will be over-writing.

Deathwolf

lolz