Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an ARM Assembly code subroutine to approximate the square root of an argum

ID: 3756163 • Letter: W

Question

Write an ARM Assembly code subroutine to approximate the square root of an argument using the bisection method. 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.

Base your software on the following pseudocode:

INPUT: Argument x, endpoint values a, b, such that a

Explanation / Answer

#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){ PUSH {R4-R7} ; LDR R1, =0 ;done = 0 LDR R7, =0 ;a=0 LDR R2, =0xFFFF ;b=2^31 -1 LDR R3, =0xFFFFFFFF ;c=-1 DO_WHILE MOV R4, R3 ;c_old = c HERE R4 is being considered as c_old ADDS R5, R2, R7 ;R5 = a+b ASRS R5, R5, #1 ;R5 >> 1 which is nothing but (a+b/2) MOV R3, R5 ;c = (a+b)/2 MOV R6,R3 MULS R6, R3, R6 ;R6 = c*c CMP R6,R0 ;compare c*c and x (input integer) BEQ DONE BLT LESS_THAN MOV R2, R3 ;setting b=c B FINAL LESS_THAN MOV R7,R3 ;setting a=c B FINAL DONE LDR R1, =1 ;setting done = 1 B FINAL ;return C FINAL CMP R1, #1 ;compare done = 1 BEQ END BNE COMPARE COMPARE CMP R4, R3 ;compare c_old and c BNE DO_WHILE BEQ END END MOV R0, R3 POP {R4-R7} BX LR } void printCpuCycles(double readValue, int number, int sqrt) { double cpuCycles = 0; cpuCycles = (readValue * 48000000); // read value * frequqncy of clock pc.printf("Sqrt of %d using bisection menthod is %d ", number, sqrt); pc.printf("Total time of execution for sqrt of(%d)is %f ", number, readValue); pc.printf("Number of CPU cycles for sqrt(%d): %d ", number, (int)cpuCycles); } MAIN function *----------------------------------------------------------------------------*/ /** * @brief Main function * * Detailed description of the main */ int main(void){ volatile int r, j=0; double timer_read = 0.0; double sum = 0; pc.printf("/*----------------------------------------------------------------------------*/ "); pc.printf("Frequency of KL25Z (ARM Cortex M0+): 48 MHZ "); timer.start(); timer.reset(); r = my_sqrt(2); // should be 1 timer_read = timer.read(); printCpuCycles(timer_read, 2, r); sum = sum + timer_read; timer.reset(); r = my_sqrt(4); // should be 2 timer_read = timer.read(); printCpuCycles(timer_read, 4, r); sum = sum + timer_read; timer.reset(); r = my_sqrt(22); // should be 4 timer_read = timer.read(); printCpuCycles(timer_read, 22, r); sum = sum + timer_read; timer.reset(); r = my_sqrt(121); // should be 11 timer_read = timer.read(); printCpuCycles(timer_read, 121, r); sum = sum + timer_read; pc.printf("Total time of operation: %f ", sum); sum = sum * 48000000; pc.printf("CPU cycles: %d ", (int)(sum)); pc.printf("/*----------------------------------------------------------------------------*/ "); }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote