-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubtractionOfPowersOld.ino
168 lines (139 loc) · 4.57 KB
/
SubtractionOfPowersOld.ino
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <Arduino.h>
/*
SubtractionOfPowers.ino
Purpose - Demonstrate (8-bit) Analog to Digital conversion using the
subtraction-of-powers method
Robert Raffaele 'GorrillaRibs' Miller
ACES 2016/17
A loop would be more intelligent, but my knowledge of them is
currently lacking
*/
int result = 0b00000000; // r12
int value = 211; // r13
uint8_t bit0 = 0b00000001; // r14
uint8_t bit1 = 0b00000010; // r15
uint8_t bit2 = 0b00000100; // r16
uint8_t bit3 = 0b00001000; // r17
uint8_t bit4 = 0b00010000; // r18
uint8_t bit5 = 0b00100000; // r19
uint8_t bit6 = 0b01000000; // r20
uint8_t bit7 = 0b10000000; // r21
// r22 and r23 are backup registers
void setup() {
Serial.begin(9600);
//Serial.print("The binary equivilent of " + String(value) + " is ");
Serial.print("Binary Equivilent of the value is ");
asm (
"LDS r12, result \n" // load everything
"LDS r13, value \n"
"LDS r14, bit0 \n"
"LDS r15, bit1 \n"
"LDS r16, bit2 \n"
"LDS r17, bit3 \n"
"LDS r18, bit4 \n"
"LDS r19, bit5 \n"
"LDS r20, bit6 \n"
"LDS r21, bit7 \n"
"MOV r13, r22 \n" // backup data
"MOV r21, r23 \n" // backup first thing to comare to
"AND r13, r21 \n" // AND the data + the mask
"CP r13, r23 \n" // compare the 2...
"BREQ addBit7 \n" // ...and branch to another function if equal
"testBit7: \n"
"MOV r13, r22 \n" // again
"MOV r20, r23 \n"
"AND r13, r20 \n"
"CP r13, r23 \n"
"BREQ addBit6 \n"
"testBit6: \n"
"MOV r13, r22 \n" // again
"MOV r19, r23 \n"
"AND r13, r19 \n"
"CP r13, r23 \n"
"BREQ addBit5 \n"
"testBit5: \n"
"MOV r13, r22 \n" // again
"MOV r18, r23 \n"
"AND r13, r18 \n"
"CP r13, r23 \n"
"BREQ addBit4 \n"
"testBit4: \n"
"MOV r13, r22 \n" // again
"MOV r17, r23 \n"
"AND r13, r17 \n"
"CP r13, r23 \n"
"BREQ addBit3 \n"
"testBit3: \n"
"MOV r13, r22 \n" // again
"MOV r16, r23 \n"
"AND r13, r16 \n"
"CP r13, r23 \n"
"BREQ addBit2 \n"
"testBit2: \n"
"MOV r13, r22 \n" // again
"MOV r15, r23 \n"
"AND r13, r15 \n"
"CP r13, r23 \n"
"BREQ addBit1 \n"
"testBit1: \n"
"MOV r13, r22 \n" // again
"MOV r14, r23 \n"
"AND r13, r14 \n"
"CP r13, r23 \n"
"BREQ addBit0 \n"
"addBit7: \n"
"ADD r12, r21 \n" // add the bit to the result
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r21 \n" // subtract the power from the value
"RCALL testBit7 \n"
"addBit6: \n"
"ADD r12, r20 \n"
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r20 \n"
"RCALL testBit6 \n"
"addBit5: \n"
"ADD r12, r19 \n"
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r19 \n"
"RCALL testBit5 \n"
"addBit4: \n"
"ADD r12, r18 \n"
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r18 \n"
"RCALL testBit4 \n"
"addBit3: \n"
"ADD r12, r17 \n"
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r17 \n"
"RCALL testBit3 \n"
"addBit2: \n"
"ADD r12, r16 \n"
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r16 \n"
"RCALL testBit2 \n"
"addBit1: \n"
"ADD r12, r15 \n"
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r15 \n"
"RCALL testBit1 \n"
"addBit0: \n"
"ADD r12, r14 \n"
"MOV r22, r13 \n"
"MOV r23, r21 \n"
"SUB r13, r14 \n"
"RCALL saveResult \n"
"saveResult: \n"
"STS result, r12 \n"
);
Serial.print("The result is");
Serial.println(result);
}
void loop() {
}