Win x64 Tutorial #38: Simple console application
bat-filecls
set masm64_path=\masm64\
set filename=%1
del %filename%.exe
%masm64_path%bin\ml64 /Cp /c /I"%masm64_path%Include" %filename%.asm || exit
%masm64_path%bin\link /SUBSYSTEM:CONSOLE /LIBPATH:"%masm64_path%Lib" ^
/entry:WinMain %filename%.obj /LARGEADDRESSAWARE:NO ^
/ALIGN:16 /SECTION:.text,W ^
/BASE:0x400000 /STUB:%masm64_path%\bin\stubby.exe || exit
del %filename%.objasm-fileinclude win64a.inc
BLACK equ 0
BLUE equ 1
GREEN equ 2
CYAN equ 3
RED equ 4
PURPLE equ 5
YELLOW equ 6
SYSTEM equ 7
GREY equ 8
BRIGHTBLUE equ 9
BRIGHTGREEN equ 10
BRIGHTCYAN equ 11
BRIGHTRED equ 12
BRIGHTPURPLE equ 13
BRIGHTYELLOW equ 14
WHITE equ 15
MAXSCREENX = 80
MAXSCREENY = 25
buffersize = 200
.code
WinMain proc
local LENS:qword
local hOut:qword
local BUFF[buffersize]:byte
local ConsoleWindow:SMALL_RECT
push rbp
mov ebp,esp
sub esp,(28h+2*8+buffersize+sizeof SMALL_RECT+15)and(-16)
call FreeConsole;release the existing console
call AllocConsole;form the console
mov ecx,STD_OUTPUT_HANDLE
call GetStdHandle;receive the handle for a conclusion
mov hOut,rax
mov rcx,rax ; hConsoleOutput
call GetLargestConsoleWindowSize
; rax return in 31-16 bits: dwCoord.y
; 15-00 bits: dwCoord.x
lea r8d,ConsoleWindow
mov [r8+SMALL_RECT.Left],0
mov [r8+SMALL_RECT.Top],0
sub ax, MAXSCREENX
sbb edx, edx
and ax, dx
add ax, MAXSCREENX-1
mov [r8+SMALL_RECT.Right],ax
shr eax, 16
sub eax, MAXSCREENY
sbb edx, edx
and eax, edx
add eax, MAXSCREENY-1
mov [r8+SMALL_RECT.Bottom],ax
mov edx,TRUE ; bAbsolute
mov rcx,hOut ; hConsoleOutput
call SetConsoleWindowInfo
mov edx,MAXSCREENY*10000h+MAXSCREENX;dwCoord
mov rcx,hOut ; hConsoleOutput
call SetConsoleScreenBufferSize;establish the new size of a window of the console
mov ecx,offset STR2
call SetConsoleTitle;definition of a title bar
mov edx,0A0000h;Y=10(0Ah)X=0
mov rcx,hOut
call SetConsoleCursorPosition;establish a cursor position
mov edx,BRIGHTGREEN;color of the output text
mov rcx,hOut
call SetConsoleTextAttribute;set color attributes of the output text
mov qword ptr [rsp+20h],0
lea r9d,BUFF
mov r8d,sizeof STR1
mov edx,offset STR1
mov rcx,hOut
call WriteConsole;display a line of symbols
mov edx,STD_INPUT_HANDLE
mov ecx,3000;small delay
call Sleep
call FreeConsole;close the console
xor ecx,ecx
call ExitProcess
WinMain endp
STR1 db 'Win64 Assembly is Great!'
STR2 db 'Iczelion''s tutorial #38',0
end