TASM 1.4 - 显示特定颜色的字符串?

TASM 1.4 - Displaying a particular colored string?

我正在使用 TASM 1.4,我正在尝试制作一个将显示不同颜色句子的输出,所有这些都在同一个屏幕上。我可以制作显示彩色文本的东西,但所有单词的颜色都相同。如何制作显示不同颜色 strings/sentences 的东西? 例如,类似于:

Hi, what group would you like to join?
The Founders
The Vox Populi

"Hi, what group would you like to join?" 颜色为绿色。 "The Founders" 蓝色。 "The Vox Populi" 颜色为红色,也许我想要另一个闪烁的句子?我只能将它们全部显示为蓝色、红色或绿色。有什么帮助吗? 谢谢

我将提供一种使用 DOS 实现此目的的方法 int 21h

楼主说他们用的是TASM 1.4(TASM installer), which is a DOSBox based environment with TASM 3.0 and Turbo Debugger 3.1. In the days of DOS there was a video console driver that could be added to CONFIG.SYS with a command like DEVICE=C:\DOS\ANSI.SYS . This driver enhanced DOS int 21h output so you could specify attributes(background color, foreground color, blinking); clear the screen; reset back to default (usually black on white). DOSBox has a partial implementation of ANSI.SYS built in. The following code takes advantage of these ANSI codes:

.model small
.stack 100h

