"sum of squares" program in assebly language programming?

Posted on 2008-11-16
Last Modified: 2012-05-05
The objective is to write a MIPS Assembly language program, that prompts a user to enter a positive integer n, and computes and print the sum 1sqaure + 2square + ....+ nsquare. (should not use formula).

Can somebody help me with a sample working code.
Question by:thirst4truth
    LVL 13

    Expert Comment

    We can't really give full code, as this is an assignment.

    Maybe I can give some pointers.

    1. You should use a counter... say CX, initialize this to your integer n.

    2. You should use a a conditional jump (jz or jnz) together with a loop to ensure that you are able to loop n number of times.

    3. n ^ 2 is simply a number multiplied by itself. So you can use the MUL operation.

    4. You should use a temporary address (or register) to store your results as you loop.

    Maybe you can post some code, and then we can take it from here:

    Hope that helps
    - Kelvin

    Author Comment

    Hi Kelvin King

    I made the previous program working,  Thank you.
    However, Now i am writing a program for harmonic series..
    following is my code. I am getting syntax error for div instruction in the code

    # ***** This program caluculates the Hormonic Series ****

    # $t0 - n , No. of loops
    # $t1 - temporary reg, hold 1/nth value
    # $t2 - sum of the series
    # $t3 - counter

          li $v0, 5
          move $t0, $v0

            li $t2, 0
          li $t3, 0
          beq $t3, $t0, endloop
          add $t3, $t3, 1
          div $t1, 1, $t3
          add $t2, $t2, $t1
          b loop
          ## Print out $t2.
          move $a0, $t2
          li $v0, 1

          li $v0, 10

    Accepted Solution

    I got the program working. thx

    Author Comment

    I got it working by myself

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now