Write a recursive ARM assembly program that calculates the n
Write a recursive ARM assembly program that calculates the number of stones in a ten-level ziggurat.
Hobbies, Again
I enjoy building stone ziggurats in my backyard. To build an N-level ziggurat, I first build an N ×N square of stones on the ground. Then I build an N 1×N 1 3 square of stones for the second level, then an N 2 × N 2 square of stones for the third level, and so forth, until I finally place a single stone on the top level. Write a recursive ARM assembly program that calculates the number of stones in a ten-level ziggurat.
Hint: The number of stones in a ten-level ziggurat is the number in a nine-level ziggurat plus 102 . In general, stones(N) = stones(N 1) + N 2 Oh yeah, how are you going to calculate N2 ?
Solution
program
DIM org 200
choice = 0
t = 1
sp = 13
link = 14
REM Range of legal values
min = 1
max = 4
WriteS = 1
NewLine = 3
FOR pass=0 TO 2 STEP 2
P%=org
[ opt pass
;Multiway branch in ARM assembler
;choice contains code, min..max of routine to call
;If out of range, error is called
;
STMFD (sp)!,{t,link}
SUBS choice, choice, #min ;Choice <min?
BCC error ;Yes, so error
CMP choice, #max-min ;Choice >max?
BHI error ;Yes, so error
ADR link, return ;Set-up return address
ADR t,table ;Get address of table base
ADD PC, t, choice, LSL #2 ;Jump to table+choice*4
;
.error
SWI WriteS
EQUS \"Range error\"
EQUB 0
ALIGN
;
.return
SWI NewLine
LDMFD (sp)!,{t,PC}
;
;
;Table of branches to routines
.table
B add
B delete
B amend
B list
;
.add
SWI WriteS
EQUS \"Add command\"
EQUB 0
ALIGN
MOV PC,link
;
.delete
SWI WriteS
EQUS \"Delete command\"
EQUB 0
ALIGN
MOV PC,link
;
.amend
SWI WriteS
EQUS \"Amend command\"
EQUB 0
ALIGN
MOV PC,link
;
.list
SWI WriteS
EQUS \"List command\"
EQUB 0
ALIGN
MOV PC,link
]
NEXT
REPEAT
INPUT \"Choice \",A%
CALLorg
UNTIL FALSE


