Link to home
Start Free TrialLog in
Avatar of thirst4truth
thirst4truth

asked on

"sum of squares" program in assebly language programming?

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.
Avatar of Kelvin_King
Kelvin_King
Flag of Singapore image

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
Avatar of thirst4truth
thirst4truth

ASKER

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

main:
      li $v0, 5
      syscall
      move $t0, $v0

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

      li $v0, 10
      syscall
ASKER CERTIFIED SOLUTION
Avatar of thirst4truth
thirst4truth

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got it working by myself