ARM Assembly SOS中的64位划分 [英] 64 bit division in ARM Assembly SOS
问题描述
我正在计算加在一起的16个64位数字的平均值,我认为我正确地完成了所有加法运算,但是现在我需要弄清楚如何将64位数字除以16,这样我就卡住了!非常感谢您的任何帮助。到目前为止,这是我的代码。
tableSize EQU 16
sum EQU 0x40000000
平均EQU 0x40000008
MOV r8,#14
ADR r0,表
LDR r9,= sum
LDR r10,=平均值
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设置标志
ADC r6, r1,r3; hi 1 + hi 2 +携带
MOV r1,r6
MOV r2,r5
CMP r8,0
BNE SUM
STR r1,[r9],#8
STR r2,[r9]
平均
;卡在此处
表DCQ 0x0200200AD00236DD
DCQ 0x00003401AAC4D097
DCQ 0x000001102ACFF200
DCQ 0x00010AA0AD3C66DF
DCQ 0x0000FC3D76400CCB
DCQ 0x000090045ACDD097
DCQ 0x00000FF000004551
DCQ 0x00000030000C $ 340B
DCQ 0x000001102ACFF200
DCQ 0x00010AA0AD3C66DF
DCQ 0x1000FC3D76400CCB
DCQ 0x000090045ACDD097
DCQ 0x00000FF000004551
DCQ 0x00000000003C66DF
$ c p $ p>
解决方案鉴于 r0
和 r1
,可以按照以下说明将其除以16:
lsl r2,r0,#28
asr r0,r0,#4
orr r1 ,r2,r1和lsr#4
总而言之,我们要做的就是将两者都转移减半,然后将 r0
的低四位放入 r1
的高四位。
要获得无符号除法,应使用 lsr
而不是 asr
。
在两种情况下,结果都会四舍五入为负无穷大。要将结果四舍五入到最接近的整数,可以在除法之前将8加到该整数上。另外,可以向正无穷大的方向加15。
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
解决方案 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.
这篇关于ARM Assembly SOS中的64位划分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!