-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqrt.s
96 lines (70 loc) · 2.4 KB
/
sqrt.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@ Authors: Robert Rose, Alex, Mira
@ This is a quick demo of the possible sqrt function using CORDIC with
@ multiplication. We didn't feel like writing the extra credit one without
@ mul was really neccessary and it simplifies the alogrithm quite a bit to
@ be able to use mul.
@ Register use:
@ r0 = 8 since ARM can't do cmp with immediates, we store it in register.
@ r1 loop counter, "i"
@ r2 CORDIC_input, the number being processed, "x"
@ r3 CORDIC_ROOT, the sqrt result of the number, "y"
@ r4 CORDIC_BASE, the base we're currently using, "base"
@ r5 scratch register
@ r8 register for accessing addresses
.data
@ Number of times we should loop.
CORDIC_N: .word 8
@ Value of the final angle returned by the CORDIC algorithm.
CORDIC_ret: .word 0
@ A number between 0 and 65536
CORDIC_input: .word 64
@ The starting base to use for CORDIC
CORDIC_BASE: .word 128
.text
.global _start
_start:
@ num elements in Alpha because ARM can't do math with constants
mov r0, #8
@ for loop counter ; referred to throughout as "i"
mov r1, #0
@ load address into address register
ldr r8, =CORDIC_BASE
@ get value from the address. this is to prepopulate the cosine value.
ldr r4, [r8]
@ populate the initial sine value.
mov r3, #0
@ get the address of the input angle.
ldr r8, =CORDIC_input
@ load in the input angle.
ldr r2, [r8]
@ Beginning of for loop to do CORDIC
for_1:
@ i <= 8
cmp r1, r0
@ if i > 8, we break the end of the loop, otherwise keep going into the
@ loop body with the if statements.
bgt exit_for
cond_1:
@ add base to y
add r3, r4, r3
@ multiply y by itself
mul r5, r3, r3
@ see if y * y exceeds x
cmp r5, r2
@ if greater than, continue to if_1, otherwise branch to the else_1
ble skip_if_1
@ this is if we go over in estimating the sqrt, we should subtract
@ the base we just added.
if_1:
@ subtract the base from y because we went over
sub r3, r4, r3
skip_if_1:
@ i++
asr r4, r4, #1
add r1, r1, #1
b for_1 @ repeat loop by jumping back to the beginning
@@ When we exit the for loop all we have to do is store the results into memory.
exit_for:
ldr r8, =CORDIC_ret @ load address
str r3, [r8] @ get val from address
.end