我正在尝试将Ackermann函数编写为汇编代码(在ARMV8中)。
({http://mathworld.wolfram.com/AckermannFunction.html https://en.wikipedia.org/wiki/Ackermann_function)
它需要递归。我的代码进行了无限递归。我找不到有关堆栈指针处理的问题。
X0和X1是输入寄存器。我要在X0中返回结果。
ack:
SUB SP, SP, #24
STUR X0,[SP,#16] // save input1 to stack pointer
STUR X1,[SP,#8] // save input2 to stack pointer
STUR X30,[SP,#0] // save return adress
SUBS XZR,X0,#0 // check input1 <= 0
B.GT cond1
ADD X0,X1,#1
B last
cond1:
SUBS XZR,X1,#0
B.GT gobig
SUB X0,X0,#1
MOVZ X1,0
ADD X1,X1,1
BL ack // call ack with input1-1 and 1
LDUR X30,[SP,#0] //restore return address
ADD SP,SP,#24
BR X30 // return register adress
gobig:
LDUR X0,[SP,#16]
LDUR X1,[SP,#8]
SUB X1,X1,#1
BL ack //call ack with input1 and input2-1 (call result r)
ADD X1,X0,#0
LDUR X0,[SP,#16]
SUB X0,X0,#1
BL ack //call ack wih input1-1 and r
last:
LDUR X30,[SP,#0]
ADD SP,SP,#24 //restore stack pointer
BR X30 //return adress register