Introduction
Introduction Statistics Contact Development Disclaimer Help
global main
type main,"function"
p2align 4
This program will solve the Tower of Hanoi puzzle
using a recursive function.
ain:
ldr x1, =src
ldr x2, =dest
ldr x3, =aux
ldr x5, =no_discs
ldr x4,[x5]
sub sp,sp,#48 // Pushing four values and reserving a place for x30
stp x1,x2,[sp]
stp x3,x4,[sp,#16]
bl move_discs
add sp,sp,#48
bl exit
Function: move_discs
Arguments: Source,Destination,Auxiliary,Number of Discs
Purpose: To move discs recursively from the source to the
destination using the auxiliary rod.
ove_discs:
# Store x30, the return point
str x30,[sp,#32]
# Get the arguments from the stack
# into registers
ldp x1,x2,[sp]
ldp x3,x4,[sp,#16]
# First step: move all discs but the bottom one from the source to the
# auxiliary rod
# Only one disc? Print move
cmp x4,#1
b.eq print_move
# Otherwise, move the all discs except the bottom one to "aux"
sub sp,sp,#48
stp x1,x3,[sp]
sub x4,x4,#1
stp x2,x4,[sp,#16]
bl move_discs
# Upon return, restore the stack pointer
add sp,sp,#48
# Second step: move the bottom disc from the source to the destination
rint_move:
ldp x1,x2,[sp]
ldr x0,=format_string
bl printf
# Third step: move all discs but the bottom one from the Auxiliary rod to
# To the destination
#First, get the arguments from the stack to the registers
ldp x1,x2,[sp]
ldp x3,x4,[sp,#16]
# Only one disc? Return
cmp x4,#1
b.eq function_end
#Othersise, mova all remaining discs from "aux" to "dst"
sub sp,sp,#48
stp x3,x2,[sp]
sub x4,x4,#1
stp x1,x4,[sp,#16]
bl move_discs
add sp,sp,#48
unction_end:
ldr x30,[sp,#32]
ret
data
ormat_string:
.asciz "Moving from %s to %s\n"
rc:
.asciz "src"
est:
.asciz "dst"
ux:
.asciz "aux"
o_discs:
.xword 4
You are viewing proxied material from zaibatsu.circumlunar.space. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.