2
$\begingroup$

I want to know what the best way is to count the number of executed commands for a given MIPS program. Let's say we're given a simple for loop that counts from $0$ to $100$. For this I would write the following MIPS code:

li $s1, 0 					
li $t0, 100                    

for_loop: 
    addi $s1, $s1, 1            
    beq $s1, $t0, exit      
    j for_loop              

exit:

Since I have $2$ li's before the loop, that would be $+2$ commands and then within the for_loop, the 99 loops pass for addi, beq and j which makes it $99⋅3$ and then $2$ for addi and beq. That makes it a total of $301$ executed commands. Is my answer correct? If not then what is the correct procedure to count the number of executed commands and if in the future I come across a more complex example, then how should I deal with it?

$\endgroup$
  • $\begingroup$ We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. It's usually better to ask about a specific conceptual issue you're uncertain about. As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. $\endgroup$ – D.W. Jan 8 at 23:04
2
$\begingroup$

There are two ways: You use a simulator and count the actual number of instructions (or do it manually), which of course only works for fixed input like your case here. Or you use mathematics to calculate the number of instructions, just as you did.

In general, mathematical analysis ranges from easy to hard to impossible for humans, so there is no general method.

Sometimes you’re lucky to get a result like “about 2.25 n^2”, or “$\Theta (n^2)$ or similar. How do you deal with it? You do your best.

$\endgroup$
  • $\begingroup$ That makes sense but I also want to know how to do it manually. Therefore is my answer of $301$ correct? $\endgroup$ – xxxtentacion Jan 6 at 15:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.