Tower of hanoi help
Page 1 of 1

Author:  box3211 [ Fri May 14, 2010 9:39 pm ]
Post subject:  Tower of hanoi help

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)
cout << "Moving " <<n<< " from " <<src<< " to " << dst << endl;
int main(){


Author:  profkelly [ Sat May 15, 2010 2:12 am ]
Post subject: 

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.

Author:  box3211 [ Sat May 15, 2010 5:35 am ]
Post subject: 

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.

        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
        MOVE.B  #9,D0
        TRAP    #15             Halt Simulator

        END     START

Author:  profkelly [ Sat May 15, 2010 1:15 pm ]
Post subject: 

The SOLVE subroutine must save and restore all registers that is uses. In this code SOLVE is changing several registers.

Author:  lee [ Sat May 15, 2010 2:18 pm ]
Post subject: 

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.


Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group