如何用汇编语言打印彩色字符串?

时间:2015-04-05 17:46:37

标签: windows assembly masm tasm

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

3 个答案:

答案 0 :(得分:1)

有多种方法可以实现目标在文本模式

  1. 按字符显示字符:这样您就可以为每个字符选择一种颜色。
  2. 访问屏幕内存:在段0B800:0。
  3. 显示相同颜色的整个字符串。
  4. 下一个代码使用第三个选项(最简单的)完成工作。它是用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
    

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

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

    让我们知道它是否适合您。

    选择颜色的公式如下:

    text-background * 16 + text-color

    接下来是颜色:

    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

    现在,让我们在 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表示。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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