Edit the loop my_loop in the exercise below so that it starts by writing 10 to [ap], continues by writing the decreasing sequence and then returns. Don’t forget the ret instruction. Verify that your code works as expected by looking at the memory.
func main():
[ap] = 2; ap++
my_loop:
[ap] = [ap - 1] * [ap - 1]; ap++
[ap] = [ap - 1] + 1; ap++
jmp my_loop
end
Solution
The code would be:
func main():
[ap] = 10; ap++
my_loop:
[ap] = [ap - 1] - 1; ap++
jmp my_loop if [ap - 1] != 0
ret
end
And its output showing the regressive count:
$ cairo-compile exercise.cairo --output exercise.json
$ cairo-run --program=exercise.json --print_memory --relocate_prints
>>>
Addr Value
-----------
⋮
1 5189976364521848832
2 10
3 5198420613823168512
4 -1
5 145944781866893311
6 -2
7 2345108766317314046
8 21
9 21
10 10
11 9
12 8
13 7
14 6
15 5
16 4
17 3
18 2
19 1
20 0