Question

IN MIPS ASSEMBLY, computer Taylor series for ex. Program should compute until x^4/4! Example: Input 2 22 23 24 Needed: 1+ 2+

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

Please below solution, let me know if you have any query.

=========================================================================

prompt_lower: .asciiz "enter lower bound:"
prompt_upper: .asciiz "enter upper bound:"
prompt_size: .asciiz "enter step size:"
end_prompt: .asciiz "Would you like to run another calculation? (0 to No, 1 to Yes) "
  
bound_error: .asciiz "You have enter lower bound which was greater than the upper bound. \n"
step_size_wrn:.asciiz "A step size lower than 0.1 is not recommended due to lack of required precision with floats, Would you like to continue or re-enter your inputs? (0 to Continue, 1 to Re-enter)"
  
main:
# lower bound in $f7
# upper bound in $f8
# step size in $f9

# lower bound input
la $a0, prompt_lower
li $v0, 4
syscall

li $v0, 6
syscall

# save value
mov.s $f7, $f0

# upper bound input
la $a0, prompt_upper
li $v0, 4
syscall

li $v0, 6
syscall

# save value
mov.s $f8, $f0

# to check lower bound <= upper bound
c.le.s $f7, $f8
bc1f wrong_input

# Step size input
la $a0, prompt_size
li $v0, 4
syscall

li $v0, 6
syscall

# save value
mov.s $f9, $f0

# step size check for lower than 0.1
l.s $f10, precision
c.lt.s $f9, $f10
bc1t prec_warning

# user interface
prepare_main:
# print spacing lines
la $a0, lines
li $v0, 4
syscall

# print header
la $a0, header
li $v0, 4
syscall

# print spacing lines
la $a0, lines
li $v0, 4
syscall

# printing the requested values by loop
loop_main:
# stop the loop if lower bound register value is not less than or equal upper bound value
c.lt.s $f7, $f8
bc1f ret_main

# lower bound to the screen
mov.s $f12, $f7
li $v0, 2
syscall

# tab seperator
la $a0, tab
li $v0, 4
syscall

# load current lower bound value
mov.s $f0, $f7

# calculate exp
jal exp

# current value to the console
li $v0, 2
syscall

# tab seperator
la $a0, tab
li $v0, 4
syscall

# reload current the lower bound value into argument register
mov.s $f0, $f7

# calculate ln
jal ln

# ln current value to the console
li $v0, 2
syscall

# increment lower bound value by one
add.s $f7, $f7, $f9

la $a0, new_line
li $v0, 4
syscall

j loop_main
  
# notify user about if incorrect input regarding the bounds
wrong_input:
# print error message
la $a0, bound_error
li $v0, 4
syscall

j main

# notify user about potential accuracy lost when choosing step sizes below 0.1
prec_warning:
la $a0, step_size_wrn
li $v0, 4
syscall

li $v0, 5
syscall

# if 1, repeat program, if 0, continue
bne $v0, $zero, main
j prepare_main

ret_main:
# print spacing lines
la $a0, lines
li $v0, 4
syscall
  
# print end message
la $a0, end_prompt
li $v0, 4
syscall

# read user input
li $v0, 5
syscall
# if 1, repeat program, if 0, end program
bne $v0, $zero, main

la $a0, goodbye
li $v0, 4
syscall
  
li $v0, 10
syscall
.end main


exp:
# argument x in $f0
# variable nom in $f1
# variable den in $f2
# loop variable i (int) in $t0
# loop condition variable in $t1
# return variable sum in $f12
# loop variable i (float) in $f3
# i-th summand in $f4
# lower bound in $f29
# upper bound in $f30

l.s $f29, exp_lower_bound
l.s $f30, exp_upper_bound

c.lt.s $f0, $f29
bc1t exp_out_of_bounds
  
c.le.s $f0, $f30
bc1f exp_out_of_bounds

# initialize nom and den with 1.0
li.s $f1, 1.0
li.s $f2, 1.0

# set initial value for sum
li.s $f12, 1.0

# set loop variables
li $t0, 1
li $t1, 20

# loop to concatenate summands
loop_exp:
# leave loop if loop variable has become larger than loop condition variable
bgt $t0, $t1, ret_exp

