Question

lhere are two progang questions for yon ito do. Please submit two s oram les Tn tho Settings menu of SPIM set Bare Machine OFF, Allow Pseudo Instructions ON, Exception Handler ON, Delayed Branches ON, Delayed Loads ON and Mapped IO OFF 1. (5 points) Sum of Integers Write a program to calculate 1 +2+3++n and then print the sum out. n is entered by the user. So if the user enters 10, your program will calculate 1+2+3+ +10 which is equal to 55. The user dialog will look something like this: Console Enter a positive integer n: 10 The sum of of the first n integers is: 55

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The key here is to use the formula for sum of n natural number,

1+2+3+... + n = \frac{n(n+1)}{2}

Below is a code which uses this formula:

.data

inputPrompt: .asciiz "Enter a positive integer n: "

resultStr: .asciiz "The sum of the first n integers is: "

.text

.globl main

main:

li $v0, 4 # syscall 4 (print_str)

la $a0, inputPrompt # argument: string

syscall # print the string

# System call for user input

li $v0, 5 # 5 is for read integer

syscall

# Copy this to temp 0 register

move $t0, $v0

# Create another copy in t1 with value $v0+1

addi $t1, $v0, 1

# n*(n+1)

mult    $t0, $t1            # $t0 * $t1 = Hi and Lo registers

mflo    $t2                 # copy Lo to $t2, thus result is in $t2

addi $t3, $0, 2    # Move 2 to $t3, for dividing n*(n+1)

div     $t2, $t3            # $t2 / $t3

mflo    $t2                 # $t2 = floor($t2 / $t3), result is in $t2

# Print out the output text

li $v0, 4 # 4 for printing string

la $a0, resultStr # Location of string to print in $a0

syscall

# Print out the result

li $v0, 1 # 1 for printing integer

move $a0, $t2 # Integer to print in $a0

syscall

jr $ra # return to caller

Screenshots:

Result:

Add a comment
Know the answer?
Add Answer to:
lhere are two progang questions for yon ito do. Please submit two s oram les Tn...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Euclid’s Elements (300 BC) shows that the greatest common divisor of two integers does not change...

    Euclid’s Elements (300 BC) shows that the greatest common divisor of two integers does not change if the smaller is subtracted from the larger. Write a program (in MIPS assembly) that implements this algorithm to find the GCD of two integers. Follow this algorithm: 1. Call the two integers a and b. 2. If a and b are equal: stop: a is the GCD. 3. Subtract the smaller from the larger. Replace the larger with the result 4. Repeat steps...

  • Write a program to evaluate the arithmetic expression: (25xy - 10x - 6y + 28)/7 Use...

    Write a program to evaluate the arithmetic expression: (25xy - 10x - 6y + 28)/7 Use symbolic addresses x, y, answer, and remainder. Pick two registers for x and y and load them from memory. At the end of the program, store the answer and remainder to memory. Assume that the values are small enough so that all results fit into 32 bits. Since load delays are turned on in SPIM be careful what instructions are placed in the load...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT