Using a random number generator in MIPS? -
so reading few threads on site , found 1 on how make one.. can't find link explains more how code it.. textbook course didn't provide information rng @ no there.
the code
li $a1, 4 li $v0, 42 add $a0, $a0, 1
is correct asking range between 1-3? tried outputting random number gave me same number constantly.
#sw $a0, 0($s0) li $a1, 4 li $v0, 42 add $a0, $a0, 1 #syscall li $v0, 4 la $a0, checking syscall li $v0, 1 move $t0, $a0 syscall
i saw sw $a0, 0($s0)
i'm not sure -- needed output? (i took out because after pushed key go rng, said program crashed) keep getting output of 268501267 i'm not sure means edit: started giving me 268500992 time
can explain little more in depth?
logically speaking -- understand 42 comes , why need add +1 (this won't value of 0)
from there, have no clue on why code won't output number in range gave.
as stated mars documentation (help > syscalls):
random int 41 $a0 = i.d. of pseudorandom number generator (any int). $a0 contains next pseudorandom, uniformly distributed int value random number generator's sequence
so
li $v0, 41 ; service 41, random int xor $a0, $a0, $a0 ; select random generator 0 syscall ; generate random int (returns in $a0) li $v0, 1 ; service 1, print int syscall ; print generated random int
works fine me, every time different number printed.
Comments
Post a Comment