如何用汇编语言打印彩色string?

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

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

 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 

在文本模式下有几种方法来实现你的目标:

  1. 按char显示char:这样你可以为每个字符选择一种颜色。
  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

现在,让我们在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结束。

 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