简体   繁体   中英

How to convert a memory address to a code segment address?

How can I convert an address to a 16 bit code segment (CS register)? For example, if the .text segment starts at 00E51000, how is the CS register computed for that segment? This question is specific to 32bit x86 architecture.

In 32-bit mode, you can (and usually should) always use a CS value that references a GDT entry with base=0 / limit=-1. If you're running in user-space under a mainstream OS, your process will already start with that being the case. In fact, DS/ES/SS will be set up the same way, ie a flat memory model. (FS or GS might have a non-zero base for thread-local storage.)

Then you can reference memory in that section/segment with offset = 0x00E51000. eg mov eax, 0x00E51234 / jmp eax .

With DS/ES/SS also being 0 / -1, you have a flat memory model where mov eax, [0x00E51234] loads the same bytes that you would have jumped to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM