Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MIPS Exercise: Various infinite series for have been discovered. The first such series found was the GregoryLeibniz series: = 4( 1 - 1/3 + 1/5

MIPS Exercise:

Various infinite series for have been discovered. The first such series found was the GregoryLeibniz series: = 4( 1 - 1/3 + 1/5 - 1/7 + 1/9 - . . .) This series is of little practical value because enormous numbers of terms (and very high precision in the data type used) are required to achieve a good approximation. However, it does provide good programming practice. Write a SPIM program that writes out the sum of the first 1000, 2000, 3000, ..., 10000 terms of the series (e.g., count to 10000 stopping to print after every 1000 passes through the loop). Write both iterative and recursive versions of the function to approximate . Return the value in $f0. Have the program call the iterative version then the recursive version for each value, saving the results in arrays, and then print the corresponding values from the two calls, iterative then recursive, one pair per line, with a tab between the values and a newline after the second value.

Here is my solution so far:

.text .globl main

main: # Prolog sub $sp, $sp, 4 # 1. Push return address. sw $ra, ($sp) sub $sp, $sp, 4 # 2. Push caller's frame pointer. sw $fp, ($sp) # 3. No S registers to push. sub $fp, $sp, 8 # 4. $fp = $sp - space_for_variables move $sp, $fp # 5. $sp = $fp

li $t0, 1 # Initialize counter to 1000. sw $t0, 0($sp) # i = 1000

loop: bgt $a0, 10, epilog # if ($a0 > 10000) End the program # write("The result after ") li $v0, 4 # Print string service. la $a0, prompt1 # Address of prompt syscall

# print( i ) lw $a0, 0($fp) # Load i into $a0. li $v0, 1 # Print integer service. syscall

# write("iteration is ") li $v0, 4 # Print string service. la $a0, prompt2 # Address of prompt syscall

# Subroutine call # 1. No T registers to push. lw $a0, 0($fp) # 2. Put argument into $a0. jal pi_iter # 3. Jump and link to subroutine.

# Return from subroutine # 1. No T registers to restore.

s.s $f0, array1($fp) # num = pi_iter( i )

# print( num ) l.s $f0, array1($fp) # Load a into $a0. mov.s $f12, $f1 li $v0, 2 # Print float service. syscall

# End the print line li $v0, 4 # Print string service. la $a0, lf # Address of line feed syscall b loop

epilog: # Epilog # 1. No return value. add $sp, $fp, 8 # 2. $sp = $fp + space_for_variables # 3. No S registers to pop. lw $fp, ($sp) # 4. Pop $fp. add $sp, $sp, 4 # lw $ra, ($sp) # 5. Pop $ra. add $sp, $sp, 4 # jr $ra # Return to OS.

pi_iter: # Prolog sub $sp, $sp, 4 # 1. Push return address. sw $ra, ($sp) sub $sp, $sp, 4 # 2. Push caller's frame pointer. sw $fp, ($sp) sub $sp, $sp, 4 # 3. Push registers $s0, $s1, $s2, $s3, and $s4. sw $s0, ($sp) sub $sp, $sp, 4 sw $s1, ($sp) sub $sp, $sp, 4 sw $s2, ($sp) sub $sp, $sp, 4 sw $s3, ($sp) sub $sp, $sp, 4 sw $s4, ($sp) sub $fp, $sp, 16 # 4. $fp = $sp - space_for_variables move $sp, $fp # 5. $sp = $fp

# Body of subroutine move $s0, $a0 # Save argument in $s0. li.s $f0, 0.0 # Initialize sum. mfc1 $s1, $f0 # $s1 = $f0 li $s2, 1 # Initialize denominator. li $s3, 1 # Initialize sign. li $s4, 0 # Initialize counter. li.s $f1, 4.0 # Initialize temporary register to 4. mfc1 $t0, $f1 # $t0 = $f1 li $t3, -1

loop2: bgt $s4, $s0, epilog2 # if (i > n) Return the sum div $t0, $s2 # Divide 4 by the denominator. mflo $t1 mult $s3, $t1 # Multply the sign by (4 / denominator). mflo $t2 add $s1, $s1, $t2 # Increment the sum. mtc1 $s1, $f0 # $f0 = $s1 s.s $f0, 0($fp) # sum += sign * 4.0 add $s2, $s2, 2 # Increment the denominator by 2. sw $s2, 4($fp) # denominator += 2 mult $s3, $t3 # Multiply the sign by -1. mflo $s3 sw $s3, 8($fp) # sign *= -1 addi $s4, $s4, 1 # Increment the counter by 1. sw $s4, 12($fp) # i++ b loop2 epilog2: # Epilog # 1. Return value is already in $f0. add $sp, $fp, 8 # 2. $sp = $fp + space_for_variables lw $s4, ($sp) # 3. Pop registers $s4, $s3, $s2, $s1, and $s0. add $sp, $sp, 4 # lw $s3, ($sp) # add $sp, $sp, 4 # lw $s2, ($sp) # add $sp, $sp, 4 # lw $s1, ($sp) # add $sp, $sp, 4 # lw $s0, ($sp) # add $sp, $sp, 4 # lw $fp, ($sp) # 4. Pop $fp. add $sp, $sp, 4 # lw $ra, ($sp) # 5. Pop $ra. add $sp, $sp, 4 # jr $ra # 6. Return to caller.

.data prompt1: .asciiz "The result after " prompt2: .asciiz " iteration is " array1: .word 0:10 lf: .asciiz " "

Right now, its giving an error message after the line jr $ra in the subroutine pi_iter:

Exception occurred at PC = 0x00000000

Bad address in text read: 0x00000000

For some reason, its not going back to main. Can someone help me fix this issue?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions

Question

Explain the concept of a neural network and its applications.

Answered: 1 week ago