近来有点悠闲,所以无聊中做了一个基于二进制试商法的无符号整型除法实现。
以下函数仅对于ARMv5T架构或更高版本架构的处理器有效。汇编用的是RVCT3.1或更高版本
算法汇编文件:
AREA MY_TEST, CODE, READONLY
PRESERVE8 {TRUE}
export my_udiv
CODE32
my_udiv
mov r12, r0
mov r0, #0
cmp r12, #0
bxeq lr
cmp r1, #0
bxeq lr
cmp r12, r1
bxlo lr
MY_UDIV_LOOP
clz r2, r12
clz r3, r1
sub r2, r3, r2
mov r3, r1, LSL r2
cmp r12, r3
sublo r2, r2, #1
movlo r3, r3, LSR #1
sub r12, r12, r3
mov r3, #1
orr r0, r0, r3, LSL r2
cmp r12, r1
bhs MY_UDIV_LOOP
bxlo lr
END
主机端的测试程序:
#include <stdio.h>
extern unsigned my_udiv(unsigned dividend, unsigned divisor);
static int g1 =12345678, g2 =3;
int main(void)
{
int a = my_udiv(g1, g2);
printf("The value is: %d\r\n", a);
int b = g1 / g2;
printf("The value is: %d\r\n", b);
}
有条件的朋友可以尝试执行一下~