汇编文字颜色

问题描述 投票:4回答:2

我在汇编中做了一个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
assembly colors x86-16 bootloader
2个回答
4
投票

作为初步建议,请始终设置引导加载程序所依赖的段寄存器。在这里,因为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寄存器。
  • 另一点是,此函数将为每个ASCII代码显示一个彩色标志符号。因此,除非您采取措施,否则回车(13)和换行(10)将无法正确解释。
  • 然而,最重要的事实是该功能不会使光标前进。幸运的是,有一个巧妙的伎俩。只需连续调用09h和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, 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

2
投票

您可以使用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

用于调查此问题的其他资源可能包括:

© www.soinside.com 2019 - 2024. All rights reserved.