forked from gedobbles/sudoku-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.s
370 lines (345 loc) · 6.56 KB
/
main.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
.text
.global _start
_start:
ldr r1, =hello
ldr r2, =lh
bl print @ print welcome message
loop:
bl prints @ print sudoku field
read:
ldr r1, =input @ input = //all zeros
mov r0, #0
str r0, [r1], #4
str r0, [r1]
ldr r1, =input @ read(STDIN, *input, 8);
mov r2, #6 @6 chars
mov r7, #3 @read
svc 0
bl print @ print input
ldr r1, =input @ r8 = input[0];
ldrb r8, [r1], #2
cmp r8, #120 @'x' @ if(r8 == 'x')
beq exit @ exit();
magic: @ store input
ldrb r9, [r1], #2 @ r9 = input[2];
ldrb r10, [r1] @ r10 = input[4];
sub r8, #49 @ r8 -= 0x31; //ascii to int
sub r9, #49 @ r9 -= 0x31;
sub r10, #49 @ r10-= 0x31; ascii und eins weniger
mov r0, #1 @ r0 = 1 << r10
lsl r0, r10
orr r0, #0x4000 @set bit 14 to check
bl stnr
magic2: @ solve SUDOKU
mvn r8, #0 @ for(i = 0;i<9;i++){
m2x:
add r8, #1
cmp r8, #9
beq dat_out
mvn r9, #0 @ for(j = 0;j<9;j++){
m2y:
add r9, #1
cmp r9, #9
beq m2x
bl ldnr @ if(ldnr.toCheck){
tst r0, #0x4000
beq m2y
bl check @ check();
b magic2 @ goto magic 2;
@ }
@ }
@ }
dat_out: @ daten in Text umwandeln
ldr r1, =dat
ldr r2, =message
mov r5, #3 @3 Zeilen
dat_l1:
mov r4, #9 @9 Blöcke, dann eins weiter
dat_l2:
mov r3, #3 @3 Zeichen pro Block
dat_l3:
ldrh r0, [r1], #2
bl countbits @convert...
bne dat_d
sub r1, #2
ldrh r0, [r1]
orr r0, #0x8000 @set bit 15 finished
strh r0, [r1], #2
mov r0, r6
b dat_st
dat_d:
cmp r0, #0
mov r0, #88 @'X'
beq dat_st
mov r0, #95 @'_'
dat_st:
strb r0, [r2], #2 @eins weiter und noch eins für leerzeichen
subs r3, #1
bne dat_l3
add r2, #1 @Leerzeichen zw Blöcken
subs r4, #1
bne dat_l2
add r2, #1 @zusätzlicher Umbruch
subs r5, #1
bne dat_l1
b loop
exit:
mov r0, #0
return:
mov r7, #1
svc 0
prints: @no args
push {r11, lr}
ldr r1, =message
ldr r2, =len
b p1
print: @r1 str. addr, r2 len
push {r11, lr}
p1:
mov r0, #1
mov r7, #4
svc 0
pop {r11, pc}
countbits: @ r0 result in r0, eq flag falls eindeutig und char in r6
push {r1, r2, r3, r11, lr}
mov r1, r0
mov r0, #0
mov r2, #9
mov r3, #0
mov r6, #49
c1:
push {r3}
ands r3, r1, #1
pop {r3}
beq c2
add r0, #1
add r3, #1
c2:
cmp r3, #0
bne c3
add r6, #1
c3:
lsr r1, #1
subs r2, #1
bne c1
cmp r0, #1 @set eq flag falls eindeutig
pop {r1, r2, r3, r11, pc}
stnr: @r0 dat, r8 x coord, r9 y coord
push {r1, r8, r11, lr}
ldr r1, =dat
add r1, r9, LSL#1 @ dat[r9 * 18 + (r8)<<1] = r0;
add r1, r9, LSL#4
lsl r8, #1 @...
strh r0, [r1, r8] @x direction
pop {r1, r8, r11, pc}
ldnr: @r8 x coord, r9 y coord, r0: erg
push {r1, r8, r11, lr}
ldr r1, =dat
add r1, r9, LSL#1 @ dat[r9 * 18 + (r8)<<1] = r0;
add r1, r9, LSL#4
lsl r8, #1 @...
ldrh r0, [r1, r8] @x direction
pop {r1, r8, r11, pc}
check:
push {r11, lr}
bl ldnr
eor r0, #0x4000 @clear bit 14 to check
push {r0}
bl countbits @falls finished
pop {r0}
bne cs
bl influence
bl heur
cs:
bl stnr
pop {r11, pc}
influence: @r8 x coord, r9 y coord
push {r0, r5, r6, r7, r8, r9, r11, lr}
@reg usage: r0: ldnr()/stnr(), r5: k, r6/7: i/j
bl ldnr
mvn r6, #0
ix:
add r6, #1
cmp r6, #9
beq ixf
cmp r6, r8
beq ix
push {r8}
mov r8, r6
bl inr
pop {r8}
b ix
ixf:
mvn r7, #0
iy:
add r7, #1
cmp r7, #9
beq iyf
cmp r7, r9
beq iy
push {r9}
mov r9, r7
bl inr
pop {r9}
b iy
iyf:
mov r6, #0
mov r7, #0
ibxc:
cmp r8, #3 @ carry flag is cleared on borrow
blo ibyc
add r6, #3
sub r8, #3
b ibxc
ibyc:
cmp r9, #3
blo ibcf
add r7, #3
sub r9, #3
b ibyc
ibcf:
mvn r5, #0
ibl:
add r5, #1
cmp r5, #3
beq ibf
mov r8, r6
add r9, r7, r5
bl inr
add r8, #1
bl inr
add r8, #1
bl inr
b ibl
ibf:
pop {r0, r5, r6, r7, r8, r9, r11, pc}
inr: @r0: orignr, r8 x coord, r9 y coord
push {r0, r6, r7, r11, lr}
@reg usage: r7 orig nr, r6: 0x1ff
mov r7, r0
bl ldnr
tst r0, #0x8000
bne inrf
mov r6, #511
and r7, r6
bic r0, r0, r7
push {r0}
bl countbits
pop {r0}
ite eq
orreq r0, #0xC000 @this was the bug
orrne r0, #0x4000 @set bit 14 to check
bl stnr
inrf:
pop {r0, r6, r7, r11, pc}
heur:
push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r11, lr}
@reg usage: r0: stnr()/ldnr(), r1: 0x8000, r2: a, r3: amask, r4: t, r5: o, r6/7: i/j, r8/9: x, y
mov r1, #0x8000
reheur:
mvn r2, #0
hal: @for every number
add r2, #1
cmp r2, #9
beq hf
mov r3, #1
lsl r3, r2 @amask = 1<<a;
mvn r7, #0
hjl1: @for every row
add r7, #1
cmp r7, #9
beq hrf
mov r5, #0 @o = 0;
mvn r6, #0
hil1:
add r6, #1
cmp r6, #9
beq hif1
push {r8, r9}
mov r8, r6
mov r9, r7
bl ldnr
pop {r8 ,r9}
mov r4, r0 @t = ldnr(j, i);
tst r4, r3 @if(t & amask)
beq hil1
tst r4, r1 @if(t & 0x8000)
bne already_there_r @goto already_there_r
add r5, #1 @o++;
mov r8, r6 @ x = j;
mov r9, r7 @ y = i;
b hil1
hif1:
cmp r5, #1 @if(o == 1)
bne already_there_r
mov r0, r3
orr r0, r1
bl stnr
bl influence
b reheur
already_there_r:
b hjl1
hrf:
mvn r7, #0
hjl2: @for every column
add r7, #1
cmp r7, #9
beq hcf
mov r5, #0 @o = 0;
mvn r6, #0
hil2:
add r6, #1
cmp r6, #9
beq hif2
push {r8, r9}
mov r8, r7
mov r9, r6
bl ldnr
pop {r8, r9}
mov r4, r0 @t = ldnr(j, i);
tst r4, r3 @if(t & amask)
beq hil2
tst r4, r1 @if(t & 0x8000)
bne already_there_c @goto already_there_r
add r5, #1 @o++;
mov r8, r7 @ x = j;
mov r9, r6 @ y = i;
b hil2
hif2:
cmp r5, #1 @if(o == 1)
bne already_there_c
mov r0, r3
orr r0, r1
bl stnr
bl influence
b reheur
already_there_c:
b hjl2
hcf:
b hal
hf:
pop {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r11, pc}
@-----------------------------------
.data
message:
.ascii "_ _ _ _ _ _ _ _ _ \n_ _ _ _ _ _ _ _ _ \n_ _ _ _ _ _ _ _ _ \n\n"
.ascii "_ _ _ _ _ _ _ _ _ \n_ _ _ _ _ _ _ _ _ \n_ _ _ _ _ _ _ _ _ \n\n"
.asciz "_ _ _ _ _ _ _ _ _ \n_ _ _ _ _ _ _ _ _ \n_ _ _ _ _ _ _ _ _ \n\n"
len = .-message
hello:
.ascii "SUDOKU-solver by gedobbles \n"
.ascii "Format: x y n where x,y are coordinates (1-9) and n is the number.\n"
.asciz "Enter x to exit.\n\n"
lh = .-hello
input:
.byte 1,2,3,4,5,6,7,8,0
dat: @ bit31:finished,bit30:to check,bit 8-0:nr possible
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1
.byte 255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1,255,1