Reputation: 311
The following is a piece of loop code I am trying analyze and understand how loops work:
;the ecx register is the loop counter
mov ecx,6
mov edx, offset space
myloop:
mov eax,ecx
dec eax
call writedec
call writestring
loop myloop
call crlf
mov ecx,6
mov edx, offset space
myloop2:
mov eax,6
sub eax, ecx
call writedec
call writestring
loop myloop2
My questions are:
offset space
mean? mov edx, offset space
mean?offset space
is the source?ecx
into register eax
?offset space
the source and register edx
the destination?Thank you so much for all your help.
Upvotes: 3
Views: 498
Reputation: 4764
The offset
operator returns the offset of a memory location relative to the beginning of the segment (DS in the case of MOV
) to which the location belongs (see here). space
is likely a variable that's defined somewhere earlier in the code, so that offset space
would return the number of bytes relative to the beginning of the segment (usually the DS
data segment) to that variable.
In other words, mov edx, offset space
will copy a pointer to the space
variable (of type DWORD
, 32-bit) to be placed in the edx
register. The reason it's placed in edx
must be found in the writedec
and writestring
routines.
As for the loop you're asking about, that's where the ecx
register comes in. The loop
instruction will cause the code pointer to move to its destination as long as ecx
is not zero, decreasing it by 1 immediately before checking it. The loop value is also used by your code somewhere, which is why it's copied to eax
so that other code will not change the value of ecx
and mess up your loop.
Upvotes: 1