Question

Your code must approximate the square root of an integer between 0 and 2^31-1 Using integer...

Your code must approximate the square root of an integer between 0 and 2^31-1 Using integer maths is sufficient (no floating point or fractions are required); your code will return the truncated (integer portion) of the square root. Your code must be in an assembly language subroutine which is called by a C function for testing. Be sure to use registers according to the ARM calling convention.

Base your software on the following pseudocode:

Approximate square root with bisection method
INPUT: Argument x, endpoint values a, b, such that a < b OUTPUT: value which differs from sqrt(x) by less than 1

done = 0
a=0
b = square root of largest possible argument (e.g. ~216). c = -1
do {

c_old <- c
c <- (a+b)/2
if (c*c == x) {

               done = 1
        } else if (c*c < x) {

a <- c } else {

b <- c

}
} while (!done) && (c != c_old)

return c

Write an assembly code subroutine to approximate the square root of an
argument using the bisection method. All math is done with integers, so the
resulting square root will also be an integer

GOOD LUCK!
*----------------------------------------------------------------------------*/

__asm int my_sqrt(int x){
  
   //Write your code here

}

/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main(void){
   volatile int r, j=0;
   int i;
r = my_sqrt(0); // should be 0
r = my_sqrt(25); // should be 5
   r = my_sqrt(133);    // should be 11
for (i=0; i<10000; i++){
       r = my_sqrt(i);
j+=r;
}
   while(1)
       ;
}

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

#include <stdio.h>

#include <math.h>

int my_sqrt(int x){

int ivar; //taking integer

float fvar; //taking float

fvar=sqrt((double)x); //calculating root of number

ivar=fvar; //converting float to integer

if(ivar==fvar) // if float and int both are same then it is perfect square

return ivar; //returning root

else

return 0;

}

int main(){

int r, j=0;

int i;

r = my_sqrt(0); // should be 0

r = my_sqrt(25); // should be 5

r = my_sqrt(133); // should be 11

for (i=0; i<10000; i++){

r = my_sqrt(i);

j+=r;

}

printf("%d",j);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Your code must approximate the square root of an integer between 0 and 2^31-1 Using integer...
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
  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • Write three functions that compute the square root of an argument using three different methods. The...

    Write three functions that compute the square root of an argument using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number is the values such that x . For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that is in the range 0.0 to 100.0 You program should have a main() that asks the user for x and an accuracy...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • 5. Consider the following C code: int main() int x = 1; switch (x) case 1:...

    5. Consider the following C code: int main() int x = 1; switch (x) case 1: i=1; case 2: i=5; return 0; Based on this code, answer the following: (a) Convert the above C code to MIPS assembly. (b) The condition you are testing is met at Case 1. You do not want the Case 2 to be tested. Add appropriate instructions in MIPS for this implementation. (c) If int x is any integer other than 1 or 2, you...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Given the following C Code segment convert it to ARM assembly. Assume the following register assignment...

    Given the following C Code segment convert it to ARM assembly. Assume the following register assignment have been made before your section of code begins. C Variable Register assignment r1 y r2 r3 r10 j r11 int x=0, y=0, z=0; int main() { for (int i = 0; i<10; i++) for (int j 0; j < 20; j++) if (i* j > 100) X++; if (i j >= 15) y++; + فہه Z = X + y; }

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int n;       ...

  • The equation f(x) = (1 ‐ x) cos x ‐ sin x = 0 has at...

    The equation f(x) = (1 ‐ x) cos x ‐ sin x = 0 has at least one root between a = 0 and b = 1 since f(a)f(b) < 0. The bisection method of finding the root proceeds as follows: a. It finds the midpoint r = (a + b)/2. b. If f(r) = 0, then r is the root. If |b ‐ a| is very small less than ∈ then also we can take r as the root....

  • Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

    Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { ​int temp = numbers[i];​ /* put numbers[i] somewhere to keep it safe */ ​numbers[i] = numbers[j]; /* put numbers[j] at index i */ ​numbers[j] = temp;​ /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...

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