Assembly language examples using NASM on Windows

Console Message - 32 bit

Output a message to the console window, 32 bit.



Open a console window and run the executable.

_WriteFile@20 was chosen as the output can be redirected to a file using > filename.
A string length needs to be supplied, in this case, by looping over the string until a 0 is found.
For buffer overrun protection, there are a maximum number of loops. If you alter the message text, don't forget to change this value (21).

_WriteConsoleA@20 could replace _WriteFile@20, as this takes a null terminated string directly, so a length is not needed.
The downside is, the output can't be redirected to a file.


                                                ; Console Message, 32 bit. V1.01
NULL              EQU 0                         ; Constants

extern _GetStdHandle@4                          ; Import external symbols
extern _WriteFile@20                            ; Windows API functions, decorated
extern _ExitProcess@4

global Start                                    ; Export symbols. The entry point

section .data                                   ; Initialized data segment
 Message db "Console Message 32", 0Dh, 0Ah, 0

section .bss                                    ; Uninitialized data segment
 StandardHandle resd 1
 Written        resd 1

section .text                                   ; Code segment
 call  _GetStdHandle@4
 mov   dword [StandardHandle], EAX

 mov   ESI, Message
 xor   EDI, EDI

.LengthLoop:                                    ; Find the string length by searching for
 cmp   byte [ESI + EDI], 0                      ; the null terminator
 je    .LengthDone

 add   EDI, 1
 cmp   EDI, 21                                  ; Maximum length, prevents buffer overrun
 jne   .LengthLoop                              ; if there is no null terminator

 push  NULL                                     ; 5th parameter
 push  Written                                  ; 4th parameter
 push  EDI                                      ; 3rd parameter
 push  Message                                  ; 2nd parameter
 push  dword [StandardHandle]                   ; 1st parameter
 call  _WriteFile@20                            ; Output can be redirect to a file using >

 push  NULL
 call  _ExitProcess@4