-
-
Save tremblerz/9b757f08bd7c60c94f8a to your computer and use it in GitHub Desktop.
Towers of Hanoi MIPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# print_msg function takes three arguments and is equivalent to | |
# snprintf(“Move disk %d from %d to %d.”, $a0, $a1, $a2); | |
hanoi: | |
slti $t0, $a0, 2 | |
bne $t0, $0, caseOne | |
addiu $sp, $sp, -20 | |
sw $a0, 0($sp) | |
sw $a1, 4($sp) | |
sw $a2, 8($sp) | |
sw $a3, 12($sp) | |
sw $ra, 16($sp) | |
# switch end and other | |
addiu $a0, $a0, -1 | |
addiu $t0, $a2, 0 | |
addiu $a2, $a3, 0 | |
addiu $a3, $t0, 0 | |
addiu $a0, $a0, -1 | |
jal hanoi | |
lw $a0, 0($sp) | |
lw $a1, 4($sp) | |
lw $a2, 8($sp) | |
jal print_msg | |
lw $a0, 0($sp) | |
lw $a1, 12($sp) # load other into start | |
lw $a2, 4($sp) # load start into end | |
lw $a3, 8($sp) # load end into other | |
addiu $a0, $a0, -1 | |
jal hanoi | |
lw $ra, 16($sp) | |
addiu $sp, $sp, 20 | |
jr $ra | |
caseOne: | |
addiu $sp, $sp, -4 | |
sw $ra, 0($sp) | |
jal print_msg | |
lw $ra, 0($sp) | |
addiu $sp, $sp, 4 | |
jr $ra |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment