Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to get the program to function like the example output in (http://pastebi

ID: 3556065 • Letter: T

Question

Trying to get the program to function like the example output in (http://pastebin.com/uaayFwxu) the main calls the simpsion function then the simpsion function calls the poly function and returns the value to main, the work is done it is just putting the program in working order. (may need to change the $sn values in simpsion to $fn for floats)


.data
.align 2
prompt1: .asciiz " This program computes definite integrals"
prompt2: .asciiz " Enter the left endpoint: "
prompt3: .asciiz " Enter the right endpoint: "
prompt4: .asciiz "Enter the number of subintervals (must be even): The definite integral is "
prompt5: .asciiz "Enter 1 to continue, any other number to quit: "
answer: .asciiz " "
newline: .asciiz " "
zero: .float 0.0
three: .float 3.0
two: .float 2.0
seven: .float 7.0
five: .float 5.0
.text
.globl main
main:
# print the prompt messages
li $v0, 4 # $v0 = print_string system call code
la $a0, prompt1 # $a0 = address of first prompt
syscall # print first prompt
l.s $f20, zero # $f20 = 0.0
# Read numbers and find f(x)
read: li $v0, 4 # $v0 = print_string system call code
la $a0, prompt2 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 6 # $v0 = read_float system call code
syscall # read a float - x

li $v0, 4 # $v0 = print_string system call code
la $a0, prompt3 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 6 # $v0 = read_float system call code
syscall # read a float - x

li $v0, 4 # $v0 = print_string system call code
la $a0, prompt4 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 6 # $v0 = read_float system call code
syscall # read a float - x

li $v0, 4 # $v0 = print_string system call code
la $a0, prompt5 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 6 # $v0 = read_float system call code
syscall # read a float - x







c.lt.s $f0, $f20 # set cond. flag if x < 0.0
bc1t quit # end program if number <= 0.0
mov.s $f12, $f0 # put input number into function argument
li $v0, 4 # $v0 = print_string system call code
la $a0, answer # $a0 = address of answer
syscall # print part of answer
jal poly # call function to evaluate p(x)
mov.s $f12, $f0 # put p(x) into $f12 for printing
li $v0, 2 # $v0 = print_float system call code
syscall # print p(x)
b read # go back and read another target
# Call system exit
quit: li $v0, 10 # $v0 = exit system call code
syscall # halt program execution


# h = (b - a)/n
# sum = p(a) + p(b)
# for i = 1 to n-1 step 2
# sum = sum + 4*p(a+i*h)
# for i = 2 to n-2 step 2
# sum = sum + 2*p(a+i*h)
# sum = sum*h/3
# return sum
#$s1= h
#$s2= b
#$s3= a
#$s4= n
#$s5= p
#$s6= i

simpson: addiu $sp, $sp, 24 # allocate stack space -- default of 24 here
sw $fp, 0($sp) # save frame pointer of caller
sw $ra, 4($sp) # save return address
addiu $fp, $sp, 20 # setup frame pointer of main

sub $s1,$s2,$s3 # h = (b - a)
div $s1,$s1,$s4 # h = (b - a)/n

#assuming the returning sum is in $v0 and each value is 32 bit value
add $v0,$0,$0 #sum = 0
add $s7,$0,$0 #sum = 0
add $s7,$s3,$s3
add $s7,$s7,$s7
add $s7,$s5,$s7
lw $s7,0($s7)
add $v0,$v0,$s7 #sum = p(a)
add $s7,$s2,$s2
add $s7,$s7,$s7
add $s7,$s5,$s7
lw $s7,0($s7)
add $v0,$v0,$s7 #sum = p(a) + p(b)
addi $s6,$0,1 #i = 1
addi $s0,$s4,-1 # s0 = i-1 to check the end of for loop
  
loop1: mul $s7,$s6,$s1 # i*h
add $s7,$s7,$s3 # a+i*h
add $s7,$s7,$s7
add $s7,$s7,$s7 # 4*(a+i*h)
add $s7,$s7,$s5
lw $s7,0($s7) # p(a+i*h)
add $s7,$s7,$s7
add $s7,$s7,$s7 # 4*p(a+i*h)
add $v0,$v0,$s7 # sum = sum + 4*p(a+i*h)
addi $s6,$s6,2 # i = i + 2
blt $s6,$s0,loop1

addi $s6,$0,2 #i = 2
addi $s0,$s4,-2 # s0 = i-2 to check the end of for loop
loop2: mul $s7,$s6,$s1 # i*h
add $s7,$s7,$s3 # a+i*h
add $s7,$s7,$s7
add $s7,$s7,$s7 # 4*(a+i*h)
add $s7,$s7,$s5
lw $s7,0($s7) # p(a+i*h)
add $s7,$s7,$s7 # 2*p(a+i*h)
add $v0,$v0,$s7 # sum = sum + 4*p(a+i*h)
addi $s6,$s6,2 # i = i + 2
blt $s6,$s0,loop2

div $v0,$v0,3 # sum = sum/3
mul $v0,$v0,$s1 # sum = sum*h/3

# Epilogue for main -- restore stack & frame pointers and return
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore frame pointer for caller
addiu $sp, $sp, 24 # restore frame pointer for caller
jr $ra # return to caller
  

poly: #P-E-M-D-A-S
#convert below equation in trace to: f(x) = 5x^2

Explanation / Answer

.data
.align 2
prompt1: .asciiz " This program computes definite integrals"
prompt2: .asciiz " Enter the left endpoint: "
prompt3: .asciiz " Enter the right endpoint: "
prompt4: .asciiz "Enter the number of subintervals (must be even): "
promptans: .asciiz "The definite integral is: "
prompt5: .asciiz " Enter 1 to continue, any other number to quit: "
answer: .asciiz " "
newline: .asciiz " "
zero: .float 0.0
three: .float 3.0
two: .float 2.0
seven: .float 7.0
five: .float 5.0
one: .float 1.0
four: .float 4.0
.text
.globl main
main:
# print the prompt messages
li $v0, 4 # $v0 = print_string system call code
la $a0, prompt1 # $a0 = address of first prompt
syscall # print first prompt
l.s $f20, zero # $f20 = 0.0
# Read numbers and find f(x)
read: li $v0, 4 # $v0 = print_string system call code
la $a0, prompt2 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 6 # $v0 = read_float system call code
syscall # read a float - x
mov.s $f3,$f0

li $v0, 4 # $v0 = print_string system call code
la $a0, prompt3 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 6 # $v0 = read_float system call code
syscall # read a float - x
mov.s $f2,$f0

li $v0, 4 # $v0 = print_string system call code
la $a0, prompt4 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 5 # $v0 = read_float system call code
syscall # read a float - x
move $s4,$v0

jal simpson

li $v0, 4 # $v0 = print_string system call code
la $a0, promptans # $a0 = address of second prompt
syscall # print second prompt

mov.s $f12,$f0
li $v0,2
syscall

li $v0, 4 # $v0 = print_string system call code
la $a0, prompt5 # $a0 = address of second prompt
syscall # print second prompt
li $v0, 5 # $v0 = read_float system call code
syscall # read a float - x

addi $s0,$0,1
bne $v0,$s0,quit
b read

# Call system exit
quit: li $v0, 10 # $v0 = exit system call code
syscall # halt program execution

# h = (b - a)/n
# sum = p(a) + p(b)
# for i = 1 to n-1 step 2
# sum = sum + 4*p(a+i*h)
# for i = 2 to n-2 step 2
# sum = sum + 2*p(a+i*h)
# sum = sum*h/3
# return sum
#$f1= h
#$f2= b
#$f3= a
#$s4= n
#$s5= p
#$s6= i
#$f5 = sum temporary until return
simpson:
addiu $sp, $sp, 24 # allocate stack space -- default of 24 here
sw $fp, 0($sp) # save frame pointer of caller
sw $ra, 4($sp) # save return address
addiu $fp, $sp, 20 # setup frame pointer of main

sub.s $f1,$f2,$f3 # h = (b - a)
mtc1 $s4,$f4
cvt.s.w $f4,$f4 #n
div.s $f1,$f1,$f4 # h = (b - a)/n
#assuming the returning sum is in $v0 and each value is 32 bit value
l.s $f5,zero # sum = 0

mov.s $f12,$f3 #f12 = a
jal poly # this will return p(a) in $f0
add.s $f5,$f5,$f0 #sum = sum + p(a)

mov.s $f12,$f3 #f12 = b
jal poly # this will return p(b) in $f0
add.s $f5,$f5,$f0 #sum = sum + p(b)

addi $s6,$0,1
addi $s0,$s4,-1 # s0 = n-1 to check the end of for loop
  
loop1:
mtc1 $s6,$f6
cvt.s.w $f6,$f6 #i
mul.s $f7,$f6,$f1 # i*h
add.s $f7,$f7,$f3 # a+i*h

mov.s $f12,$f7 #f12 = a+i*h
jal poly # this will return p(a+i*h) in $f0
l.s $f8,four
mul.s $f0,$f0,$f8 #4*p(a+i*h)
add.s $f5,$f5,$f0 #sum = sum + 2*p(a+i*h)

addi $s6,$s6,2 # i = i + 2
blt $s6,$s0,loop1

addi $s6,$0,2 #i = 2
addi $s0,$s4,-2 # s0 = n-2 to check the end of for loop

loop2:
mtc1 $s6,$f6
cvt.s.w $f6,$f6 #i
mul.s $f7,$f6,$f1 # i*h
add.s $f7,$f7,$f3 # a+i*h

mov.s $f12,$f7 #f12 = a+i*h
jal poly # this will return p(a+i*h) in $f0
l.s $f8,two
mul.s $f0,$f0,$f8 #2*p(a+i*h)
add.s $f5,$f5,$f0 #sum = sum + 2*p(a+i*h)

addi $s6,$s6,2 # i = i + 2
blt $s6,$s0,loop2

l.s $f4,three
div.s $f5,$f5,$f4 # sum = sum/3
mul.s $f0,$f5,$f1 # f0 = sum*h/3

# Epilogue for simpson -- restore stack & frame pointers and return
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore frame pointer for caller
addiu $sp, $sp, 24 # restore frame pointer for caller
jr $ra # return to caller
  
poly: #P-E-M-D-A-S
#convert below equation in trace to: f(x) = 5x^2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote