aarch64-pg
4.8 · Post-test Loops in Assembly

Post-test Loops in Assembly

note

The Assembly language discussed in this lesson refers specifically to ARMv8 AArch64 Assembly.

prerequisite

What if you wanted a loop to execute at least once before deciding whether it should continue? Unlike a pre-test loop, where the condition is checked before the loop body executes, a post-test loop executes the loop body first and checks the condition afterward. This means that even if the condition is initially false, the loop body will still execute once. This lesson covers the implementation of post-test loop in Assembly.

The Definition

A post-test loop is a type of loop in which the loop condition is evaluated after each iteration, including the first. In other words, the loop body is executed before the condition is evaluated to determine whether the loop should continue. Consequently, a post-test loop executes at least once, as the loop body is never skipped during the initial iteration, even if the condition is initially false.

The implementation of this type of loop in high-level programming languages such as C is commonly achieved using a do-while loop, in which the loop condition is evaluated after each iteration of the loop body. The following code demonstrates an example of a post-test loop implemented in C.

#include <stdio.h>int main() {    // do-while loop implementation    // Printing the numbers 1 to 5    printf("Post-test loop implementation\n");    int i = 1;    do {        printf("%d\n", i);        i++;    } while (i <= 5);    printf("Loop finished\n");    return 0;}

The above loop in C produces the output of printing the numbers 1 to 5 using the do-while loop, which is a common implementation of a post-test loop.

note

The term post-conditional loop refers to a post-test loop. These terminologies are generally used interchangeably because both refer to a loop where the condition is evaluated after the loop body executes.

The Logic Behind It

Based on the observations from the loops above, the underlying logic can be simplified as follows:

// PseudocodePRINT "Post-test loop implementation\n"DECLARE i AS INTEGERi = 1PRINT ii = i + 1IF i <= 5 THEN    JUMP BACK TO PRINT iPRINT "Loop Finished"

Correlating this logic with the pre-test loop logic, the key difference lies in when the condition is checked. In a post-test loop, the loop body is executed first, after which the condition is checked. This is exactly the behaviour we want to replicate in Assembly.

Recalling the Assembly implementation of a pre-test loop, the flow of execution branches to the loop condition before the loop body is executed. In a post-test loop, however, the flow of execution goes directly to the loop body, ensuring that it executes at least once before the loop condition is checked. Therefore, to implement a post-test loop in Assembly, the initial branching to the loop condition used in a pre-test loop can simply be removed, allowing execution to proceed directly to the loop body.

The Assembly Implementation

The above C code and pseudocode can be implemented in Assembly as follows:

// Pseudo-ops and directives...// string1 = "Post-test loop implementation\n"// string2 = "%d\n"// string3 = "Loop finished\n"main:    // Program prologue    ...    // Printing the "Post-test loop implementation" String    ldr x0, =string1    bl printf    // Initialising the Loop variable (i)    mov x20, 1    // Going straight down to the loop bodyloopbody:    ldr x0, =string2        // loading the format string    mov x1, x20             // loading the i to argument register    bl printf    add x20, x20, 1         // Incrementing i by one                            // x20 = x20 + 1loopcond:    cmp x20, 5              // Comparing i with 5    b.le loopbody           // if i is less than or equal to 5, branch to loop body                            // otherwise code below is executed    ldr x0, =string3    bl printf    // program epilogue.
Open in playground

The implementation of the post-test loop is very similar to the pre-test loop implementation discussed previously while still achieving the same behaviour as the C code. The same instructions are used to initialise the loop variable, execute the loop body, increment the variable, and evaluate the loop condition. The key difference lies in the initial flow of execution. Unlike the pre-test loop, execution does not branch to the loop condition before entering the loop. Instead, execution proceeds directly to the loop body, ensuring that it is executed at least once. After the first iteration, the loop condition is evaluated and the program branches back to the loop body if the condition is satisfied.

Putting it All Together

The following code demonstrates the complete process of implementing a post-test loop in Assembly, including the pseudo-ops, program prologue, and epilogue.

loading editor...
figure 4.8.1runnable -- step it and watch the registersOpen in playground