Does anyone know how to change the colour of strings using x86 NASM Assembler? I've found tutorials like this:

mov  ah,  09h 
mov  al,  97 
mov  bx,  100b
mov  cx,  01h 
int  10h

Which prints the letter "A" once in the colour you choose. This is a handy tutorial, but I would like to be able to print the entire screen text (i.e more than one string) in the same colour. Anyone have any ideas?

Dani AI

Generated

NotNull is right that in text mode each screen cell is a char+attribute pair. If your goal is to write whole strings in one color (without looping per character), use BIOS INT 10h AH=13h "Write String". Set AL=01h to take the attribute from BL, point ES:BP at your buffer, and let BIOS handle wrapping/scrolling.

; Write a string in one color at row DH, col DL
; BL = attribute (bg<<4 | fg), BH = page (usually 0)
push cs
pop  es
mov  bp, msg
mov  cx, msg_end - msg
mov  bh, 0
mov  bl, 0x1F          ; white on blue
mov  dh, 5
mov  dl, 10
mov  ax, 0x1301        ; AH=13h, AL=01h: use BL attribute, move cursor
int  0x10
msg      db 'Hello, world!',0
msg_end:

Reference: INT 10h AH=13h - Write String.

If you want to recolor what is already on screen without changing characters, walk the text buffer and overwrite only the attribute bytes. This touches every odd byte at B800:0000 + 1, +3, ... for 80*25 cells.

; Recolor entire active page, preserving characters
; BL = new attribute
push ax es di cx
mov  ax, 0xB800
mov  es, ax
mov  di, 1             ; start at first attribute byte
mov  cx, 2000          ; 80*25 cells
.recolor:
mov  [es:di], bl
add  di, 2
loop .recolor
pop  cx di es ax
ret

Background: VGA text memory is at 0xB8000; each cell is [char][attr], with attr bits 7..4 = background (bit 7 is blink or background intensity), 3..0 = foreground. OSDev: Printing to Screen.

Tip: if you want bright background colors instead of blink, disable blink once at startup:

mov ax, 0x1003         ; Toggle intensity/blink
xor bx, bx             ; BL=0 -> enable background intensity, disable blink
int 0x10

See INT 10h AX=1003h - Toggle intensity/blinking.

Color text-mode memory lies at segment 0xb800 and is 4000 bytes
in length 80*25*2 cuz each visible character on the screen
is accompanied by a text attribute byte.
00000000 Text Attribute Byte
XXBGxxFG
The low-nibble defines the foreground and the high-nibble
the background, and there is a way to disable the blink
bit.

Function 9 - Output Char And Attribute At Hardware Cursor
CX-Repeat
BH-Display Page
BL-Attribute
AL-Character
AH-9
INT 0x10

To clear the screen to one f/b color try this code:

clrscr:
mov dx, 0 ; Set cursor to top left-most corner of screen
mov bh, 0
mov ah, 0x2
int 0x10
mov cx, 2000 ; print 2000 chars
mov bh, 0
mov bl, 0x21 ; green bg/blue fg
mov al, 0x20 ; blank char
mov ah, 0x9
int 0x10
ret

This code writes a blue 'A' char to 0,0:

mov cx, 0xb800
mov es, cx
mov ax, 0x141
mov [es:0], ax
waitkey:
mov ah, 0x6
mov dl, 0xff
int 0x21
jz waitkey
ret
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.