I know that this is a very basic program, but this assembly language is giving me a serious headache.
Basically, I'm trying to write the following java program in MIPS.
CODE
public class Hanoi {
public static void moveDisc(int numDiscs, int fromPeg, int toPeg, int auxPeg) {
if(numDiscs > 0) {
moveDisc(numDiscs-1, fromPeg, auxPeg, toPeg);
System.out.println("Move a disc from peg " + fromPeg + " to peg " + toPeg);
moveDisc(numDiscs-1, auxPeg, toPeg, fromPeg);
}
}
public static void main(String[] args) {
moveDisc(1,1,2,3);
}
}
And this is what I have so far.
CODE
# BJ Hazelwood -- 09/22/08
# hanoi.asm -- simulates the towers of hanoi
# global data segment declarations
.data
FROM: .asciiz "Move a disc from peg "
TO: .asciiz " to peg "
.text
main: #main
li $a0, 1 #set $a0 to 1
li $a1, 1 #set $a1 to 1
li $a2, 2 #set $a2 to 2
li $a3, 3 #set $a3 to 3
jal movedisc #call function
li $v0, 10
syscall
move: #recursive function to move discs
addi $sp, $sp, -12 #save register to stack
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $a0, 8($sp)
bgt $a0, $0, disc #if numDisc>0, go to disc
disc: #rest of move
move $s0, $a0
addi $a0, $a0, -1
jal move
echo:
It's not finished. I'm pretty sure I can get the echo part to print what the text and everything. What I am having trouble with is the recursive function. Something is just not clicking. Can anybody point me in the right direction from here, or atleast tell me if I've got anything that looks halfway correct so far. Thanks everyone and keep up the great work here.