4.6. Doing Work in LoopsΒΆ

To do work in a loop, we just have to place the work we want to do in the body of the basic loop structure. Below is an example of implementing ARM assembly roughly equivalent to this C++ code:

int sum = 0;
int max = 10;

//r2 will be our counter "variable" - however it will just live in a register and not be stored into memory
for(r2 = 1; r2 <= max; r2++)
   sum += r2;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   .data
   sum:   .word   0
   max:   .word   10

   .text
   _start:
   LDR   r1, =max    @r1 = &max
   LDR   r1, [r1]    @r1 = max

   MOV   r2, #0      @r2 is counting variable (i)
   MOV   r3, #0      @r3 is sum

   @Use test at end loop strategy
   B     loopTest    @Start loop by jumping to test
   loopStart:
   ADD   r3, r3, r2  @sum += r2
   ADD   r2, r2, #1  @r2++
   loopTest:
   CMP   r2, r1      @Compare counter in r2 to max in r1
   BLE   loopStart   @branch to start if i (r2) <= y (r1)

   LDR   r4, =sum    @r4 = &sum
   STR   r3, [r4]    @store r3 back to sum to record final answer
Try sample
You have attempted of activities on this page
Next Section - 4.7. ARM Trick - Conditional Execution