ASM & Register Safety
#1
ASM & Register Safety

NOTE: You must already know the basics of ASM before reading this thread! This is not for noobs or people who pretend to be real coders.

If you have been browsing the Cheat Codes forum for quite some time, you may notice that some codes may start with the following ASM:


stwu r1,-80(r1)
stmw r14,8(r1)


..and end with the following ASM:


lmw r14,8(r1)
addi r1,r1,80

These ASM functions are done for what is known as "Register Safety" There are 32 normal integer registers for the Wii's PowerPC processor (starting at r0 all the up to r31). Some registers are more 'important' than others, some registers have different purposes, etc etc. Registers 14 and up are known as 'non-volatile' registers. Making an ASM code using these registers in a plain sense is dangerous. Thus, you can effect the game in many unwanted ways, or just crash the game in general.

For Registers 0, and 3 thru 12, it really depends on your specific code whether or not they are safe to use. Registers 11, and 12 (99% of time) are safe to use in any code. Register 1 is for the stack pointer, it has its own purpose. Thus you should never use it in a plain sense. Moving onto Register 2 & 13....

Never use Registers 2 & 13 ever! These registers are used by the CPU to access specific areas of memory related to constants, global variables, etc.  They will be read during interrupts which you will not see or know about. 

Ok so let's say you are writing an ASM, and it appears that only Registers 11 and 12 are safe for extra use in your ASM. Well how can we use the non-volatile registers (r14 - r31)? We use the stack pointer register (r1) and store the values of Registers 14 - 31 onto the stack. This is done via the following ASM functions:

stwu r1,-80(r1) # make space for 18 registers
stmw r14,8(r1) # push r14-r31 onto the stack

Alright great, you can now use those non-volatile registers without worry. You make your ASM and you're done, right? Wrong, we need to move those registers' values back off the stack, or else you will crash the game. Once you are done with writing your ASM for your code, end it off with the following ASM....

lmw r14,8(r1) # pop r14-r31 off the stack
addi r1,r1,80 # release the space

Keep in mind that doing this push/pop stack stuff uses up memory. Will it ever lag the game? Maybe, if you are using a crap ton of ASM codes that use the push/pop stack ASM functions. If you are knit-picky, only use the push/pop stack ASM functions when necessary.

Now if you are wondering about Floating Point Registers, you cannot use similar ASM functions for 'push/pop' the stack. Each non-volatile floating point register must be pushed/popped on its own line of ASM code.


#Push all non-volatile FPR's onto the Stack#
stfs f20,88(r1)
stfs f21,92(r1)
stfs f22,96(r1)
stfs f23,100(r1)
stfs f24,104(r1)
stfs f25,108(r1)
stfs f26,112(r1)
stfs f27,116(r1)
stfs f28,120(r1)
stfs f29,124(r1)
stfs f30,128(r1)
stfs f31,132(r1)

#Pop all non-volatile FPR's off the stack#
lfs f20,88(r1)
lfs f21,92(r1)
lfs f22,96(r1)
lfs f23,100(r1)
lfs f24,104(r1)
lfs f25,108(r1)
lfs f26,112(r1)
lfs f27,116(r1)
lfs f28,120(r1)
lfs f29,124(r1)
lfs f30,128(r1)
lfs f31,132(r1)

For a more technical approach of this info, plus more details.. visit this link - https://gamehacking.org/wiird/forum/inde...555.0.html

All Credits to:
dcx2
Y.S.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)