What's the default segment register when using

2019-06-05 01:37发布

问题:

As we all know, we can read the value of an address using square brackets in NASM, such as that:

mov esi,  [ebp +8]
mov edi, [var]   -->  (var is a variable)

Those values in square brackets represent the offset address, but when we read values using that format, we absolutely need a segment, whether we are in real or protected mode, so what's the default segment register that NASM uses? I know that Windows uses flat mode, but if I set different values to different segment register(cs, ss, ds, es, fs, gs), which segment register will NASM use? I guess it is ds, right? Is the segment register different when using different expressions in square brackets? Is the default segment register the same both in real and protected mode? Thanks.

回答1:

The default segment register is ds for all 'base registers' except

 mov esi, [ebp + 542] ; // uses ss:
 mov esi, [esp + 123] ; // uses ss: too
 mov eax, [eax + esp] ; // uses ds, because eax is the base 
                        // and esp is the scalable register (with scale==1)

 stosb   ;; uses es:

It's not the property of assembler, but of the processor. To override it, there's a one byte segment override prefix before the instruction.