64 bit division in ARM Assembly SOS

泪湿孤枕 提交于 2019-12-13 12:35:30

问题


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

回答1:


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

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.

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

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.



来源:https://stackoverflow.com/questions/26633800/64-bit-division-in-arm-assembly-sos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!