10

I'm just a beginner in assembly language and have probably silly question. What is difference between those two lines?

3e 8b 06 mov eax,DWORD PTR ds:[esi]
8b 06    mov eax,DWORD PTR [esi]

In my opinion it does the same, I also tried this program:

int main()
{
    __asm
    {
        mov esi, ebx
        mov eax,DWORD PTR [esi]
        mov ebx,DWORD PTR ds:[esi]
    }

    return 0;
}

And it confirmed my guess, so asking for you guys, if there is any difference. Why would we need two instructions that does the same but have different length opcodes.

ST3
  • 8,826
  • 3
  • 68
  • 92
  • 2
    `ds` is the default segment in that case, so indeed there is no difference except for the extra prefix byte. Also, under a typical OS, the segments `cs`, `ds`, `es` and `ss` all reference the same memory. – Jester May 05 '15 at 10:59
  • @Jester just wondering, what is not typical OS with segments in different memory. – ST3 May 05 '15 at 11:14
  • 1
    For example, a real-mode one such as DOS (if you can still call that an OS). Also, the segments `fs` and/or `gs` are used for special purposes such as thread-local storage, and those are normally placed at an offset. – Jester May 05 '15 at 11:18
  • The only mainstream OSes to use segments nontrivially were MS-DOS and Windows 3.x. But then, those were 16-bit, so 32-bit registers eax and such were not typically used. – Seva Alekseyev May 05 '15 at 17:55
  • Possible duplicate of [What does "DS:\[40207A\]" mean in assembly?](https://stackoverflow.com/questions/3819699/what-does-ds40207a-mean-in-assembly) – user202729 Aug 06 '18 at 09:38

2 Answers2

6

The 3E byte in the first instruction is a DS segment override prefix (see "2.1.1 Instruction Prefixes" in Intel's Software Developer's Manual).

Group 2
— Segment override prefixes:
• 3EH—DS segment override prefix (use with any branch instruction is reserved)

In this case it's redundant because ds is the default segment for most memory accesses. Also, if you have a flat 32-bit memory space you typically never explicitly specify the segment register since they're set up to point to the same memory anyway.

Michael
  • 57,169
  • 9
  • 80
  • 125
4

1) DS = DATA-segment

2) Two different opcodes because two different "adressing modes". 3) These are two different types of x86 er based so called "adressing modes". A very basic stuff in asm.

a) https://cs.nyu.edu/courses/fall10/V22.0201-002/addressing_modes.pdf b) http://www.ic.unicamp.br/~celio/mc404s2-03/addr_modes/intel_addr.html

icbytes
  • 1,831
  • 1
  • 17
  • 27