Joined: 09 Aug 2004 Posts: 3
Location: cape town south africa
Post subject: SPIM
hi everyone, i am studying JAVA at the university of cape town but we are now doing SPIM and i was wondering if anyone could give me a link where i can get tutorials on how to do it, or tips on using SPIM....i suck at low level languages!!!!
Mon Sep 27, 04 12:36 pm
Belal User
Joined: 08 Mar 2004 Posts: 84
Location: Dhaka, Bangladesh
Post subject:
Hello goofy,
If you clear your topics (SPIM) more clearly, I can check some for you. Write your need with some more details.
Hope to hear from you.
belal _________________ we've lot of things to think abt curr probs
so, i don't have time to think abt religion or wonder of sceice .......... how ppl can waste their time like this?
Mon Sep 27, 04 11:14 pm
goofy Just In
Joined: 09 Aug 2004 Posts: 3
Location: cape town south africa
Post subject: SPIM
Thanks belal; actually i need work on looping and return addresses to be exact,
Joined: 05 Dec 2004 Posts: 6
Location: Helsinki, Finland
Post subject:
This may be a stupid question, but I am supposed to make a program that counts the number of 1 digits in an array, but SPIM won't allow a line like this:
lw $t1,$t0($a0)
How can I express the proceeding in the array?
Sun Dec 05, 04 2:55 am
Tousif Beginner User
Joined: 25 May 2004 Posts: 20
Location: Nashville, Tennessee, USA
Post subject:
Hi,
So, let me get this clear. First you have an array and now with that line you are trying to access a particular address, lets say the 5th element..right? Where $a0 holds the starting address of the array and $t0 holds the element number.
assuming you already have the address in $a0, move it to another register so that we don't mess with the argument register.
move $s0, $a0
li $t0, 5 (element number)
mul $t0, $t0, 4 ( each word is 4 byte long and the momory is word aligned - so you need to jump by 4 byte)
li $t1, 0 (just to make sure $t1 does not hold any garbage - I don't remember the command to flush a register)
lw $t1, $t0($s0)
now this should load the value stored at the address held by register $a0. Now if you are doing it in a loop, just have a marker like
loop: move .........
.................
here check if you have reached the end.. if so jump to the next
b loop ( branch back to loop)
next : ........
(something like this..)
Hope thats helpful.
Tousif
Sun Dec 05, 04 4:33 am
Jogilius Just In
Joined: 05 Dec 2004 Posts: 6
Location: Helsinki, Finland
Post subject:
Tousif wrote:
mul $t0, $t0, 4 ( each word is 4 byte long and the momory is word aligned - so you need to jump by 4 byte)
Tousif
Um, actually $a1 contains the size of array in 32-bit words, so when counting bits, shouldn't it be multiplied with 32? I didn't use the mul-(pseudo?)instruction, and I should've used lb instead of lw, but anyways, it still doesn't seem to count the bits properly.
Here's the code:
Code:
bitcount:
li $v0,0
move $t0,$a0
add $t2,$a1,$a1 # t2 = 2 * a1
add $t2,$t2,$t2 # t2 = 4 * a1
add $t2,$t2,$t2 # t2 = 8 * a1
add $t2,$t2,$t2 # t2 = 16 * a1
add $t2,$t2,$t2 # t2 = 32 * a1,
addi $t2,$t2,-1
slt $t2,$a1,1 #
beq $t2,1, end # a1 = empty -> go to end
lb $t1,0($t0) # $t1 gets the value of the first bit
loop:
bne $t1,1,skip # if $t1 not 1, skip adding the number of 1 bits
addi $v0,$v0,1 # the number of 1 bits added by one
skip:
addi $t0,$t0,1 #
lb $t1,0($t0) #
slt $t3,$t2,$t0 # in the end of the array or not...
beq $t3,$zero,loop # ...if not, continue the array...
end:
jr $ra
.end
Thanks for trying to help anyway.
e: corrected the slt-line, but now it says "bad address in data/stak read".
Last edited by Jogilius on Sun Dec 05, 04 8:35 am; edited 2 times in total
Sun Dec 05, 04 6:26 am
Tousif Beginner User
Joined: 25 May 2004 Posts: 20
Location: Nashville, Tennessee, USA
Post subject:
OK..I am sorry to say this but what you've just posted makes very little sense to me.
Starting from
li $v0, 0 --> Line 1
from line 3 to line 8, you've made lots off addtion (2^5 * $a1) which shifted whats in $a1 by 5 bits to the left but then at "line 9" you are shifting $a1 by 1 bit and storing it in $t2 again!! Then why did you do all the addtions before?
At "line 10" you are comparing $t2 and 1. Well, this branch will never be taken unless $a1 is 2^31 before and you did a shift left to bring that MSB bit 1 at the beginning. At "line 11" you are just loading whats in $t0.
In the comment for line 11 , you are saying get the first bit, well, you don't get the first bit, you get byte (8 bit).
I dont recall if MIPS has a way to access bit positions in a word. I can tell you one thing if you want to get a particular bit in a word do a bitwise "and".
For example Lets say you have a 8 digit binary: 00100111. You want to see if the 3rd bit from left is 1 or not: do an and with 00000100. This "and" will filter the rest of the bit. So, now if it is 1 you will get 00000100 , or if 0 you will get 0. Now to compare, shift it right 2 bits and compare it with 1.
Anyways, It would be easier if clearly state what is it that you are trying to do.
Tousif
Sun Dec 05, 04 7:52 am
Jogilius Just In
Joined: 05 Dec 2004 Posts: 6
Location: Helsinki, Finland
Post subject:
Tousif wrote:
OK..I am sorry to say this but what you've just posted makes very little sense to me.
Starting from
li $v0, 0 --> Line 1
from line 3 to line 8, you've made lots off addtion (2^5 * $a1) which shifted whats in $a1 by 5 bits to the left but then at "line 9" you are shifting $a1 by 1 bit and storing it in $t2 again!! Then why did you do all the addtions before?
Damn. :P I've changed the registers a couple of times and didn't go through the code to ensure that I didn't fuck up other things. Well, I did. The t2 should've been t4 or something on that slt-line, like this:
slt $t4,$t2,1 #
beq $t4,1, end
Quote:
In the comment for line 11 , you are saying get the first bit, well, you don't get the first bit, you get byte (8 bit).
Yep, I wanted to get the bit out of it.
Quote:
I dont recall if MIPS has a way to access bit positions in a word. I can tell you one thing if you want to get a particular bit in a word do a bitwise "and".
For example Lets say you have a 8 digit binary: 00100111. You want to see if the 3rd bit from left is 1 or not: do an and with 00000100. This "and" will filter the rest of the bit. So, now if it is 1 you will get 00000100 , or if 0 you will get 0. Now to compare, shift it right 2 bits and compare it with 1.
Um, I didn't quite understand that...
Quote:
Anyways, It would be easier if clearly state what is it that you are trying to do.
Tousif
This is an excercise, in which the program should count the number of 1-bits (10011000 has three 1-bits for example) in an array. $a0 contains the address of array; $a1 contains the size of array in 32-bit words.
Sun Dec 05, 04 8:23 am
Jogilius Just In
Joined: 05 Dec 2004 Posts: 6
Location: Helsinki, Finland
Post subject:
Tousif wrote:
I dont recall if MIPS has a way to access bit positions in a word. I can tell you one thing if you want to get a particular bit in a word do a bitwise "and".
I tried to take the first bit out of a byte like this:
So I subtracted 128 from the 8 bits, after which the 8 bit figure should be a positive number, if the first bit was 1, and otherwise a negative one. Right?
ed: If I run one step at a time, I can see that it actually doesn't go to the loop...
Sun Dec 05, 04 8:54 am
Jogilius Just In
Joined: 05 Dec 2004 Posts: 6
Location: Helsinki, Finland
Post subject:
How do I take the first bit from a byte?
Code:
li $t6,0
lb $t1,0($t0)
slt $t6,$t1,$t5
Should'nt this put the first byte of $t0 into register $t6?
Tue Dec 07, 04 7:21 am
Tousif Beginner User
Joined: 25 May 2004 Posts: 20
Location: Nashville, Tennessee, USA
Post subject:
That would simply set register $t6 to either '0' or '1'. Here is an example of how you could count the number of 1's in a byte:
lets assume $a0 holds the size, i.e. how many times I have to run the loop
$a1 holds the address
Code:
move $t0, $a0
lb $t1, 0($a1)
li $s0, 0 (will act as a counter )
(now $t1 has the byte..you need to count how many 1's there are)
Code:
loop: li $t2, 0
andi $t2, $t1, 1 (bitwise "and". LSB = '1' => $t2 = 1 else $t2 = 0)
addu $s0, $s0, $t2 (if we have "1" add it in $s2)
srl $t1, $t1, 1 (shift it so that we can compare the next bit)
subiu $t0, $t0, 1 (counting how many more to go)
blez $t0, done (no more elements left to count)
j loop
done: ...................
now $s0 stores how many 1's you have
I dont have SPIM to test it..but this should work....
Hope this would be helpful...
Cheers,
Tousif
Tue Dec 07, 04 10:58 am
Jogilius Just In
Joined: 05 Dec 2004 Posts: 6
Location: Helsinki, Finland
Post subject:
Tousif wrote:
That would simply set register $t6 to either '0' or '1'. Here is an example of how you could count the number of 1's in a byte:
lets assume $a0 holds the size, i.e. how many times I have to run the loop
$a1 holds the address
Code:
move $t0, $a0
lb $t1, 0($a1)
li $s0, 0 (will act as a counter )
(now $t1 has the byte..you need to count how many 1's there are)
Code:
loop: li $t2, 0
andi $t2, $t1, 1 (bitwise "and". LSB = '1' => $t2 = 1 else $t2 = 0)
addu $s0, $s0, $t2 (if we have "1" add it in $s2)
srl $t1, $t1, 1 (shift it so that we can compare the next bit)
subiu $t0, $t0, 1 (counting how many more to go)
blez $t0, done (no more elements left to count)
j loop
done: ...................
now $s0 stores how many 1's you have
I dont have SPIM to test it..but this should work....
Hope this would be helpful...
Cheers,
Tousif
I'm not sure if that helped. It still doesn't work properly but thanks a lot anyway...