![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Module 1 ECEN 5803 Project 1 Submitted by 1. Rahul Yamasani 2. Srivishnuj Alvakonda
main.cpp@0:a7b003d6eac1, 2017-10-12 (annotated)
- Committer:
- sral4054
- Date:
- Thu Oct 12 03:22:25 2017 +0000
- Revision:
- 0:a7b003d6eac1
Module 1 ECEN 5803 Project 1; ; Submitted by; 1. Rahul Yamasani; 2. Srivishnuj Alvakonda;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sral4054 | 0:a7b003d6eac1 | 1 | #include "mbed.h" |
sral4054 | 0:a7b003d6eac1 | 2 | Serial pc(USBTX, USBRX); //serial channel over HDK USB interface |
sral4054 | 0:a7b003d6eac1 | 3 | Timer timer; |
sral4054 | 0:a7b003d6eac1 | 4 | /************************************************************************//** |
sral4054 | 0:a7b003d6eac1 | 5 | * \file main.c |
sral4054 | 0:a7b003d6eac1 | 6 | * \brief LAB EXERCISE 5.2 - SQUARE ROOT APPROXIMATION |
sral4054 | 0:a7b003d6eac1 | 7 | * |
sral4054 | 0:a7b003d6eac1 | 8 | * Write an assembly code subroutine to approximate the square root of an |
sral4054 | 0:a7b003d6eac1 | 9 | * argument using the bisection method. All math is done with integers, so the |
sral4054 | 0:a7b003d6eac1 | 10 | * resulting square root will also be an integer |
sral4054 | 0:a7b003d6eac1 | 11 | ****************************************************************************** |
sral4054 | 0:a7b003d6eac1 | 12 | * GOOD LUCK! |
sral4054 | 0:a7b003d6eac1 | 13 | ****************************************************************************/ |
sral4054 | 0:a7b003d6eac1 | 14 | |
sral4054 | 0:a7b003d6eac1 | 15 | #include "stdint.h" |
sral4054 | 0:a7b003d6eac1 | 16 | |
sral4054 | 0:a7b003d6eac1 | 17 | |
sral4054 | 0:a7b003d6eac1 | 18 | /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */ |
sral4054 | 0:a7b003d6eac1 | 19 | #include "string.h" |
sral4054 | 0:a7b003d6eac1 | 20 | /** @endcond */ |
sral4054 | 0:a7b003d6eac1 | 21 | |
sral4054 | 0:a7b003d6eac1 | 22 | /** |
sral4054 | 0:a7b003d6eac1 | 23 | * @brief 5863 Write a brief description of the function here |
sral4054 | 0:a7b003d6eac1 | 24 | * |
sral4054 | 0:a7b003d6eac1 | 25 | * Detailed description of the function after one line gap |
sral4054 | 0:a7b003d6eac1 | 26 | * |
sral4054 | 0:a7b003d6eac1 | 27 | * @note You can also add a note this way |
sral4054 | 0:a7b003d6eac1 | 28 | * |
sral4054 | 0:a7b003d6eac1 | 29 | * @param[in] |
sral4054 | 0:a7b003d6eac1 | 30 | * You can also start writing on a new line. |
sral4054 | 0:a7b003d6eac1 | 31 | * And continue on the next line. This is how you describe an input parameter |
sral4054 | 0:a7b003d6eac1 | 32 | * |
sral4054 | 0:a7b003d6eac1 | 33 | * @return |
sral4054 | 0:a7b003d6eac1 | 34 | * This is how you describe the return value |
sral4054 | 0:a7b003d6eac1 | 35 | * |
sral4054 | 0:a7b003d6eac1 | 36 | */ |
sral4054 | 0:a7b003d6eac1 | 37 | |
sral4054 | 0:a7b003d6eac1 | 38 | __asm int my_sqrt(int x){ |
sral4054 | 0:a7b003d6eac1 | 39 | |
sral4054 | 0:a7b003d6eac1 | 40 | |
sral4054 | 0:a7b003d6eac1 | 41 | PUSH {R4-R7} ; |
sral4054 | 0:a7b003d6eac1 | 42 | LDR R1, =0 ;done = 0 |
sral4054 | 0:a7b003d6eac1 | 43 | LDR R7, =0 ;a=0 |
sral4054 | 0:a7b003d6eac1 | 44 | LDR R2, =0xFFFF ;b=2^31 -1 |
sral4054 | 0:a7b003d6eac1 | 45 | LDR R3, =0xFFFFFFFF ;c=-1 |
sral4054 | 0:a7b003d6eac1 | 46 | DO_WHILE |
sral4054 | 0:a7b003d6eac1 | 47 | MOV R4, R3 ;c_old = c HERE R4 is being considered as c_old |
sral4054 | 0:a7b003d6eac1 | 48 | ADDS R5, R2, R7 ;R5 = a+b |
sral4054 | 0:a7b003d6eac1 | 49 | ASRS R5, R5, #1 ;R5 >> 1 which is nothing but (a+b/2) |
sral4054 | 0:a7b003d6eac1 | 50 | MOV R3, R5 ;c = (a+b)/2 |
sral4054 | 0:a7b003d6eac1 | 51 | MOV R6,R3 |
sral4054 | 0:a7b003d6eac1 | 52 | MULS R6, R3, R6 ;R6 = c*c |
sral4054 | 0:a7b003d6eac1 | 53 | CMP R6,R0 ;compare c*c and x (input integer) |
sral4054 | 0:a7b003d6eac1 | 54 | BEQ DONE |
sral4054 | 0:a7b003d6eac1 | 55 | BLT LESS_THAN |
sral4054 | 0:a7b003d6eac1 | 56 | MOV R2, R3 ;setting b=c |
sral4054 | 0:a7b003d6eac1 | 57 | B FINAL |
sral4054 | 0:a7b003d6eac1 | 58 | |
sral4054 | 0:a7b003d6eac1 | 59 | LESS_THAN |
sral4054 | 0:a7b003d6eac1 | 60 | MOV R7,R3 ;setting a=c |
sral4054 | 0:a7b003d6eac1 | 61 | B FINAL |
sral4054 | 0:a7b003d6eac1 | 62 | |
sral4054 | 0:a7b003d6eac1 | 63 | |
sral4054 | 0:a7b003d6eac1 | 64 | DONE |
sral4054 | 0:a7b003d6eac1 | 65 | LDR R1, =1 ;setting done = 1 |
sral4054 | 0:a7b003d6eac1 | 66 | B FINAL ;return C |
sral4054 | 0:a7b003d6eac1 | 67 | |
sral4054 | 0:a7b003d6eac1 | 68 | |
sral4054 | 0:a7b003d6eac1 | 69 | FINAL |
sral4054 | 0:a7b003d6eac1 | 70 | CMP R1, #1 ;compare done = 1 |
sral4054 | 0:a7b003d6eac1 | 71 | BEQ END |
sral4054 | 0:a7b003d6eac1 | 72 | BNE COMPARE |
sral4054 | 0:a7b003d6eac1 | 73 | |
sral4054 | 0:a7b003d6eac1 | 74 | |
sral4054 | 0:a7b003d6eac1 | 75 | COMPARE |
sral4054 | 0:a7b003d6eac1 | 76 | CMP R4, R3 ;compare c_old and c |
sral4054 | 0:a7b003d6eac1 | 77 | BNE DO_WHILE |
sral4054 | 0:a7b003d6eac1 | 78 | BEQ END |
sral4054 | 0:a7b003d6eac1 | 79 | |
sral4054 | 0:a7b003d6eac1 | 80 | |
sral4054 | 0:a7b003d6eac1 | 81 | END |
sral4054 | 0:a7b003d6eac1 | 82 | MOV R0, R3 |
sral4054 | 0:a7b003d6eac1 | 83 | POP {R4-R7} |
sral4054 | 0:a7b003d6eac1 | 84 | BX LR |
sral4054 | 0:a7b003d6eac1 | 85 | } |
sral4054 | 0:a7b003d6eac1 | 86 | |
sral4054 | 0:a7b003d6eac1 | 87 | void printCpuCycles(double readValue, int number, int sqrt) |
sral4054 | 0:a7b003d6eac1 | 88 | { |
sral4054 | 0:a7b003d6eac1 | 89 | double cpuCycles = 0; |
sral4054 | 0:a7b003d6eac1 | 90 | |
sral4054 | 0:a7b003d6eac1 | 91 | cpuCycles = (readValue * 48000000); // read value * frequqncy of clock |
sral4054 | 0:a7b003d6eac1 | 92 | pc.printf("Sqrt of %d using bisection menthod is %d \n\r", number, sqrt); |
sral4054 | 0:a7b003d6eac1 | 93 | pc.printf("Total time of execution for sqrt of(%d)is %f \n\r", number, readValue); |
sral4054 | 0:a7b003d6eac1 | 94 | pc.printf("Number of CPU cycles for sqrt(%d): %d\n\r\n\r", number, (int)cpuCycles); |
sral4054 | 0:a7b003d6eac1 | 95 | } |
sral4054 | 0:a7b003d6eac1 | 96 | /*---------------------------------------------------------------------------- |
sral4054 | 0:a7b003d6eac1 | 97 | MAIN function |
sral4054 | 0:a7b003d6eac1 | 98 | *----------------------------------------------------------------------------*/ |
sral4054 | 0:a7b003d6eac1 | 99 | /** |
sral4054 | 0:a7b003d6eac1 | 100 | * @brief Main function |
sral4054 | 0:a7b003d6eac1 | 101 | * |
sral4054 | 0:a7b003d6eac1 | 102 | * Detailed description of the main |
sral4054 | 0:a7b003d6eac1 | 103 | */ |
sral4054 | 0:a7b003d6eac1 | 104 | int main(void){ |
sral4054 | 0:a7b003d6eac1 | 105 | |
sral4054 | 0:a7b003d6eac1 | 106 | volatile int r, j=0; |
sral4054 | 0:a7b003d6eac1 | 107 | |
sral4054 | 0:a7b003d6eac1 | 108 | double timer_read = 0.0; |
sral4054 | 0:a7b003d6eac1 | 109 | double sum = 0; |
sral4054 | 0:a7b003d6eac1 | 110 | |
sral4054 | 0:a7b003d6eac1 | 111 | pc.printf("/*----------------------------------------------------------------------------*/\r\n\r\n"); |
sral4054 | 0:a7b003d6eac1 | 112 | pc.printf("Frequency of KL25Z (ARM Cortex M0+): 48 MHZ\n\r\n\r"); |
sral4054 | 0:a7b003d6eac1 | 113 | |
sral4054 | 0:a7b003d6eac1 | 114 | timer.start(); |
sral4054 | 0:a7b003d6eac1 | 115 | |
sral4054 | 0:a7b003d6eac1 | 116 | timer.reset(); |
sral4054 | 0:a7b003d6eac1 | 117 | r = my_sqrt(2); // should be 1 |
sral4054 | 0:a7b003d6eac1 | 118 | timer_read = timer.read(); |
sral4054 | 0:a7b003d6eac1 | 119 | printCpuCycles(timer_read, 2, r); |
sral4054 | 0:a7b003d6eac1 | 120 | sum = sum + timer_read; |
sral4054 | 0:a7b003d6eac1 | 121 | timer.reset(); |
sral4054 | 0:a7b003d6eac1 | 122 | |
sral4054 | 0:a7b003d6eac1 | 123 | |
sral4054 | 0:a7b003d6eac1 | 124 | |
sral4054 | 0:a7b003d6eac1 | 125 | r = my_sqrt(4); // should be 2 |
sral4054 | 0:a7b003d6eac1 | 126 | timer_read = timer.read(); |
sral4054 | 0:a7b003d6eac1 | 127 | printCpuCycles(timer_read, 4, r); |
sral4054 | 0:a7b003d6eac1 | 128 | sum = sum + timer_read; |
sral4054 | 0:a7b003d6eac1 | 129 | timer.reset(); |
sral4054 | 0:a7b003d6eac1 | 130 | |
sral4054 | 0:a7b003d6eac1 | 131 | |
sral4054 | 0:a7b003d6eac1 | 132 | |
sral4054 | 0:a7b003d6eac1 | 133 | r = my_sqrt(22); // should be 4 |
sral4054 | 0:a7b003d6eac1 | 134 | timer_read = timer.read(); |
sral4054 | 0:a7b003d6eac1 | 135 | printCpuCycles(timer_read, 22, r); |
sral4054 | 0:a7b003d6eac1 | 136 | sum = sum + timer_read; |
sral4054 | 0:a7b003d6eac1 | 137 | timer.reset(); |
sral4054 | 0:a7b003d6eac1 | 138 | |
sral4054 | 0:a7b003d6eac1 | 139 | r = my_sqrt(121); // should be 11 |
sral4054 | 0:a7b003d6eac1 | 140 | timer_read = timer.read(); |
sral4054 | 0:a7b003d6eac1 | 141 | printCpuCycles(timer_read, 121, r); |
sral4054 | 0:a7b003d6eac1 | 142 | sum = sum + timer_read; |
sral4054 | 0:a7b003d6eac1 | 143 | |
sral4054 | 0:a7b003d6eac1 | 144 | pc.printf("Total time of operation: %f\n\r\n\r", sum); |
sral4054 | 0:a7b003d6eac1 | 145 | sum = sum * 48000000; |
sral4054 | 0:a7b003d6eac1 | 146 | pc.printf("CPU cycles: %d\n\r\n\r", (int)(sum)); |
sral4054 | 0:a7b003d6eac1 | 147 | |
sral4054 | 0:a7b003d6eac1 | 148 | pc.printf("/*----------------------------------------------------------------------------*/\r\n\r\n"); |
sral4054 | 0:a7b003d6eac1 | 149 | |
sral4054 | 0:a7b003d6eac1 | 150 | } |
sral4054 | 0:a7b003d6eac1 | 151 | |
sral4054 | 0:a7b003d6eac1 | 152 | // *******************************ARM University Program Copyright © ARM Ltd 2014*************************************/ |
sral4054 | 0:a7b003d6eac1 | 153 |