4

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
muffel
  • 7,004
  • 8
  • 57
  • 98
  • Another recent duplicate: [How to get segment memory address, when i have physical address?](https://stackoverflow.com/q/62299565) – Peter Cordes Apr 06 '23 at 01:35
  • Another recent duplicate: [How to get segment memory address, when i have physical address?](https://stackoverflow.com/q/62299565). And see [Find Segment address from given physical and effective address](https://stackoverflow.com/q/56072884) re: splitting `0xACF04` into parts if you know you want `0x1234` as the offset part, instead of something simple with leading zeros in the offset. – Peter Cordes Apr 06 '23 at 01:52

1 Answers1

4

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Necrolis
  • 25,836
  • 3
  • 63
  • 101
  • thank you for that answer. Anyhow, I don't care if there are multiple possibilities of pairs Segment:Offset. Do you know any way to calculate just one of those without knowing as well the segment as the offset and without doing any kind of "bruteforce"? – muffel Feb 27 '12 at 12:15
  • @muffel: see my updated answer. this is one of the problems where you need more than one equation or some given side affect that allows you to solve for two unknowns. – Necrolis Feb 27 '12 at 12:27
  • 1
    Splitting a linear address into `seg:off` is precise but not *unique*. There are many possible solutions. @muffel: The most obvious choices for a canonical form are with a 4-bit offset, or with a 4-bit segment value, putting the other 16 bits of a 20-bit linear address into the other component of the seg:off. In a related question, [Find Segment address from given physical and effective address](//stackoverflow.com/q/56072884), the `off` part is known so we can simply solve for the segment. – Peter Cordes May 10 '19 at 15:43
  • "should" isn't a strong enough word for the linear address of a `seg:`. It automatically is aligned by definition of how it factors into the calculation. You can have a data or code "segment" in source code that isn't (16-byte) paragraph-aligned, but then the lowest `OFFSET` in that program segment isn't guaranteed to be 0. Don't confuse asm source sections that call themselves segments with actual segment register values. – Peter Cordes May 10 '19 at 15:45
  • This needed a major edit; unless I was totally misunderstanding something, it looked wrong. The "function" involving `& ~16` made no sense, that clears one bit. – Peter Cordes May 10 '19 at 15:57
  • @PeterCordes that appears to have been a typo on my part, should have been `~15`. good catch – Necrolis May 14 '19 at 12:56