1

I need to write a program for writing ASCII symbols to the video memory.

.model tiny
.stack
.data
.code
main: 

mov ax, 0b800h
mov ds, ax

mov aL, 'x'
mov es, bx
mov es:[1], aL

mov ah, 4ch
int 21h

end main

This code is inert as of yet; it compiles, runs and then exits without printing anything. I think i'm missing a line.

user2241226
  • 11
  • 3
  • 9
  • possible duplicate of [Printing a string without OS](http://stackoverflow.com/questions/15462807/printing-a-string-without-os) – nrz Apr 07 '13 at 09:34
  • 1
    You set `ds` to `0b800h`, and `es` to undefined value or at least not `0b800h` (`bx`). And then you use `es` to address video memory, which obviously fails. – nrz Apr 07 '13 at 09:36
  • brilliant, that was stupid of me. however I am still unable to print a character in this manner, and the program behaves as before. – user2241226 Apr 07 '13 at 09:36
  • In assembly level (at least in x86) string is not a datatype, it's only an abstraction in the programmer's head. You can call the data "ASCII symbols" or "string", in assembly level it's all the same: some values stored in some memory addresses, that's all. – nrz Apr 07 '13 at 09:43
  • Good to know, but I still don't see what is missing here exactly. So far I've only been able to print strings consecutively I haven't been able to write to vid. memory. – user2241226 Apr 07 '13 at 09:48
  • 1
    You have invalid offset for the ASCII code (`es:[1]`) (odd offsets are for colors and attributes) and you don't define the the color (well, your color & attribute byte is `0x78`, that is `x`). See my answer http://stackoverflow.com/questions/15462807/printing-a-string-without-os/15463548#15463548 . – nrz Apr 07 '13 at 09:53
  • how do I define the color? – user2241226 Apr 07 '13 at 09:59
  • The top 4 bits specify the background color and the low 4 bits specify the text color (or the other way around). Try using 20h. AFAIR, that's black text on green. – Alexey Frunze Apr 07 '13 at 10:16
  • forgive my ignorance, could you show me in context? – user2241226 Apr 07 '13 at 10:21
  • 1
    @user - Each position on the screen is made up of two bytes, one holds the character code and the other holds the color. If you don't set both, you might end up with a black character on a black background. Pretty hard to read! – Bo Persson Apr 07 '13 at 10:54
  • what kind of color code goes into es[n] to produce a text and bg color? – user2241226 Apr 07 '13 at 11:25

1 Answers1

2

To be able to write to the video memory, set ES to 0b800h Specify the place (Offset), on the screen where you want to write. Set DI to (row*(max. rows (80))+col)*2 (ROW and COL are null-based). For example to write to the third char in the 10th line use "mov di, (2*80+9)*2" Specify the colors in AH. Set AH to (backgroundcolor*16+foregroundcolor) for example to write light gray (7) letters on a black (0) background use "mov ah, 7" Use the STOSW instruction instead of "mov es:[adress]", al. This way you can dynamically write to different places on the screen.

Van Uitkon
  • 356
  • 1
  • 6