我正在学习如何进行汇编,但我找不到任何有关如何向不同消息行添加不同颜色的示例(例如 msg1 = 绿色背景,msg2 = 红色背景)。在每个 mov ax, seg msg 之前添加类似 mov bl,11101100b 的内容仅适用于第一行。
.model small
.stack
.data
name_msg db "Name:", 13, 10, "$"
age_msg db "Age:", 13, 10, "$"
birthdate_msg db "Birthdate:", 13, 10, "$"
course_msg db "Course:", 13, 10, "$"
.code
main proc
mov ax, @data
mov ds, ax
mov ah, 09h
lea dx, name_msg
int 21h
lea dx, age_msg
int 21h
lea dx, birthdate_msg
int 21h
lea dx, course_msg
int 21h
mov ah, 4Ch
int 21h
main endp
end main
在文本模式下,屏幕上的每个字段都由 2 个字节表示:第一个是 character,第二个是 attribute。
在内存中,字符位于偶数地址下,属性位于奇数地址下。
属性字节的格式如下所示:
7654 3210
bxxx ifff
b - blinking
x - color of the background
i - intensity
f - color of the character
因此,使用这种格式,我们只能使用8种颜色作为背景(0 -7)。
但是如果我们更改视频卡寄存器中的值,则所有16 种颜色 都将可用。
这是一个小例子。
.model small
.stack
.data
name_msg db "Name:", 13, 10, "$"
age_msg db "Age:", 13, 10, "$"
birthdate_msg db "Birthdate:", 13, 10, "$"
course_msg db "Course:", 13, 10, "$"
color db 00001111b ; foreground color will be light white
.code
main proc
mov ax, @data
mov ds, ax
mov ax, 0003h ; text mode, clear screen
int 10h
mov ax, 0b800h ; text mode address in system memory
mov es, ax
mov di, 1 ; odd addresses are character's attribute
cli ; turn off interupts
mov dx, 3dah ; this will allow
in al, dx ; to use blinking (default) bit
; bit 7 in character attribute byte as another
mov dx, 3c0h ; 8 colors of the background
mov al, 10h ; and we can change this in
out dx, al ; attribute mode control register
; if we set bit 3 to 0
mov al, 00h ;
out dx, al
mov cx, 80 ; number of columns, 0 - 79
mov dx, 25 ; number of rows, 0 - 24
mov al, color
change_bkg_color: ; this will change background color of each line before displaying any msgs
push cx
chars_attribute:
mov es:[di], al
add di, 2
dec cx
jnz chars_attribute
pop cx
add al, 16 ; only change bckg attribute, bits b xxx i fff
; | ||| | \|/
; /--------------------> blink \|/ | |
; | | | foreground
; | background |
; | |
; | intensity
; now this is additional 8 colors
dec dx
jnz change_bkg_color
sti ; turn on interupts
mov cx, 6 ; print 6 times name, age, birth, course
mov ah, 09h
print_msg:
lea dx, name_msg
int 21h
lea dx, age_msg
int 21h
lea dx, birthdate_msg
int 21h
lea dx, course_msg
int 21h
dec cx
jnz print_msg
mov ah, 08h ; wait for key press
int 21h
mov ah, 4Ch ; exit to dos
int 21h
main endp
end main