# calculate new nominator: nom = nom * x
mul.s $f1, $f1, $f0

# calculate new denominator: den = den * i
mtc1 $t0, $f3
cvt.s.w $f3, $f3 # convert i into float
mul.s $f2, $f2, $f3

# calculate i-th summand
div.s $f4, $f1, $f2

# append i-th summand to the total sum
add.s $f12, $f12, $f4

# increment loop variable i
addi $t0, $t0, 1

j loop_exp
  
# set error value if argument is out of bounds
exp_out_of_bounds:
li.s $f12, -1.0

# exit point when end of loop is reached
ret_exp:
jr $ra
.end exp

ln0:
# argument x in $f0
# variable nom in $f1
# variable den in $f2
# quotient nom / den in $f3
# i-th summand in $f4
# variable sign (float) in $f5
# value (x - 1) in $f6
# constant -1.0 in $f13
# variable sign in $t0
# constant -1 in $t1
# loop variable i in $t2
# loop condition in $t3
# return variable sum in $f12

# initialize constants:
li.s $f13, -1.0
li $t1, -1

# initialize nom with value x - 1.0
add.s $f1, $f0, $f13
mov.s $f6, $f1 # save value for later

# initialize den with 1.0
li.s $f2, 1.0

# initialize sign variable positively
li $t0, 1

# make first summand returnable in case loop is not used
mov.s $f12, $f1
  
# set loop variables
li $t2, 1
li $t3, 250

# loop to concatenate summands
loop_ln0:
# leave loop if loop variable has become larger than loop condition variable
bgt $t2, $t3, ret_ln0

# calculate i-th nominator: nom = nom * (x - 1)
mul.s $f1, $f1, $f6

# calculate i-th denominator: den = den + 1
sub.s $f2, $f2, $f13 # den + 1 == den - (-1)

# invert sign variable
mul $t0, $t0, $t1

# calculate i-th (nom / den)
div.s $f3, $f1, $f2

# multiply (nom / den) with i-th sign -> determine i-th summand
mtc1 $t0, $f5
cvt.s.w $f5, $f5 # convert i-th sign integer to float
mul.s $f4, $f3, $f5

# append i-th summand to the total sum
add.s $f12, $f12, $f4

# increment loop variable
addi $t2, $t2, 1

j loop_ln0
  
# exit point when end of loop is reached
ret_ln0:
jr $ra
.end ln0

ln:
# argument x in $f0
# variable ln(x) in $f1
# variable b (float) in $f2
# constant 2.0 in $f3
# constant ln(2) in $f4
# value b * ln(2) in $f5
# variable b (int) in $t4
# return variable sum in $f12
# lower bound in $f31

l.s $f31, ln_lower_bound

c.le.s $f0, $f31
bc1t ln_out_of_bounds

# initialize constant values
li.s $f3, 2.0

# initialize b with value 0
li $t4, 0

# loop for splitting x into a * 2^b
loop_ln:
# stop the loop if a has become less or equal the value of 2.0
c.le.s $f0, $f3
bc1t fin_ln

# divide x by 2
div.s $f0, $f0, $f3

# increment b
addi $t4, $t4, 1

j loop_ln
  
# exit point when end of loop is reached
fin_ln:
# calculate ln(x) (x* = x/2^n * 2^n)

addi $sp, $sp, -4
sw $ra, 0($sp)
jal ln0
  
# reinitialize constants
li.s $f3, 2.0
li.s $f4, 0.6931464

# calculate product b * ln(2)
mtc1 $t4, $f2
cvt.s.w $f2, $f2 # convert interger variable b into float
mul.s $f5, $f2, $f4

# calculate final sum
add.s $f12, $f12, $f5
  
lw $ra, 0($sp)
addi, $sp, $sp, 4
j ret_ln

# set error value if argument is out of bounds
ln_out_of_bounds:
li.s $f12, -1.0

# exit point when calculation of ln(x) is reached.
ret_ln:
jr $ra
.end ln

Add a comment
Know the answer?
Add Answer to:
IN MIPS ASSEMBLY, computer Taylor series for ex. Program should compute until x^4/4! Example: Input 2...
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
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
ADVERTISEMENT