-
Notifications
You must be signed in to change notification settings - Fork 0
/
hanoi.asm
202 lines (139 loc) · 3.54 KB
/
hanoi.asm
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
.data
input:.asciiz "Enter the number of the disks you want to use: "
print_step: .asciiz "\nStep "
print_move: .asciiz ":move disk "
print_from: .asciiz" from "
print_to: .asciiz " to "
# TODO END
.text
main:
li $t8,1
li $v0, 4 #ask for input
la $a0, input
syscall
jal readInt
add $a0, $v0, $zero #read input and save it in v0
addi $s1, $zero, 65 #A=source
addi $s2, $zero, 67 #C=aux
addi $s3, $zero, 66 #B=target
jal Hanoi #Start the process
li $v0, 10 # exit the program
syscall
Hanoi:
addi $t1, $zero, 1
bne $a0, $t1, Recursion #if n=1 then continue in this subroutine, if not go to recursion
#move disk 1 from source to target
move $t2,$a0 #Save n in t2 because we need a0 for syscall
li $v0,4
la $a0,print_step #Step
syscall
li $v0,1
move $a0,$t8 #number of steps
syscall
addi $t8,$t8,1
li $v0, 4 # print move
la $a0, print_move
syscall
li $v0,1
move $a0,$t2 #print disk label
syscall
li $v0, 4 # print from
la $a0, print_from
syscall
li $v0, 11
move $a0, $s1 #print source
syscall
li $v0, 4 # print to
la $a0, print_to
syscall
li $v0, 11
move $a0, $s2 #print target
syscall
move $a0,$t2
jr $ra
Recursion:
addi $sp, $sp, -20
#Save input into the stack
sw $ra, 16($sp)
sw $s3, 12($sp) #aux
sw $s2, 8($sp) #target
sw $s1, 4($sp) #source
sw $a0, 0($sp) #n
#MOVE(n - 1, source, auxiliary, target)
addi $t3, $s3, 0 #Swap
addi $s3, $s2, 0
addi $s2, $t3, 0
addi $a0, $a0, -1
jal Hanoi
lw $ra, 16($sp)
lw $s3, 12($sp) #load aux
lw $s2, 8($sp) #load target
lw $s1, 4($sp) #load source
lw $a0, 0($sp) #load n
#move disk n from source to target
move $t2,$a0
addi $t1, $zero, 1 #compare with the disk label
li $v0,4
la $a0,print_step
syscall
li $v0,1
move $a0,$t8
syscall
addi $t8,$t8,1
li $v0, 4 # print move
la $a0, print_move
syscall
li $v0,1
move $a0,$t2
syscall
li $v0, 4 # print move
la $a0, print_from
syscall
li $v0, 11 #Print Source
move $a0, $s1
syscall
li $v0, 4 # print to
la $a0, print_to
syscall
li $v0, 11 # print end_peg
move $a0, $s2
syscall
move $a0,$t2 # restore $a0
addi $t3, $s3, 0
addi $s3, $s1, 0 #Move source to aux
addi $s1, $t3, 0 #Move aux to source
addi $a0, $a0, -1
jal Hanoi #MOVE(n - 1, auxiliary, target, source)
lw $ra, 16($sp)
addi $sp, $sp, 20 #Restore stack
jr $ra
# TODO END
li $v0, 10 # exit the program
syscall
# TODO: your functions here
# TODO END
########### Helper functions for IO ###########
# read an integer
# int readInt()
readInt:
li $v0, 5
syscall
jr $ra
# print an integer
# printInt(int n)
printInt:
li $v0, 1
syscall
jr $ra
# print a character
# printChar(char c)
printChar:
li $v0, 11
syscall
jr $ra
# print a null-ended string
# printStr(char *s)
printStr:
li $v0, 4
syscall
jr $ra