what does the : operator do in assembly?
seen in code like: DS:DX
i haven't found any documentation for this operator.
(im using NASM)
That's actually just a register separator, not an operator. It means use the DX register as an offset from the DS segment register base.
What it actually gives you as an address depends on what mode you're running in (real or protected).
For example in real mode, the segment register is multiplied by sixteen and added to the offset register to give you a 20-bit physical address. If DS is 0x1234 and DX is 0x5678:
12340
+ 05678
-----
179B8
In protected mode, DS is acutally a segment selector which is used to lookup a base address for the memory in a table, then add that to the offset register to give a value.
That value is invariably a virtual address which is mapped to a physical address by the memory management unit (MMU), after ensuring that the relevant virtual memory is paged in from external storage.
The : operator differentiates a segment register/selector from a general-purpose register, and signifies which segment to access the register value in. So for instance,
DS:DX
means in 16-bit real mode to access the 16-bit value in the DX register offset from the DS segment value, which increments in 16-byte values (i.e., the difference between segment values 0x0000 and 0x0001 represent a 16-byte offset). So for instance, if the value in DS was 0xA000, and the value in DX was 0xFF, then you would be addressing the value at 0xA00FF.
In a 32-bit protected mode, the segment selector represents a 16-bit descriptor value where bits 3-15 are used as a look-up offset in the CPU's global descriptor table or local descriptor table which contains information on the 32-bit segment addresses range, as well as the ring-level access permissions for that segment (i.e., kernel-level, user-level, etc.). Bits 0-1 represent the request privledge level, which prevents a user-level process from loading a higher-permission segement value. Bit 2 is a flag, which when cleared means to look up the segment in the GDT, or if set means to offset into the LDT. For instance, a DS value of 0x0010 would mean to look up the third slot in the GDT with a request privledge level of 0 (i.e., you would need to be in kernel level mode to set this value). The value in DX would then be offset from the start of the address range set for that specific segment in the GDT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With