Loops and conditional statements translated into assembly
In this blog, we will look at how conditions statements and looping constructs in higher level languages are translated into assembly. For the purpose of this example, I have used the below code.
In the above code sample, we have a while loop and multiple “if” conditions within this loop. Let’s try to disect the disassembly of this code and understand patterns. Here is the equivalent annotated assembly:
One pattern I found with microsoft compilers is that it tends to use the **cmp, jmp/j** instructions whenever there is an **"if(condition)"** and the **test, jmp/j** instructions whenever it evaluates conditions for a loop.
Here’s our example, our while(true) loop was translated to:
Each of our else if conditions were translated to:
This is a useful pattern to note, but other compilers may do things differently, so do not assume this will be true always.
Another pattern that you can observe is that whenever the code has an else if ladder structure, the will be a jump statement that takes you to the end of the ladder. This is a useful pattern to note.
That’s it for now, we’ll pick on another pattern in the next blog.