打印彩色的字符串

时间:2019-04-20 22:23:35

标签: assembly x86-16 tasm

我正在尝试在汇编中打印彩色的字符串,但是它不起作用。

DATA:
MSG db 'hey$'

PROC PrintScore
push bp
mov bp,sp
push ax
push bx
push cx
push dx
; set cursor location to (dl,dh)
MOV AH,09H
MOV BH, 0
MOV BL,4 ;4=RED
mov cx, 10
int 10h
mov bh, 0
mov ah, 2h
mov dh, 0
mov dl, 0   ;that will put the cursor location up left
int 10h
;print the string
mov dx, offset Score 
mov ah, 9h
int 21h
pop dx
pop cx
pop bx
pop ax
pop bp  
ret
ENDP PrintScore

2 个答案:

答案 0 :(得分:4)

您使用错误的中断功能:

INT 10h, AH=09h一次打印几个相同的字符。该计数在CX寄存器中传递。要打印字符串,您必须像设置字符串中的字符一样频繁地调用它,并设置其他参数。字符必须在AL寄存器中传递,属性/颜色必须在BL寄存器中传递。 BH应该(可能)停留在0,而CX应该停留在1DLDH未被该功能使用,因此您可以删除相应的命令。

可以使用功能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:
  ; ...

用于打印字符串直到结束字符INT 21h的DOS函数$不在乎传递给BIOS函数INT 10h的属性,因此颜色将被忽略,并且您可以从;print the stringINT 21h删除相应的代码。

答案 1 :(得分:3)

zx485的答案已经解释了为什么您当前的程序不起作用。根据{{​​3}},您确实可以一次性打印整个彩色的字符串。 BIOS为我们提供了your commentES: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
相关问题