本文介绍了ARM Assembly SOS 中的 64 位除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算 16 个 64 位数字相加的平均值,我认为我已经正确完成了所有的加法,但现在我需要弄清楚如何将 64 位数字除以 16,我被卡住了!任何帮助都会非常感谢你.到目前为止,这是我的代码.

I am calculating the average of sixteen 64 bit numbers added together and I think that I have done all the addition correctly, but now I need to figure out how to divide a 64 bit number by 16 and I am stuck! Any help would be great thank you so much. Here is my code so far.

tableSize       EQU     16
sum             EQU     0x40000000
average         EQU     0x40000008
                MOV r8, #14

                ADR r0, table
                LDR r9, =sum
                LDR r10,=average

                LDR r1, [r0], #1    ;hi #1
                LDR r2, [r0], #1    ;lo #1
SUM
                SUB r8, r8, #1
                LDR r3, [r0], #1    ;hi #2
                LDR r4, [r0], #1    ;lo #2
                ADDS    r5, r2, r4  ;lo 1 + lo 2 set flags
                ADC r6, r1, r3  ;hi 1 + hi 2 + carry
                MOV r1, r6
                MOV r2, r5
                CMP r8, 0
                BNE SUM

                STR r1, [r9], #8
                STR r2, [r9]
 average
                ;stuck here


table           DCQ     0x0200200AD00236DD
                DCQ     0x00003401AAC4D097
                DCQ     0x000001102ACFF200
                DCQ     0x00010AA0AD3C66DF
                DCQ     0x0000FC3D76400CCB
                DCQ     0x000090045ACDD097
                DCQ     0x00000FF000004551
                DCQ     0x00000000003C66DF
                DCQ     0x1000200AD00236DD
                DCQ     0x00003401AAC4D097
                DCQ     0x000001102ACFF200
                DCQ     0x00010AA0AD3C66DF
                DCQ     0x1000FC3D76400CCB
                DCQ     0x000090045ACDD097
                DCQ     0x00000FF000004551
                DCQ     0x00000000003C66DF

推荐答案

鉴于 r0r1 中有一个 64 位有符号整数,可以将其除以 16使用以下说明:

Given that there's a 64 bit signed integer in r0 and r1, one can divide it by 16 with the following instructions:

    lsl     r2, r0, #28
    asr     r0, r0, #4
    orr     r1, r2, r1, lsr #4

简而言之,我们需要做的就是将两半移动四位,并将r0的低四位放入r1的高四位.

In a nutshell, all we need to do is to shift both halves by four and put lower four bits of r0 into four upper bits of r1.

要获得无符号除法,应使用 lsr 而不是 asr.

To get unsigned division, one should use lsr instead of asr.

在这两种情况下,结果都将向负无穷大四舍五入.要将结果四舍五入到最接近的整数,可以在除法前将整数加 8.此外,可以加 15 以向正无穷大舍入.

In both cases the result will be rounded towards minus infinity. To round the result towards the nearest integer one can add 8 to the integer before division. Also, one can add 15 to round towards plus infinity.

这篇关于ARM Assembly SOS 中的 64 位除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:19