-
Notifications
You must be signed in to change notification settings - Fork 32
/
div.s
69 lines (57 loc) · 955 Bytes
/
div.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@ ARM software division implementation
@ These functions are assembled using neatas and are included in gen.c
.global __udivdi3
__udivdi3:
mov r2, #0
mov r3, #0
@ zero divider
tst r1, r1
beq .end
@ shift the operand
.shl:
movs r12, r1, LSL r2
add r2, r2, #1
bpl .shl
mov r12, #1
@ the main division algorithm
.shr:
subs r2, r2, #1
bmi .end
cmps r0, r1, LSL r2
bcc .shr
sub r0, r0, r1, LSL r2
add r3, r3, r12, LSL r2
b .shr
.end:
mov r1, r0
mov r0, r3
mov pc, lr
.global __umoddi3
__umoddi3:
stmfd sp!, {lr}
bl __udivdi3
mov r0, r1
ldmfd sp!, {pc}
.global __divdi3
__divdi3:
stmfd sp!, {r4, r5, lr}
mov r4, r0
mov r5, r1
@ handle negative operands
tst r0, r0
rsbmi r0, r0, #0
tst r1, r1
rsbmi r1, r1, #0
bl __udivdi3
@ result is negative
teq r4, r5
rsbmi r0, r0, #0
tst r4, r4
rsbmi r1, r1, #0
ldmfd sp!, {r4, r5, pc}
.global __moddi3
__moddi3:
stmfd sp!, {lr}
bl __divdi3
mov r0, r1
ldmfd sp!, {pc}