yoav
yoav

Reputation: 322

Print a colored string (Assembly 8086)

How is it possible to make a string colored in assembly 8086?

Upvotes: 1

Views: 3261

Answers (2)

Sep Roland
Sep Roland

Reputation: 39166

Why your current program didn't work was already explained in the answer by zx485. As per your comment you can indeed print the whole colored string in one go. BIOS offers us video function 13h. The full pointer to the text is expected in ES:BP, so make sure that the ES segment register is setup properly.

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

Upvotes: 3

zx485
zx485

Reputation: 29022

You are using the interrupt functions wrong:

INT 10h, AH=09h prints several, same characters at a time. The count is passed in the CX register. To print a string, you have to call it as often as characters are in the string, with the other parameters set. The character has to be passed in the AL register and the attribute/color has to be passed in the BL register. BH should (probably) stay 0 and CX should stay 1. DL and DH are not used by this function, hence you can remove the respective commands.

The initial cursor position can be set with the function INT 10h, AH=02h. Make sure that the BH value matches the one in the above code(0).

So your code could look like this:

  ; ...
  ; 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:
  ; ...

The DOS function INT 21h which prints a string till the end-char $ does not care about the attribute passed to the BIOS function INT 10h, so the color is ignored by it and you can remove the corresponding code from ;print the string to INT 21h.

Upvotes: 4

Related Questions