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...

1 answer below »
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,="">
Answered 3 days AfterApr 04, 2021

Answer To: 1. (60 points) Please implement two functions, where one calls the other, following the MIPS calling...

Gaurav answered on Apr 07 2021
157 Votes
mips2.asm
# mips2.asm - calculates cosine appromixation using infinite series x(n) = x(n - 1) + (-1)^n * x^2n / 2n! where n = 0 - infinity
    .data
f_1:    .float 1.00
# function main calls appcos a
nd display the results
# Register use
#    $f0 = -5.0
#    $a0 - argument 1 = x
#    $a1 - argument 2 = n
#     $v0 - syscall parameter
#    $f12 - syscall parameter
    .text
main:
    li.s $f0, -5.0
    mfc1 $a0, $f0
    li $a1, 30
    jal appcos
    
    mtc1 $v0, $f12
    li $v0,2
    syscall
    
eixt:    li $v0,10
    syscall
# Function appcos calculates cosine appromixation using infinite series x(n) = x(n - 1) + (-1)^n * x^2n / 2n! where n = 0 - infinity
# Function starts with initial value of 1.0, each term have x^2 in common, and factorial part incremented by 2,
# so x^2 is taken as constant and multiplied with each iteration, for factorial simply divide by n + 1 and n + 2
# of the previous term. then adds and subtracts alternatively, with the initial value
# Arguments
#    $a0 - x
#    $a1 - n
# returns
#    $v0 - cos(x)
# Register Use
#    $f0 - calculation result
#    $f2 - x^2 term
#    $f3 - factorial counter
#    $f4 - single term value
#    $f20 - constant 1.0
#    $t0 - $t1 - temporary variable
appcos:
    ldc1 $f0, f_1($zero)        # $f0 holds the initial value of 1.0
    mov.s $f20, $f0            # $f20 holds the constant value of 1.0 for incrementing in factorial calculations
    
    addi $t0, $zero, 1        # number of terms, initial value already have 1.0
    mtc1 $a0, $f2            # $f2 gets the x
    mul.s $f2, $f2, $f2        # $f2 = $f2 * $f2, constant of x^2
    addi $t1, $zero, 2        
    mtc1 $t1, $f3            # n = 2!
    cvt.s.w $f3, $f3        
    div.s $f4, $f2, $f3        # $f4 = x^2 / 2!
loop:    
    andi $t1, $t0, 0x01
    beqz $t1, even_position        # add or subtract in alternate manner starts with addition
    sub.s $f0, $f0, $f4        # $f0 = $f0 - $f4
    b continue
    
even_position:
    add.s $f0, $f0, $f4        # $f0 = $f0 + $f4
    
continue:
    mul.s $f4, $f4, $f2        # $f4 * x^2
    add.s $f3, $f3, $f20        # n + 1
    div.s $f4, $f4, $f3        # $f4 / (n +...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here