Towers of Hanoi: Difference between revisions
Content added Content deleted
m (→pictorial moves: added verbiage to the REXX section header.) |
PatGarrett (talk | contribs) (→{{header|360 Assembly}}: Section added) |
||
Line 1: | Line 1: | ||
{{task|Classic CS problems and programs}}[[Category:Recursion]][[Category:Games]] |
{{task|Classic CS problems and programs}}[[Category:Recursion]][[Category:Games]] |
||
In this task, the goal is to solve the [[wp:Towers_of_Hanoi|Towers of Hanoi]] problem with recursion. |
In this task, the goal is to solve the [[wp:Towers_of_Hanoi|Towers of Hanoi]] problem with recursion. |
||
=={{header|360 Assembly}}== |
|||
{{trans|PLI}} |
|||
<lang 360asm> |
|||
* Towers of Hanoi 08/09/2015 |
|||
HANOITOW CSECT |
|||
USING HANOITOW,R12 r12 : base register |
|||
LR R12,R15 establish base register |
|||
ST R14,SAVE14 save r14 |
|||
BEGIN LH R2,=H'4' n <=== |
|||
L R3,=C'123 ' stating position |
|||
BAL R14,MOVE r1=move(m,n) |
|||
RETURN L R14,SAVE14 restore r14 |
|||
BR R14 return to caller |
|||
SAVE14 DS F static save r14 |
|||
PG DC CL44'xxxxxxxxxxxx Move disc from pole X to pole Y' |
|||
NN DC F'0' |
|||
POLEX DS F current poles |
|||
POLEN DS F new poles |
|||
* .... recursive subroutine move(n, poles) [r2,r3] |
|||
MOVE LR R10,R11 save stackptr (r11) in r10 temp |
|||
LA R1,STACKLEN amount of storage required |
|||
GETMAIN RU,LV=(R1) allocate storage for stack |
|||
USING STACKDS,R11 make storage addressable |
|||
LR R11,R1 establish stack addressability |
|||
ST R14,SAVE14M save previous r14 |
|||
ST R10,SAVE11M save previous r11 |
|||
LR R1,R5 restore saved argument r5 |
|||
BEGINM STM R2,R3,STACK push arguments to stack |
|||
ST R3,POLEX |
|||
CH R2,=H'1' if n<>1 |
|||
BNE RECURSE then goto recurse |
|||
L R1,NN |
|||
LA R1,1(R1) nn=nn+1 |
|||
ST R1,NN |
|||
XDECO R1,PG nn |
|||
MVC PG+33(1),POLEX+0 from |
|||
MVC PG+43(1),POLEX+1 to |
|||
XPRNT PG,44 print "move disk from to" |
|||
B RETURNM |
|||
RECURSE L R2,N n |
|||
BCTR R2,0 n=n-1 |
|||
MVC POLEN+0(1),POLEX+0 from |
|||
MVC POLEN+1(1),POLEX+2 via |
|||
MVC POLEN+2(1),POLEX+1 to |
|||
L R3,POLEN new poles |
|||
BAL R14,MOVE call move(n-1,from,via,to) |
|||
LA R2,1 n=1 |
|||
MVC POLEN+0(1),POLEX+0 from |
|||
MVC POLEN+1(1),POLEX+1 to |
|||
MVC POLEN+2(1),POLEX+2 via |
|||
L R3,POLEN new poles |
|||
BAL R14,MOVE call move(1,from,to,via) |
|||
L R2,N n |
|||
BCTR R2,0 n=n-1 |
|||
MVC POLEN+0(1),POLEX+2 via |
|||
MVC POLEN+1(1),POLEX+1 to |
|||
MVC POLEN+2(1),POLEX+0 from |
|||
L R3,POLEN new poles |
|||
BAL R14,MOVE call move(n-1,via,to,from) |
|||
RETURNM LM R2,R3,STACK pull arguments from stack |
|||
LR R1,R11 current stack |
|||
L R14,SAVE14M restore r14 |
|||
L R11,SAVE11M restore r11 |
|||
LA R0,STACKLEN amount of storage to free |
|||
FREEMAIN A=(R1),LV=(R0) free allocated storage |
|||
BR R14 return to caller |
|||
LTORG |
|||
DROP R12 base no longer needed |
|||
STACKDS DSECT dynamic area |
|||
SAVE14M DS F saved r14 |
|||
SAVE11M DS F saved r11 |
|||
STACK DS 0F stack |
|||
N DS F r2 n |
|||
POLES DS F r3 poles |
|||
STACKLEN EQU *-STACKDS |
|||
YREGS |
|||
END HANOITOW |
|||
</lang> |
|||
{{out}} |
|||
<pre style="height:20ex"> |
|||
1 Move disc from pole 1 to pole 3 |
|||
2 Move disc from pole 1 to pole 3 |
|||
3 Move disc from pole 2 to pole 3 |
|||
4 Move disc from pole 2 to pole 3 |
|||
5 Move disc from pole 1 to pole 2 |
|||
6 Move disc from pole 1 to pole 2 |
|||
7 Move disc from pole 3 to pole 2 |
|||
8 Move disc from pole 3 to pole 2 |
|||
9 Move disc from pole 1 to pole 2 |
|||
10 Move disc from pole 1 to pole 2 |
|||
11 Move disc from pole 3 to pole 2 |
|||
12 Move disc from pole 3 to pole 2 |
|||
13 Move disc from pole 1 to pole 3 |
|||
14 Move disc from pole 1 to pole 3 |
|||
15 Move disc from pole 2 to pole 3 |
|||
</pre> |
|||
=={{header|8th}}== |
=={{header|8th}}== |