So I was reading a few threads on this site and I found one on how to make one..
But I can't really find a link that explains more about how to code it..
My textbook for the course didn't provide any information about RNG at all so no help there.
The code was
li $a1, 4
li $v0, 42
add $a0, $a0, 1
is this correct for asking for a range between 1-3?
I tried outputting what random number it was but it gave me the 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 the sw $a0, 0($s0) but I'm not sure what that does -- is it needed to output? (I took it out because after I pushed a key to go to the RNG, it said the program crashed)
I keep getting the output of 268501267 which I'm not sure what that means
edit: now it started giving me 268500992 all the time
Can anyone help explain this a little more in depth?
Logically speaking -- I understand the where the 42 comes from and why I need to add +1 (This is so I won't get the value of 0)
From there, I have no clue on why the code won't output a number in the range I gave.
As stated by MARS documentation (in the GUI Help > Syscalls):
random int 41 $a0 = i.d. of pseudorandom number generator (any int).
$a0 contains the next pseudorandom, uniformly distributed int value
from this random number generator's sequence
So
li $v0, 41 ; Service 41, random int
li $a0, 0 ; Select random generator 0
syscall ; Generate random int (returns in $a0)
li $v0, 1 ; Service 1, print int
syscall ; Print previously generated random int
Works fine for me, every time a different number is printed.
You don't need to initialize or seed a random stream before using it, you can just use stream 0.
Related
so currently my code is just as shown below:
li $v0, 4 # printt first
la $a0, first
syscall
li $v0, 5 # receive input for first
syscall
add $s0, $v0, $zero # move first to s0
We are learning pipeline processing and I have an exercise, where I have to count the number of cycles a MIPS code runs for - by hand. We have the following code in C and MIPS:
0. for (i=0; i<10; i++)
1. som = som + a[i];
0. add $t0, $zero , $zero
1. L:
2 sll $t1 , $t2, 2
3 add $t1 , $a0 , $t1
4 lw $t1, 0($t1)
5 add $v0, $v0, $t1
6 slti $t2, $t0, 9
7 addi $t0, $t0, 1
8 bne $t2, $zero, L
9 nop
The code inside the loop has 7 instructions and it is executed 10 times, therefore we get 70 cycles. Then we have a stall cycles in line 5 because of the load use hazard on $t1. Again the code runs 10 times, thus an additional 10 cycles. In total we now have 80 cycles. Because of the bne, we have 9 flushes (not 10 because after 9 we don't come back down). Therefore, now we have 89 cycles. We then have 1 cycle for the one NOP and 1 cycle for the initialisation of the variable i. This gives us a total of 91 cycles. In the MIPS reference sheet, it says to always add 4, because of the first line of code, thus bringing our total to 95 cycles.
Assuming I'm right, which I should be, is there a faster way to count the number of cycles in any MIPS code? I could use the equation CPI = # of cycles / clock rate but assuming we're not given that information and only have the given code, is there a way? I found that if you use the following equation, you get the right answer for the given code.
# of cycles = 4 + # of instructions x (# of pipeline levels - 1) + # of NOPs
However, I'm not sure if this works in general or I got lucky with my specific example. BTW - # refers to the number of.
I need to loop through this array of bytes
testCases: .byte 0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47
Im assuming I would do something like this, but not sure
ori $a1, $0, 0x0 # Initialize index with 0
LOOP:
lw $t1, testCases($a1)
...
...
addi $a1, $a1, 1 # Increment index by 1
j LOOP
and isolate the b6,b2,b1,b0 bits using a bitmask. I'm very new to mips and would appreciate any help. Thank you.
No, lw stands for load word. A word is 4 bytes. If you want to load a single byte you should use lb (if you want sign-extension) or lbu (if you want zero-extension).
Good evening. I am trying to figure out how to determine if an integer qualifies 16 bit integer in MIPS.
I understand that 2^15-1 =32767 or 2^(16-1)-1=32767 and that we want 16 bit values for binary number. Anyway, I am trying to determine if an integer passes the test. I wrote this:
addi $s3, $zero, 32767
bgt $t2, $s3, else #branch to else if t2>s3
move $v0, $t2 #if no overflow; place t2 in v0
addi $v1, $zero, 0 #if no overflow; place zero in v1
else:
addi $v0, $zero, 0 #if overflow; place 0 in v0
addi $v1, $zero, -1 #if overflow; place -1 in v1
Anyway, There's a problem with my logic when I try and evaluate negative numbers. I have assignment due tomorrow. I am learning MIPS programming. I am not a programming snob so any helpful advice is appreciate. Thank you for your time.
This is too late for you assignment1.
It's a bit unclear whenever you want to test a) if a number N encoded as a 32-bit two's complement number can be also encoded as a 16-bit two's complement number or if you want to test b) if a 32-bit number can be encoded as a 16-bit number.
In the case of b) you just need to test if any bit higher than the 16th is set:
#Assume $t0 is the number to test
lui $t1, 0xffff #$t1 = 0xffff0000
and $t1, $t1, $t0 #$t1 is zero if all higher bits of $t0 are zero
beq $t1, $0 fits16bits #Jump to label if fits
#Here the number doesn't fit 16 bits
For the case a) the key is to understand that just like the number 0x00f1 and the number 0x0000000f1 are the same, the leading zeros are not significant, the number 0xffff and the number 0xffffffff are the same number in two's complement (the number -1).
To extend a 16-bit two's complement number to 32-bit we need to perform a sign extension, i.e. replicate the most significant bit (the sign bit) of the original number in the upper 16 bits.
So 0x7fff becomes 0x00007fff, 0xc000 becomes 0xffffc0000.
A simple way to test that all the upper 17 bits are equal is to shift them right arithmetically so that if they actually are equal we end up with 0x00000000 or 0xffffffff.
sra $t1, $t0, 15 #Shift right 16 bits duplicanting the MSb
beqz $t1, $0, fits16bits #Jump to label if fits (All zero)
addiu $t1, 1 #Add 1
beqz $t1, $0, fits16bits #Jump to label if fits (Before +1 was all ones)
#Here the numbers doesn't fit
1 Maybe it's better this way.
I'm trying to sort a list of floating point integers in MIPS, which I think I'll be able to figure out, however, each of those float points corresponds to the score of a player in a game. I'm trying to figure out a way to remember that, for example, Bobby's score was 12.3, after I sort the floats in descending order, so that I could return something like:
"Bobby 12.3, Johnny 10.2, Carl 8.8".
Currently I'm storing the floats in the F registers as I read them in, and the names in a block of dynamically allocated memory whose size is generated based on how many players the user is considering.
.data
Prompt1: .asciiz "Please enter number of players.\n"
Prompt2: .asciiz "Please enter name of a player. (Max len 21 characters).\n"
Prompt3: .asciiz "Enter player's points per game.\n"
Prompt4: .asciiz "Enter player's minutes per game.\n"
numPlayers: .space 4
.text
.align 2
.globl main
main:
subu $sp, $sp, 32
sw $ra, 0($sp)
li $v0, 4
la $a0, Prompt1
syscall
li $v0, 5
la $a0, numPlayers
syscall
move $a0, $v0
jal collect
collect:
subu $sp, $sp, 32
sw $ra, 0($sp)
sw $a0, 4($sp)#number of players
addi $t1, $zero, 21 #21 is max length of player name in characters; 21 bytes
mult $t0, 4($sp), $t1 #number of bytes you're going to need for strings for x players
lw $a0, $t0 #a0 now has number of bytes you want for all of the strings
li $v0, 9
syscall
la $t2, $v0 #store memory allocated address into t2
la $t9, $t2 #remember the head you started at for strings
#memory has been made for strings
#make memory for floats now
addi $t0, $zer0, 32 #bits for a floating int, 1 calculation per player
mult $t0, $t0, 4($sp) #number of players times floating point space
lw $a0, $t0 #a0 now has number of bytes for all the floats
li $v0, 9
syscall
la $t8, $v0 #store memory allocated address into $t8 for floats
la $t7, $t8 #remember head you started at for floats
loop:
beq $t0, $zero, sort
la $a0, Prompt2 #print string asking for a player name
la $v0, 4
syscall
la $a0, $t2 #load address of huge memory space
la $a1, 21 #max characters to read is 21
la $v0, 8 #read string and store into address supplied by $a0
syscall
addi $t2, $t2, 21 #move up 21 places in the memory for t2
#so that you're sure you're at an empty space in memory for the next string
#time to read floats
#ask for the points per game
la $a0, Prompt3 #load string to ask points per game
li $v0, 4
syscall
li $v0, 6
syscall #f0 has the float
mov.s $f1, $f0 #f1 has points per game for player 1
la $a0, Prompt5 #load string to ask minutes per game
li $v0, 4
syscall
li $v0, 6
syscall #f0 has the float
mov.d $f2, $f0 #f2 has minutes per game
div.d $f3, $f1, $f2 #divide f1 by f2 store in f3
mov.d $t8, $f3 #t8 now has the points per minute for player
addi $t8, $t8, 32 #move up 32 spots in memory
#how to associate name and player?
addi $t0, $t0, -1 #decrement counter of how many more players you need to do
b loop
sort:
#to be figured out later
You can just do the same with the names as with the scores when you do the sorting, that is switching places, appending to a temporary list, etc.
That being said, it might be easier is you make a list of addresses for your names instead of all the names after each other. Thus, allocate a block of memory per name and store in the list. This way, sorting will be easier.