Random Halfword Generator PPC ASM
#1
I haven't tested this yet, but this is my attempt of writing the Middle Square Theorem (using the Weyl sequence to prevent convergence). I had to 'downsize' though since my Seed is 32 bits instead of 64. Thus, halfword output is used for the random output instead of a word.

The seed is using the lower 32 bits of the TB. Ofc, including the upper 32 bits is not a good idea.

For more info, visit this link - https://pthree.org/2018/07/30/middle-squ...ence-prng/

======

Args:
r3 = Amount of times to cycle the generator (recommend that this number be at least 4)
r3 must be anything from 1 to 0xFFFFFFFF (value treated logically for # of cycles)

Error Code:
r3 = 0 if r3's arg was invalid

Source: #The following snippet of code is meant to be used as a subroutine reached via a branch-link instruction

cmpwi r3, 0
beqlr-

mtctr r3 #LR doesn't need to be backed up for this subroutine
mftbl r4 #Seed register

li r3, 0 #Square-value register, start at 0
li r5, 0 #Starting Weyl variable, start at 0

clrlwi. r0, r4, 31 #Seed needs to be an odd number
bne- loop

addi r4, r4, 1 #Make seed odd

loop:
mullw r3, r3, r3 #Square the Value

add r5, r4, r5 #Add the constant seed to Weyl variable. Result is new Weyl variable
add r3, r3, r5 #Add Weyl variable to squared value

srwi r3, r3, 16 #Place upper 16 bits into lower 16 bits, upper 16 bits are now cleared
bdnz+ loop

blr #r3 contains random halfword result

Check out my SEEPROM Viewer HBC App! - https://github.com/VegaASM/Seeprom-Viewer

~Over 100 MKWii Cheat Codes created
~Main Time Updater for the MKWii Player's Page
~Borrowing Star's USB Gecko and owes JoshuaMK $1000 for 1000th post

Reply
#2
This is really cool.
(10-14-2019, 11:55 PM)Vega Wrote: Was lucky enough to be one of few to learn proper ASM. Now I just post random garbage on Make it to a 1000 thread.
Was lucky enough to be one of few to learn proper ASM. Now I'm writing codes that I hope amaze!  Tongue
Reply
#3
Thx. It's part of a code I was working on. Thought i would share it.

Do you have any ideas for a 64 bit seed? If so, i can use mullw mulhwu for a random word output instead of halfword

Check out my SEEPROM Viewer HBC App! - https://github.com/VegaASM/Seeprom-Viewer

~Over 100 MKWii Cheat Codes created
~Main Time Updater for the MKWii Player's Page
~Borrowing Star's USB Gecko and owes JoshuaMK $1000 for 1000th post

Reply
#4
I'll see if I can come up with something
(10-14-2019, 11:55 PM)Vega Wrote: Was lucky enough to be one of few to learn proper ASM. Now I just post random garbage on Make it to a 1000 thread.
Was lucky enough to be one of few to learn proper ASM. Now I'm writing codes that I hope amaze!  Tongue
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)