打印彩色字符串(组装 8086)

Print a colored string (Assembly 8086)

如何使汇编 8086 中的字符串着色?

您使用的中断函数有误:

INT 10h, AH=09h 一次打印几个相同的字符。计数在 CX 寄存器中传递。要打印一个字符串,您必须在设置了其他参数的情况下,根据字符串中字符的次数调用它。字符必须在 AL 寄存器中传递,attribute/color 必须在 BL 寄存器中传递。 BH 应该(可能)留在 0CX 应该留在 1。此函数不使用 DLDH,因此您可以删除相应的命令。

初始光标位置可以用函数INT 10h, AH=02h设置。确保 BH 值与上面代码中的值匹配 (0).

因此您的代码可能如下所示:

  ; ...
  ; Print character of message
  ; Make sure that your data segment DS is properly set
  MOV SI, offset Msg
  mov DI, 0      ; Initial column position 
lop:
  ; Set cursor position
  MOV AH, 02h
  MOV BH, 00h    ; Set page number
  MOV DX, DI     ; COLUMN number in low BYTE
  MOV DH, 0      ; ROW number in high BYTE
  INT 10h
  LODSB          ; load current character from DS:SI to AL and increment SI
  CMP AL, '$'    ; Is string-end reached?
  JE  fin        ; If yes, continue
  ; Print current char
  MOV AH,09H
  MOV BH, 0      ; Set page number
  MOV BL, 4      ; Color (RED)
  MOV CX, 1      ; Character count
  INT 10h
  INC DI         ; Increase column position
  jmp lop
fin:
  ; ...

DOS 函数 INT 21h 打印字符串直到结束字符 $ 不关心传递给 BIOS 函数 INT 10h 的属性,因此颜色被忽略它,你可以删除相应的代码从 ;print the stringINT 21h.

zx485 的回答中已经解释了为什么您当前的程序不起作用。根据 you can indeed print the whole colored string in one go. BIOS offers us video function 13hES:BP 中需要指向文本的完整指针,因此请确保 ES 段寄存器设置正确。

score db '12345'

...

PROC PrintScore
    pusha
    mov     bp, offset score ; ES:BP points at text
    mov     dx, 0000h        ; DH=Row 0, DL=Column 0
    mov     cx, 5            ; Length of the text
    mov     bx, 0004h        ; BH=Display page 0, BL=Attribute RedOnBlack
    mov     ax, 1300h        ; AH=Function number 13h, AL=WriteMode 0
    int     10h        
    popa
    ret
ENDP PrintScore