如何在DOS下打印彩色字符串? [英] How to print colored string in DOS?

查看:259
本文介绍了如何在DOS下打印彩色字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同于通常的白色文本颜色的其他颜色打印以下一组数据块,这可以通过使用另一个DOS中断(dx:string-address; ah,08H; int 21h)来实现.

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$"

推荐答案

有几种方法可以实现目标在文本模式下:

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

  1. 按字符显示字符:这样,您可以为每个字符选择一种颜色.
  2. 访问屏幕内存:位于段0B800:0.
  3. 以相同的颜色显示整个字符串.

下一个代码使用第三个选项(最简单)完成任务.它是用EMU8086制成的:

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

注意,我删除了$符号(因为13h服务需要字符串长度而不是$).对于其他颜色,只需更改数据段中颜色"变量的值(181).

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.

选择颜色的公式如下:

文本背景* 16 +文本颜色

接下来是颜色

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

使用给定的公式,如果您想要红色背景和黄色文本,则需要使用颜色78:

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

4 * 16 + 14 = 78

现在,让我们在 GRAPHICS MODE (图形模式)中进行操作:

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

我的图形算法要求将char(13)用于换行符(而不是13,10),并且字符串以0结尾.

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

这篇关于如何在DOS下打印彩色字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