flat assembler
Message board for the users of flat assembler.

Index > DOS > Writing with colors

Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 24 Apr 2004, 22:11
How can now write some text on the screen with some color?
For example, how to write "Hello!" with blue background and white foreground color?
I've saw the int 10,09 function, how can I use it in very simple way?

Thanks a lot!!
Post 24 Apr 2004, 22:11
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 24 Apr 2004, 23:25
From the old kelvar example:
Code:
display_character:
; al = character
; bl = color
        xor     bh,bh
        mov     cx,1
        mov     ah,9
        int     10h
        mov     ah,0Eh
        int     10h
        ret
display_text:
; ds:si - text
; bl = color
        xor     bh,bh
        mov     cx,1
      .display:
        lodsb
        or      al,al
        jz      .end
        cmp     al,0Dh
        je      .type
        cmp     al,0Ah
        je      .type
        mov     ah,9
        int     10h
      .type:
        mov     ah,0Eh
        int     10h
        jmp     .display
      .end:
        ret    
Post 24 Apr 2004, 23:25
View user's profile Send private message Visit poster's website Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 24 Apr 2004, 23:56
It worked!! Thanks!! But how to know the color number to put on bl?
I mean how to how the number of red, blue, yellow...,red on blue, white on red....
Thanks
Post 24 Apr 2004, 23:56
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 25 Apr 2004, 07:55
there are 2 colors: foreground (in low order 4 bits) and background (in high order 4 bits). Each 4 bits escribe color like this:
0: red
1: green
2: blue
3: add light
so, for example 0001b is red, 0010b is green, 0011b is red + green etc. But i think it is easier to remember these 16 colors. and then just combine values in bl:
Code:
mov bl,foregound + (background shl 4)
    
Post 25 Apr 2004, 07:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 25 Apr 2004, 10:31
This can be also described this way: the four-bit values for colors are as following (the highest bit is the brightness bit, so to get the value of light red you add value of red to 8, getting 0Ch, etc.):

Normal:
0 black
1 blue
2 green
3 cyan
4 red
5 magenta
6 brown
7 light gray

Bright:
8 dark gray
9 light blue
A light green
B light cyan
C light red
D light magenta
E yellow
F white

The pair 6 and 0Eh of colors is a small exception, where bright color has a bit different hue that the normal one.

To get the attribute for a character (the byte that you pass to the function 9 of interrupt 10h, or write directly into screen memory), you put the color for a background into the high nibble, and color for the text into the low nibble of byte value.
For example 1Bh is the light cyan text on the blue background and 7Fh is white text on the light gray background.

But there are few more tricks about the attributes - first, the brightess bit in the nibble describing the color for the background may have different meaning (and it has by default) - it makes the character blink instead of making the backround color brigther. So depending of the current setting 9Fh may mean white text on the light blue background, or blinking white text on the normal blue background. You can toogle this setting with function 1003h of interrupt 10h. Pass BX=0 to that function to enable background brightening, or BX=1 to enable blinking.

And the last, a bit less well-known trick is that on the VGA you can enable highest bit of low nibble (the one describing text color) to choose one of the two character sets instead of brigthening the text color. This way you can use two different fonts at the same time in text mode. This one is a bit more tricky, you do it with 11xxh functions.
Post 25 Apr 2004, 10:31
View user's profile Send private message Visit poster's website Reply with quote
neonz



Joined: 02 Aug 2003
Posts: 62
Location: Latvia
neonz 25 Apr 2004, 11:43
Also there is alternative way, by using ANSI escape sequences (control codes) with DOS (Int21h) functions. But it requires that ANSI driver (ansi.sys on MS-DOS, nansi.sys on FreeDOS) is loaded.

http://www.delmar.edu/Courses/CIS415L/ANSIsys.htm - here is list of DOS ANSI sequences.
Post 25 Apr 2004, 11:43
View user's profile Send private message Visit poster's website Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 25 Apr 2004, 22:41
Thanks, it's working... but how can I clean the screen paint it with blue, for example (I don't want the black background)?

Thanks!!
Post 25 Apr 2004, 22:41
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 26 Apr 2004, 04:55
In DOS, you can write directly to video ram screen buffer.
It is *usually* located at segment B800.
Here follows 2D array that describes screen
array[1..y] of array [1..x] of word
where word (2 bytes) describes one character. first byte is character ASCII code and second is color. So clearing screen could be:
Code:
mov ax,((1 shl 4) shl 16) ;blue
mov cx,x*y
rep stosw
    

i just don't remember how to get "x" and "y", i think there is some BIOS procedure for it.
Post 26 Apr 2004, 04:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 26 Apr 2004, 19:03
I isn't working... and I don't want to write directly to video ram buffer... Can't use interrupt to do it?
Post 26 Apr 2004, 19:03
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 26 Apr 2004, 19:23
You can do it this way:
Code:
        mov     ax,0600h        ; scroll up window, AL=0 means: clear entire window
        mov     bh,4Ch          ; attribute used to fill the window
        mov     cx,0000h        ; row and column of upper left corner
        mov     dx,184Fh        ; row and column of lower right corner
        int     10h    


You can look up all the VGA functions in the Ralph Brown's Interrupt List.
Post 26 Apr 2004, 19:23
View user's profile Send private message Visit poster's website Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 26 Apr 2004, 19:39
Thanks Privalov!! It worked allright!!
Post 26 Apr 2004, 19:39
View user's profile Send private message Reply with quote
neonz



Joined: 02 Aug 2003
Posts: 62
Location: Latvia
neonz 01 May 2004, 20:12
vid wrote:
In DOS, you can write directly to video ram screen buffer.
It is *usually* located at segment B800.


Well, it's basically only in two places - segment B800 (for CGA and compatible adapters in text mode) or B000 (for MDA and compatible adapters). So, you simply need to detect MDA (for example, using BIOS equipment word), and use B000 instead of B800 if adapter is MDA.
Post 01 May 2004, 20:12
View user's profile Send private message Visit poster's website Reply with quote
Gambino



Joined: 20 Jul 2003
Posts: 44
Location: Romania
Gambino 07 Jul 2004, 18:44
Hey i have some code to help you out !
You can find it here http://board.flatassembler.net/topic.php?t=1806
Don't worry it's working very well but i want to improve it so i need some help, but that should allow you to print text anywhere on the screen, with any combination of backgroun/foreground color, and the strings can be long as u want ( almost Smile ) and don't have to be terminated by a character like '0' or '$'
Post 07 Jul 2004, 18:44
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 09 Sep 2004, 17:22
you might want to see this topic:

http://board.flatassembler.net/topic.php?t=2186

MATRIX
Post 09 Sep 2004, 17:22
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.