Implementing functions in MIPS. The requirements and template code are attached.
1. (60 points) Please implement two functions, where one calls the other, following the MIPS calling conventions: a. (20 points) A function called “sameparity” which takes in $a0 and $a1 two integers and returns 1 in $v0 if they are both even or both odd. b. (40 points) A function called “count” which takes two inputs: · $a0: the starting address of an array of integers · $a1: the length of the array “count” should scan every neighboring two elements in the array, call “sameparity” to find if they are of the same parity, and return in $v0 the total number of neighboring pairs that have the same parity. · For example, if the array is 41, 28, 3, 65, 9, “count” should return 2, because there are two neighboring pairs, (3, 65) and (65,9), that have the same parity. 2.(40 points) Please implement a function called “appcos” that approximates cos(x) by 1 - x2/2! + x4/4! – x6/6! … The function takes two inputs: · $a0: x · $a1: The number of terms uses in the approximation. For example, the above equation uses 4 terms. The function should return the result in $v0. You should use single precision. Sample output: $a0 = 4, $a1 = 30: -0.65364337 $a0 = 12, $a1 = 30: 0.84343189 As we discussed in the class, due to the quantization error of single precision, there will be errors, especially for large x. There is, however, a way to optimize the code to handle large numbers better. If your program can produce accurate results up to x=4 but no further, the maximum points to receive is 35. If your program can handle up to x=12, the maximum points to receive is 40. Note: 1. You should document your code properly. You may use the following code as a template: # newton.asm – a simple program calculating the square root of a given # is number n using singles. The value of the square root of n, # denoted by x, approximated by x=(x+n/x)/2, until the the # absolute value of the difference between new x and the previous # x is smaller than a given threshold such as 1.0E-3. # function main # calling the calsqrt function and display the results # Register use: # $a0 syscall parameter # $v0 syscall parameter # $f0 exp and syscall return value # $f12 exp and syscall parameter .text .globl main main: li.s $f0, 361.0 mfc1 $a0, $f0 jal calsqrt done: mtc1 $v0, $f12 li $v0,2 syscall eixt: li $v0,10 syscall # function calsqrt # calculating the square root of a given number n using singles. # The value of the square root of n, denoted by x, approximated # by x=(x+n/x)/2, until the the absolute value of the difference # between new x and the previous x is smaller than a given # threshold such as 1.0E-3. # Register use: # $a0 parameter from calling routine # $v0 return value # $f0 storing the value of n as a single precision number # $f1 current value of x # $f2 next value of x # $f3 tempory vaiable # $f20 storing constant 2 for dividing # $f21 storing constant 0.001 for exit comparision calsqrt: addi $sp, $sp, -24 swc1 $f0, 20($sp) swc1 $f1, 16($sp) swc1 $f2, 12($sp) swc1 $f3, 8($sp) swc1 $f20, 4($sp) swc1 $f21, 0($sp) mtc1 $a0, $f0 # $f0 gets n li.s $f20, 2.0 # $f20 storing constant 2 for dividing li.s $f21, 0.001 # $f21 storing constant 0.001 for exit comparision div.s $f1, $f0, $f20 # $f1 gets n/2 calsqrtloop: div.s $f2, $f0, $f1 # $f2 gets n/x add.s $f2, $f2, $f1 # $f2 gets n/x + x div.s $f2, $f2, $f20 # $f2 gets x'=(n/x + x)/2 sub.s $f3, $f2, $f1# $f3 gets x'-x abs.s $f3, $f3# $f3 gets |x'-x| c.lt.s $f3, $f21# set the flag if |x'-x| < 0.001 bc1t calsqrtdone mov.s $f1, $f2 j calsqrtloop calsqrtdone: mfc1 $v0, $f2 lwc1 $f0, 20($sp) lwc1 $f1, 16($sp) lwc1 $f2, 12($sp) lwc1 $f3, 8($sp) lwc1 $f20, 4($sp) lwc1 $f21, 0($sp) addi $sp, $sp, 24 jr $ra .data msg_done: .asciiz "done\n" 1. your programs will be evaluated using the following criteria: proper use of registers and memory (0-8,9-16,17-20) proper use of load, store and branch instructions (0-4,5-8,9-10) proper use of integer arithmetic instructions (0-4,5-8,9-10) proper use of floating point arithmetic instructions (0-4,5-8,9-10) proper use of subprogram calls [including use of stack] (0-4,5-8,9-10) proper use of return instructions [including use of stack] (0-2,3-4,5-5) proper design of a subprogram meet specified requirements (0-8,9-16,17-20) proper use of appropriate tools to assemble, test and debug program (0-2,3-4,5-5) appropriate documentation (0-4,5-8,9-10) the three point ranges next to each item specify the range of scores that will be used to represent ineffective, effective and highly effective performance, respectively. 0.001="" bc1t="" calsqrtdone="" mov.s="" $f1,="" $f2="" j="" calsqrtloop="" calsqrtdone:="" mfc1="" $v0,="" $f2="" lwc1="" $f0,="" 20($sp)="" lwc1="" $f1,="" 16($sp)="" lwc1="" $f2,="" 12($sp)="" lwc1="" $f3,="" 8($sp)="" lwc1="" $f20,="" 4($sp)="" lwc1="" $f21,="" 0($sp)="" addi="" $sp,="" $sp,="" 24="" jr="" $ra="" .data="" msg_done:="" .asciiz="" "done\n"="" 1.="" your="" programs="" will="" be="" evaluated="" using="" the="" following="" criteria:="" proper="" use="" of="" registers="" and="" memory="" (0-8,9-16,17-20)="" proper="" use="" of="" load,="" store="" and="" branch="" instructions="" (0-4,5-8,9-10)="" proper="" use="" of="" integer="" arithmetic="" instructions="" (0-4,5-8,9-10)="" proper="" use="" of="" floating="" point="" arithmetic="" instructions="" (0-4,5-8,9-10)="" proper="" use="" of="" subprogram="" calls="" [including="" use="" of="" stack]="" (0-4,5-8,9-10)="" proper="" use="" of="" return="" instructions="" [including="" use="" of="" stack]="" (0-2,3-4,5-5)="" proper="" design="" of="" a="" subprogram="" meet="" specified="" requirements="" (0-8,9-16,17-20)="" proper="" use="" of="" appropriate="" tools="" to="" assemble,="" test="" and="" debug="" program="" (0-2,3-4,5-5)="" appropriate="" documentation="" (0-4,5-8,9-10)="" the="" three="" point="" ranges="" next="" to="" each="" item="" specify="" the="" range="" of="" scores="" that="" will="" be="" used="" to="" represent="" ineffective,="" effective="" and="" highly="" effective="" performance,=""> 0.001 bc1t calsqrtdone mov.s $f1, $f2 j calsqrtloop calsqrtdone: mfc1 $v0, $f2 lwc1 $f0, 20($sp) lwc1 $f1, 16($sp) lwc1 $f2, 12($sp) lwc1 $f3, 8($sp) lwc1 $f20, 4($sp) lwc1 $f21, 0($sp) addi $sp, $sp, 24 jr $ra .data msg_done: .asciiz "done\n" 1. your programs will be evaluated using the following criteria: proper use of registers and memory (0-8,9-16,17-20) proper use of load, store and branch instructions (0-4,5-8,9-10) proper use of integer arithmetic instructions (0-4,5-8,9-10) proper use of floating point arithmetic instructions (0-4,5-8,9-10) proper use of subprogram calls [including use of stack] (0-4,5-8,9-10) proper use of return instructions [including use of stack] (0-2,3-4,5-5) proper design of a subprogram meet specified requirements (0-8,9-16,17-20) proper use of appropriate tools to assemble, test and debug program (0-2,3-4,5-5) appropriate documentation (0-4,5-8,9-10) the three point ranges next to each item specify the range of scores that will be used to represent ineffective, effective and highly effective performance, respectively.>