3

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:

  1. What does offset space mean?
  2. What does mov edx, offset space mean?
  3. I don't understand how the offset space is the source?
  4. How do you move register ecx into register eax?
  5. Why is the offset space the source and register edx the destination?

Thank you so much for all your help.

zx485
  • 28,498
  • 28
  • 50
  • 59
jackson blackson
  • 311
  • 1
  • 3
  • 13
  • "offset space" looks like its a variable/constants that's been defined earlier in the code. "mov edx, offset space " copies the value of the variable into the edx register – Joe Bourne Sep 15 '16 at 08:09
  • i apologize but i don't understand how that is a variable. I am very new to assembly. Thanks – jackson blackson Sep 15 '16 at 08:10
  • 3
    If you don't know how to move one register into another, you should really read a book or tutorial before asking questions. It's pretty much the first thing you learn. – interjay Sep 15 '16 at 08:24
  • 1
    @JoeBourne: `offset space` gets the address as an immediate, not the value as a load. – Peter Cordes Sep 15 '16 at 08:44

1 Answers1

1

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.

zx485
  • 28,498
  • 28
  • 50
  • 59
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
  • It's still just `DS`; not `EDS`. Segment registers in protected mode index into the Global Descriptor Table of segment *descriptors*, instead of being plain values that are shifted and added. I wouldn't even mention segmentation in an answer that asks how to move one register to another. In normal code (not 16-bit), OFFSET just gets the address. – Peter Cordes Sep 15 '16 at 08:46
  • @PeterCordes: Yesterday I did the "evil" deed of incorporating the rectifying aspects of your objection into the answer. I hope you will approve of that :-) – zx485 Sep 17 '16 at 10:09
  • @zx485: that's generally a good thing. I was lazy and didn't do it myself. (Partly because I wanted to suggest that Alex should remove most of the first paragraph to keep it simple (segmentation can be pretty much ignored outside of 16-bit mode), so that would not be a good edit.) – Peter Cordes Sep 17 '16 at 14:17