Skip to content

Instantly share code, notes, and snippets.

@vsreekanti
Last active February 11, 2016 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vsreekanti/39c14c012b52e0c4a59e to your computer and use it in GitHub Desktop.
Save vsreekanti/39c14c012b52e0c4a59e to your computer and use it in GitHub Desktop.
Towers of Hanoi MIPS
# 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