-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHANOI.MIC
248 lines (203 loc) · 3.22 KB
/
HANOI.MIC
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
# Towers of Hanoi - recursive - works for up to 4 disks!
# (C) 2024 LambdaMikel
# MIC file can be read by PicoRAM 2090, Firmware v1.1
00:fo8
01:f0e
02:f1f
03:fff
04:f02
05:1ae # A -> source
06:1cd # C -> dest
07:1bc # B -> spare
08:f0e
09:11f # move "return marker 1" top top of return stack (F); marker 1 -> 0b
0a:c0d # goto hanoi(n, source, dest, spare); "recursive" call
# end
0b:f60 # stop
0c:f00
### hanoi(n, source, dest, spare) @0d
## f = 01?
0d:f0e # swap in value stack
0e:91f
0f:D15
## = 01; base case
10:f3d
11:ff0 # this doesn't destroy the value stack, as only 8-f has been swapped in!
12:f02
13:f0e # swap back normal registers
14:cb0 # goto jumpblock ; return
## > 01; rec call and add
# create 4 value stack entries
15:f0e # restore normal registers
16:b70 # shift value stack down
17:b70
18:b70
19:b70
# swap in upper bank to put values on value stack
# for first rec. call:
# FUNCTION MoveTower(disk, source, dest, spare):
# MoveTower(disk - 1, source, spare, dest) // Step 1 above
1a:f0e # swap in upper values bank
1b:0bf # copy "n" value
1c:71f # sub 1 from n -> n-1
1d:0ae # source -> source
1e:09c # dest -> spare
1f:08d # spare -> dest
20:f0e
21:b50 # shift return stack down
22:12f # -> move "return marker 2" top top of return stack (F); "marker 2 -> 24"
# parameters were prepared, do the call
23:c0d # goto sum(n); recursive call
24:b60 # shift return stack up
25:b90 # shift value stack up
26:b90
27:b90
28:b90
# output
# move disk from source to dest // Step 2 above
29:f0e
2a:f3d
2b:ff0
2c:f02
2d:f0e
#
# prepare 2nd recursive call
#
2e:b70 # shift value stack down
2f:b70
30:b70
31:b70
# swap in upper bank to put values on value stack
# for second rec. call:
# FUNCTION MoveTower(disk, source, dest, spare):
# MoveTower(disk - 1, spare, dest, source) // Step 3 above
32:f0e # swap in upper values bank
33:0bf # copy "n" value
34:71f # sub 1 from n -> n-1
35:08e # spare -> source
36:09d # dest -> dest
37:0ac # source -> spare
38:f0e
39:b50 # shift return stack down
3a:13f # -> move "return marker 3" top top of return stack (F); "marker 3 -> 3c"
# parameters were prepared, do the call
3b:c0d # goto sum(n); recursive call
3c:b60 # shift return stack up
3d:b90 # shift value stack up
3e:b90
3f:b90
40:b90
41:cb0 # goto jumpblock ; return
###
# return stack shift down:
50:098
51:0a9
52:0ba
53:0cb
54:0dc
55:0ed
56:0fe
57:f07
####
# return stack shift up:
60:0ef
61:0de
62:0cd
63:0bc
64:0ab
65:09a
66:089
67:f07
####
# value stack shift down:
70:f0d
71:f0e
72:010
73:021
74:032
75:043
76:054
77:065
78:076
79:087
7a:098
7b:0a9
7c:0ba
7d:0cb
7e:0dc
7f:0ed
80:0fe
81:f0e
82:f0d
83:f07 # ret
####
# value stack shift up:
90:f0d
91:f0e
92:0ef
93:0de
94:0cd
95:0bc
96:0ab
97:09a
98:089
99:078
9a:067
9b:056
9c:045
9d:034
9e:023
9f:012
a0:001
a1:f0e
a2:f0d
a3:f07 # ret
###
# jump block
b0:91f # marker 1 ?
b1:E0b # goto 0A
b2:92f # marker 2 ?
b3:E24 # goto 24
b4:93f # marker 3 ?
b5:E3c # goto 3c
b6:F00 # error
#---------------
#
#Test run with 3 disks:
#
#A B C
#-----
#1
#2
#3
#-----
#
#2
#3 1
#-----
#
#
#3 2 1
#-----
#
# 1
#3 2
#-----
#
# 1
# 2 3
#-----
#
#
#1 2 3
#-----
#
# 2
#1 3
#-----
# 1
# 2
# 3
#-----
#
#7 Steps