0

因此,对于一个作业,我需要创建一个汇编程序来查找从二进制文件传入的浮点的立方根。一切似乎都正常,除了在计算结束时有一个无限循环。

    calcCR:
    stp     x29, x30, [sp, -16]!
    mov     x29, sp
    //d0 contains the initial value
    fmov    d31, d0
    //first step is x = input/3.0, this  is initial setup and only should be done once, loop starts on step 2
    adrp    x9, a_m   //a_m is 3.0
    add     x9, x9, :lo12:a_m
    ldr     d1, [x9]        //d1 = 3.0
    adrp    x10, b_m    //b_m is 1.0e-10
    add     x10, x10, :lo12:b_m
    ldr     d16, [x10]      //d16 = 1.0e-10
    fmul    d17, d31, d16   //d17 = input*1.0e-10
    fdiv    d2, d31, d1     //d2 = x

calcTop:
    //second step is y = x*x*x
    fmul    d3, d2, d2
    fmul    d3, d3, d2      //d3 = y
    //third step is dy = y-input
    fsub    d4, d3, d31     //d4 = dy
    //fourth step is dy/dx = 3.0*x*x
    ldr     d1, [x9]
    fmul    d5, d1, d2
    fmul    d5, d5, d2      //d5 = dy/dx
    //fifth step is x = x-dy / (dy/dx), this will be the new trial value for x
    fsub    d6, d2, d4
    fdiv    d2, d6, d5      //d2 = new trial x
    //sixth step is get the absolute value of dy
    fabs    d7, d4  //d7 = abs dy, using this value causes an infinite loop in the comparison
    //seventh step is compare the absolute value of dy to input*1.0e-10
    //if |dy| is greater, go back to calcTop, if it is less then we're done, exit loop
    fcmp    d7, d17   //this comparison and branch is causing the infinite loop.
    b.ge    calcTop

    fmov    d0, d2
    ldp     x29, x30, [sp], 16
    ret

计算部分的分配说明是:“牛顿法需要对立方根进行初始猜测;使用 x = 输入 / 3.0。计算 y,即当前猜测 x 的立方,使用 y = x * x * x。然后计算 y 与输入值之间的差 dy:使用 dy = y - input。用 dy/dx = 3.0 * x * x 计算导数 dy/dx。然后用 x = x - dy 计算 x 的新试验值“ 而且我相当有信心我正确地遵循了这一点,但显然不是,否则我认为不会有问题。

我 100% 不明白为什么它不能很好地工作,所以任何帮助都将不胜感激。

4

0 回答 0