Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

assembly - How to print colored string in DOS?

I want to print the below set of datablock(s) in a different color other than the usual white text color which can be achieved by using another DOS interrupt (dx:string-address; ah,08H; int 21h).

Jan             db  "         January$          "     
string          db  "Sun Mon Tue Wed Thu Fri Sat$"
string1         db  "                 1   2   3$"
string2         db  " 4   5   6   7   8   9  10$"
string3         db  "11  12  13  14  15  16  17$"
string4         db  "18  19  20  21  22  23  24$"
string5         db  "25  26  27  28  29  30  31$"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several ways to achieve your goal IN TEXT MODE :

  1. Display char by char : this way you can choose one color for every character.
  2. Access screen memory : at segment 0B800:0.
  3. Display the whole string with the same color.

Next code does the job with the third option (the easiest). It was made with EMU8086:

.stack 100h
.data

Jan   db  "         January           ",13,10 
      db  "Sun Mon Tue Wed Thu Fri Sat",13,10 
      db  "                 1   2   3 ",13,10 
      db  " 4   5   6   7   8   9  10 ",13,10 
      db  "11  12  13  14  15  16  17 ",13,10 
      db  "18  19  20  21  22  23  24 ",13,10 
      db  "25  26  27  28  29  30  31 "       
color db 181

.code          
;INITIALIZE DATA SEGMENT.
  mov  ax,@data
  mov  ds,ax 

;DISPLAY STRING WITH COLOR.
  mov  es,ax ;ES SEGMENT MUST POINT TO DATA SEGMENT.
  mov  ah,13h ;SERVICE TO DISPLAY STRING WITH COLOR.
  mov  bp,offset Jan ;STRING TO DISPLAY.
  mov  bh,0 ;PAGE (ALWAYS ZERO).
  mov  bl,color
  mov  cx,201 ;STRING LENGTH.
  mov  dl,0 ;X (SCREEN COORDINATE). 
  mov  dh,5 ;Y (SCREEN COORDINATE). 
  int  10h ;BIOS SCREEN SERVICES.  

;FINISH THE PROGRAM PROPERLY.
  mov  ax,4c00h
  int  21h

Notice I removed the $ signs (because 13h service requires string length, not $). For a different color just change the value (181) for the "color" variable in data segment.

To display diferent colors for diferent strings, copy-paste the display block for every string.

Let us know if it worked for you.

The formula to choose color goes like this:

text-background * 16 + text-color

Next are the colors :

Black         =  0
Blue          =  1
Green         =  2
Cyan          =  3
Red           =  4
Magenta       =  5
Brown         =  6
LightGray     =  7
DarkGray      =  8
LightBlue     =  9
LightGreen    = 10
LightCyan     = 11
LightRed      = 12
LightMagenta  = 13
Yellow        = 14
White         = 15

With the given formula, if you want red background with yellow text you need the color 78 :

4 * 16 + 14 = 78

Now, let's do it in GRAPHICS MODE :

.stack 100h
.data

Jan   db  "         January           ",13
      db  "Sun Mon Tue Wed Thu Fri Sat",13
      db  "                 1   2   3 ",13
      db  " 4   5   6   7   8   9  10 ",13
      db  "11  12  13  14  15  16  17 ",13
      db  "18  19  20  21  22  23  24 ",13
      db  "25  26  27  28  29  30  31 ",0
color db 181
x     db 0     ;SCREEN COORDINATE (COL).
y     db 0     ;SCREEN COORDINATE (ROW).

.code          
;INITIALIZE DATA SEGMENT.
  mov  ax,@data
  mov  ds,ax 

;SWITCH SCREEN TO GRAPHICS MODE.
  mov  ah,0
  mov  al,13h  ;320x240x256.
  int  10H

  mov  di, offset jan
while:      
  call gotoxy  ;SET CURSOR POSITION FOR CURRENT CHAR.
  mov  al, [ di ]  ;CHAR TO DISPLAY.
  cmp  al, 13    ;IF CHAR == 13
  je   linebreak ;THEN JUMP TO LINEBREAK.
  cmp  al, 0   ;IF CHAR == 0
  je   finish  ;THEN JUMP TO FINISH.
  call char_display  ;DISPLAY CHAR IN AL WITH "COLOR".
  inc  x  ;NEXT CHARACTER GOES TO THE RIGHT.
  jmp  next_char
linebreak:  
  inc  y  ;MOVE TO NEXT LINE.    
  mov  x, 0  ;X GOES TO THE LEFT.
next_char:
  inc  di  ;NEXT CHAR IN "JAN".
  jmp  while

finish:

;WAIT FOR ANY KEY.
  mov  ah,7
  int  21h

;FINISH THE PROGRAM PROPERLY.
  mov  ax,4c00h
  int  21h        

;-------------------------------------------------     
;DISPLAY ONE CHARACTER IN "AL" WITH "COLOR".

proc char_display
  mov  ah, 9
  mov  bh, 0
  mov  bl, color  ;ANY COLOR.
  mov  cx, 1  ;HOW MANY TIMES TO DISPLAY CHAR.
  int  10h
  ret
endp    

;-------------------------------------------------     
proc gotoxy
  mov dl, x
  mov dh, y
  mov ah, 2 ;SERVICE TO SET CURSOR POSITION.
  mov bh, 0 ;PAGE.
  int 10h   ;BIOS SCREEN SERVICES.  
  ret
endp

My graphics algorithm requires the use of char(13) for linebreaks (not 13,10) and the string to finish with 0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...