This is part of my code, and I don't know why, $t1 always ends up with 10, when it should be 16. At this point, the data is the following:
$t5 = 4
$t3 = 1
$t2 = 0
and $t1 is 0
(and $t0 is an address)
This is the part of the code:
mul $t1, $t3 , $t5
add $t1, $t1, $t2
mul $t1, $t1, 4
**From here, $t1 should be 16, but it always turns out 10 even if I do li $t1, 16****
add $t1, $t1, $t0
lw $t6, ($t1)
I'm using MIPS 32 with QTSpim
If you set registers->Decimal, the result will be 16. I guess, going from the information you give.
Related
I'm currently working on a palindrome function in MIPS that takes a char array and returns 0 if it is not a palindrome, else 1. I've got everything seemingly working except for the part where I find data at index i and array.length-i-1. If someone could lead me in the right direction I'd really appreciate it.
Here's the java code I'm going off of:
int Palindrome(char[] s) {
for(int i = 0; i < (s.length / 2); i++) {
if(s[i] != s[s.length - 1 - i])
return 0;
}
return 1;
}
Here's the Palindrome function:
palindrome:
# Get length of array and store it in $t1
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $a0, 4($sp)
li $t1, 0
lengthWhile:
lw $t2, 0($a0)
beq $t2, $zero, startPalindrome
addi $t1, $t1, 1
addi $a0, $a0, 4
j lengthWhile
startPalindrome:
div $t2, $t1, 2 # Stores a.length / 2 into $t2
add $t3, $t3, $zero # i value (0 - (a.length/2 - 1))
add $t4, $t4, $t1
subi $t4, $t4, 1 # a.length-1
# $t1 = a.length
# $t2 = a.length / 2
# $t3 = i
# $t4 = a.length - 1
palLoop:
bge $t3, $t2, exitLoop
sub $t4, $t4, $t3 # Puts a.length-1-i into $t4
sll $t5, $t3, 2
add $t6, $a0, $t5
lw $s1, 0($t6)
sll $s3, $t4, 2
add $t7, $a0, $s3
lw $s2, 0($t7)
bne $s1, $s2, return0
# Increase i and start loop again
addi $t3, $t3, 1
j palLoop
return0:
li $v0, 0
jr $ra
exitLoop:
li $v0, 1
jr $ra
Here's the main:
.data
charArray: .word 'a', 'b', 'c', 'd', 'c', 'w', 'a'
.text
main:
# Palindrom Function
la $a0, charArray
jal palindrome
move $a0, $v0
li $v0, 1
syscall
# Exit Program
li $v0, 10
syscall
Thank you in advance for any help. (To specify, palLoop is where I'm having trouble).
Not directly part of your question, but charArray: .word 'a', 'b', 'c', 'd', 'c', 'w', 'a' is really a word array containing a chatarter value, not a character array.
It looks like that will wok as you are using word array everywhere, however wont wok if you use a proper character array.
add $t3, $t3, $zero is t3 = t3 + 0 - what is t3 prior to that?
add $t4, $t4, $t1 - same comment
sub $t4, $t4, $t3 # Puts a.length-1-i into $t4 - will overwrite a.length-1 (t4) so is going not be right on the 2nd time through the loop.
The instructions is:
rol $t0, $t1, n
rolv $t0, $t1, $t2
n: 1bit- 31bit
Is this correct translation of the above instructions?
srl $t1, $s1, 1
sll $t2, $s1, 31
or $s2, $t1, $t2 #combine_words
For rol $t0, $t1, n I would use $at as the scratch register.
Suppose you want to rotate 4 binary digits to the left:
srl $at, $t1, 28 # 32-4 = 28
sll $t0, $t1, 4
or $t0, $t0, $at
For rolv $t0, $t1, $t2 I would emit ($t2 holds the number of bits to rotate):
sllv $t0, $t1, $t2
subu $at, $zero, $t2
addiu $at, $at, 32 # $at = 32-$t2
srlv $at, $t1, $at
or $t0, $t0, $at
This is boiling my brain, I've just started learn MIPS. The assignment asks us to use loops and maybe even a stack to do a simple multiplication by squaring a three digit number. For the assignment to be correct, the program needs to run for a minimum of 30 seconds doing the same calculation over and over.
Obviously I'm not asking for you to do my assignment (I want to learn), however I'm stumped on how to implement nested loops in MIPS, there isn't much online. I found something on Stack Overflow and tried to implement the same style but it doesn't work. A single loop works fine, it only runs for one second though so its nowhere near close enough. My problem is how to input a second loop really.
Here's my code:
.data
enterNumber: .asciiz "Enter three digit number \n"
.text
main:
addi $t3, $zero, 0
addi $t1, $zero, 0 #counter for second for loop
#asks for number
li $v0, 4
la $a0, enterNumber
syscall
#receives number
li $v0, 5
syscall
move $t0, $v0 #move number to t0
For1:
beq, $t3, $t0, exit #if counter= t0 then loop ends
For2:
beq, $t1, $t0, For1 #if counter= t0 then loop ends
addi $t2, $zero, 0 #resets t2 to 0
mul $t2, $t0, $t0 #multiply number multiplied by number
addi $t1, $t1, 1 #add 1 to counter
j For2 #jump back to the top
addi $t2, $zero, 0 #resets t2 to 0
mul $t2, $t0, $t0 #multiply number multiplied by number
addi $t3, $t3, 1 #add 1 to counter
j For1 #jump back to for loop
exit:
li $v0, 1
move $a0, $t2 #print out multiplication
syscall
#tell system to stop
li $v0, 10
syscall
The program runs through the inner loop (For2) fine, but its not incrementing the outer loop at all. Thanks in advance
The only problem with your code is that everything you've written in the outer loop(For1), after the inner loop (For2) will never be executed, because of the way you've written the conditions in For2. You only need to make a small change to your loops code: make For2 jump to the last part of your For1 loop in case counter = t0 instead of making it jump to For1.
For1:
beq, $t3, $t0, exit #if counter= t0 then loop ends
For2:
beq, $t1, $t0, exit2 #if counter= t0 then loop ends
addi $t2, $zero, 0 #resets t2 to 0
mul $t2, $t0, $t0 #multiply number multiplied by number
addi $t1, $t1, 1 #add 1 to counter
j For2 #jump back to the top
exit2:
addi $t2, $zero, 0 #resets t2 to 0
mul $t2, $t0, $t0 #multiply number multiplied by number
addi $t3, $t3, 1 #add 1 to counter
j For1 #jump back to for loop
You need to make a small change to your loops code.
Make For2 jump to the last part of your For1 loop in case counter = t0 instead of making it jump to For1.
write below code under For1 loop.
For2exit:
addi $t2, $zero, 0 #resets t2 to 0
mul $t2, $t0, $t0 #multiply number multiplied by number
addi $t3, $t3, 1 #add 1 to counter
j For1 #jump back to for loop
and make change in the for2. make it jump to For2exit rather to For1.
I have this code that goes through an array of 5000 ASCII values of characters and then counts the number of each character and stores it into an array in alphabetical order.
.text
la $s7,results
array1 is the array of 5000 ASCII characters
la $s6,array1
main:
addi $s0,$zero, 5000
addi $s3, $zero,96
addi $s4, $zero,65
loop:lw $s2,0($s6)
addi,$t1,$zero,0
addi $s6,$s6,4
addi, $s5,$zero, 0
addi $s0,$s0, -1
bge $s2, $s3, convert
The substaction of of $s2, - $s4($s4 is # )is to get the positon of where the count needs to be stored
ex F - # = 6
sub $s5, $s2, $s4
I was wondering if this was right for it getting to the correct position and to increment the count?
addi $t1, $t1, 1
loop2: addi $s7, $s7, 4
lw $t0,($s7)
add $t0, $t0, $t1
sw $t0,($s7)
addi, $s5,$s5, -1
beq $s5, $zero, loop2
bne $s0,$zero, loop
This converts the character to an upper case ASCII value.
convert: subi $s2,$s2, 32
addi $s5, $s5, 0
sub $s5, $s2, $s4
j loop2
.data
results: .space 128 # Words to be used to store results
In this MIPS program
When this code fragment is run, how many instructions will be executed in total?
i1: ori $t0, $0, 1000
i2: ori $t1, $0, 2000
i3: addi $t2, $t0, 100
i4: lw $t3, 0($t1)
i5: lw $t4, 0($t0)
i6: add $t3, $t3, $t4
i7: sra $t3, $t3, 1
i8: sw $t3, 0($t0)
i9: sw $t3, 0($t1)
i10: addi $t0, $t0, 4
i11: addi $t1, $t1, 4
i12: slt $t3, $t0, $t2
i13: bne $0, $t3, i4
On every cycle, it adds 4 to $t0, which is initialized to 1000. At the end of the cycle, it checks if $t0 is greater than $t2, which is initialized to 1100. If it is, then the cycle breaks. So, it's the original 13 instructions, plus 10 for every cycle until it breaks. And it cycles an additional 25 times. And I may have added wrong, but if not, it's 263 instructions.