Question

Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...

Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer

Please make sure the program runs in QTSpim and prints the results. Also explain, thanks.

Homework Answers

Answer #1

# Compute the sum of N integers: 1 + 3 + ... + N
# Here take Input N as 99

main:
# Input N from user
li $v0,5 # read_int syscall code = 5
syscall   
move $t0,$v0 # syscall results returned in $v0

# Initialize registers
li $t1, 0 # initialize counter (i)
li $t2, 0 # initialize sum

# Loop begins
loop: addi $t1, $t1, 2 # i = i + 2 increment 2 every time eg. 1 3 5
add $t2, $t2, $t1 # sum = sum + i take sum of odd numbers
beq $t0, $t1, exit # if i = N terminating condition
j loop

# Exit begins
exit: li $v0, 4 # print_string syscall code = 4
la $a0, msg1
syscall

# Print sum
li $v0,1 # print_string syscall code = 4
move $a0, $t2
syscall

# Print newline
li $v0,4 # print_string syscall code = 4
la $a0, lf
syscall
li $v0,10 # exit
syscall

# Start .data segment (data!)
.data
msg1: .asciiz "Sum = "
lf: .asciiz "\n"

#code Ends

I have added explanation in form of comments.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in MASM (32 bit) that calculates the sum of all odd numbers in...
Write a program in MASM (32 bit) that calculates the sum of all odd numbers in the Fibonacci sequence between 0 and 1,000,000. ***Please Complete in MASM 32 bit!!***
Write a MIPS assembly program that asks the user first for a string, then for a...
Write a MIPS assembly program that asks the user first for a string, then for a character. The program should search the string for the character and print out the number of times the character appears in the string. Restrictions can be used, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
write a MIPS assembly program to get an integer input from the user and multiply it...
write a MIPS assembly program to get an integer input from the user and multiply it by 2 and print output to the console
1) Use a loop structure to calculate the sum of all odd numbers from 1 to...
1) Use a loop structure to calculate the sum of all odd numbers from 1 to 15 2) Use a loop structure to calculate the product of all odd numbers from 1 to 9. 3) Write a program to convert US dollars to Bangaldeshi taka
1- Write a program to print only the even numbers from 100 to 200 2- Write...
1- Write a program to print only the even numbers from 100 to 200 2- Write a program to print the odd numbers from 22 – 99 3- Write a program to calculate howe many even number from 10 to 120 1- Write a program to find only the even numbers for 12 different numbers 2- Write a program to find the odd numbers for 10 different numbers 3- Write a program to calculate howe many even number and how...
Write a program in C that calculate and print the summation of odd numbers between 1-15...
Write a program in C that calculate and print the summation of odd numbers between 1-15 and then find the average of the summation of these numbers, please use double data type to declare all variables. Use for or do/while loop, please explain the steps aswell
Write a program in MIPS ASSEMBLY LANGUAGE.. Take no of students as input from the user,...
Write a program in MIPS ASSEMBLY LANGUAGE.. Take no of students as input from the user, then take marks of 5 subjects for each student from the user. Then find Minimun, Maximum and Median of all the subjects. and comment each line in code also make flow chart
JAVA Write an application that will input four positive integers from the user. The program should...
JAVA Write an application that will input four positive integers from the user. The program should print out the largest and smallest number. It should print how many of the numbers are even and how many are odd. It should print how many of the numbers are one digit and how many of the numbers are two digit. It should print out the sum of all even numbers and the sum of all odd numbers. It should print out the...
Write a MASM program that computes the sum of the integers from 1 to N where...
Write a MASM program that computes the sum of the integers from 1 to N where N is a positive integer. Use the equal sign directive to define N. Save the sum in the EAX register. You must use loops. For example, 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Language ( Assembly)...
The Java program should sum the odd integers from 1 to the integer that the user...
The Java program should sum the odd integers from 1 to the integer that the user enters. For example, if the user enters 30, the program should sum the odd integers from 1 to 30.   Print "Please enter a positive nonzero integer." Declare a Scanner and obtain an integer from the user.   Use a Do...while statement and calculate the sum Use the integer that the obtained from the previous step for the loop-continuation condition. Display the sum Print the sum...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT