Calculate Segment:Offset from absolute address

muffel picture muffel · Feb 27, 2012 · Viewed 9.4k times · Source

I can calculate an address Segment:Offset as Segment * 0x10 + Offset. But how do I calculate the opposite?

E.g. how do I get from 0xF4170 to F400:0170 and from 0xACF04 to ABCD:1234?

Answer

Necrolis picture Necrolis · Feb 27, 2012

You would be required to have either the base or the offset to start with, along with the linear address, as multiple Segment:Offset pairs can map to the same linear address.

so if we have the segment 0xF400 and the linear address 0xF4170, we get the offset being 0xF4170 - (0xF400 << 4) which is 0x170.


Doing this with only knowing the linear address doesn't have a unique solution, so you have to choose a convention for splitting a 20-bit address into a 16-byte-aligned seg part and a byte offset. One possible function is this:

  • Segement = linear >> 4 (top 16 bits)
  • offset = linear & 0x0F (low 4 bits)

You might choose a canonical form with 12:8 bits, leaving room for future expansion with wider linear addresses.