forked from aviatorRHK/rpiForth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaths.s
executable file
·52 lines (46 loc) · 1.18 KB
/
maths.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
/******************************************************************************
* maths.s
* by Alex Chadwick
*
* A sample assembly code implementation of the input02 operating system.
* See main.s for details.
*
* maths.s contains the rountines for mathematics.
******************************************************************************/
/*
* DivideU32 Divides one unsigned 32 bit number in r0 by another in r1 and
* returns the result in r0 and the remainder in r1.
* C++ Signature: u32x2 DivideU32(u32 dividend, u32 divisor);
* This is implemented as binary long division.
*/
.globl DivideU32
DivideU32:
result .req r0
remainder .req r1
shift .req r2
current .req r3
clz shift,r1
clz r3,r0
subs shift,r3
lsl current,r1,shift
mov remainder,r0
mov result,#0
blt divideU32Return$
divideU32Loop$:
cmp remainder,current
blt divideU32LoopContinue$
add result,result,#1
subs remainder,current
lsleq result,shift
beq divideU32Return$
divideU32LoopContinue$:
subs shift,#1
lsrge current,#1
lslge result,#1
bge divideU32Loop$
divideU32Return$:
.unreq current
mov pc,lr
.unreq result
.unreq remainder
.unreq shift