组装文字颜色 [英] Assembly text colors
问题描述
我正在用汇编程序编写一个iso文件,我想为文本添加颜色(在这种情况下为红色).
有人知道怎么做吗?
I'm doing an iso file in assembly and I want to add color to the text (in this case: red).
Does anyone know how to do it?
[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
就足够了.
As a preliminary advice, always setup the segment registers that your bootloader depends on. Here, because of lodsb
together with [ORG 0x7C00]
, you must set DS=0
.
Best also make sure the direction flag DF is in a known state. A simple cld
will be enough.
回答您的问题.您使用的BIOS.Teletype函数0Eh可以产生所需的红色,但只能在图形视频模式下. 因此,下一个解决方案将起作用:
To answer your question. The BIOS.Teletype function 0Eh that you use, can produce the desired red color but only while in a graphics video mode. Next solution will thus work:
[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是正确的选择.
If however you want to work with the text video mode then BIOS.WriteCharacterWithAttribute function 09h is the right choice.
- 请注意,因为参数不同.
BL
现在拥有同时指定两种颜色的属性字节(低半字节为前景,高半字节为背景),并且额外的参数使用了CX
寄存器. - 另一点是,此功能将为每个ASCII码显示彩色字形.因此,除非采取措施,否则将无法正确解释回车(13)和换行(10).
- 然而,最重要的事实是此功能不会使光标前进.幸运的是,有一个巧妙的把戏.只需连续调用函数09h和0Eh,然后......
- Pay attention because the parameters are different.
BL
now holds an attribute byte that specifies 2 colors at the same time (foreground in the low nibble and background in the high nibble) and an extra parameter uses theCX
register. - Another point is that this function will show a colored glyph for every ASCII code. So the carriage return (13) and linefeed (10) will not get interpreted correctly unless you take measures.
- The most important fact however is that this function does not advance the cursor. Luckily there's a neat trick. Just invoke both functions 09h and 0Eh in a row and voilà...
示例:
[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
这篇关于组装文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!