基于ARMv5TE架构的软件除法

近来有点悠闲,所以无聊中做了一个基于二进制试商法的无符号整型除法实现。

以下函数仅对于ARMv5T架构或更高版本架构的处理器有效。汇编用的是RVCT3.1或更高版本

算法汇编文件:

 1     AREA    MY_TEST, CODE, READONLY  
2
3 PRESERVE8 {TRUE}
4
5 export my_udiv
6
7 CODE32
8
9 my_udiv
10
11 mov r12, r0
12 mov r0, #0
13 cmp r12, #0
14 bxeq lr
15 cmp r1, #0
16 bxeq lr
17 cmp r12, r1
18 bxlo lr
19
20
21 MY_UDIV_LOOP
22
23 clz r2, r12
24 clz r3, r1
25 sub r2, r3, r2
26 mov r3, r1, LSL r2
27
28 cmp r12, r3
29 sublo r2, r2, #1
30 movlo r3, r3, LSR #1
31
32 sub r12, r12, r3
33 mov r3, #1
34 orr r0, r0, r3, LSL r2
35
36 cmp r12, r1
37 bhs MY_UDIV_LOOP
38 bxlo lr
39
40 END

 

主机端的测试程序:

 1 extern unsigned my_udiv(unsigned dividend, unsigned divisor);  
2
3 int g1 =12345678, g2 =3;
4
5 int main(void)
6 {
7 int a = my_udiv(g1, g2);
8
9 printf("The value is: %d\r\n", a);
10
11 int b = g1 / g2;
12 printf("The value is: %d\r\n", b);
13 }
posted @ 2011-08-31 17:56  zenny_chen  Views(4013)  Comments(0Edit  收藏  举报