Which segment register is used by default?

倖福魔咒の 提交于 2019-11-30 20:38:53

问题


I am studying computer architecture from the Intel Manual. The thing that I understand is that the instructions that we give are logical addresses which consist of a segment selector and an offset. It is basically CS register<<4 + offset. The Segment Selector maps to the GDT or LDT as given in the TI bit of the segment selector. GDT consists of Segment Descriptors which have BASE, LIMIT and RPL and the output is base address. This base address + offset provides the logical address.

What are the rules that decide which segment register (SS, DS, etc.) applies to different memory operations? e.g. what determines which segment is used for mov eax, [edi]?


回答1:


You can use a segment override prefix to select which segment will apply to the memory operand in an instruction.

Code fetch always uses CS.

The default most of the time is DS (e.g. for mov eax, [edi]). Some disassemblers make the segment explicit even when it's the default, so you see a lot of DS: cluttering up the disassembly output.

The movs instruction reads from [DS:ESI] and writes to [ES:EDI], making it easy to copy between segments without segment override prefixes. Some other string instructions also use ES.

Memory operands using esp or ebp as the base register default to SS, and so do the implicit accesses for stack instructions like push/pop/call/ret.

FS and GS are never the default, so they can be used for special purposes (like thread-local storage) in a flat memory model system like modern 32 and 64-bit OSes.

wikipedia explains the same thing here.


This is also documented officially in Intel's ISA manuals. e.g. in Volume 2 (the instruction-set ref), Table 2-1. 16-Bit Addressing Forms with the ModR/M Byte has a footnote saying:

The default segment register is SS for the effective addresses containing a BP index, DS for other effective addresses.

(note that SP isn't a valid base address for 16-bit addressing modes.)

There's no equivalent footnote for 32 or 64-bit addressing modes, so the details must be in another volume of the manual.

See also the x86 tag wiki for more links.



来源:https://stackoverflow.com/questions/38843403/which-segment-register-is-used-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!