简体   繁体   中英

How to perform square root on a number in ARM assembly

Hi I'm new to ARM assembly and would like to know how to square root a number.

I'm currently using a raspberry pi to write and compile my code

For example:

.data

s1: .word 20

.text
.global main

main:
    LDR R1,=s1
    LDR R1,[R1]
    //How do I sqroot R1 from here?

The easiest way for such cases is to ask the C compiler for help. Given this C code:

#include <math.h>

unsigned int mysqrt(unsigned int x) {
    return sqrt(x);
}

The compiler produces (slightly cleaned up):

mysqrt:
    vmov          s0, r0    @ move x to FP register
    vcvt.f64.u32  d16, s0   @ convert from int to double
    vsqrt.f64     d16, d16  @ compute square root
    vcvt.u32.f64  s0, d16   @ convert back to integer
    vmov          r0, s0    @ move back to general purpose register
    bx            lr

So that's what you should do, too. If the number is in RAM initially, you can directly load it into a FP register like this:

    ldr           r0, =s1
    vldr          s0, [r1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM