In this blog, we will look at how a do while loop is translated into assembly language by the compiler. For the purpose of this example, I have used the below code.
In the above code sample, we have a do while loop and a set of instructions within it, that we are currently not concerned about. Let’s try to disect the disassembly of this code and understand patterns. Here is the equivalent annotated assembly:
The first few lines in the loop increment the index variable. This is a local variable, located on the stack at [ebp-8] in this frame.
As with the while loop, we see that microsoft compilers prefer using the **test, jmp/j** instructions whenever it evaluates conditions for a loop. In this case, the index variable is a local variable stored at [ebp-8]. This value is loaded into eax and then tested with 5. If the condition returns not equal, the loop continues.
This is a useful pattern to note, but other compilers may do things differently, so do not assume this will be true always. That’s it for now, we’ll pick on another pattern in the next blog.