Reputation: 1
I've only seen BLT Loop which is a comparison between the index of a for loop and the loop upper bound. So if the index is less than the bound, then branch to Loop. But how would I manage to achieve the same functionality using BGT? Would I instead say BGT Exit? So if the index is greater than the bound, then exit the loop? Would that ensure the loop keeps looping as long as the index is NOT greater than the bound? Also, for Exit, do I need to write anything after it? Or is just "Exit: " fine?
Upvotes: 0
Views: 589
Reputation: 20027
The inverse of BLT is BGE, not BGT. Note the equality. BGE or BGT would make sense if the index was decreasing, as in for (int i = 7; i >=0; --i)
.
In incremented loop one can skip out of the loop with BGE/BGT, but then one would need an additional jump back to the loop beginning.
loop_begin:
add r0, r0, #1
cmp r0, r1
b.ge out
b loop_begin
out:
It's anyway typical that a regular loop for (int i = 0; i < N; i++)
is converted by a compiler to the decremented version (when the iteration count can be computed), producing
cmp r0, 0 // assume r0 == N
b.le out // need to skip the branch if N<=0
loop_begin:
subs r0, r0, #1 // decrement and compare to zero
b.gt loop_begin
out:
If possible, one should try to formulate the loops to the form do { } while (--i)
, executing at least one iteration.
loop_begin:
subs r0, r0, #1 // decrement and compare to zero
b.gt loop_begin
Upvotes: 1