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
printCLS2:   ;printCLS may not work on Win11
        cinvoke printf, "%c[1J%c[H",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

 

15.2  Beep Beep

You can use the Beep functions to create tones on the PC speaker.  You need to import the Beep function.  It takes two parameter: frequency in hertz and duration in milliseconds.

beeps.asm

format PE console
include 'win32ax.inc'

;=======================================
section '.code' code readable executable
;=======================================
start:
        cinvoke printf, "Use the Beep function to make tones%c",10
        cinvoke printf, "You need to import Beep from kernel32%c",10
        cinvoke printf, "1st parameter is frequency in hertz%c",10
        cinvoke printf, "2nd parameter is duration in milliseconds%c",10
        invoke Beep, 660, 300
        invoke Beep, 733, 300
        invoke Beep, 825, 300
        invoke Beep, 880, 500
        invoke getch

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

Output

Use the Beep function to make tones
You need to import Beep from kernel32
1st parameter is frequency in hertz
2nd parameter is duration in milliseconds