MIPS code to sum an artithmetic series - mips

I have to write a MIPS code to sum the series 1 + 5 + 9 + ... + N, under the constraint that I should use minimum number of registers to implement the MIPS code.
Here's my solution. It'd would be nice if you could point out any mistakes.
High-level code:
int count = 1;
int sum = 1;
while(count<N){
count = count + 4;
sum = sum + count;
}
Associate variable 'count' with register $t0 and
variable 'sum' with register $s0.
Equivalent MIPS code:
addi $t0, $zero, 1
addi $s0, $zero, 1
Loop: slti $t2, $t0, N
beq $t2, $zero, Exit
addi $t0, $t0, 4
addi $s0, $s0, $t0
j Loop
Exit:

You need a register to save the actual sum. If N is hard coded, I'd do something like this (untested code);
addi $t0, $zero, 0 ; Sum
addi $t1, $zero, 1 ; Counter
Loop:
slti $t2, $t1, N + 1 ; Exit if $t1 > N
beq $t2, $zero, Exit
add $t0, $t0, $t1 ; $t0 = $t0 + $t1
addi $t1, $t1, 4 ; $t1 += 4
j Loop
Exit: ; $t0 contains the sum here

Related

Retrieving Data from an Array (cannot seem to get it working)

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.

When writing in MIPS, I am unsure whether to use li or addi. I am still unclear what the difference is.

For example, I have a piece of C code that I am trying to convert to MIPS for practice, but for the variable count, I don't know whether to use addi $t0,0 or li $t0, 0. Could I use either either or? And what is the difference?
Void haarPredict (int vector[], int N)
{
int half = N >> 1;
int count = 0;
for(int i = 0; i < half; i++)
{
int predictVal = vector[i];
int j = i + half;
vector[j] = vector[j] - predictVal
}
}
This is what I have so far after converting the above code to MIPS. Assuming $a0 is vector[] and $a1 is N. Again, I am not sure if li or addi is the correct thing to use.
srl $t0, $a1, 1 #t0 holds half. half = N >> 1
addi $t1, $t1, 0 #t1 holds count. count = 0
addi $t2, $t2, 0 #t2 holds i. i = 0
loop: slt $t3, $t2, $t0 #t3 holds 1 if i < half
beg $t3, $zero, exit #exit if t3 == 0
lw $t4, 0($a0) #t4 holds predictValue
addi $a0, $a0, 4 #4 bytes for next word address
addi $t5, $t2, $t0 #t5 holds j. j = i + half
lw $t6, $t6, $t4 #vector[j]=vector[j]-predivtVal
addi $t2, $t2, 1 #i++
j loop
exit: jr $ra
The li (Load immediate) instruction loads a specific numeric value into a register.
The addi (Add inmediate) adds a register and a sign-extended immediate value and stores the result in a register.
So, unless you are 100% sure a register has a zero value, then you shouldn't use an addi instruction to set a register.
For example:
addi $t1, $t1, 0 #t1 holds count. count = 0
You don't know if $t1 is zero at that particular moment. If thats a subroutine, you might be using a garbage value of $t1 (a value from before the invocation of the subroutine, before the jump to the address of the subroutine).
So the safe way is to set the register with li (thus, count=0), not taking into consideration the previous value of the register.

Converting C code to mips gives error

This is the C code that i have been converted so far. it gives me some error that i have been included in the following code. i don't understand which part is wrong in this c to mips conversion?
char ch[10];
int i;
for (i = 0; i != 10; i++)
ch[i] = ch[i] – 32
.data
.text
li $v0 4
syscall
#$s1 = i, $s0 base address of ch
addi $s1, $0, 0 #i =0
addi $t0, $0, 10 #t0 = 10
loop: beq $t0, $s1, end
add $t1, $s1, $s0
lw $t2, 0($t1)
addi $t2, $t2, -32
sw $t2, 0($t1)
addi $s1, $s1, 1
j loop
end:
My output:
Runtime exception at 0x00400018: address out of range 0x00000000
From the C code you are converting a char type array and in MIPS you should use lb instead of lw.
In order to print out you need a main: label and also you should declare an array like .byte or .space
You should usesyscall 11 to print a character or syscall 4 to print
string.
I have added some of these mentioned above to your code hope it helps.
.data
#ch: .byte 'a','b','c','d','e'
ch: .space 10
.text
main:
li $v0, 8 #read character
li $a1, 10 #load the space
la $a0, ch
syscall
li $v0,11 #print character
syscall
li $v0,10 # exit program
syscall
addi $s1, $0, 0 #i = 0
addi $t0, $0, 10 # $t0 = 10
loop: beq $t0, $s1, end
add $t1, $s1, $s0
lb $t2, ch($t1)
addi $t2, $t2, -32
sb $t2, ch($t1)
addi $s1, $s1, 1
j loop
end:

Nested Loops in MIPS

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.

Mips loop iteration

I have the following code which is effectively supposed to increment from 1-6 while doing arithmetic on a variable.
An example in C would be
int res = 0;
for(i=1; i<6; i++) {
res += i*i+1;
}
Now for my mips code:
.data
res: .word 0
.text
li $t0, 1 #int j = 1
loop:
beq $t0, 6, end #for(int j = 1; j < 6; j++)
add $t0, $t0, 1
mul $t1, $t0, $t0 #res += j * j + 1
addi $t1, $t1, 1
sw $t1, res
b loop
end:
li $v0, 1 #checking the end result
la $a0, res
syscall
li $v0, 10 #graceful exit
syscall
For some reason the result I get towards the end is ~300 million and that is obviously wrong. Does anyone see what the issue is? Im fairly new to mips assembly.
Thanks
You've got a few problems here.
Firstly, the line you have marked #res += j * j + 1 is only multiplying, there is no addition involved.
You seem to be attempting to use res to store a running total but you are overwriting it in each iteration. In fact, there shouldn't be any need to store res in memory, a register is much more appropriate for this purpose.
.text
main:
move $t2 $zero
li $t0 1
loop:
mul $t1 $t0 $t0
addi $t1 $t1 1
add $t2 $t2 $t1 # res += i * i + 1
addi $t0 $t0 1
blt $t0 6 loop
# print res
li $v0 1
move $a0 $t2
syscall
# exit
li $v0 10
syscall
Prints 60.
You want to do something like this.
I am assuming you wanted something like this:
int res = 0;
for(i=1; i<6; i++) {
res += i*i+1;
}
.data
res: .word 0
.text
li $t0, 1 #int i = 1
loop:
bgt $t0, 6, exit
add $t0, $t0, 1
mul $t1, $t0, $t0
addi $t1, $t1, 1
sw $t1, res // I'm sure you can just move here
b loop
exit: