Search code examples
assemblyx86nasmx86-16real-mode

Write to address without segment register


I know this code will actually write data to ds:[100h]

mov [100h], ax

But how can I write to linear address 100H directly without using any segment register for a segment base?


Solution

  • There is no way to get around segment register; every memory access is relative to some segment register. If you want to write to an absolute address, first load a segment register with an appropriate segment:

            xor cx, cx
            mov es, cx        ; es = 0000
            mov [es:100h], ax ; [0000:0100] = ax
    

    To load a linear address larger than 16 bit on an 8086 or 80286 system, try something like this:

    addr    dd 0x12345        ; the address we want to load from
            ...
            mov bl, [addr]    ; load low part
            xor bh,bh
            mov cx, [addr+1]  ; load high part
            shl cx, 4         ; adjust high part for segment selector
            mov es, cx        ; load segment register
            mov [es:bx], ax   ; store ax