【问题标题】:How to print colored string in DOS?如何在 DOS 中打印彩色字符串?
【发布时间】:2015-04-05 17:46:36
【问题描述】:

我想以不同于通常的白色文本颜色的不同颜色打印以下数据块集,这可以通过使用另一个 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$"

【问题讨论】:

  • 你用什么语言写的?有许多不同的汇编语言。
  • 你的汇编程序不做任何事情,它只是定义了一些字符串。你试过什么?
  • 如果我理解正确,您上一个问题的答案包括颜色。
  • 标签上写着“windows”,但美元符号让我觉得你可能想要 DOS。美元符号不起作用,但这个 BIOS 中断可能会做你想做的事:ctyme.com/intr/rb-0210.htm

标签: assembly dos masm x86-16 tasm


【解决方案1】:

有几种方法可以实现您的目标IN TEXT MODE

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

下一个代码使用第三个选项(最简单)完成这项工作。它是用 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)。

要为不同的字符串显示不同的颜色,请复制粘贴每个字符串的显示块。

让我们知道它是否对您有用。

选择颜色的公式如下:

文字背景 * 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:

4 * 16 + 14 = 78

现在,让我们在 图形模式 中进行:

.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 结尾。

【讨论】:

  • 如果我必须在视频模式下编写这些字符串怎么办?
  • 视频模式?你的意思是图形非文本模式?
  • 我的意思是写入显存
  • 哦,你是说图形模式。正在努力。 . .
【解决方案2】:

所有前景色和背景色组合的代码:

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

【讨论】:

    【解决方案3】:
    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
    

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多