Module 1 root square

Dependencies:   mbed MMA8451Q

Revision:
0:529f900ad93b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rootsquare.c	Thu Sep 19 06:16:00 2019 +0000
@@ -0,0 +1,107 @@
+/************************************************************************//**
+* \file main.c
+* \brief LAB EXERCISE 5.2 - SQUARE ROOT APPROXIMATION
+*
+* 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!
+ ****************************************************************************/
+
+ #include "stdint.h"
+ 
+ 
+ /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
+ #include "string.h"
+ /** @endcond */
+ 
+ /**
+ * @brief 5863 Write a brief description of the function here
+ *
+ * Detailed description of the function after one line gap
+ *
+ * @note You can also add a note this way
+ *
+ * @param[in] 
+ *  You can also start writing on a new line. 
+ *  And continue on the next line. This is how you describe an input parameter
+ *
+ * @return
+ *  This is how you describe the return value
+ * 
+ */
+__asm int my_sqrt(int x){
+    
+    MOVS r1, #0; r1 will be the register to store a, initialized to 0
+    MOVS r2,#200; r2 is for variable b, store the largest squaure root round[sqrt(2^31-1)] (46341)
+    MOVS r3,#1;  r3 is for variable c, it has been define and intialized
+  NEGS r3,r3;
+    MOVS r4,#0; r4 is for variable done
+    
+loop_while  
+    
+    MOVS r5,r3; r5 is allocated for c old within the code
+    ADDS r3, r1, r2; stores into c = a+b
+    LSRS r3,r3,#1; multiply c by 0.5 to get c= (a+b)/2, completed a shift to complete division
+  MOVS r6,r3; r6 is allocated for c within the code
+    MULS r6,r6,r6; r6 now contains x*x as implemented int code
+    
+    CMP   r6,r0;   Compare c*c and x, start of the if loop
+    BEQ sqrt_loop_done
+    BLT sqrt_elseif
+    
+  MOVS r2,r3; b is loaded with the value of c, b <--c
+    
+    B end_of_sqrtloop
+sqrt_elseif
+    MOVS r1,r3; a is loaded with the value of c, a <--c
+    B end_of_sqrtloop
+    
+sqrt_loop_done
+    
+    MOVS r4,#1; store 1 into the variable done
+    
+end_of_sqrtloop
+    MVNS r6,r4;     r6 ==!done, was c*c above
+    CMP r5, r3;     compare cold and c
+    BNE skip
+    MOVS r7,#0;      set r7 equal to 0 if cold and c are equal
+    B skip_over
+skip
+    MOVS r7,#1;
+    NEGS r7,r7;
+skip_over
+    CMP r6,r7;
+    BEQ loop_while
+    
+  BLX    r3             ; Else return from subroutine with link and returning the variable c
+    //CMP 
+    
+    //Write your code here
+
+}
+
+/*----------------------------------------------------------------------------
+ MAIN function
+ *----------------------------------------------------------------------------*/
+ /**
+ * @brief Main function
+ *
+ * Detailed description of the main
+ */
+int main(void){
+    volatile int r1,r2,r3, j=0;
+    int i;                  
+  r1 = my_sqrt(25);     // should be 0
+  r2 = my_sqrt(25);    // should be 5
+    r3 = my_sqrt(133);  // should be 11
+  for (i=0; i<10000; i++){
+        r = my_sqrt(i);
+    j+=r;
+  }
+    while(1)
+        ;
+}
+
+// *******************************ARM University Program Copyright © ARM Ltd 2014*************************************/