Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,578 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,945 people online right now. Registration is fast and FREE... Join Now!




Simple Hanoi in assembly (MIPS)

 
Reply to this topicStart new topic

Simple Hanoi in assembly (MIPS)

neurosurge
28 Sep, 2008 - 09:49 AM
Post #1

New D.I.C Head
*

Joined: 29 Nov, 2007
Posts: 4


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:21AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month