i’m currently learning x86 assembly language and wondered what is the better way for implementing loops. One way would be to mov a value to ecx register and use the loop instruction and the other way would be using a jmp instruction and then comes the loop body and then a conditional jumping eventually to the beginning of the loop body. I guess the first one will has a better readability but other then that i don’t know why to use it.
i’m currently learning x86 assembly language and wondered what is the better way for
Share
When you mention jmp+body+test, I believe you are talking about the translation of a
whileloop in high-level languages. There is a reason for the second approach. Let’s take a look.Consider
The naive way is
This has N conditional jumps and N unconditional jumps.
The second way is:
Now we still do N conditional jumps but we only do ONE unconditional jump. A small savings but it just might matter, especially because it is in a loop.
Now you did mention the
loopinstruction which is essentiallyHow would you work that in? Probably like this:
This is a pretty little solution typical of CISC processors. Is it faster than the second way above? That depends a great deal on the architecture. I suggest you do some research on the performance of the
loopinstruction in the IA-32 and Intel 64 processor architectures, if you really want to know more.