Search code examples
assemblyx86-16tasm

Print a colored string (Assembly 8086)


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


Solution

  • 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.