Loop Instruction
Last updated
Last updated
LOOP decrements ecx and checks if ecx is not zero, if that condition is met it jumps at specified label, otherwise falls through.
LOOPE decrements ecx and checks that ecx is not zero and ZF is set - if these conditions are met, it jumps at label, otherwise falls through.
LOOPNE is same as LOOPE except that it requires ZF to be not set (i.e be zero) to do the jump.
💡 LOOPE and LOOPNE are essentially LOOP instructions with one additional check (ZF).
Loop Analysis (loop.nasm)
We can see, on running the binary, “Hello world!” is printed 10 times.
Loading binary in GDB , setting intel format, checking functions, checking where loop instruction is, by seeing the disassembly of functions
Setting breakpoint at loop instruction.
Continue the execution and we can see Hello world being printed.
At brekpoint hit, we can check value of ecx, which is decrementing by 1, everytime. Acting as counter.
Loope analysis (loope.nasm)
Here, in loope instruction, we see only 1 time Hello world is printed.
Same thing as loop, loading in GDB etc etc.
As we read, that LOOPE instruction is basically LOOP instruction, with ZF (Zero flag) set. As we hit our breakpoint, we can see, ZF is not set. And when LOOPE instruction executes, it find no ZF and therefore, exits the loop as well as program.
Loopne analysis (loopne.nasm)
Here, on executing the binary, we can see Hello world being printed. Reason being, ZF is not set. And LOOPNE is LOOP instruction with NO ZF set.
Here, as expected, LOOPNE is executing.
Lets try to set ZF flag, and see if it will continue the loop or not.