project1
Dependencies: mbed TSI MMA8451Q
rootsquare.c@2:316ddc3b4384, 2019-09-19 (annotated)
- Committer:
- bcis93
- Date:
- Thu Sep 19 20:45:17 2019 +0000
- Revision:
- 2:316ddc3b4384
- Parent:
- 1:69b15daf7a7d
Adding dimming with the touch sensor (using the TSI library)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nmaududi | 0:529f900ad93b | 1 | /************************************************************************//** |
nmaududi | 0:529f900ad93b | 2 | * \file main.c |
nmaududi | 0:529f900ad93b | 3 | * \brief LAB EXERCISE 5.2 - SQUARE ROOT APPROXIMATION |
nmaududi | 0:529f900ad93b | 4 | * |
nmaududi | 0:529f900ad93b | 5 | * Write an assembly code subroutine to approximate the square root of an |
nmaududi | 0:529f900ad93b | 6 | * argument using the bisection method. All math is done with integers, so the |
nmaududi | 0:529f900ad93b | 7 | * resulting square root will also be an integer |
nmaududi | 0:529f900ad93b | 8 | ****************************************************************************** |
nmaududi | 0:529f900ad93b | 9 | * GOOD LUCK! |
nmaududi | 0:529f900ad93b | 10 | ****************************************************************************/ |
nmaududi | 0:529f900ad93b | 11 | |
nmaududi | 0:529f900ad93b | 12 | #include "stdint.h" |
bcis93 | 1:69b15daf7a7d | 13 | #include <stdio.h> |
nmaududi | 0:529f900ad93b | 14 | |
nmaududi | 0:529f900ad93b | 15 | |
nmaududi | 0:529f900ad93b | 16 | /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */ |
nmaududi | 0:529f900ad93b | 17 | #include "string.h" |
nmaududi | 0:529f900ad93b | 18 | /** @endcond */ |
nmaududi | 0:529f900ad93b | 19 | |
nmaududi | 0:529f900ad93b | 20 | /** |
nmaududi | 0:529f900ad93b | 21 | * @brief 5863 Write a brief description of the function here |
nmaududi | 0:529f900ad93b | 22 | * |
nmaududi | 0:529f900ad93b | 23 | * Detailed description of the function after one line gap |
nmaududi | 0:529f900ad93b | 24 | * |
nmaududi | 0:529f900ad93b | 25 | * @note You can also add a note this way |
nmaududi | 0:529f900ad93b | 26 | * |
nmaududi | 0:529f900ad93b | 27 | * @param[in] |
nmaududi | 0:529f900ad93b | 28 | * You can also start writing on a new line. |
nmaududi | 0:529f900ad93b | 29 | * And continue on the next line. This is how you describe an input parameter |
nmaududi | 0:529f900ad93b | 30 | * |
nmaududi | 0:529f900ad93b | 31 | * @return |
nmaududi | 0:529f900ad93b | 32 | * This is how you describe the return value |
nmaududi | 0:529f900ad93b | 33 | * |
nmaududi | 0:529f900ad93b | 34 | */ |
nmaududi | 0:529f900ad93b | 35 | __asm int my_sqrt(int x){ |
bcis93 | 1:69b15daf7a7d | 36 | PUSH {r4-r7} ; we use r4-r7, which are callee stored registers, so we need to save them on the stack |
nmaududi | 0:529f900ad93b | 37 | |
bcis93 | 1:69b15daf7a7d | 38 | MOVS r1, #0; r1 will be the register to store a, initialized to 0 |
bcis93 | 1:69b15daf7a7d | 39 | MOVS r2,#200; r2 is for variable b, store the largest squaure root round[sqrt(2^31-1)] (46341) |
bcis93 | 1:69b15daf7a7d | 40 | MOVS r3,#1; r3 is for variable c, it has been define and intialized |
nmaududi | 0:529f900ad93b | 41 | NEGS r3,r3; |
bcis93 | 1:69b15daf7a7d | 42 | MOVS r4,#0; r4 is for variable done |
nmaududi | 0:529f900ad93b | 43 | |
nmaududi | 0:529f900ad93b | 44 | loop_while |
nmaududi | 0:529f900ad93b | 45 | |
bcis93 | 1:69b15daf7a7d | 46 | MOVS r5,r3; r5 is allocated for c old within the code |
bcis93 | 1:69b15daf7a7d | 47 | ADDS r3, r1, r2; stores into c = a+b |
bcis93 | 1:69b15daf7a7d | 48 | LSRS r3,r3,#1; multiply c by 0.5 to get c= (a+b)/2, completed a shift to complete division |
nmaududi | 0:529f900ad93b | 49 | MOVS r6,r3; r6 is allocated for c within the code |
bcis93 | 1:69b15daf7a7d | 50 | MULS r6,r6,r6; r6 now contains x*x as implemented int code |
nmaududi | 0:529f900ad93b | 51 | |
bcis93 | 1:69b15daf7a7d | 52 | CMP r6,r0; Compare c*c and x, start of the if loop |
bcis93 | 1:69b15daf7a7d | 53 | BEQ sqrt_loop_done |
bcis93 | 1:69b15daf7a7d | 54 | BLT sqrt_elseif |
nmaududi | 0:529f900ad93b | 55 | |
nmaududi | 0:529f900ad93b | 56 | MOVS r2,r3; b is loaded with the value of c, b <--c |
nmaududi | 0:529f900ad93b | 57 | |
bcis93 | 1:69b15daf7a7d | 58 | B end_of_sqrtloop |
nmaududi | 0:529f900ad93b | 59 | sqrt_elseif |
nmaududi | 0:529f900ad93b | 60 | MOVS r1,r3; a is loaded with the value of c, a <--c |
bcis93 | 1:69b15daf7a7d | 61 | B end_of_sqrtloop |
nmaududi | 0:529f900ad93b | 62 | |
nmaududi | 0:529f900ad93b | 63 | sqrt_loop_done |
nmaududi | 0:529f900ad93b | 64 | |
bcis93 | 1:69b15daf7a7d | 65 | MOVS r4,#1; store 1 into the variable done |
nmaududi | 0:529f900ad93b | 66 | |
nmaududi | 0:529f900ad93b | 67 | end_of_sqrtloop |
bcis93 | 1:69b15daf7a7d | 68 | MVNS r6,r4; r6 ==!done, was c*c above |
bcis93 | 1:69b15daf7a7d | 69 | CMP r5, r3; compare cold and c |
bcis93 | 1:69b15daf7a7d | 70 | BNE skip |
bcis93 | 1:69b15daf7a7d | 71 | MOVS r7,#0; set r7 equal to 0 if cold and c are equal |
bcis93 | 1:69b15daf7a7d | 72 | B skip_over |
nmaududi | 0:529f900ad93b | 73 | skip |
bcis93 | 1:69b15daf7a7d | 74 | MOVS r7,#1; |
bcis93 | 1:69b15daf7a7d | 75 | NEGS r7,r7; |
nmaududi | 0:529f900ad93b | 76 | skip_over |
bcis93 | 1:69b15daf7a7d | 77 | CMP r6,r7; |
bcis93 | 1:69b15daf7a7d | 78 | BEQ loop_while |
bcis93 | 1:69b15daf7a7d | 79 | |
bcis93 | 1:69b15daf7a7d | 80 | MOVS r0,r3 ; store the return value in r0 |
bcis93 | 1:69b15daf7a7d | 81 | POP {r4-r7} ; restore r4-r7 |
bcis93 | 1:69b15daf7a7d | 82 | BLX lr ; Else return from subroutine with link and returning the variable c |
nmaududi | 0:529f900ad93b | 83 | //CMP |
nmaududi | 0:529f900ad93b | 84 | |
nmaududi | 0:529f900ad93b | 85 | |
nmaududi | 0:529f900ad93b | 86 | } |
nmaududi | 0:529f900ad93b | 87 | |
nmaududi | 0:529f900ad93b | 88 | /*---------------------------------------------------------------------------- |
nmaududi | 0:529f900ad93b | 89 | MAIN function |
nmaududi | 0:529f900ad93b | 90 | *----------------------------------------------------------------------------*/ |
nmaududi | 0:529f900ad93b | 91 | /** |
nmaududi | 0:529f900ad93b | 92 | * @brief Main function |
nmaududi | 0:529f900ad93b | 93 | * |
nmaududi | 0:529f900ad93b | 94 | * Detailed description of the main |
nmaududi | 0:529f900ad93b | 95 | */ |
bcis93 | 2:316ddc3b4384 | 96 | /* |
nmaududi | 0:529f900ad93b | 97 | int main(void){ |
bcis93 | 1:69b15daf7a7d | 98 | int test_values[4] = {2,4,22,121}; |
bcis93 | 1:69b15daf7a7d | 99 | volatile int result = 0; |
bcis93 | 1:69b15daf7a7d | 100 | |
bcis93 | 1:69b15daf7a7d | 101 | for (int i = 0; i < 4; i++) |
bcis93 | 1:69b15daf7a7d | 102 | { |
bcis93 | 1:69b15daf7a7d | 103 | result = my_sqrt(test_values[i]); |
bcis93 | 1:69b15daf7a7d | 104 | printf("my_sqrt(%d): %d\n\r", test_values[i], result); |
bcis93 | 1:69b15daf7a7d | 105 | } |
nmaududi | 0:529f900ad93b | 106 | } |
bcis93 | 2:316ddc3b4384 | 107 | */ |
nmaududi | 0:529f900ad93b | 108 | |
nmaududi | 0:529f900ad93b | 109 | // *******************************ARM University Program Copyright © ARM Ltd 2014*************************************/ |