更改控制台文本的颜色

时间:2015-11-30 23:57:41

标签: assembly colors nasm dos 16-bit

我正在尝试创建一个绘图程序,在当前光标位置显示一个块字符(ASCII 219)。上,下,左,右键用于在屏幕上导航。假设F1,F2,F3和F4改变颜色。退出键退出程序。使用功能键时,颜色不会像我预期的那样改变。我的代码如下:

org 100h

kol db 12
wiersz db 10
kolor db 1111b

kursor:
mov ah,3
mov bh,1
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov bl,15
int 10h    

petla:
; Get keystroke
mov ah,0
int 16h
; AH = BIOS scan code
cmp ah,48h
je gora
cmp ah,50h
je dol
cmp ah,4Bh
je lewo
cmp ah,4Dh
je prawo
cmp ah,3Bh
je F1
cmp ah,3Ch
je F2
cmp ah,3Dh
je F3
cmp ah,3Eh
je F4
cmp ah,3Fh
je F5

cmp ah,1
jne petla  ; loop until Esc is pressed

mov ah,0x4c
int 0x21

gora:
sub byte [wiersz],1
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov bx,kolor
mov cx, 1
int 10h
jmp petla

dol:
add byte [wiersz],1
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov bx,kolor
mov cx, 1
int 10h
jmp petla

lewo:
sub byte [kol],1
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov bx,kolor
mov cx, 1
int 10h
jmp petla

prawo:
add byte [kol],1
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov bx,kolor
mov cx, 1
int 10h
jmp petla

F1:
mov byte [kolor], 0111b
int 21h
jmp petla
F2:
mov byte [kolor], 1001b
int 21h
jmp petla
F3:
mov byte [kolor], 0010b
int 21h
jmp petla

F4:
mov byte [kolor], 1011b
int 21h
jmp petla
F5:
mov byte [kolor], 1100b
int 21h
jmp petla

当程序运行时,光标会在屏幕上用箭头移动,但不会出现块字符,颜色也不会改变。任何人都可以解释为什么可能出现这个问题,以及我如何能够修复此代码?

1 个答案:

答案 0 :(得分:2)

程序的顶部如下所示:

org 100h

kol db 12
wiersz db 10
kolor db 1111b

kursor:

您实际上并未将此声明为数据,因此它实际上将作为代码执行。为了将数据与代码分开并首先显示代码,请将其修改为:

org 100h

; Declare .data section. Data section will be placed in COM program after code
section .data
kol db 12
wiersz db 10
kolor db 1111b

; .TEXT section is the code section
section .text
kursor:

在此代码中:

kursor:
mov ah,3        ; AH=3 read cursor? Maybe AH=2 for set cursor?
mov bh,1
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov bl,15      ; Not sure this does anything useful?
int 10h        ; Not sure this does anything useful?

我不确定你在最后两行代码中尝试做什么,所以我认为它们可以删除。之前的行似乎是尝试将光标设置到屏幕上的初始位置。 AH = 2用于设置光标,所以我认为代码应该是:

kursor:
mov ah,2             ; AH=2 set cursor?
mov bh,0             ; Should be page 0 (not 1)
mov dh,byte [wiersz] ; Row
mov dl,byte [kol]    ; Column
int 10h

你有4行代码(一个用于向上,向下,向左和向右),如下所示:

mov bx,kolor

这会将 kolor 变量的地址移动到 BX 中。您想要移动字节值AT kolor。所以这些线应该是这样的:

mov bl,[kolor]

这会将内存位置 kolor 的字节值移动到 BL 寄存器中。 NASM 假设您要移动单个字节,因为 BL 是一个8位寄存器。

通过上述更改,您可能一直在寻找的代码是:

org 100h

section .data
kol db 12
wiersz db 10
kolor db 1111b

section .text
kursor:
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h

petla:
; Get keystroke
mov ah,0
int 16h
; AH = BIOS scan code
cmp ah,48h
je gora
cmp ah,50h
je dol
cmp ah,4Bh
je lewo
cmp ah,4Dh
je prawo
cmp ah,3Bh
je F1
cmp ah,3Ch
je F2
cmp ah,3Dh
je F3
cmp ah,3Eh
je F4
cmp ah,3Fh
je F5

cmp ah,1
jne petla  ; loop until Esc is pressed

mov ah,0x4c
int 0x21

gora:
sub byte [wiersz],1
mov ah,2
mov bh,0

mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov cx, 1
int 10h
jmp petla

dol:
add byte [wiersz],1
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov bl,[kolor]
mov cx, 1
int 10h
jmp petla

lewo:
sub byte [kol],1
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov bl,[kolor]
mov cx, 1
int 10h
jmp petla

prawo:
add byte [kol],1
mov ah,2
mov bh,0
mov dh,byte [wiersz]
mov dl,byte [kol]
int 10h
mov ah,09h
mov al,219
mov bl,[kolor]
mov cx, 1
int 10h
jmp petla

F1:
mov byte [kolor], 0111b
int 21h
jmp petla
F2:
mov byte [kolor], 1001b
int 21h
jmp petla
F3:
mov byte [kolor], 0010b
int 21h
jmp petla

F4:
mov byte [kolor], 1011b
int 21h
jmp petla
F5:
mov byte [kolor], 1100b
int 21h
jmp petla