我在汇编中做了一个iso文件,我想为文本添加颜色(在这种情况下:红色)。 有谁知道怎么做?
[BITS 16]
[ORG 0x7C00]
jmp main
main:
mov si, string ; si=string
call printstr
jmp $
printstr:
lodsb ; al=&si[0]
cmp al,0 ;FLAGS = 0
jnz print
ret
print:
mov ah,0Eh
int 10h
jmp printstr
string db "HELLO WORLD!",13,10,0
times 510 - ($-$$) db 0
dw 0xAA55
作为初步建议,请始终设置引导加载程序所依赖的段寄存器。在这里,因为lodsb
和[ORG 0x7C00]
,你必须设置DS=0
。
最好也确保方向标志DF处于已知状态。一个简单的cld
就足够了。
回答你的问题。您使用的BIOS.Teletype函数0Eh可以生成所需的红色,但仅限于图形视频模式。因此,下一个解决方
[BITS 16]
[ORG 7C00h]
jmp main
...
main:
xor ax, ax ; DS=0
mov ds, ax
cld ; DF=0 because our LODSB requires it
mov ax, 0012h ; Select 640x480 16-color graphics video mode
int 10h
mov si, string
mov bl, 4 ; Red
call printstr
jmp $
printstr:
mov bh, 0 ; DisplayPage
print:
lodsb
cmp al, 0
je done
mov ah, 0Eh ; BIOS.Teletype
int 10h
jmp print
done:
ret
string db "HELLO WORLD!",13,10,0
times 510 - ($-$$) db 0
dw 0AA55h
但是,如果您想使用文本视频模式,那么BIOS.WriteCharacterWithAttribute函数09h是正确的选择。
BL
现在拥有一个属性字节,它同时指定2种颜色(低半字节中的前景和高半字节中的背景),而额外的参数使用CX
寄存器。例:
[BITS 16]
[ORG 7C00h]
jmp main
...
main:
xor ax, ax ; DS=0
mov ds, ax
cld ; DF=0 because our LODSB requires it
mov ax, 0003h ; Select 80x25 16-color text video mode
int 10h
mov si, string
mov bl, 04h ; RedOnBlack
call printstr
jmp $
printstr:
mov cx, 1 ; RepetitionCount
mov bh, 0 ; DisplayPage
print:
lodsb
cmp al, 0
je done
cmp al, 32
jb skip
mov ah, 09h ; BIOS.WriteCharacterWithAttribute
int 10h
skip:
mov ah, 0Eh ; BIOS.Teletype
int 10h
jmp print
done:
ret
string db "HELLO WORLD!",13,10,0
times 510 - ($-$$) db 0
dw 0AA55h
您可以使用Int 10 / AH:0x09。它与Int 10 / AH:0x0E具有相同的参数,但BH是文本颜色。只需在代码中添加以下行。
mov ah, 09h
mov bh, 0xF0 ; add this line, this is the text color (white on black)
int 10h
由于BIOS功能,我使用的另一种替代方法在保护模式下不可用。使用0x0B800的内存。通用代码然后变成:
mov ebx, 0xb800 ; the address for video memeory
mov ah, 0xF0 ; the color byte is stored in the upper part of ax (ah).
printstr:
lodsb ; load char at si into al and increment si.
cmp al, 0
je .done
mov [ebx], ax ; move the character into video memeory.
add ebx, 2 ; move the video memeory pointer up two bytes.
jmp printstr
.done:
ret
用于调查此问题的其他资源可能包括: