EASy68K  
It is currently Sun Dec 16, 2018 11:02 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Tower of hanoi help
PostPosted: Fri May 14, 2010 9:39 pm 
Offline

Joined: Fri May 14, 2010 9:34 pm
Posts: 2
Hello,
I have to write a recursive program that solves the tower of hanoi puzzle. I understand how to make it work in C+ but I can't seem to wrap my head around how to get it working in 68000. If anyone could help that would be great, here is the C+ code.

#include <iostream>
using namespace std;
void solve(int n, int src, int aux, int dst)
{
if(n>0)
{
solve(n-1,src,dst,aux);
cout << "Moving " <<n<< " from " <<src<< " to " << dst << endl;
solve(n-1,aux,src,dst);
}
}
int main(){

solve(4,1,2,3);
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2010 2:12 am 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1087
To write a recursive subroutine in assembly language use the stack for all variable storage and be sure to save and restore all registers used by the subroutine. The MOVEM instruction is the best way to save and restore registers. If you need more than registers for variable storage then you will need to create a stack frame for local variables, refer to the LINK and UNLINK instructions.

_________________
Prof. Kelly


Last edited by profkelly on Sat May 15, 2010 12:46 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2010 5:35 am 
Offline

Joined: Fri May 14, 2010 9:34 pm
Posts: 2
I understand how to do that, I just don't get how you can call the subroutine again with src, aux, dst moved around like in the C+ call. This is what I have so far.

Code:
        ORG     $400

PROMPT1 DC.B    'Select number of discs(3-8): ',0
PROMPT2 DC.B    'Moving from peg ',0
PROMPT3 DC.B    ' to peg ',0

DST     EQU     3
AUX     EQU     2
SRC     EQU     1

START   ORG     $1000
       
ENTER   MOVE.B  #14,D0
        MOVE.L  #PROMPT1,A1     ;moves PROMPT1 into A1
        TRAP    #15             ;displays PROMPT1
        MOVE.B  #4, D0          ;gets user input
        TRAP    #15
        CMP     #3,D1
        BLT     ENTER
        CMP     #8,D1
        BGT     ENTER

        MOVE.W  #DST,-(SP)
        MOVE.W  #AUX,-(SP)
        MOVE.W  #SRC,-(SP)
        MOVE.W  D1,-(SP)
        BSR     SOLVE
       
        ORG     $1500
       
SOLVE   LINK    A0,#-2
        CMP.W   #0,10(SP)
        BEQ     EXITSUB
        MOVE.W  10(SP),-2(A0)
        SUBQ.W  #1,-2(A0)
        MOVE.W  -2(A0),-(SP)
        LEA     2(SP),SP
       
        BSR     SOLVE
       
        MOVE.B  #14,D0
        MOVE.L  #PROMPT2,A1     ;moves PROMPT2 into A1
        TRAP    #15             ;displays PROMPT2
       
        MOVE.B  #SRC,D1         ;moves src into a1
        MOVE.B  #3,D0           ;displays src
        TRAP    #15
       
        MOVE.B  #14,D0
        MOVE.L  #PROMPT3,A1     ;moves PROMPT3 into A1
        TRAP    #15             ;displays PROMPT2
       
        MOVE.B  #DST,D1         ;moves src into a1
        MOVE.B  #3,D0           ;displays src
        TRAP    #15
       
        MOVE.B  #0,D0           ;creates a new line
        MOVE.B  #0,D1
        TRAP    #15
       
        bsr   solve
       
EXITSUB UNLK    A0
        RTS
 
        MOVE.B  #9,D0
        TRAP    #15             Halt Simulator

        END     START


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2010 1:15 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1087
The SOLVE subroutine must save and restore all registers that is uses. In this code SOLVE is changing several registers.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2010 2:18 pm 
Offline

Joined: Mon Dec 27, 2004 11:40 pm
Posts: 318
Before you call solve you push values on the stack. After solve returns these values must be removed from the stack.

You also need to handle what happens after the initial call to solve returns.

Lee.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group