Portion of a for loop
; .......
jmp SHORT $LN3@clearArray ; enter the loop body
$LN2@clearArray: ; incrementation
mov eax, DWORD PTR _p$2534[ebp]
add eax, 4
mov DWORD PTR _p$2534[ebp], eax
$LN3@clearArray:
mov eax, DWORD PTR _p$2534[ebp] ; check conditions
cmp eax, DWORD PTR _length$[ebp]
jae SHORT $LN4@clearArray ; when loop condition fails...
; 6 : {
; 7 : *p = 0;
mov eax, DWORD PTR _p$2534[ebp] ; loop body
mov DWORD PTR [eax], 0
; 8 : }
jmp SHORT $LN2@clearArray
$LN4@clearArray:
; ........
When $LN2 is completed, how does it return back to $LN3?
This is generated by Visual Studio 2010 C++ assembler output.
Thank you.
A
jmpinstruction acts like agotostatement. It transfers control to a new location and execution continues at that point; you don’t return from ajmp(though you could issue anotherjmp).In this particular example, the code at $LN2 falls through to $LN3, so every time $LN2 executes, $LN3 will execute. The code at $LN3 is comparing the loop counter to see if it has reached the maximum value. The
cmp(“compare”) andjae(“jump above or equal”) instructions perform the comparison, and then exit the loop if the condition has been met (i.e., counter is equal to or greater than length). In other words, if your loop counter < length, thenjaedoes nothing and falls through to the next instruction. However, if counter >= length, then you jump to $LN4 which exits the loop.