简体   繁体   English

如何在DOS中打印彩色字符串?

[英]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).我想用不同于通常的白色文本颜色的不同颜色打印下面的一组数据块,这可以通过使用另一个 DOS 中断 (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. Display char by char : this way you can choose one color for every character.按字符显示字符:这样您可以为每个字符选择一种颜色。
  2. Access screen memory : at segment 0B800:0.访问屏幕内存:在段 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:它是用 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 $).注意我删除了 $ 符号(因为 13h 服务需要字符串长度,而不是 $)。 For a different color just change the value (181) for the "color" variable in data segment.对于不同的颜色,只需更改数据段中“颜色”变量的值 (181)。

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文字背景 * 16 + 文字颜色

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 :使用给定的公式,如果您想要带有黄色文本的红色背景,则需要颜色 78 :

4 * 16 + 14 = 78 4 * 16 + 14 = 78

Now, let's do it in GRAPHICS MODE :现在,让我们在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.我的图形算法需要使用 char(13) 作为换行符(不是 13,10),并且字符串以 0 结束。

Code For All combinations of foreground and background colors:前景色和背景色的所有组合的代码:

Include Irvine32.inc

.data

str1 byte "F",0dh,0ah,0          ;character initialized (0dh,0ah for next line)

foreground Dword ?               ;variable declaration
background Dword ?
counter Dword ?

.code

main PROC

mov ecx,16                     ; initializing ecx with 16

l1:                             ;outer loop
mov counter,ecx
mov foreground,ecx
dec foreground
mov ecx,16



l2:                            ;inner loop

mov background , ecx
dec background

mov eax,background      ; Set EAX = background
shl eax,4               ; Shift left, equivalent to multiplying EAX by 16
add eax,foreground      ; Add foreground to EAX

call settextcolor       ;set foreground and background color


mov edx, offset str1    ; string is moved to edx for writing
call writestring


loop l2                  ;calling loop

mov ecx,counter

loop l1
    exit
main ENDP
END main
Include Irvine32.inc

.data

str1 BYTE "different color string",0dh,0ah,0        ;string initializing

counter dword ?                                     ;save loop value in counter

.code

main PROC

mov ecx,4                                          ;loop repeated 4 times

l1:
    mov counter,ecx                                ;counter save loop no

    mov eax,counter                          ;eax contain loop no 
                                                ;which is equal to counter
                                                                           ; color no

    call    SetTextColor                           ;Call color library

    mov edx,offset  str1                       ;string reference is moved to edx

    call    WriteString                            ;string is write from reference

loop l1                                            ; calling loop l1

    exit
main ENDP
END main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM