Chapter 15 - Bells & Whistles

15.1  Color Output
 

Color output in the command prompt is done by printing ANSI escape sequences. 

For Windows 10, you may need to enable the ANSI escape sequences since they are disabled by default. It can be enabled with the following registry modification (run from the command prompt as administrator):

reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f
 

colors.asm

format PE console
include 'win32ax.inc'

;=======================================
section '.code' code readable executable
;=======================================
start:

        call printRed
        cinvoke printf, "This is Red%c", 10
        call printGreen
        cinvoke printf, "This is Green%c", 10
        call printYellow
        cinvoke printf, "This is Yellow%c", 10
        call printBlue
        cinvoke printf, "This is Blue%c", 10
        call printPurple
        cinvoke printf, "This is Purple%c", 10
        call printCyan
        cinvoke printf, "This is Cyan%c", 10
        call printWhite
        cinvoke printf, "This is White%c", 10
        call printDefault
        cinvoke printf, "This is Default Gray%c", 10
        invoke getch

printCLS:    ;clears the screen
        cinvoke printf, "%c[2J%c[1;1H",27,27
        ret
printRed:
        cinvoke printf, "%c[31;1m",27
        ret
printGreen:
        cinvoke printf, "%c[32;1m",27
        ret
printYellow:
        cinvoke printf, "%c[33;1m",27
        ret
printBlue:
        cinvoke printf, "%c[34;1m",27
        ret
printPurple:
        cinvoke printf, "%c[35;1m",27
        ret
printCyan:
        cinvoke printf, "%c[36;1m",27
        ret
printWhite:
        cinvoke printf, "%c[37;1m",27
        ret
printDefault:
        cinvoke printf, "%c[0m",27
        ret

;====================================
section '.idata' import data readable
;====================================
library msvcrt,'msvcrt.dll',kernel32,'kernel32.dll'
import msvcrt,printf,'printf',scanf,'scanf',getch,'_getch'

Output

This is Red
This is Green
This is Yellow
This is Blue
This is Purple
This is Cyan
This is White
This is Default Gray