.data
a_cls        db 27, "[2J$"    ; Clear entire screen in currently set attributes
a_reset      db 27, "[0m$"    ; Reset attributes to standard (black on white)
a_blink      db 27, "[5m$"    ; Characters blink (blink doesn't work in all environments)
a_bright     db 27, "[1m$"    ; Bright colored characters
a_dim        db 27, "[2m$"    ; Dim colored characters
a_fg_black   db 27, "[30m$"   ; Foreground colors
a_fg_red     db 27, "[31m$"
a_fg_green   db 27, "[32m$"
a_fg_yellow  db 27, "[33m$"
a_fg_blue    db 27, "[34m$"
a_fg_magenta db 27, "[35m$"
a_fg_cyan    db 27, "[36m$"
a_fg_white   db 27, "[37m$"
a_bg_black   db 27, "[40m$"   ; Background colors
a_bg_red     db 27, "[41m$"
a_bg_green   db 27, "[42m$"
a_bg_yellow  db 27, "[43m$"
a_bg_blue    db 27, "[44m$"
a_bg_magenta db 27, "[45m$"
a_bg_cyan    db 27, "[46m$"
a_bg_white   db 27, "[47m$"

text1        db "Blinking Bright Yellow on Blue$"
text2        db " Back to Normal$" 
text3        db " Bright Yellow on Black$"
combined     db 27, "[0;5;2;31;42m Blinking Dim Red on Green$" 

.code
begin:  
    mov ax,@data
    mov ds,ax
    mov es,ax
    mov ah,09h
    lea dx,a_cls      ; clear screen
    int 21h           
    lea dx,a_bright   ; Bright colored characters
    int 21h
    lea dx,a_blink    ; make the characters blink (if supported)
    int 21h
    lea dx,a_fg_yellow; make the characters yellow
    int 21h
    lea dx,a_bg_blue  ; blue background for characters
    int 21h
    lea dx,text1      ; print text
    int 21h
    lea dx,a_reset    ; reset to defaults 
    int 21h
    lea dx,text2      ; print normal text 
    int 21h
    lea dx,a_bright   ; bright characters
    int 21h
    lea dx,a_fg_yellow; make the characters yellow
    int 21h
    lea dx,text3      ; print text
    int 21h
    lea dx,combined   ; print combined text
    int 21h
    lea dx,a_reset    ; reset to defaults before exiting back to DOS prompt
    int 21h
    .exit
end begin

在 DOS 中,ANSI 代码以 ESCAPE 字符(十进制 27)开头,后跟 [(左括号)。对于属性,可以使用 m 命令。属性可以用分号分隔。除了 m 之外还有一些其他命令,它们可以在 ANSI specs 中找到。如果您查看上面的代码,您会看到我在哪里分解了原始发布者可能感兴趣的许多命令的 ANSI 代码。

以下代码是组合多个属性(使用 ANSI)而不是单独执行它们的示例。

combined     db 27, "[0;5;2;31;42mBlinking Dim Red on Green$"

我们从 ESCAPE [ 开始。 0 将当前属性重置为默认值(大多数硬件是黑底白字)。其余属性由 SEMI COLONS 分隔。下一个属性5是闪烁的,下一个是2是暗色,31是前景色红色, 42 是绿色背景,后跟命令 mm 结束命令,后面的字符是要打印的文本。

根据前面代码中的转义序列,以下将满足发帖者提出的具体问题:

.model small
.stack 100h

.data
a_reset      db 27, "[0m$"    ; Reset attributes to standard (black on white)

             ; 13, 10 is carriage return line feed combination(CR/LF)
line1        db 27, "[1;32mHi, what group would you like to join?", 13, 10, "$" 
line2        db 27, "[1;34mThe Founders", 13, 10, "$"
line3        db 27, "[1;31mThe Vox Populi", 13, 10, "$"
line4        db 27, "[1;5;31;47mand maybe I'd want another sentence that blinks?$" 

.code
begin:  
    mov ax,@data
    mov ds,ax
    mov es,ax
    mov ah,09h
    lea dx,line1      ; print line 1 (bright green)
    int 21h
    lea dx,line2      ; print line 2 (bright blue)
    int 21h
    lea dx,line3      ; print line 3 (bright red)
    int 21h
    lea dx,line4      ; print line 4 (blinking bright red on white background)
    int 21h
    lea dx,a_reset    ; reset colors back to default before exiting
    int 21h
    .exit
end begin

已知闪烁在 DOSBox、VMWare、Bochs 中有效,但在 emu8086 和 Windows NT+ DOS 提示符中无效。

在此海报的前一组评论中,其他 我确实建议替代方法是编写一个过程或函数,它接受一个字符串并使用 DOS 中断 and/or BIOS 中断来更改逐个字符地显示属性并将它们显示到屏幕上(同时跟踪光标)。最有效的方法是直接将属性写入显存(即0xb000:0)。我使用的上面的 ANSI.SYS 方法是最容易上手的,并且应该在现代版本的 DOSBox 中默认工作。

您可以使用 INT 10h / AH=13h 来达到这个目的:

.MODEL small
.STACK 1000h

.DATA
    msg1 db "Hi, what group would you like to join?", 13, 10
    msg1_len = $ - msg1
    msg2 db "The Founders", 13, 10
    msg2_len = $ - msg2
    msg3 db "The Vox Populi", 13, 10
    msg3_len = $ - msg3
    msg4 db "Blinkers", 13, 10
    msg4_len = $ - msg4

.CODE

main PROC
    mov ax, @data
    mov ds, ax              ; Don't forget to initialize DS!
    mov es, ax              ; Segment needed for Int 10h/AH=13h

    lea bp, msg1            ; ES:BP = Far pointer to string
    mov cx, msg1_len        ; CX = Length of string
    mov bl, 2               ; green (
    call print

    lea bp, msg2            ; ES:BP = Far pointer to string
    mov cx, msg2_len        ; CX = Length of string
    mov bl, 3               ; blue (
    call print

    lea bp, msg3            ; ES:BP = Far pointer to string
    mov cx, msg3_len        ; CX = Length of string
    mov bl, 4               ; red (
    call print

    lea bp, msg4            ; ES:BP = Far pointer to string
    mov cx, msg4_len        ; CX = Length of string
    mov bl, 8Eh             ; blinking yellow (Bit7 set, works at least in DOSBox)
    call print

    mov ax, 4C00h           ; Return 0
    int 21h
main ENDP

print PROC                  ; Arguments:
                            ;   ES:BP   Pointer to string
                            ;   CX      Length of string
                            ;   BL      Attribute (color)

    ; http://www.ctyme.com/intr/rb-0088.htm
    push cx                 ; Save CX (needed for Int 10h/AH=13h below)
    mov ah, 03h             ; VIDEO - GET CURSOR POSITION AND SIZE
    xor bh, bh              ; Page 0
    int 10h                 ; Call Video-BIOS => DX is current cursor position
    pop cx                  ; Restore CX

    ; http://www.ctyme.com/intr/rb-0210.htm
    mov ah, 13h             ; VIDEO - WRITE STRING (AT and later,EGA)
    mov al, 1               ; Mode 1: move cursor
    xor bh, bh              ; Page 0
    int 10h                 ; Call Video-BIOS

    ret
print ENDP

END main