The base address for the video memory in MS-DOS is 0xB8000. I am trying to write to this address using debug.exe, but I am getting an error:

1165:0103 mov [b8000],ax
                   ^ Error
  • What you are really asking is "How do I write to video memory on an IBM PC/compatible system with MS-DOS or PC-DOS". MS-DOS did not have a set address for video memory. IBM set the standard - and most other vendors copied within a couple of years - but writing to video memory was initially just a way to speed up (by quite a bit) program output. The "correct" way in MS-DOS was to use a system call (can't remember which one at the moment) to write characters to the primary output, which almost always had the same RAM address. This complicated multitasking/windowing/etc. OS until the 80386 CPU. – manassehkatz Jun 7 '17 at 14:25
  • @manassehkatz I'm guessing you're thinking of INT 21H AH=09H with a $-terminated string. Of course this had the pretty big downside in some situations of scrolling the whole screen after you wrote to the last character cell, making full-screen TUIs impossible to implement (though you could kinda-sorta do it by very deliberately not writing to the last character cell). That said, I think we can ignore the whole MS-DOS/PC-DOS part entirely and just focus on the question of how to do inter-segment writes in 8088/8086 assembly... – a CVn Jun 9 '17 at 12:05

You need to use a segment and offset, 0xB8000 can’t be represented directly in 16 bits:

mov ax, b800
mov ds, ax
; set AX appropriately here, or write an immediate value
mov [0000], ax

You need to go through another register to write to DS because mov ds, imm16 isn’t valid.

Note that by writing a word (AX), you’re writing a character and its attribute in one operation (in text mode).

Somewhat amusingly, mov ax, imm16 is encoded as 0xB8, so mov ax, b800 is 0xB800B8...

Another approach, as pointed out by tofro, is to use ES and the index registers:

mov ax, b800
mov es, ax
xor di, di
; set AX appropriately here

This has the same effect, using stosw to write the contents of AX to the memory pointed at by ES:DI (and incrementing DI in the process, so the next stosw writes to the next character on screen). This is how one would commonly go about writing to the screen, in combination with rep and movsb or movsw to copy content from DS:SI to ES:DI. For example, here’s how to clear the screen in 80×25 mode:

mov ax, b800
mov es, ax
xor di, di
mov cx, 07d0
mov ax, 2000
rep stosw

This writes 0x0020 (black-on-black space) to the screen 2000 times (80×25, 0x07D0).

  • If you're going to preserve AX anyway, you might as well use PUSH and POP to set DS directly—e.g., push B800h+pop ds. – Cody Gray Jun 7 '17 at 6:36
  • 2
    @Cody there are multiple ways of going about things ;-). push imm16 requires a 286 and isn’t supported by debug, so you’d need to go through another register anyway (or manually write to the stack). I’m only preserving AX because I don’t know what comes before the OP’s mov here. – Stephen Kitt Jun 7 '17 at 6:39
  • Well of course you're right… I had totally forgotten about push imm not existing prior to the 286. It's just a compulsion where I look at code and see ways to optimize it. – Cody Gray Jun 7 '17 at 6:42
  • @Cody also, on slower PCs mov reg, reg is much, much faster than push reg and pop reg (2 cycles for mov, at least 19 for push / pop on 8086), and it takes the same amount of code space. – Stephen Kitt Jun 7 '17 at 6:43
  • 1
    I'd rather use ES to point to video memory, as you normally have "some more program" you want to use DS for, and SI or DI to index into it, also gives you the opportunity to use fast string instructions for larger memory moves. But this might be a matter of taste. – tofro Jun 7 '17 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.