Question
i need to translate this simple c++ code into MIPS assembly code. all the C++ code does is find the longest consecutive run of positive
i need to translate this simple c++ code into MIPS assembly code. all the C++ code does is find the longest consecutive run of positive values in an array. I've attached what i have for my assembly code so far and want to know if what i have will work or if i need to change it. thanks!
C++ code:
#include
using namespace std;
/* Find the longest consectutive run of positive values
in an array.
*/
int posrun(int list[], int size)
{
int count = 0;
int maxrun = 0;
for (int k = 0; k < size; k++)
{
if (list[k] > 0)
{
count++;
}
else
{
count = 0;
}
if (count > maxrun)
{
maxrun = count;
}
}
return maxrun;
}
int main()
{
int list[10] = {3, 0, 1, 2, 6, -2, 4, 7, 3, 7};
cout << posrun(list, 10) << endl;
return 0;
}
My MIPS assembly code:
# posrun.s
# An assembly language outline of the C++ program posrun.cpp
.text
.globl posrun
# Preconditions:
# 1st parameter (a0) address of the first element of the array
# 2nd parameter (a1) size of the array
# Postconditions:
# result (v0) length of the longest consecutive run of positive
# numbers in the array
posrun: li $t0, 0 # t0 is count, count = 0
li $v0, 0 # v0 is maxrun, maxrun = 0
li $t1, 0 # t1 is k, k = 0
for: slt $s0, $t1, $a1 # (k < size) ?
beq $s0, $zero, endfor # if not, branch to end of loop
add $t2, $t1, $t1 # t2 = k+k (calculate address of list[k])
add $t2, $t2, $t2 # t2 = 2*k + 2k
add $t3, $a0, $t2 # t3 = address of list[k]
lw $t4, 0($t3) # t4 = list[k]
slt $s0, $0, $t4 # (0 < list[k]) ?
beq $s0, $0, else1 # if not, branch to else
if1: addi $t0, $t0, 1 # count++
j endif1 # branch to end of if
else1: li $t0, 0 # count = 0
endif1: slt $0, $v0, $t0 # (maxrun < count) ?
beq $s0, $0, endif2 # if not, branch to end of if
if2: add $v0, $zero, $t0 # maxrun = count
endif2: addi $t1, $t1, 1 # k++
j for # back to top of loop
endfor: jr $ra # return
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started